Starter project for Bluetooth SIG hands-on training course
Embed:
(wiki syntax)
Show/hide line numbers
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 Animation 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 // 8th March 2017 00027 00028 #include "MicroBit.h" 00029 #include "Animator.h" 00030 #include <math.h> 00031 00032 MicroBit uBit; 00033 00034 Animator my_animator(uBit.display); 00035 00036 void animationLoop() { 00037 while (my_animator.playing == 1) { 00038 switch (my_animator.current_animation) { 00039 case ANIMATION_TYPE_FLASH: 00040 my_animator.flash(); 00041 break; 00042 case ANIMATION_TYPE_RIPPLE: 00043 my_animator.ripple(); 00044 break; 00045 case ANIMATION_TYPE_SPIRAL: 00046 my_animator.spiral(); 00047 break; 00048 } 00049 fiber_sleep(my_animator.sleep_time); 00050 } 00051 } 00052 00053 void onConnected(MicroBitEvent) 00054 { 00055 uBit.display.print("C"); 00056 } 00057 00058 void onDisconnected(MicroBitEvent) 00059 { 00060 uBit.display.print("D"); 00061 } 00062 00063 void animationControl(MicroBitEvent e) 00064 { 00065 if (e.value == ANIMATION_CONTROL_START) { 00066 if (my_animator.playing == 0) { 00067 my_animator.start(); 00068 create_fiber(animationLoop); 00069 } 00070 return; 00071 } 00072 if (e.value == ANIMATION_CONTROL_STOP) { 00073 my_animator.stop(); 00074 return; 00075 } 00076 if (e.value == ANIMATION_CONTROL_FASTER) { 00077 my_animator.faster(); 00078 return; 00079 } 00080 if (e.value == ANIMATION_CONTROL_SLOWER) { 00081 my_animator.slower(); 00082 return; 00083 } 00084 } 00085 00086 void animationType(MicroBitEvent e) 00087 { 00088 my_animator.setAnimationType(e.value); 00089 } 00090 00091 void onButton(MicroBitEvent e) 00092 { 00093 if (e.source == MICROBIT_ID_BUTTON_B) { 00094 uBit.display.scroll(uBit.getName()); 00095 return; 00096 } 00097 } 00098 00099 int main() 00100 { 00101 // Initialise the micro:bit runtime. 00102 uBit.init(); 00103 00104 // Insert your code here! 00105 uBit.display.scroll("A"); 00106 00107 uBit.messageBus.listen(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_CONNECTED, onConnected); 00108 uBit.messageBus.listen(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_DISCONNECTED, onDisconnected); 00109 00110 uBit.messageBus.listen(ANIMATION_CONTROL_EVENT, MICROBIT_EVT_ANY, animationControl); 00111 uBit.messageBus.listen(ANIMATION_TYPE_EVENT, MICROBIT_EVT_ANY, animationType); 00112 00113 uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButton); 00114 00115 /* NOTES 00116 * ----- 00117 * 00118 * Add #include "MicroBitAnimationService.h" to MicroBitBLEManager.h 00119 * 00120 * In MicroBitConfig.h: 00121 * #define MICROBIT_BLE_OPEN 1 00122 * #define MICROBIT_SD_GATT_TABLE_SIZE 0x500 00123 * 00124 * See https://developer.mbed.org/teams/Bluetooth-Low-Energy/code/BLE_GATT_Example/ for additional information on mbed support for Bluetooth low energy 00125 * 00126 */ 00127 new MicroBitAnimationService(*uBit.ble); 00128 00129 // If main exits, there may still be other fibers running or registered event handlers etc. 00130 // Simply release this fiber, which will mean we enter the scheduler. Worse case, we then 00131 // sit in the idle task forever, in a power efficient sleep. 00132 release_fiber(); 00133 }
Generated on Sun Jul 24 2022 05:43:06 by
![doxygen](doxygen.png)