This repository will be used for Bluetooth World 2018.
Fork of BluetoothAsia2018Peripheral by
main.cpp@14:fcab3952e945, 2018-08-06 (annotated)
- Committer:
- krenbluetoothsig
- Date:
- Mon Aug 06 08:26:38 2018 +0000
- Revision:
- 14:fcab3952e945
- Parent:
- 10:bdd5a54dd4ce
1. NEW: just update "microbit" and folked it.
Who changed what in which revision?
User | Revision | Line number | New 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 | 2:ceab125d609b | 26 | // 8th March 2017 |
bluetooth_mdw | 0:b821de9e1c1f | 27 | #include "MicroBit.h" |
bluetooth_mdw | 0:b821de9e1c1f | 28 | #include "Animator.h" |
bluetooth_mdw | 0:b821de9e1c1f | 29 | #include <math.h> |
bluetooth_mdw | 0:b821de9e1c1f | 30 | |
bluetooth_mdw | 0:b821de9e1c1f | 31 | MicroBit uBit; |
bluetooth_mdw | 0:b821de9e1c1f | 32 | |
bluetooth_mdw | 0:b821de9e1c1f | 33 | Animator my_animator(uBit.display); |
bluetooth_mdw | 0:b821de9e1c1f | 34 | |
bluetooth_mdw | 0:b821de9e1c1f | 35 | void animationLoop() { |
bluetooth_mdw | 0:b821de9e1c1f | 36 | while (my_animator.playing == 1) { |
bluetooth_mdw | 0:b821de9e1c1f | 37 | switch (my_animator.current_animation) { |
bluetooth_mdw | 0:b821de9e1c1f | 38 | case ANIMATION_TYPE_FLASH: |
bluetooth_mdw | 0:b821de9e1c1f | 39 | my_animator.flash(); |
bluetooth_mdw | 0:b821de9e1c1f | 40 | break; |
bluetooth_mdw | 0:b821de9e1c1f | 41 | case ANIMATION_TYPE_RIPPLE: |
bluetooth_mdw | 0:b821de9e1c1f | 42 | my_animator.ripple(); |
bluetooth_mdw | 0:b821de9e1c1f | 43 | break; |
bluetooth_mdw | 0:b821de9e1c1f | 44 | case ANIMATION_TYPE_SPIRAL: |
bluetooth_mdw | 0:b821de9e1c1f | 45 | my_animator.spiral(); |
bluetooth_mdw | 0:b821de9e1c1f | 46 | break; |
bluetooth_mdw | 0:b821de9e1c1f | 47 | } |
bluetooth_mdw | 0:b821de9e1c1f | 48 | fiber_sleep(my_animator.sleep_time); |
bluetooth_mdw | 0:b821de9e1c1f | 49 | } |
bluetooth_mdw | 0:b821de9e1c1f | 50 | } |
bluetooth_mdw | 0:b821de9e1c1f | 51 | |
bluetooth_mdw | 0:b821de9e1c1f | 52 | void onConnected(MicroBitEvent) |
bluetooth_mdw | 0:b821de9e1c1f | 53 | { |
bluetooth_mdw | 0:b821de9e1c1f | 54 | uBit.display.print("C"); |
bluetooth_mdw | 0:b821de9e1c1f | 55 | } |
bluetooth_mdw | 0:b821de9e1c1f | 56 | |
bluetooth_mdw | 0:b821de9e1c1f | 57 | void onDisconnected(MicroBitEvent) |
bluetooth_mdw | 0:b821de9e1c1f | 58 | { |
bluetooth_mdw | 0:b821de9e1c1f | 59 | uBit.display.print("D"); |
bluetooth_mdw | 0:b821de9e1c1f | 60 | } |
bluetooth_mdw | 0:b821de9e1c1f | 61 | |
bluetooth_mdw | 0:b821de9e1c1f | 62 | void animationControl(MicroBitEvent e) |
bluetooth_mdw | 0:b821de9e1c1f | 63 | { |
bluetooth_mdw | 0:b821de9e1c1f | 64 | if (e.value == ANIMATION_CONTROL_START) { |
bluetooth_mdw | 0:b821de9e1c1f | 65 | if (my_animator.playing == 0) { |
bluetooth_mdw | 0:b821de9e1c1f | 66 | my_animator.start(); |
bluetooth_mdw | 0:b821de9e1c1f | 67 | create_fiber(animationLoop); |
bluetooth_mdw | 0:b821de9e1c1f | 68 | } |
bluetooth_mdw | 0:b821de9e1c1f | 69 | return; |
bluetooth_mdw | 0:b821de9e1c1f | 70 | } |
bluetooth_mdw | 0:b821de9e1c1f | 71 | if (e.value == ANIMATION_CONTROL_STOP) { |
bluetooth_mdw | 0:b821de9e1c1f | 72 | my_animator.stop(); |
bluetooth_mdw | 0:b821de9e1c1f | 73 | return; |
bluetooth_mdw | 0:b821de9e1c1f | 74 | } |
bluetooth_mdw | 0:b821de9e1c1f | 75 | if (e.value == ANIMATION_CONTROL_FASTER) { |
bluetooth_mdw | 0:b821de9e1c1f | 76 | my_animator.faster(); |
bluetooth_mdw | 0:b821de9e1c1f | 77 | return; |
bluetooth_mdw | 0:b821de9e1c1f | 78 | } |
bluetooth_mdw | 0:b821de9e1c1f | 79 | if (e.value == ANIMATION_CONTROL_SLOWER) { |
bluetooth_mdw | 0:b821de9e1c1f | 80 | my_animator.slower(); |
bluetooth_mdw | 0:b821de9e1c1f | 81 | return; |
bluetooth_mdw | 0:b821de9e1c1f | 82 | } |
bluetooth_mdw | 0:b821de9e1c1f | 83 | } |
bluetooth_mdw | 0:b821de9e1c1f | 84 | |
bluetooth_mdw | 0:b821de9e1c1f | 85 | void animationType(MicroBitEvent e) |
bluetooth_mdw | 0:b821de9e1c1f | 86 | { |
bluetooth_mdw | 0:b821de9e1c1f | 87 | my_animator.setAnimationType(e.value); |
bluetooth_mdw | 0:b821de9e1c1f | 88 | } |
bluetooth_mdw | 0:b821de9e1c1f | 89 | |
bluetooth_mdw | 0:b821de9e1c1f | 90 | void onButton(MicroBitEvent e) |
bluetooth_mdw | 0:b821de9e1c1f | 91 | { |
bluetooth_mdw | 0:b821de9e1c1f | 92 | if (e.source == MICROBIT_ID_BUTTON_B) { |
bluetooth_mdw | 0:b821de9e1c1f | 93 | uBit.display.scroll(uBit.getName()); |
bluetooth_mdw | 0:b821de9e1c1f | 94 | return; |
bluetooth_mdw | 0:b821de9e1c1f | 95 | } |
bluetooth_mdw | 0:b821de9e1c1f | 96 | } |
bluetooth_mdw | 0:b821de9e1c1f | 97 | |
bluetooth_mdw | 0:b821de9e1c1f | 98 | int main() |
bluetooth_mdw | 0:b821de9e1c1f | 99 | { |
bluetooth_mdw | 0:b821de9e1c1f | 100 | // Initialise the micro:bit runtime. |
bluetooth_mdw | 0:b821de9e1c1f | 101 | uBit.init(); |
bluetooth_mdw | 0:b821de9e1c1f | 102 | |
bluetooth_mdw | 0:b821de9e1c1f | 103 | // Insert your code here! |
bluetooth_mdw | 0:b821de9e1c1f | 104 | uBit.display.scroll("A"); |
bluetooth_mdw | 0:b821de9e1c1f | 105 | |
bluetooth_mdw | 0:b821de9e1c1f | 106 | uBit.messageBus.listen(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_CONNECTED, onConnected); |
bluetooth_mdw | 0:b821de9e1c1f | 107 | uBit.messageBus.listen(MICROBIT_ID_BLE, MICROBIT_BLE_EVT_DISCONNECTED, onDisconnected); |
bluetooth_mdw | 0:b821de9e1c1f | 108 | |
bluetooth_mdw | 0:b821de9e1c1f | 109 | uBit.messageBus.listen(ANIMATION_CONTROL_EVENT, MICROBIT_EVT_ANY, animationControl); |
bluetooth_mdw | 0:b821de9e1c1f | 110 | uBit.messageBus.listen(ANIMATION_TYPE_EVENT, MICROBIT_EVT_ANY, animationType); |
bluetooth_mdw | 0:b821de9e1c1f | 111 | |
bluetooth_mdw | 0:b821de9e1c1f | 112 | uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButton); |
bluetooth_mdw | 0:b821de9e1c1f | 113 | |
bluetooth_mdw | 0:b821de9e1c1f | 114 | /* NOTES |
bluetooth_mdw | 0:b821de9e1c1f | 115 | * ----- |
bluetooth_mdw | 0:b821de9e1c1f | 116 | * |
bluetooth_mdw | 0:b821de9e1c1f | 117 | * Add #include "MicroBitAnimationService.h" to MicroBitBLEManager.h |
bluetooth_mdw | 0:b821de9e1c1f | 118 | * |
bluetooth_mdw | 0:b821de9e1c1f | 119 | * In MicroBitConfig.h: |
bluetooth_mdw | 0:b821de9e1c1f | 120 | * #define MICROBIT_BLE_OPEN 1 |
bluetooth_mdw | 0:b821de9e1c1f | 121 | * #define MICROBIT_SD_GATT_TABLE_SIZE 0x500 |
bluetooth_mdw | 0:b821de9e1c1f | 122 | * |
bluetooth_mdw | 0:b821de9e1c1f | 123 | * 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 | 124 | * |
bluetooth_mdw | 0:b821de9e1c1f | 125 | */ |
bluetooth_mdw | 0:b821de9e1c1f | 126 | new MicroBitAnimationService(*uBit.ble); |
bluetooth_mdw | 0:b821de9e1c1f | 127 | |
bluetooth_mdw | 0:b821de9e1c1f | 128 | // If main exits, there may still be other fibers running or registered event handlers etc. |
bluetooth_mdw | 0:b821de9e1c1f | 129 | // Simply release this fiber, which will mean we enter the scheduler. Worse case, we then |
bluetooth_mdw | 0:b821de9e1c1f | 130 | // sit in the idle task forever, in a power efficient sleep. |
bluetooth_mdw | 0:b821de9e1c1f | 131 | release_fiber(); |
bluetooth_mdw | 0:b821de9e1c1f | 132 | } |