Skip to content
Snippets Groups Projects
Commit c1bb4488 authored by ja3-saxby's avatar ja3-saxby
Browse files

I've worked out what the issue is

If interacting with the singleton MicroBit instance via a reference or pointer, setting up event listeners doesn't work.
So, the solution is to use a global variable for it.
Perhaps this should be reported as a bug in mBed OS...
parent 84e05765
Branches
Tags
No related merge requests found
......@@ -17,36 +17,38 @@
#include "MicroBit.h"
MicroBit micro_bit;
MicroBit ubit;
class EventThingy {
public:
EventThingy() {
ubit.messageBus.listen(
MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, this, &EventThingy::event_handler_a
);
ubit.messageBus.listen(
MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, this, &EventThingy::event_handler_b
);
}
void event_handler_a(MicroBitEvent) {
micro_bit.display.image.setPixelValue(0, 2, 1);
ubit.display.image.setPixelValue(2, 2, 1);
}
void event_handler_b(MicroBitEvent) {
micro_bit.display.image.setPixelValue(4, 2, 1);
ubit.display.image.setPixelValue(2, 2, 0);
}
};
EventThingy eventer;
int main() {
// Initialise the micro:bit runtime.
micro_bit.init();
micro_bit.messageBus.listen(
MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, &eventer, &EventThingy::event_handler_a
);
ubit.init();
micro_bit.messageBus.listen(
MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, &eventer, &EventThingy::event_handler_b
);
EventThingy eventer;
while (true) {
micro_bit.sleep(10); // 10ms sleep, or 100Hz tick rate
ubit.sleep(10); // 10ms sleep, or 100Hz tick rate
}
// TODO: potentially remove this call, if clarified that it is not required.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment