Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
GpioEventManager.cpp
Go to the documentation of this file.
2#include <cstdio>
3#include <hardware/gpio.h>
4
5#include "events/Event.h"
8#include "framework_config.h"
9
11 static GpioEventManager instance;
12 return instance;
13}
14
15void GpioEventManager::enableInterrupt(uint pin, uint32_t edgeMask) {
16 if(!handler_set){
17 gpio_set_irq_callback(gpio_event_handler);
18 handler_set = true;
19 }
20 gpio_set_irq_enabled(pin, edgeMask, true);
21}
22
24 gpio_set_irq_enabled(pin, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, false);
25 listeners.erase(pin);
26}
27
29 listeners[pin].push_back(cb);
30}
31
33 listeners.erase(pin);
34}
35
36void GpioEventManager::gpio_event_handler(uint gpio, uint32_t events) {
37 GpioEvent gpioEvent = {
38 static_cast<uint16_t>(gpio),
39 static_cast<uint16_t>(events)
40 };
41
42 // Dispatch to listeners
43#if GPIO_EVENT_HANDLING & GPIO_NOTIFICATIONS
44
45 auto it = listeners.find(gpio);
46 if (it != listeners.end()) {
47 for (auto& cb : it->second) {
48 cb(gpioEvent);
49 }
50 }
51#endif
52
53 // Also send an Event to EventManager if anyone wants to subscribe
54#if GPIO_EVENT_HANDLING & GPIO_EVENTS
55 Event evt = Event(SystemNotification::GpioChange, gpioEvent, sizeof(GpioEvent)); // broadcast event to anyone subscribed as target isn't specified
56 AppContext::get<EventManager>()->postEvent(evt); // EventManager knows whther it's an ISR or not
57#endif
58}
Event pub/sub manager for embedded applications using FreeRTOS.
Defines the Event structure and related utilities for event messaging.
Posts GPIO change events (rising/falling edge) via EventManager.
static constexpr std::uintptr_t getTypeKey()
Definition AppContext.h:91
GpioEventManager registers interrupts and posts GpioChange events to multiple listeners per pin.
void registerCallback(uint pin, GpioCallback cb)
Register a callback for GPIO events on a specific pin.
std::function< void(const GpioEvent &)> GpioCallback
Initialize the GPIO event manager.
static void gpio_event_handler(uint gpio, uint32_t events)
void unregisterAll(uint pin)
Unregister listeners for a GPIO pin.
void disableInterrupt(uint pin)
Disable GPIO interrupts for a specific pin.
static GpioEventManager & getInstance()
Get the singleton instance of GpioEventManager.
void enableInterrupt(uint pin, uint32_t edgeMask)
Enable GPIO interrupts for a specific pin and edge mask.
static std::map< uint, std::vector< GpioCallback > > listeners
Delegates to user or system configuration.
Represents a framework event, optionally carrying payload data.
Definition Event.h:24
Structure representing a GPIO event.
Definition GpioEvent.h:11