matthewturner / eventually Goto Github PK
View Code? Open in Web Editor NEWThis project forked from johnnyb/eventually
A library for event-based programming to make Arduino programming more fun and intuitive
License: MIT License
This project forked from johnnyb/eventually
A library for event-based programming to make Arduino programming more fun and intuitive
License: MIT License
extraData was removed from EvtListener (probably to save some memory). However it is very useful to have the option to assign extraData to a listener (e.g. an object uses a listener and in the callback the object has to be assigned. extraData can be used to store a pointer to the object that owns the listener)
I would propose to reintroduce extraData to EvtListener that was removed here: 0a3f2e4#diff-2f154c55f0e4e9f7f0ecadefc29fde25da27e0fe45a7661e8c40c0caff81cbca
Is it really wanted that the same listener can be added twice?
EvtManager mgr(true); // true to manage memory
EvtOnceListener readShtListener((EvtAction)[](IEvtListener *lst, IEvtContext *ctx) {
readSht();
return false; // do not stop the current action chain, execute other events
});
Serial.println("listenerCount = " + mgr.listenerCount());
mgr.addListener(&readShtListener);
Serial.println("listenerCount = " + mgr.listenerCount());
mgr.addListener(&readShtListener);
Serial.println("listenerCount = " + mgr.listenerCount());
outputs
listenerCount = 1
listenerCount = 2
listenerCount = 3
Just for information:
#ifndef EvtOnceListener_h
#define EvtOnceListener_h
#include "EvtListener.h"
class EvtOnceListener : public EvtListener {
public:
EvtOnceListener();
EvtOnceListener(EvtAction triggerAction);
~EvtOnceListener();
void reset();
bool isEventTriggered();
bool performTriggerAction(IEvtContext *);
};
EvtOnceListener::EvtOnceListener() {
// Nothing to construct
}
EvtOnceListener::EvtOnceListener(EvtAction triggerAction) {
_triggerAction = triggerAction;
}
EvtOnceListener::~EvtOnceListener() {
// Nothing to destruct
}
void EvtOnceListener::reset() {
// Nothing to setup
}
/**
* @brief Evaluate if action shall be triggered / executed (performTriggerAction())
* @author Holger Mueller
* @date 2024-02-20
*
* @return returns true if the event is triggered.
*/
bool EvtOnceListener::isEventTriggered() {
return _enabled;
}
/**
* @brief Perform the triggered action and disable further triggers
* @author Holger Mueller
* @date 2024-02-21
*
* @param ctx EvtManager context
* @return Return value of the action.
*/
bool EvtOnceListener::performTriggerAction(IEvtContext *ctx) {
Serial.println("EvtOnceListener::performTriggerAction execution started"); // TODO: delme
bool returnVal = (*_triggerAction)(this, ctx);
disable();
Serial.println("EvtOnceListener::performTriggerAction execution ended"); // TODO: delme
return returnVal;
}
#endif
Hi, I'm working on a complex project where I need to work with classes. So I tried the SimpleButtonBlink example, but since resetContext() does not exist anymore I replaced it with reset(), changed pins and made a couple more changes.
#include <Eventually.h>
#define LIGHT_PIN 13
#define BUTTON_PIN 3
bool speed = LOW;
bool pin_state = LOW;
EvtManager mgr;
bool blink()
{
pin_state = !pin_state;
digitalWrite(LIGHT_PIN, pin_state);
return false;
}
bool run()
{
mgr.reset();
// Won't this call overflow the stack???
mgr.addListener(new EvtPinListener(BUTTON_PIN, 20, LOW, (EvtAction)&run));
mgr.addListener(new EvtTimeListener(speed == HIGH ? 100 : 500, true, (EvtAction)&blink));
speed = !speed;
}
void setup()
{
pinMode(LIGHT_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
run();
}
void loop()
{
mgr.loopIteration();
}
So far so good, worked without problems. Because I need classes in my project, next step was to create a Blinker class to encapsulate the main functionality:
#include <Eventually.h>
#define LIGHT_PIN 13
#define BUTTON_PIN 3
EvtManager mgr;
class Blinker
{
private:
bool pin_state = HIGH;
bool speed = HIGH;
uint8_t lightPin;
uint8_t btnPin;
bool blink();
public:
Blinker(uint8_t led, uint8_t button);
bool run();
};
Blinker::Blinker(uint8_t led, uint8_t button)
{
lightPin = led;
btnPin = button;
}
bool Blinker::blink()
{
pin_state = !pin_state;
digitalWrite(lightPin, pin_state);
return false;
}
bool Blinker::run()
{
mgr.reset();
// Won't this call overflow the stack???
mgr.addListener(new EvtPinListener(BUTTON_PIN, 20, LOW, (EvtAction)&run));
mgr.addListener(new EvtTimeListener(speed == HIGH ? 100 : 500, true, (EvtAction)&blink));
speed = !speed;
}
Blinker blinker = Blinker(LIGHT_PIN, BUTTON_PIN);
void setup()
{
blinker.run();
}
void loop()
{
mgr.loopIteration();
}
But this code doesn't work. The private fields do not seem to retain state, but that's just a guess. I made several other attempts, including passing a pointer to mgr and creating the listeners as variables inside the class (equivalent to the "consider declaring the listeners as global variables..." part in the README). No luck.
It might doing something terribly wrong here, but after several attempts I still have no clue.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.