コード例 #1
0
        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);
        }