From c1bb448867e888c625973c9dbf5216e95596ee6a Mon Sep 17 00:00:00 2001 From: Joshua Saxby <Joshua2.Saxby@live.uwe.ac.uk> Date: Sun, 9 Feb 2020 01:37:31 +0000 Subject: [PATCH] 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... --- source/main.cpp | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/source/main.cpp b/source/main.cpp index 0c72d35..d77d9ca 100755 --- a/source/main.cpp +++ b/source/main.cpp @@ -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. -- GitLab