Class project using MicroBit coded in C/C++ to send notifications between MicroBit units via radio signal that prompted the LED field to display A if button A was pressed and B if button B was pressed.
main.cpp@0:4c31e3dcca0c, 2019-11-26 (annotated)
- Committer:
- tsfarber
- Date:
- Tue Nov 26 04:19:13 2019 +0000
- Revision:
- 0:4c31e3dcca0c
Tested and verified
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tsfarber | 0:4c31e3dcca0c | 1 | /* |
tsfarber | 0:4c31e3dcca0c | 2 | The MIT License (MIT) |
tsfarber | 0:4c31e3dcca0c | 3 | |
tsfarber | 0:4c31e3dcca0c | 4 | Copyright (c) 2016 British Broadcasting Corporation. |
tsfarber | 0:4c31e3dcca0c | 5 | This software is provided by Lancaster University by arrangement with the BBC. |
tsfarber | 0:4c31e3dcca0c | 6 | |
tsfarber | 0:4c31e3dcca0c | 7 | Permission is hereby granted, free of charge, to any person obtaining a |
tsfarber | 0:4c31e3dcca0c | 8 | copy of this software and associated documentation files (the "Software"), |
tsfarber | 0:4c31e3dcca0c | 9 | to deal in the Software without restriction, including without limitation |
tsfarber | 0:4c31e3dcca0c | 10 | the rights to use, copy, modify, merge, publish, distribute, sublicense, |
tsfarber | 0:4c31e3dcca0c | 11 | and/or sell copies of the Software, and to permit persons to whom the |
tsfarber | 0:4c31e3dcca0c | 12 | Software is furnished to do so, subject to the following conditions: |
tsfarber | 0:4c31e3dcca0c | 13 | |
tsfarber | 0:4c31e3dcca0c | 14 | The above copyright notice and this permission notice shall be included in |
tsfarber | 0:4c31e3dcca0c | 15 | all copies or substantial portions of the Software. |
tsfarber | 0:4c31e3dcca0c | 16 | |
tsfarber | 0:4c31e3dcca0c | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
tsfarber | 0:4c31e3dcca0c | 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
tsfarber | 0:4c31e3dcca0c | 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
tsfarber | 0:4c31e3dcca0c | 20 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
tsfarber | 0:4c31e3dcca0c | 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
tsfarber | 0:4c31e3dcca0c | 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
tsfarber | 0:4c31e3dcca0c | 23 | DEALINGS IN THE SOFTWARE. |
tsfarber | 0:4c31e3dcca0c | 24 | */ |
tsfarber | 0:4c31e3dcca0c | 25 | |
tsfarber | 0:4c31e3dcca0c | 26 | #include "MicroBit.h" |
tsfarber | 0:4c31e3dcca0c | 27 | |
tsfarber | 0:4c31e3dcca0c | 28 | |
tsfarber | 0:4c31e3dcca0c | 29 | MicroBit uBit; |
tsfarber | 0:4c31e3dcca0c | 30 | |
tsfarber | 0:4c31e3dcca0c | 31 | void onData(MicroBitEvent) |
tsfarber | 0:4c31e3dcca0c | 32 | { |
tsfarber | 0:4c31e3dcca0c | 33 | ManagedString s = uBit.radio.datagram.recv(); |
tsfarber | 0:4c31e3dcca0c | 34 | |
tsfarber | 0:4c31e3dcca0c | 35 | if (s == "1") |
tsfarber | 0:4c31e3dcca0c | 36 | uBit.display.print("A"); |
tsfarber | 0:4c31e3dcca0c | 37 | |
tsfarber | 0:4c31e3dcca0c | 38 | if (s == "2") |
tsfarber | 0:4c31e3dcca0c | 39 | uBit.display.print("B"); |
tsfarber | 0:4c31e3dcca0c | 40 | } |
tsfarber | 0:4c31e3dcca0c | 41 | |
tsfarber | 0:4c31e3dcca0c | 42 | |
tsfarber | 0:4c31e3dcca0c | 43 | |
tsfarber | 0:4c31e3dcca0c | 44 | int main() |
tsfarber | 0:4c31e3dcca0c | 45 | { |
tsfarber | 0:4c31e3dcca0c | 46 | // Initialise the micro:bit runtime. |
tsfarber | 0:4c31e3dcca0c | 47 | uBit.init(); |
tsfarber | 0:4c31e3dcca0c | 48 | uBit.radio.enable(); |
tsfarber | 0:4c31e3dcca0c | 49 | |
tsfarber | 0:4c31e3dcca0c | 50 | |
tsfarber | 0:4c31e3dcca0c | 51 | uBit.messageBus.listen(MICROBIT_ID_RADIO, MICROBIT_RADIO_EVT_DATAGRAM, onData); |
tsfarber | 0:4c31e3dcca0c | 52 | |
tsfarber | 0:4c31e3dcca0c | 53 | |
tsfarber | 0:4c31e3dcca0c | 54 | while(1) |
tsfarber | 0:4c31e3dcca0c | 55 | { |
tsfarber | 0:4c31e3dcca0c | 56 | if (uBit.buttonA.isPressed()) |
tsfarber | 0:4c31e3dcca0c | 57 | { |
tsfarber | 0:4c31e3dcca0c | 58 | uBit.radio.datagram.send("1"); |
tsfarber | 0:4c31e3dcca0c | 59 | uBit.display.print("A"); |
tsfarber | 0:4c31e3dcca0c | 60 | } |
tsfarber | 0:4c31e3dcca0c | 61 | else if (uBit.buttonB.isPressed()) |
tsfarber | 0:4c31e3dcca0c | 62 | { |
tsfarber | 0:4c31e3dcca0c | 63 | uBit.radio.datagram.send("2"); |
tsfarber | 0:4c31e3dcca0c | 64 | uBit.display.print("B"); |
tsfarber | 0:4c31e3dcca0c | 65 | } |
tsfarber | 0:4c31e3dcca0c | 66 | uBit.sleep(100); |
tsfarber | 0:4c31e3dcca0c | 67 | } |
tsfarber | 0:4c31e3dcca0c | 68 | } |