Logo Pico-Framework A web-first embedded framework for C++
Loading...
Searching...
No Matches
Event.h
Go to the documentation of this file.
1
10#pragma once
11#include <cstdint>
12#include <cstddef>
13#include <type_traits>
14#include <string>
15#include "events/Notification.h"
16#include "events/GpioEvent.h"
17
18class FrameworkTask;
19
23struct Event
24{
26 union {
28 const void* data;
29 };
30 size_t size = 0;
31 void *source = nullptr;
32 FrameworkTask *target = nullptr;
33
37 Event() = default;
38
48 Event(SystemNotification type, const void *data = nullptr, size_t size = 0,
49 void *source = nullptr, FrameworkTask *target = nullptr)
51
61 size_t size = sizeof(GpioEvent),
62 void* source = nullptr, FrameworkTask* target = nullptr)
64
65
75 Event(uint8_t userCode, const void *data = nullptr, size_t size = 0,
76 void *source = nullptr, FrameworkTask *target = nullptr)
78
79
81 inline bool isUser() const {
83 }
84
86 inline bool isSystem() const {
88 }
89
91 inline uint8_t userCode() const {
93 }
94
97 return notification.system;
98 }
99
100};
101
102// global scope for convenience
103
113template<typename Enum, typename T>
114inline Event userEvent(Enum e, const T& data) {
115 return Event(static_cast<uint8_t>(e), &data, sizeof(T));
116}
117
118template<typename Enum>
119inline Event userEvent(Enum e, const char* data, size_t size) {
120 static_assert(std::is_enum<Enum>::value, "Enum type required");
121 return Event(static_cast<uint8_t>(e), data, size);
122}
Event userEvent(Enum e, const T &data)
Helper to create a user-defined Event with a payload.
Definition Event.h:114
SystemNotification
System-defined notification types reserved by the framework.
Base class for FreeRTOS-aware tasks in the framework.
Represents a framework event, optionally carrying payload data.
Definition Event.h:24
bool isUser() const
Returns true if this is a user-defined event.
Definition Event.h:81
SystemNotification systemCode() const
Returns the system-defined enum value.
Definition Event.h:96
Notification notification
Notification identifier (system or user)
Definition Event.h:25
FrameworkTask * target
Optional specific target (for directed delivery)
Definition Event.h:32
size_t size
Size of payload data
Definition Event.h:30
Event(SystemNotification type, const GpioEvent &gpioEvent, size_t size=sizeof(GpioEvent), void *source=nullptr, FrameworkTask *target=nullptr)
Construct an event with a GpioEvent payload.
Definition Event.h:60
uint8_t userCode() const
Returns the raw user-defined code (safe to cast in app code)
Definition Event.h:91
Event(SystemNotification type, const void *data=nullptr, size_t size=0, void *source=nullptr, FrameworkTask *target=nullptr)
Construct an event with a system notification.
Definition Event.h:48
void * source
Optional source (e.g. controller that generated the event)
Definition Event.h:31
GpioEvent gpioEvent
Inline data if GpioChange.
Definition Event.h:27
const void * data
For user use.
Definition Event.h:28
bool isSystem() const
Returns true if this is a system-defined event.
Definition Event.h:86
Event()=default
Default constructor (creates a SystemNotification::None event).
Event(uint8_t userCode, const void *data=nullptr, size_t size=0, void *source=nullptr, FrameworkTask *target=nullptr)
Construct an event with a user-defined notification.
Definition Event.h:75
Structure representing a GPIO event.
Definition GpioEvent.h:11
A tagged union representing either a system or user-defined notification.
SystemNotification system
NotificationKind kind
uint8_t user_code