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