Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 #include "MicroBit.h" 00002 #include "MicroBitSamples.h" 00003 #include "MicroBitUARTService.h" 00004 MicroBit uBit; 00005 MicroBitUARTService *uart; 00006 00007 int connected = 0; 00008 00009 void onConnected(MicroBitEvent e) 00010 { 00011 uBit.display.scroll("C"); 00012 00013 connected = 1; 00014 00015 // my client will send ASCII strings terminated with the colon character 00016 ManagedString eom(":"); 00017 00018 while (connected == 1) { 00019 ManagedString msg = uart->readUntil(eom); 00020 uBit.display.scroll(msg); 00021 } 00022 00023 } 00024 00025 void onDisconnected(MicroBitEvent e) 00026 { 00027 uBit.display.scroll("D"); 00028 connected = 0; 00029 } 00030 00031 void onButtonA(MicroBitEvent e) 00032 { 00033 if (connected == 0) { 00034 uBit.display.scroll("NC"); 00035 return; 00036 } 00037 uart->send("YES"); 00038 uBit.display.scroll("YES"); 00039 } 00040 00041 void onButtonB(MicroBitEvent e) 00042 { 00043 if (connected == 0) { 00044 uBit.display.scroll("NC"); 00045 return; 00046 } 00047 uart->send("NO"); 00048 uBit.display.scroll("NO"); 00049 } 00050 00051 void onButtonAB(MicroBitEvent e) 00052 { 00053 if (connected == 0) { 00054 uBit.display.scroll("NC"); 00055 return; 00056 } 00057 uart->send("GOT IT!!"); 00058 uBit.display.scroll("GOT IT!!"); 00059 } 00060 00061 /* 00062 Recommend disabling the DFU and Event services in MicroBitConfig.h since they are not needed here: 00063 00064 #ifndef MICROBIT_BLE_DFU_SERVICE 00065 #define MICROBIT_BLE_DFU_SERVICE 0 00066 #endif 00067 00068 #ifndef MICROBIT_BLE_EVENT_SERVICE 00069 #define MICROBIT_BLE_EVENT_SERVICE 0 00070 #endif 00071 */ 00072 00073 int main() 00074 { 00075 // Initialise the micro:bit runtime. 00076 uBit.init(); 00077 00078 // listen for Bluetooth connection state changes 00079 uBit.messageBus.listen(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_CONNECTED, onConnected); 00080 uBit.messageBus.listen(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_DISCONNECTED, onDisconnected); 00081 00082 // listen for user button interactions 00083 uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA); 00084 uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB); 00085 uBit.messageBus.listen(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK, onButtonAB); 00086 00087 00088 // Note GATT table size increased from default in MicroBitConfig.h 00089 // #define MICROBIT_SD_GATT_TABLE_SIZE 0x500 00090 uart = new MicroBitUARTService(*uBit.ble, 32, 32); 00091 uBit.display.scroll("AVM"); 00092 00093 // If main exits, there may still be other fibers running or registered event handlers etc. 00094 // Simply release this fiber, which will mean we enter the scheduler. Worse case, we then 00095 // sit in the idle task forever, in a power efficient sleep. 00096 release_fiber(); 00097 }
Generated on Tue Aug 2 2022 03:39:41 by
1.7.2