Exemplo n.º 1
0
        private void pollInterrupt(Object p)
        {
            pollParams pp     = (pollParams)p;
            int        errCnt = 0;

            while (!pp.cancelToken.IsCancellationRequested)
            {
                try
                {
                    if (poll(this.deviceHandle, this.pollTimeout))
                    {
                        OnInterruptOccured();
                    }
                    errCnt = 0;
                } catch (IOException)
                {
                    errCnt++;
                    if (errCnt > 3)
                    {
                        throw;
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(10); // * (int)Math.Pow(10, errCnt-1));
                    }
                }
            }
        }
Exemplo n.º 2
0
        public GpioEdgeDetector(byte pin, EdgeDetectionMode edge, int pollTimeout = 1000)
        {
            this.pin         = pin;
            this.deviceName  = String.Format("/sys/class/gpio/gpio{0}/value", pin);
            this.pollTimeout = pollTimeout;

            // enable edge detection
            setEdgeDetection(pin, edge);

            // open gpio file handle
            openEdgeDetectionFileHandle();

            // start async polling task
            cancelTokenSource = new System.Threading.CancellationTokenSource();
            pollParams p = new pollParams();

            p.cancelToken = cancelTokenSource.Token;

            // TODO: Error Handling!
            Task.Factory.StartNew(new Action <Object>(pollInterrupt), p, cancelTokenSource.Token, TaskCreationOptions.LongRunning, TaskScheduler.Default).
            ContinueWith((t) => {
                Console.WriteLine(String.Format("GpioEdgeDetector Error: {0}", (t.Exception != null ? t.Exception.ToString() : String.Empty)));
            }, System.Threading.CancellationToken.None, TaskContinuationOptions.OnlyOnFaulted, TaskScheduler.Current);
        }