Pragmatic solution but not very elegant (need to change my lib whenever the core yield changes). Anyway, it does the job, so I implemented it this way.I would override it and copy also existing yield functionality (backward compatibility)
run_from_yield()
void setup()
{
attachToYield(myFunction)
}
void yield()
{
// do all the usual yield stuff
myFunction();
}
Yes, currently I'm replacing it with my code but in libraries this is quite rude since users might need SerialEvents or the EventResponder.... Also copied the yield content from the core to my yield which works of course but that gets messy when the core yield gets updated as just happened.
#include "EventResponder.h"
EventResponder er;
void testfunc(EventResponderRef r)
{
digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
}
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
er.attach(testfunc);
}
void loop()
{
}
#include "EventResponder.h"
EventResponder er;
void testfunc(EventResponderRef r)
{
digitalWriteFast(LED_BUILTIN, !digitalReadFast(LED_BUILTIN));
r.triggerEvent(); // trigger the next event after finishing this one
}
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
er.attach(testfunc);
er.triggerEvent(); // start the chain
}
void loop()
{
}
#include "EventResponder.h"
void myCallback() // test function will be called from yield
{
digitalToggleFast(0);
}
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
pinMode(0,OUTPUT);
attachYieldFunc(myCallback); // attach the callback to the standard yield stack
}
void loop()
{
digitalToggleFast(LED_BUILTIN);
delay(250);
}
// helpers-------------------------------------------------
void attachYieldFunc(void (*_callback)(void)) // pass a pointer to the function you want to be called from yield
{
static void (*callback)() = _callback; // store the passed in function pointer
static EventResponder er; // define a new EventResponder which will handle the calls.
er.attach([](EventResponderRef r) { // we can not directly attach our function to the responder since we need to retrigger the repsonder
callback(); // instead we attach an inline defined relay function which ...
r.triggerEvent(); //... calls our function and then retriggers the responder to schedule the next call.
});
er.triggerEvent(); // to start the call chain we need to trigger the responder once
}
void *
__wrap_malloc (size_t c)
{
printf ("malloc called with %zu\n", c);
return __real_malloc (c);
}
void TeensyYield() {
// real PJRC yeild
}
attribute 'weak' >> void yield() { TeensyYield(); }