/// <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);
        }
        /// <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);
            }));
        }
 public override void OnGPIO(int number, bool value)
 {
     base.OnGPIO(number, value);
     if (CheckPinNumber(number))
     {
         PinChanged?.Invoke(Pins[number], value);
     }
 }
Пример #4
0
        private async Task ClearPin(HttpListenerContext context, IPin pin)
        {
            bool currentState = false;

            pin.Write(currentState ? PinValue.High : PinValue.Low);

            await RespondPinValue(context, pin.PinNumber, currentState);

            PinChanged?.Invoke(this, new PinEventArgs(context.Request, pin));
        }
Пример #5
0
        private async Task UpdatePin(HttpListenerContext context, IPin pin)
        {
            var request      = context.Request;
            var inputStream  = request.InputStream;
            var currentState = pin.Read() == PinValue.High;

            using (var streamReader = new StreamReader(inputStream))
            {
                var content = await streamReader.ReadToEndAsync();

                var    obj   = JObject.Parse(content);
                JToken token = null;
                if (obj.TryGetValue("state", out token))
                {
                    switch (token.Type)
                    {
                    case JTokenType.Boolean:
                        currentState = token.Value <bool>();
                        break;

                    case JTokenType.String:
                        var str = token.Value <string>();
                        if (str == "toggle")
                        {
                            currentState = !currentState;
                        }
                        break;
                    }
                }
            }

            pin.Write(currentState ? PinValue.High : PinValue.Low);

            await RespondPinValue(context, pin.PinNumber, currentState);

            PinChanged?.Invoke(this, new PinEventArgs(request, pin));
        }
Пример #6
0
 void OnPinChanged(int port, bool value)
 {
     PinChanged?.Invoke(this, new RelayChangedEventArgs(port, value));
 }
Пример #7
0
 private void btnPin_Click(object sender, EventArgs e)
 {
     SetPinned(!Pinned);
     PinChanged?.Invoke(this, EventArgs.Empty);
 }