EventEmitter is a lightweight, memory‑efficient event emitter library for Arduino. It brings the familiar Node.js event model to embedded systems, allowing you to attach callbacks to events, emit events with custom arguments, and manage one‑time or recurring events—all without dynamic memory allocation!
The simplist way to install is searching "EventEmitter" in the Arduino IDE lib manager!
- Download: EventEmitter.zip
- In the Arduino IDE's menu bar click: Sketch > Include Library > Add .ZIP Library
- Select "EventEmitter.zip"
- Done!
In the Arduino IDE's menu bar click: File > Examples > EventEmitter > Choose an example
- Event Registration: Attach multiple callbacks to an event.
- Event Emission: Emit events and pass arguments to callbacks.
- One‑Shot Listeners: Automatically remove listeners after their first call.
- Listener Management: Remove individual or all listeners.
- Memory Efficiency: Uses fixed‑size buffers and statically allocated arrays, ideal for microcontrollers.
Below are simple snippet examples to help you get started.
Attach a callback that will be called whenever the event is triggered.
// Define a simple event handler.
void onMyEvent() {
// Insert code to run when "myEvent" occurs.
}
// Register the "myEvent" event.
emitter.on("myEvent", onMyEvent);
Trigger the event, with or without arguments.
// Emit "myEvent" with no arguments.
emitter.emit("myEvent");
// Emit an event with arguments (for example, an integer and a float).
emitter.emit("dataEvent", 42, 3.14);
Register a listener that automatically removes itself after the first call.
// The lambda will be executed only once when "startup" is emitted.
emitter.once("startup", [](){
// Code that runs just once.
});
Remove a previously registered listener.
// Remove the onMyEvent callback from "myEvent".
emitter.removeListener("myEvent", onMyEvent);
Clear all callbacks for a specific event or for all events.
// Remove all listeners from "myEvent".
emitter.removeAllListeners("myEvent");
// Remove every listener from all events.
emitter.removeAllListeners();
Limit or retrieve the maximum number of listeners that can be registered per event.
// Set the maximum number of listeners per event to 3.
emitter.setMaxListeners(3);
// Retrieve the current maximum listener count.
size_t maxListeners = emitter.getMaxListeners();
This project is licensed under the MIT License.