✅ Making custom Events
I am working with a pi using the System.Device.Gpio. is there a way to create an event for when a pin changes?
I have never worked with making events only ever subscribed to known events, so i don't know if you even can, but I dont see why you couldn't.
the GPIO library has 2 functions. waitForEvent, and waitForEventAsync. Previously I just made a task list like this: (This is a poor rendition, these functions would do alot more)
this seems wrong, and if I could just make a pinChange event and subscribe a method to it, it would be alot easier. Effectively like an interrupt.
TLDR
What is the correct way to look for pin chages? should i just stick with making a list of tasks, or is there a way to make an event system for pin changes?
6 Replies
in the end u can have one async loop awaiting the event and then firing of the c# event
this is just a rough draft tho, there is a lot of stuff missing, like starting the actual event loop, cancellation management, etc.
Yeah ok. I thought about doing it this way, but having a while true loop just seems wrong. Ik in something like c code you can have interrupts and I was wondering if there was a way to set that up with events. I get c is lower level so it can do that better but this has to put a thread in the thread pool waiting for an event. Makes sense you have to do if that way but I was just wondering if there was a better way
well, while its
await
ing, its not blocking a thread, so its basically the same as an event callbackoh, there is also this: https://learn.microsoft.com/en-us/dotnet/api/system.device.gpio.gpiocontroller.registercallbackforpinvaluechangedevent?view=iot-dotnet-latest
GpioController.RegisterCallbackForPinValueChangedEvent Method (Syst...
Adds a callback that will be invoked when pinNumber has an event of type eventType.
so u can basically throw this code away ;p
so something like the following would be enough
Cool. I’ll look more into this