void bgworker_DoWork(object sender, DoWorkEventArgs e) { do { js_event myevent = ReadStruct <js_event> (devicefile); if (myevent.type == this.JS_EVENT_AXIS) { SetAxisValue(myevent.number, myevent.value); } else if (myevent.type == this.JS_EVENT_BUTTON) { this.buttons [myevent.number] = Convert.ToBoolean(myevent.value); } } while (!bgworker.CancellationPending); }
public void DetectChange(js_event buff) { // JS_EVENT_INIT if (checkBit(buff.type, (byte)MODE.CONFIGURATION)) { if (checkBit(buff.type, (byte)TYPE.AXIS)) { byte key = (byte)buff.number; if (!Axis.ContainsKey(key)) { Axis.Add(key, 0); return; } } else if (checkBit(buff.type, (byte)TYPE.BUTTON)) { byte key = (byte)buff.number; if (!Button.ContainsKey(key)) { Button.Add((byte)buff.number, false); return; } } } if (checkBit(buff.type, (byte)TYPE.AXIS)) { short value = buff.value; Axis[(byte)buff.number] = value; return; } else if (checkBit(buff.type, (byte)TYPE.BUTTON)) { Button[(byte)buff.number] = buff.value == (byte)STATE.PRESSED; return; } }
public void Start(string deviceFile) { //Mono.Unix.UnixSymbolicLinkInfo i = new Mono.Unix.UnixSymbolicLinkInfo( deviceFile ); //Console.WriteLine(i.ContentsPath); // ../js0 //Console.WriteLine(i.FullName); // fullpath //Console.WriteLine(i.Name); // filename //var path = "/dev/input/by-id/" + i.ContentsPath; var path = deviceFile; if (!File.Exists(path)) { throw new FileNotFoundException(path); } SemaphoreSlim run = new SemaphoreSlim(1); new Thread(() => { if (fs != null) { fs.Close(); } // Read loop. fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); log.Info("opened " + path); var buff = new js_event() { raw = new byte[8] }; Joystick j = new Joystick(); var started = false; while (true) { try { // Read 8 bytes from file and analyze. var read = fs.Read(buff.raw, 0, 8); if (read != 8) { continue; } //Console.WriteLine(buff.ToJSON()); j.DetectChange(buff); /* * // Prints Axis values * foreach (byte key in j.Axis.Keys) * { * Console.WriteLine(string.Format("Axis{0}: {1}", key, j.Axis[key])); * } * * // Prints Buttons values * foreach (byte key in j.Button.Keys) * { * Console.WriteLine(string.Format("Button{0}: {1}", key, j.Button[key])); * } */ state = new LinuxJoystickState( j.Axis.ToDictionary(a => a.Key, a => (ushort)((int)a.Value + (65535 / 2))), j.Button.ToDictionary(a => a.Key, a => a.Value)); if (started == false) { log.Info("Joystick Task Started"); run.Release(); } started = true; } catch (Exception ex) { log.Error(ex); break; } } log.Info("Joystick Task Ended"); }).Start(); // wait for the task to start if (!run.Wait(1000)) { log.Error("Failed to Start joystick task"); throw new Exception("Fail to start joystick task"); } Thread.Sleep(200); }