Solution to the Bluetooth animation service exercise

Dependencies:   microbit

Fork of microbit-animator by Martin Woolley

Committer:
bluetooth_mdw
Date:
Mon Feb 06 09:44:09 2017 +0000
Revision:
7:1878a4427199
Parent:
5:83b5fdb655d4
Solution for Bluetooth SIG hands-on training course

Who changed what in which revision?

UserRevisionLine numberNew contents of line
bluetooth_mdw 0:394f7f99082b 1 /*
bluetooth_mdw 0:394f7f99082b 2 The MIT License (MIT)
bluetooth_mdw 0:394f7f99082b 3
bluetooth_mdw 0:394f7f99082b 4 Copyright (c) 2016 British Broadcasting Corporation.
bluetooth_mdw 0:394f7f99082b 5 This software is provided by Lancaster University by arrangement with the BBC.
bluetooth_mdw 0:394f7f99082b 6
bluetooth_mdw 0:394f7f99082b 7 Permission is hereby granted, free of charge, to any person obtaining a
bluetooth_mdw 0:394f7f99082b 8 copy of this software and associated documentation files (the "Software"),
bluetooth_mdw 0:394f7f99082b 9 to deal in the Software without restriction, including without limitation
bluetooth_mdw 0:394f7f99082b 10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
bluetooth_mdw 0:394f7f99082b 11 and/or sell copies of the Software, and to permit persons to whom the
bluetooth_mdw 0:394f7f99082b 12 Software is furnished to do so, subject to the following conditions:
bluetooth_mdw 0:394f7f99082b 13
bluetooth_mdw 0:394f7f99082b 14 The above copyright notice and this permission notice shall be included in
bluetooth_mdw 0:394f7f99082b 15 all copies or substantial portions of the Software.
bluetooth_mdw 0:394f7f99082b 16
bluetooth_mdw 0:394f7f99082b 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
bluetooth_mdw 0:394f7f99082b 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
bluetooth_mdw 0:394f7f99082b 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO Animation SHALL
bluetooth_mdw 0:394f7f99082b 20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
bluetooth_mdw 0:394f7f99082b 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
bluetooth_mdw 0:394f7f99082b 22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
bluetooth_mdw 0:394f7f99082b 23 DEALINGS IN THE SOFTWARE.
bluetooth_mdw 0:394f7f99082b 24 */
bluetooth_mdw 0:394f7f99082b 25
bluetooth_mdw 0:394f7f99082b 26 #include "MicroBit.h"
bluetooth_mdw 0:394f7f99082b 27 #include "Animator.h"
bluetooth_mdw 0:394f7f99082b 28 #include <math.h>
bluetooth_mdw 0:394f7f99082b 29
bluetooth_mdw 0:394f7f99082b 30 MicroBit uBit;
bluetooth_mdw 0:394f7f99082b 31
bluetooth_mdw 0:394f7f99082b 32 Animator my_animator(uBit.display);
bluetooth_mdw 0:394f7f99082b 33
bluetooth_mdw 0:394f7f99082b 34 void animationLoop() {
bluetooth_mdw 0:394f7f99082b 35 while (my_animator.playing == 1) {
bluetooth_mdw 0:394f7f99082b 36 switch (my_animator.current_animation) {
bluetooth_mdw 0:394f7f99082b 37 case ANIMATION_TYPE_FLASH:
bluetooth_mdw 0:394f7f99082b 38 my_animator.flash();
bluetooth_mdw 0:394f7f99082b 39 break;
bluetooth_mdw 0:394f7f99082b 40 case ANIMATION_TYPE_RIPPLE:
bluetooth_mdw 0:394f7f99082b 41 my_animator.ripple();
bluetooth_mdw 0:394f7f99082b 42 break;
bluetooth_mdw 0:394f7f99082b 43 case ANIMATION_TYPE_SPIRAL:
bluetooth_mdw 0:394f7f99082b 44 my_animator.spiral();
bluetooth_mdw 0:394f7f99082b 45 break;
bluetooth_mdw 0:394f7f99082b 46 }
bluetooth_mdw 0:394f7f99082b 47 fiber_sleep(my_animator.sleep_time);
bluetooth_mdw 0:394f7f99082b 48 }
bluetooth_mdw 0:394f7f99082b 49 }
bluetooth_mdw 0:394f7f99082b 50
bluetooth_mdw 0:394f7f99082b 51 void onConnected(MicroBitEvent)
bluetooth_mdw 0:394f7f99082b 52 {
bluetooth_mdw 0:394f7f99082b 53 uBit.display.print("C");
bluetooth_mdw 0:394f7f99082b 54 }
bluetooth_mdw 0:394f7f99082b 55
bluetooth_mdw 0:394f7f99082b 56 void onDisconnected(MicroBitEvent)
bluetooth_mdw 0:394f7f99082b 57 {
bluetooth_mdw 0:394f7f99082b 58 uBit.display.print("D");
bluetooth_mdw 0:394f7f99082b 59 my_animator.stop();
bluetooth_mdw 0:394f7f99082b 60 }
bluetooth_mdw 0:394f7f99082b 61
bluetooth_mdw 0:394f7f99082b 62 void animationControl(MicroBitEvent e)
bluetooth_mdw 0:394f7f99082b 63 {
bluetooth_mdw 0:394f7f99082b 64 if (e.value == ANIMATION_CONTROL_START) {
bluetooth_mdw 0:394f7f99082b 65 my_animator.start();
bluetooth_mdw 0:394f7f99082b 66 create_fiber(animationLoop);
bluetooth_mdw 0:394f7f99082b 67 return;
bluetooth_mdw 0:394f7f99082b 68 }
bluetooth_mdw 0:394f7f99082b 69 if (e.value == ANIMATION_CONTROL_STOP) {
bluetooth_mdw 0:394f7f99082b 70 my_animator.stop();
bluetooth_mdw 0:394f7f99082b 71 return;
bluetooth_mdw 0:394f7f99082b 72 }
bluetooth_mdw 0:394f7f99082b 73 }
bluetooth_mdw 0:394f7f99082b 74
bluetooth_mdw 0:394f7f99082b 75 void animationType(MicroBitEvent e)
bluetooth_mdw 0:394f7f99082b 76 {
bluetooth_mdw 0:394f7f99082b 77 my_animator.setAnimationType(e.value);
bluetooth_mdw 0:394f7f99082b 78 }
bluetooth_mdw 5:83b5fdb655d4 79
bluetooth_mdw 5:83b5fdb655d4 80 void onButton(MicroBitEvent e)
bluetooth_mdw 5:83b5fdb655d4 81 {
bluetooth_mdw 5:83b5fdb655d4 82 if (e.source == MICROBIT_ID_BUTTON_B) {
bluetooth_mdw 5:83b5fdb655d4 83 uBit.display.scroll(uBit.getName());
bluetooth_mdw 5:83b5fdb655d4 84 return;
bluetooth_mdw 5:83b5fdb655d4 85 }
bluetooth_mdw 5:83b5fdb655d4 86 }
bluetooth_mdw 0:394f7f99082b 87
bluetooth_mdw 0:394f7f99082b 88 int main()
bluetooth_mdw 0:394f7f99082b 89 {
bluetooth_mdw 0:394f7f99082b 90 // Initialise the micro:bit runtime.
bluetooth_mdw 0:394f7f99082b 91 uBit.init();
bluetooth_mdw 0:394f7f99082b 92
bluetooth_mdw 0:394f7f99082b 93 // Insert your code here!
bluetooth_mdw 0:394f7f99082b 94 uBit.display.scroll("A");
bluetooth_mdw 0:394f7f99082b 95
bluetooth_mdw 0:394f7f99082b 96 uBit.messageBus.listen(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_CONNECTED, onConnected);
bluetooth_mdw 0:394f7f99082b 97 uBit.messageBus.listen(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_DISCONNECTED, onDisconnected);
bluetooth_mdw 0:394f7f99082b 98
bluetooth_mdw 0:394f7f99082b 99 uBit.messageBus.listen(ANIMATION_CONTROL_EVENT, MICROBIT_EVT_ANY, animationControl);
bluetooth_mdw 0:394f7f99082b 100 uBit.messageBus.listen(ANIMATION_TYPE_EVENT, MICROBIT_EVT_ANY, animationType);
bluetooth_mdw 0:394f7f99082b 101
bluetooth_mdw 5:83b5fdb655d4 102 uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButton);
bluetooth_mdw 0:394f7f99082b 103
bluetooth_mdw 0:394f7f99082b 104 /* NOTES
bluetooth_mdw 0:394f7f99082b 105 * -----
bluetooth_mdw 0:394f7f99082b 106 *
bluetooth_mdw 0:394f7f99082b 107 * Add #include "MicroBitAnimationService.h" to MicroBitBLEManager.h
bluetooth_mdw 0:394f7f99082b 108 *
bluetooth_mdw 0:394f7f99082b 109 * In MicroBitConfig.h:
bluetooth_mdw 0:394f7f99082b 110 * #define MICROBIT_BLE_OPEN 1
bluetooth_mdw 0:394f7f99082b 111 * #define MICROBIT_SD_GATT_TABLE_SIZE 0x500
bluetooth_mdw 0:394f7f99082b 112 *
bluetooth_mdw 0:394f7f99082b 113 * See https://developer.mbed.org/teams/Bluetooth-Low-Energy/code/BLE_GATT_Example/ for additional information on mbed support for Bluetooth low energy
bluetooth_mdw 0:394f7f99082b 114 *
bluetooth_mdw 0:394f7f99082b 115 */
bluetooth_mdw 0:394f7f99082b 116 new MicroBitAnimationService(*uBit.ble);
bluetooth_mdw 0:394f7f99082b 117
bluetooth_mdw 0:394f7f99082b 118 // If main exits, there may still be other fibers running or registered event handlers etc.
bluetooth_mdw 0:394f7f99082b 119 // Simply release this fiber, which will mean we enter the scheduler. Worse case, we then
bluetooth_mdw 0:394f7f99082b 120 // sit in the idle task forever, in a power efficient sleep.
bluetooth_mdw 0:394f7f99082b 121 release_fiber();
bluetooth_mdw 0:394f7f99082b 122 }
bluetooth_mdw 0:394f7f99082b 123