I probably don’t fully understanding how threads work, as this is my first adventure into RTOS - but at the same time I want to think this is valid code .
What I am doing in my app is using an EventQueue
to execute non-safe interrupt code everytime an interrupt occurs.
In other words, I have an interrupt which when “triggered”, will add a function to the EventQueue
to be executed later via a separate Thread
I invoked (in addition to the main()
thread).
This was working all nice and dandy until I decided to use a Timeout
object inside this function getting executed by the EventQueue
.
Here is the code
class TouchyLighty
{
public:
MPR121 *pads;
IS31FL3736 *leds;
EventQueue *queue;
Timeout timout;
volatile int row = 0;
volatile uint8_t lastTouched;
TouchyLighty(int _row, MPR121 *touch_ptr, IS31FL3736 *leds_ptr, EventQueue *queue_ptr)
{
pads = touch_ptr;
leds = leds_ptr;
queue = queue_ptr;
row = _row;
pads->attachInteruptCallback(queue->event(callback(pads, &MPR121::handleTouch)));
pads->attachCallbackTouched(callback(this, &TouchyLighty::onTouch));
pads->attachCallbackReleased(callback(this, &TouchyLighty::onRelease));
};
void init()
{
pads->init();
pads->enable();
}
void deluminate() {
leds->setLED(row, lastTouched, false);
}
void onTouch(uint8_t pad)
{
lastTouched = pad;
leds->setLED(row, lastTouched, true);
}
void onRelease(uint8_t pad)
{
lastTouched = pad;
// deluminate();
timout.attach(this, &TouchyLighty::deluminate, 0.3);
}
};
Without going to much into detail, the class uses touch pads, which when touched, triggers and interrupt routine which then adds a function to the EventQueue
to then communicate with the touch IC, find out which pad was touched, and then turn on/off some corresponding LEDs.
If you look at the onRelease()
method, I use the Timeout
object to turn off the LED 0.3 seconds after my finger is released from the touch pad.
timout.attach(this, &TouchyLighty::deluminate, 0.3);
↑↑↑ This is what is freezing the app ↑↑↑
I kinda figured this would be safe to call in a function being executed by EventQueue
, but I guess it is a little sketchy because I am asking the queue to execute something at a later time…
Anyone have expertise on this? Should I be using another Thread
perhaps? Which some Timer
object or something?