Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
GpioEventManager Class Reference

GpioEventManager registers interrupts and posts GpioChange events to multiple listeners per pin. More...

#include <GpioEventManager.h>

+ Collaboration diagram for GpioEventManager:

Public Types

using GpioCallback = std::function< void(const GpioEvent &)>
 Initialize the GPIO event manager.
 

Public Member Functions

void enableInterrupt (uint pin, uint32_t edgeMask)
 Enable GPIO interrupts for a specific pin and edge mask.
 
void disableInterrupt (uint pin)
 Disable GPIO interrupts for a specific pin.
 
void registerCallback (uint pin, GpioCallback cb)
 Register a callback for GPIO events on a specific pin.
 
void unregisterAll (uint pin)
 Unregister listeners for a GPIO pin.
 

Static Public Member Functions

static GpioEventManagergetInstance ()
 Get the singleton instance of GpioEventManager.
 

Private Member Functions

 GpioEventManager ()=default
 

Static Private Member Functions

static void gpio_event_handler (uint gpio, uint32_t events)
 

Private Attributes

bool handler_set = false
 

Static Private Attributes

static std::map< uint, std::vector< GpioCallback > > listeners
 

Detailed Description

Definition at line 21 of file GpioEventManager.h.

Member Typedef Documentation

◆ GpioCallback

using GpioEventManager::GpioCallback = std::function<void(const GpioEvent &)>

This declares the callback used by the pico sdk when an interrupt occurs.

Definition at line 37 of file GpioEventManager.h.

Constructor & Destructor Documentation

◆ GpioEventManager()

GpioEventManager::GpioEventManager ( )
privatedefault

Member Function Documentation

◆ disableInterrupt()

void GpioEventManager::disableInterrupt ( uint  pin)

This unregisters the pin from the pico SDK and stops listening for events.

Parameters
pinThe GPIO pin number to disable interrupts for.

Definition at line 23 of file GpioEventManager.cpp.

23 {
24 gpio_set_irq_enabled(pin, GPIO_IRQ_EDGE_RISE | GPIO_IRQ_EDGE_FALL, false);
25 listeners.erase(pin);
26}
static std::map< uint, std::vector< GpioCallback > > listeners

References listeners.

◆ enableInterrupt()

void GpioEventManager::enableInterrupt ( uint  pin,
uint32_t  edgeMask 
)

This registers the pin with the pico SDK and sets up the interrupt handler.

Parameters
pinThe GPIO pin number to enable interrupts for.
edgeMaskBitmask of edges to listen for (GPIO_IRQ_EDGE_RISE, GPIO_IRQ_EDGE_FALL).

Definition at line 15 of file GpioEventManager.cpp.

15 {
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}
static void gpio_event_handler(uint gpio, uint32_t events)

References gpio_event_handler(), and handler_set.

Referenced by App::onStart().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getInstance()

GpioEventManager & GpioEventManager::getInstance ( )
static

This is a thread-safe singleton implementation.

Definition at line 10 of file GpioEventManager.cpp.

10 {
11 static GpioEventManager instance;
12 return instance;
13}
GpioEventManager registers interrupts and posts GpioChange events to multiple listeners per pin.

Referenced by AppContext::initFrameworkServices().

+ Here is the caller graph for this function:

◆ gpio_event_handler()

void GpioEventManager::gpio_event_handler ( uint  gpio,
uint32_t  events 
)
staticprivate

Definition at line 36 of file GpioEventManager.cpp.

36 {
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}
static constexpr std::uintptr_t getTypeKey()
Definition AppContext.h:91
Represents a framework event, optionally carrying payload data.
Definition Event.h:24
Structure representing a GPIO event.
Definition GpioEvent.h:11

References AppContext::getTypeKey(), GpioChange, and listeners.

Referenced by enableInterrupt().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ registerCallback()

void GpioEventManager::registerCallback ( uint  pin,
GpioCallback  cb 
)

This allows multiple listeners to be registered for the same pin.

Parameters
pinThe GPIO pin number to register the callback for.
cbThe callback function to call when an event occurs.

Definition at line 28 of file GpioEventManager.cpp.

28 {
29 listeners[pin].push_back(cb);
30}

References listeners.

◆ unregisterAll()

void GpioEventManager::unregisterAll ( uint  pin)

This removes the the list of listeners for the specified pin.

Parameters
pinThe GPIO pin number to unregister the callback from.
cbThe callback function to remove.

Definition at line 32 of file GpioEventManager.cpp.

32 {
33 listeners.erase(pin);
34}

References listeners.

Member Data Documentation

◆ handler_set

bool GpioEventManager::handler_set = false
private

Definition at line 80 of file GpioEventManager.h.

Referenced by enableInterrupt().

◆ listeners

std::map<uint, std::vector<GpioCallback> > GpioEventManager::listeners
inlinestaticprivate

The documentation for this class was generated from the following files: