Kai Ren / BluetoothWorld2018Peripheral

Dependencies:   microbit

Fork of BluetoothAsia2018Peripheral by Kai Ren

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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