/// <summary> /// Start polling for pin events /// </summary> /// <param name="cancellationToken">Optional cancellation token</param> /// <returns>Asynchronous task</returns> public unsafe Task StartMonitoring(CancellationToken cancellationToken = default) { if (_reqFd < 0) { throw new IOException("Pin is not configured"); } return(Task.Run(() => { gpioevent_data eventData = new gpioevent_data(); int sizeOfEventData = Marshal.SizeOf(typeof(gpioevent_data)); do { if (Interop.read(_reqFd, new IntPtr(&eventData), sizeOfEventData) == sizeOfEventData) { Value = (eventData.id == (uint)GpioEvent.GPIOEVENT_EVENT_RISING_EDGE); PinChanged?.Invoke(this, Value); } else { throw new IOException("Read returned invalid size"); } }while (!cancellationToken.IsCancellationRequested); })); }
/// <summary> /// Start polling for pin events /// </summary> /// <param name="cancellationToken"></param> public unsafe void StartMonitoring(int pollInterval = 500, CancellationToken cancellationToken = default) { if (_reqFd < 0) { throw new IOException("Pin is not configured"); } _ = Task.Run(() => { gpioevent_data eventData = new gpioevent_data(); int sizeOfEventData = Marshal.SizeOf(typeof(gpioevent_data)); do { if (Interop.read(_reqFd, new IntPtr(&eventData), sizeOfEventData) == sizeOfEventData) { Value = (eventData.id == (uint)GpioEvent.GPIOEVENT_EVENT_RISING_EDGE); PinChanged?.Invoke(this, Value); } else { // FIXME: Handle exception here Console.WriteLine("Oh snap! Read returned invalid size"); break; } } while (!cancellationToken.IsCancellationRequested); }, cancellationToken); }