An example using the DAL's radio module that sends and recieves messages to let you know when someone is nearby. This is a one-way translation of the microbit-samples repository on GitHub. Please don't try to push changes here, instead push them to the source repo at https://github.com/lancaster-university/microbit-samples
Dependencies: microbit
main.cpp
00001 /* 00002 The MIT License (MIT) 00003 00004 Copyright (c) 2016 British Broadcasting Corporation. 00005 This software is provided by Lancaster University by arrangement with the BBC. 00006 00007 Permission is hereby granted, free of charge, to any person obtaining a 00008 copy of this software and associated documentation files (the "Software"), 00009 to deal in the Software without restriction, including without limitation 00010 the rights to use, copy, modify, merge, publish, distribute, sublicense, 00011 and/or sell copies of the Software, and to permit persons to whom the 00012 Software is furnished to do so, subject to the following conditions: 00013 00014 The above copyright notice and this permission notice shall be included in 00015 all copies or substantial portions of the Software. 00016 00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00020 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00022 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00023 DEALINGS IN THE SOFTWARE. 00024 */ 00025 00026 00027 /* 00028 * This is a simple example that uses the micro:bit radio to show if another 00029 * micro:bit running the same code is nearby. 00030 * 00031 * Each micro:bit is periodically broadcasting a group name, and listening 00032 * to see if another micro:bit in that group is also in-range. 00033 * 00034 * When another micro:bit in the same group is detected, the display of the 00035 * detecting micro:bit will show a filled-in heart shape 00036 * 00037 * Otherwise, the display will show an empty heart shape 00038 * 00039 */ 00040 00041 #include "MicroBit.h" 00042 00043 #if MICROBIT_BLE_ENABLED 00044 #ifdef YOTTA_CFG 00045 #error "This example needs BLE to be disabled. Use the yotta config.json in the proximit-heart directory to do this" 00046 #else 00047 #error "This example needs BLE to be disabled in the microbit config file in the microbit-dal: MicroBitConfig.h" 00048 #endif 00049 #endif 00050 00051 MicroBit uBit; 00052 00053 // Have we seen a friend recently? 00054 uint8_t friend_seen = 0; 00055 00056 // Are we currently sending out messages? 00057 uint8_t broadcast = 1; 00058 00059 /* We have a group name, and if any micro:bit is in range and in the group 00060 * then the others will see it. 00061 */ 00062 const char group_name[] = "tiger"; 00063 00064 const uint8_t empty_heart_arr[] { 00065 0, 1, 0, 1, 0, 00066 1, 0, 1, 0, 1, 00067 1, 0, 0, 0, 1, 00068 0, 1, 0, 1, 0, 00069 0, 0, 1, 0, 0, }; 00070 00071 const uint8_t full_heart_arr[] { 00072 0, 1, 0, 1, 0, 00073 1, 1, 1, 1, 1, 00074 1, 1, 1, 1, 1, 00075 0, 1, 1, 1, 0, 00076 0, 0, 1, 0, 0, }; 00077 00078 const uint8_t small_heart_arr[] { 00079 0, 0, 0, 0, 0, 00080 0, 1, 0, 1, 0, 00081 0, 1, 1, 1, 0, 00082 0, 1, 1, 0, 0, 00083 0, 0, 0, 0, 0, }; 00084 00085 MicroBitImage empty_heart(5,5,empty_heart_arr); 00086 MicroBitImage full_heart(5,5,full_heart_arr); 00087 MicroBitImage small_heart(5,5,small_heart_arr); 00088 00089 /* We send messages when people press buttons 'A' and 'B'. 00090 * At the moment, all micro:bits listening for messages 00091 * will see these and can respond to them 00092 * Challenge: make only certain micro:bits respond to these 00093 */ 00094 void onButtonA(MicroBitEvent) 00095 { 00096 uBit.radio.datagram.send("1"); 00097 } 00098 00099 void onButtonB(MicroBitEvent) 00100 { 00101 uBit.radio.datagram.send("2"); 00102 } 00103 00104 /* We toggle broadcasting if both buttons are pressed together */ 00105 void onButtonAB(MicroBitEvent) 00106 { 00107 broadcast = !broadcast; 00108 uBit.display.print("!"); 00109 } 00110 00111 00112 void onData(MicroBitEvent) 00113 { 00114 ManagedString s = uBit.radio.datagram.recv(); 00115 int rssi = uBit.radio.getRSSI(); 00116 00117 if (s == "1") 00118 uBit.display.print(full_heart); 00119 00120 if (s == "2") 00121 uBit.display.print(small_heart); 00122 00123 /* For detecting the presence of our friend, we require them to be sending 00124 * the same group name as we are in 00125 */ 00126 if (s == group_name && rssi < 70) { 00127 // We can make this larger to allow more missed packets 00128 friend_seen = 3; 00129 } 00130 } 00131 00132 int main() 00133 { 00134 // Initialise the micro:bit runtime. 00135 uBit.init(); 00136 00137 // Setup some button handlers to allow direct heartbeat control with buttons 00138 uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_EVT_ANY, onButtonA); 00139 uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_EVT_ANY, onButtonB); 00140 uBit.messageBus.listen(MICROBIT_ID_BUTTON_AB, MICROBIT_EVT_ANY, onButtonAB); 00141 00142 //Setup a hander to run when data is received 00143 uBit.messageBus.listen(MICROBIT_ID_RADIO, MICROBIT_RADIO_EVT_DATAGRAM, onData); 00144 00145 uBit.radio.enable(); 00146 00147 while(1) { 00148 if (friend_seen) { 00149 uBit.display.print(full_heart); 00150 friend_seen -= 1; 00151 } else { 00152 uBit.display.print(empty_heart); 00153 } 00154 00155 if (broadcast) 00156 uBit.radio.datagram.send(group_name); 00157 00158 uBit.sleep(1000); 00159 } 00160 }
Generated on Sat Jul 16 2022 00:19:15 by 1.7.2