Core51822 module helloworld. BLE disable version.
Dependencies: microbit_ble_disable
Fork of microbit-samples by
Core51822モジュールでmicro:bitのDAL(Device Abstraction Layer)を使います。
注意
BLE(Bluetooth Low Energy)は無効にしています。
source/ButtonEvents.cpp@2:5b2cd31ac474, 2016-04-08 (annotated)
- Committer:
- LancasterUniversity
- Date:
- Fri Apr 08 16:59:43 2016 +0100
- Revision:
- 2:5b2cd31ac474
Synchronized with git rev 8f9a0bcc
Author: Joe Finney
microbit-samples: Added a suite of basic samples
Also introduced MicroBitSamples.h as a simple, cross IDE mechanism to choose
which sample to build.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
LancasterUniversity | 2:5b2cd31ac474 | 1 | /* |
LancasterUniversity | 2:5b2cd31ac474 | 2 | The MIT License (MIT) |
LancasterUniversity | 2:5b2cd31ac474 | 3 | |
LancasterUniversity | 2:5b2cd31ac474 | 4 | Copyright (c) 2016 British Broadcasting Corporation. |
LancasterUniversity | 2:5b2cd31ac474 | 5 | This software is provided by Lancaster University by arrangement with the BBC. |
LancasterUniversity | 2:5b2cd31ac474 | 6 | |
LancasterUniversity | 2:5b2cd31ac474 | 7 | Permission is hereby granted, free of charge, to any person obtaining a |
LancasterUniversity | 2:5b2cd31ac474 | 8 | copy of this software and associated documentation files (the "Software"), |
LancasterUniversity | 2:5b2cd31ac474 | 9 | to deal in the Software without restriction, including without limitation |
LancasterUniversity | 2:5b2cd31ac474 | 10 | the rights to use, copy, modify, merge, publish, distribute, sublicense, |
LancasterUniversity | 2:5b2cd31ac474 | 11 | and/or sell copies of the Software, and to permit persons to whom the |
LancasterUniversity | 2:5b2cd31ac474 | 12 | Software is furnished to do so, subject to the following conditions: |
LancasterUniversity | 2:5b2cd31ac474 | 13 | |
LancasterUniversity | 2:5b2cd31ac474 | 14 | The above copyright notice and this permission notice shall be included in |
LancasterUniversity | 2:5b2cd31ac474 | 15 | all copies or substantial portions of the Software. |
LancasterUniversity | 2:5b2cd31ac474 | 16 | |
LancasterUniversity | 2:5b2cd31ac474 | 17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
LancasterUniversity | 2:5b2cd31ac474 | 18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
LancasterUniversity | 2:5b2cd31ac474 | 19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
LancasterUniversity | 2:5b2cd31ac474 | 20 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
LancasterUniversity | 2:5b2cd31ac474 | 21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
LancasterUniversity | 2:5b2cd31ac474 | 22 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
LancasterUniversity | 2:5b2cd31ac474 | 23 | DEALINGS IN THE SOFTWARE. |
LancasterUniversity | 2:5b2cd31ac474 | 24 | */ |
LancasterUniversity | 2:5b2cd31ac474 | 25 | |
LancasterUniversity | 2:5b2cd31ac474 | 26 | #include "MicroBit.h" |
LancasterUniversity | 2:5b2cd31ac474 | 27 | #include "MicroBitSamples.h" |
LancasterUniversity | 2:5b2cd31ac474 | 28 | |
LancasterUniversity | 2:5b2cd31ac474 | 29 | #ifdef MICROBIT_SAMPLE_BUTTON_EVENTS |
LancasterUniversity | 2:5b2cd31ac474 | 30 | |
LancasterUniversity | 2:5b2cd31ac474 | 31 | MicroBit uBit; |
LancasterUniversity | 2:5b2cd31ac474 | 32 | |
LancasterUniversity | 2:5b2cd31ac474 | 33 | // |
LancasterUniversity | 2:5b2cd31ac474 | 34 | // Print details of all events received to the serial port. |
LancasterUniversity | 2:5b2cd31ac474 | 35 | // Default settings are 115200 baud, 8N1 over the USB interface. |
LancasterUniversity | 2:5b2cd31ac474 | 36 | // |
LancasterUniversity | 2:5b2cd31ac474 | 37 | void onButton(MicroBitEvent e) |
LancasterUniversity | 2:5b2cd31ac474 | 38 | { |
LancasterUniversity | 2:5b2cd31ac474 | 39 | if (e.source == MICROBIT_ID_BUTTON_A) |
LancasterUniversity | 2:5b2cd31ac474 | 40 | uBit.serial.printf("BUTTON A: "); |
LancasterUniversity | 2:5b2cd31ac474 | 41 | |
LancasterUniversity | 2:5b2cd31ac474 | 42 | if (e.source == MICROBIT_ID_BUTTON_B) |
LancasterUniversity | 2:5b2cd31ac474 | 43 | uBit.serial.printf("BUTTON B: "); |
LancasterUniversity | 2:5b2cd31ac474 | 44 | |
LancasterUniversity | 2:5b2cd31ac474 | 45 | if (e.source == MICROBIT_ID_BUTTON_AB) |
LancasterUniversity | 2:5b2cd31ac474 | 46 | uBit.serial.printf("BUTTON A+B: "); |
LancasterUniversity | 2:5b2cd31ac474 | 47 | |
LancasterUniversity | 2:5b2cd31ac474 | 48 | if (e.source == MICROBIT_ID_IO_P0) |
LancasterUniversity | 2:5b2cd31ac474 | 49 | uBit.serial.printf("TOUCH P0: "); |
LancasterUniversity | 2:5b2cd31ac474 | 50 | |
LancasterUniversity | 2:5b2cd31ac474 | 51 | if (e.source == MICROBIT_ID_IO_P1) |
LancasterUniversity | 2:5b2cd31ac474 | 52 | uBit.serial.printf("TOUCH P1: "); |
LancasterUniversity | 2:5b2cd31ac474 | 53 | |
LancasterUniversity | 2:5b2cd31ac474 | 54 | if (e.source == MICROBIT_ID_IO_P2) |
LancasterUniversity | 2:5b2cd31ac474 | 55 | uBit.serial.printf("TOUCH P2: "); |
LancasterUniversity | 2:5b2cd31ac474 | 56 | |
LancasterUniversity | 2:5b2cd31ac474 | 57 | if (e.value == MICROBIT_BUTTON_EVT_DOWN) |
LancasterUniversity | 2:5b2cd31ac474 | 58 | uBit.serial.printf("DOWN"); |
LancasterUniversity | 2:5b2cd31ac474 | 59 | |
LancasterUniversity | 2:5b2cd31ac474 | 60 | if (e.value == MICROBIT_BUTTON_EVT_UP) |
LancasterUniversity | 2:5b2cd31ac474 | 61 | uBit.serial.printf("UP"); |
LancasterUniversity | 2:5b2cd31ac474 | 62 | |
LancasterUniversity | 2:5b2cd31ac474 | 63 | if (e.value == MICROBIT_BUTTON_EVT_CLICK) |
LancasterUniversity | 2:5b2cd31ac474 | 64 | uBit.serial.printf("CLICK"); |
LancasterUniversity | 2:5b2cd31ac474 | 65 | |
LancasterUniversity | 2:5b2cd31ac474 | 66 | if (e.value == MICROBIT_BUTTON_EVT_LONG_CLICK) |
LancasterUniversity | 2:5b2cd31ac474 | 67 | uBit.serial.printf("LONG_CLICK"); |
LancasterUniversity | 2:5b2cd31ac474 | 68 | |
LancasterUniversity | 2:5b2cd31ac474 | 69 | if (e.value == MICROBIT_BUTTON_EVT_HOLD) |
LancasterUniversity | 2:5b2cd31ac474 | 70 | uBit.serial.printf("HOLD"); |
LancasterUniversity | 2:5b2cd31ac474 | 71 | |
LancasterUniversity | 2:5b2cd31ac474 | 72 | if (e.value == MICROBIT_BUTTON_EVT_DOUBLE_CLICK) |
LancasterUniversity | 2:5b2cd31ac474 | 73 | uBit.serial.printf("DOUBLE_CLICK"); |
LancasterUniversity | 2:5b2cd31ac474 | 74 | |
LancasterUniversity | 2:5b2cd31ac474 | 75 | uBit.serial.printf("\n"); |
LancasterUniversity | 2:5b2cd31ac474 | 76 | } |
LancasterUniversity | 2:5b2cd31ac474 | 77 | |
LancasterUniversity | 2:5b2cd31ac474 | 78 | |
LancasterUniversity | 2:5b2cd31ac474 | 79 | int main() |
LancasterUniversity | 2:5b2cd31ac474 | 80 | { |
LancasterUniversity | 2:5b2cd31ac474 | 81 | // Initialise the micro:bit runtime. |
LancasterUniversity | 2:5b2cd31ac474 | 82 | uBit.init(); |
LancasterUniversity | 2:5b2cd31ac474 | 83 | |
LancasterUniversity | 2:5b2cd31ac474 | 84 | // Register to receive events when any buttons are clicked, including the A+B virtual button (both buttons at once). |
LancasterUniversity | 2:5b2cd31ac474 | 85 | uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_EVT_ANY, onButton); |
LancasterUniversity | 2:5b2cd31ac474 | 86 | uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_EVT_ANY, onButton); |
LancasterUniversity | 2:5b2cd31ac474 | 87 | uBit.messageBus.listen(MICROBIT_ID_BUTTON_AB, MICROBIT_EVT_ANY, onButton); |
LancasterUniversity | 2:5b2cd31ac474 | 88 | |
LancasterUniversity | 2:5b2cd31ac474 | 89 | // Also register for touch events on P0, P1 and P2. |
LancasterUniversity | 2:5b2cd31ac474 | 90 | uBit.messageBus.listen(MICROBIT_ID_IO_P0, MICROBIT_EVT_ANY, onButton); |
LancasterUniversity | 2:5b2cd31ac474 | 91 | uBit.messageBus.listen(MICROBIT_ID_IO_P1, MICROBIT_EVT_ANY, onButton); |
LancasterUniversity | 2:5b2cd31ac474 | 92 | uBit.messageBus.listen(MICROBIT_ID_IO_P2, MICROBIT_EVT_ANY, onButton); |
LancasterUniversity | 2:5b2cd31ac474 | 93 | |
LancasterUniversity | 2:5b2cd31ac474 | 94 | // Put the P0, P1 and P2 pins into touch sense mode. |
LancasterUniversity | 2:5b2cd31ac474 | 95 | uBit.io.P0.isTouched(); |
LancasterUniversity | 2:5b2cd31ac474 | 96 | uBit.io.P1.isTouched(); |
LancasterUniversity | 2:5b2cd31ac474 | 97 | uBit.io.P2.isTouched(); |
LancasterUniversity | 2:5b2cd31ac474 | 98 | |
LancasterUniversity | 2:5b2cd31ac474 | 99 | // We're done, so just enter a power efficient sleep while we wait for an event. |
LancasterUniversity | 2:5b2cd31ac474 | 100 | while (1) |
LancasterUniversity | 2:5b2cd31ac474 | 101 | uBit.sleep(10000); |
LancasterUniversity | 2:5b2cd31ac474 | 102 | } |
LancasterUniversity | 2:5b2cd31ac474 | 103 | |
LancasterUniversity | 2:5b2cd31ac474 | 104 | #endif |