johnnyb / eventually Goto Github PK
View Code? Open in Web Editor NEWA library for event-based programming to make Arduino programming more fun and intuitive
License: MIT License
A library for event-based programming to make Arduino programming more fun and intuitive
License: MIT License
Hi,
And many thanks for this nice library! I intend to use in for a project with my students.
I have two questions and one suggestion about the EvtContext::addListener
method.
I think that there is a bug in EvtContext::addListener
.
In the first for loop, I think you should use i
as the index of listeners[]
instead of listenerCount
.
In other words, you should replace
for(int i = 0; i < listenerCount; i++) { // Try to add in empty slot
if(listeners[listenerCount] == 0) {
listeners[listenerCount] = lstn;
return;
}
}
by
for(int i = 0; i < listenerCount; i++) { // Try to add in empty slot
if(listeners[i] == 0) {
listeners[i] = lstn;
return;
}
}
I'm not sure of that, but I think you should also add lstn->setupListener();
in this loop.
The result would then be:
for(int i = 0; i < listenerCount; i++) { // Try to add in empty slot
if(listeners[i] == 0) {
listeners[i] = lstn;
lstn->setupListener();
return;
}
}
This method could return a pointer to the EvtListener *
that it got in parameter.
Thus the prototype would become
EvtListener * EvtContext::addListener(EvtListener *lstn)
;
and the method return lstn
.
This would enable to make calls like:
EvtListener *pLstn;
pLstn = EvtContext::addListener(new EvtPinListener(SOME_PIN, (EvtAction)some_action));
and then use pLstn as an id to remove this event:
mgr.removeListener(pLstn);
Hi I really like the library. It would be nice to have the ability to add a listener for high and a listener for low states. Right now is seems the listener only fires when high? Just from my testing.
The feature I would like would be Similar to how other programming languages have addListenerButtonDown(run once on button down) and addListenerButtonUp(run once on button up) and addListenerButton(usually means continues firing while down)
What if I want to listen (EvtPinListener) to change pin state not in Arduino itself but in expander chip ?
I made a temperature measurer station with an esp8266 and an oled distpay. I add event handlers at setup: a Timer event handled and a Http event handler. Because I want access measured info over wifi network. I add two listener but events fired only for the first one. Second event not fired, only just the first registration.
Code is here:
https://infokristaly.hu/?q=node/850
I started out with the example code from the Readme page and from there started developing some more complex scenario involving two timers. I quickly stumbled across the issues already reported related to the addListeners
method, found another problem with non-multiFire listeners (added comment in existing issue report) but then came across a much more serious issue that crashes the executing program with an exception.
Per readme, I figured I can kill all existing timers with mgr.resetContext()
from within a callback method.
bool start_blinking() {
mgr.resetContext();
mgr.addListener(new EvtTimeListener(500, true, (EvtAction)blink_pin));
mgr.addListener(new EvtPinListener(BUTTON_PIN, (EvtAction)stop_blinking));
return true;
}
The problem is that as part of the mgr.resetContext()
method, the existing listener objects get deleted, while start_blinking()
is called FROM a listener object. Once the callback method, here start_blinking
, has completed the handleTimerEvent()
still accesses member variables of the listener object like multiFire
and even calls the non-static method setupListener()
on a deleted object. Per CPP spec, this is undefined behavior. During my experiments I see an exception with subsequent termination of the program.
My background is managed languages (C#, JS, Java) so I'm still trying to wrap my head around this, trying to figure out how this chicken-egg problem can be avoided. I guess the listener deletion would have to be deferred until the performTriggerAction
method has completed.
In the meantime, do NOT use the construct that is shown in the Readme.
Hi Jonathan
I've been using "Eventually" to de-bouncing control inputs and to control a stepper motor, at present I'm doing the following:
To control stepper motor speed I'm using the following:
mgr.addListener(new EvtTimeListener(0, true, (EvtAction)motorspeed)); //Pulse Stepper Motor
bool motorspeed() { //Pulse output to step motor
if (motorstep == 0) {
motorstep = 1;
digitalWrite(8, HIGH);
} else {
motorstep = 0;
digitalWrite(8, LOW);
}
return false;
}
However the sketch is limiting the stepper motor speed compared to using
#include <AccelStepper.h> and a while statement
Look forward to your comments
Thanks
James
stepperMotor_control.zip
It uses i to seek an empty slot then puts the new even on the end (eventcount) anyway(!)
I was having problems - with events just disappearing (not being set up) - and traced it back to this.
I would like to create a one time event, but it trigger endless fire.
Code snipped:
#include "Eventually.h"
EvtManager mgr;
void setup()
{
Serial.begin(9600);
mgr.addListener(new EvtTimeListener(1000, false, (EvtAction)sayHelloSerial));
}
void loop()
{
mgr.loopIteration();
}
bool sayHelloSerial() {
Serial.println("Hello Serial");
return false;
}
Expected result (after 1s says):
Hello Serial
Actual result (immediately prints endless):
Hello Serial
Hello Serial
Hello Serial
...
I am using version 0.1.5
Is it possible to use this library while retaining the main loop? I would like to integrate it into an existing project but I don't want to rewrite the code currently in loop().
Hi,
Hi, would I remove a particular listener, please?
Thanks
Stephen
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.