C
C#16mo ago
FacePalmGamer

✅ 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)
void async main()
{
List<task> tasks = new List<Task>();
tasks.add(ListenAsync);
tasks.add(Listen2Async);
await Task.WhenAll(Tasks)
}

async Task ListenAsync()
{
return controller.WaitForEventAsync(...)
}
void async main()
{
List<task> tasks = new List<Task>();
tasks.add(ListenAsync);
tasks.add(Listen2Async);
await Task.WhenAll(Tasks)
}

async Task ListenAsync()
{
return controller.WaitForEventAsync(...)
}
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
cap5lut
cap5lut16mo ago
in the end u can have one async loop awaiting the event and then firing of the c# event
public class Example
{
public record PinChangedEventArgs(...);
public delegate void PinChangedEventHandler(object? sender, PinChangedEventArgs e);
public event PinChangedEventHandler? PinChangedEvent;

public async Task EventLoop()
{
while (true) {
await controller.WaitForEventAsync(...);
if (PinChangedEvent is not null var handler)
{
var e = new PinChangedEventArgs(...);
handler.Invoke(this, e);
}
}
}
}
public class Example
{
public record PinChangedEventArgs(...);
public delegate void PinChangedEventHandler(object? sender, PinChangedEventArgs e);
public event PinChangedEventHandler? PinChangedEvent;

public async Task EventLoop()
{
while (true) {
await controller.WaitForEventAsync(...);
if (PinChangedEvent is not null var handler)
{
var e = new PinChangedEventArgs(...);
handler.Invoke(this, e);
}
}
}
}
this is just a rough draft tho, there is a lot of stuff missing, like starting the actual event loop, cancellation management, etc.
FacePalmGamer
FacePalmGamerOP16mo ago
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
cap5lut
cap5lut16mo ago
well, while its awaiting, its not blocking a thread, so its basically the same as an event callback
cap5lut
cap5lut16mo ago
so u can basically throw this code away ;p so something like the following would be enough
controller.RegisterCallbackForPinValueChangedEvent(1, PinEventTypes.Raising | PinEventTypes.Falling, (sender, e) => {
// do something
});
controller.RegisterCallbackForPinValueChangedEvent(1, PinEventTypes.Raising | PinEventTypes.Falling, (sender, e) => {
// do something
});
FacePalmGamer
FacePalmGamerOP15mo ago
Cool. I’ll look more into this
Want results from more Discord servers?
Add your server