コード例 #1
0
            // http://www-numi.fnal.gov/offline_software/srt_public_context/WebDocs/Errors/unix_system_errors.html
            private static IDisposable InternalSetupPolled(int pin, InterruptModeEnum mode, Action handler)
            {
                Unmanaged.Interrupts.InterruptServiceRoutine(pin, (int)mode, null);

                var thread = new Thread(o =>
                {
                    var token = (CancellationToken)o;

                    while (!token.IsCancellationRequested)
                    {
                        var result = Unmanaged.Interrupts.WaitForInterrupt(pin, 500);
                        switch (result)
                        {
                        case -1:
                            var error = Marshal.GetLastWin32Error();
                            if (error == 4)                             // EINTR
                            {
                                continue;
                            }

                            throw new Exception($"Error #{error} occured while waiting for interrupt.");

                        case -2:
                            throw new Exception("Pin descriptor not initialized.");

                        case 0:
                            break;

                        case 1:
                            handler();
                            break;

                        default:
                            throw new Exception($"Unrecognized result value '{result}' received from WaitForInterrupt .");
                        }
                    }
                });

                return(new ThreadStopper(thread));
            }
コード例 #2
0
 public static IDisposable SetupPolled(BroadcomPinNumberEnum pin, InterruptModeEnum mode, Action handler)
 => InternalSetupPolled((int)pin, mode, handler);
コード例 #3
0
            private static void InternalSetup(int pin, InterruptModeEnum mode, Action handler)
            {
                var callback = handler != null ? new InterruptCallback(handler) : null;

                Unmanaged.Interrupts.InterruptServiceRoutine(pin, (int)mode, callback);
            }
コード例 #4
0
 public static void Setup(BroadcomPinNumberEnum pin, InterruptModeEnum mode, Action handler)
 => InternalSetup((int)pin, mode, handler);