Starter project for Bluetooth SIG hands-on training course

Dependencies:   microbit

Committer:
bluetooth_mdw
Date:
Mon Feb 06 09:23:23 2017 +0000
Revision:
0:b821de9e1c1f
Child:
2:ceab125d609b
Button B display micro:bit name

Who changed what in which revision?

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