Samples of how to use the micro:bit DAL with mbed. This is a hg clone of the real git repo that can be found here: https://github.com/lancaster-university/microbit-samples
Dependencies: microbit
Fork of microbit-samples by
ButtonEvents.cpp
00001 /* 00002 The MIT License (MIT) 00003 00004 Copyright (c) 2016 British Broadcasting Corporation. 00005 This software is provided by Lancaster University by arrangement with the BBC. 00006 00007 Permission is hereby granted, free of charge, to any person obtaining a 00008 copy of this software and associated documentation files (the "Software"), 00009 to deal in the Software without restriction, including without limitation 00010 the rights to use, copy, modify, merge, publish, distribute, sublicense, 00011 and/or sell copies of the Software, and to permit persons to whom the 00012 Software is furnished to do so, subject to the following conditions: 00013 00014 The above copyright notice and this permission notice shall be included in 00015 all copies or substantial portions of the Software. 00016 00017 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00018 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00019 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 00020 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00021 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 00022 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 00023 DEALINGS IN THE SOFTWARE. 00024 */ 00025 00026 #include "MicroBit.h" 00027 #include "MicroBitSamples.h" 00028 00029 #ifdef MICROBIT_SAMPLE_BUTTON_EVENTS 00030 00031 MicroBit uBit; 00032 00033 // 00034 // Print details of all events received to the serial port. 00035 // Default settings are 115200 baud, 8N1 over the USB interface. 00036 // 00037 void onButton(MicroBitEvent e) 00038 { 00039 if (e.source == MICROBIT_ID_BUTTON_A) 00040 uBit.serial.printf("BUTTON A: "); 00041 00042 if (e.source == MICROBIT_ID_BUTTON_B) 00043 uBit.serial.printf("BUTTON B: "); 00044 00045 if (e.source == MICROBIT_ID_BUTTON_AB) 00046 uBit.serial.printf("BUTTON A+B: "); 00047 00048 if (e.source == MICROBIT_ID_IO_P0) 00049 uBit.serial.printf("TOUCH P0: "); 00050 00051 if (e.source == MICROBIT_ID_IO_P1) 00052 uBit.serial.printf("TOUCH P1: "); 00053 00054 if (e.source == MICROBIT_ID_IO_P2) 00055 uBit.serial.printf("TOUCH P2: "); 00056 00057 if (e.value == MICROBIT_BUTTON_EVT_DOWN) 00058 uBit.serial.printf("DOWN"); 00059 00060 if (e.value == MICROBIT_BUTTON_EVT_UP) 00061 uBit.serial.printf("UP"); 00062 00063 if (e.value == MICROBIT_BUTTON_EVT_CLICK) 00064 uBit.serial.printf("CLICK"); 00065 00066 if (e.value == MICROBIT_BUTTON_EVT_LONG_CLICK) 00067 uBit.serial.printf("LONG_CLICK"); 00068 00069 if (e.value == MICROBIT_BUTTON_EVT_HOLD) 00070 uBit.serial.printf("HOLD"); 00071 00072 if (e.value == MICROBIT_BUTTON_EVT_DOUBLE_CLICK) 00073 uBit.serial.printf("DOUBLE_CLICK"); 00074 00075 uBit.serial.printf("\n"); 00076 } 00077 00078 00079 int main() 00080 { 00081 // Initialise the micro:bit runtime. 00082 uBit.init(); 00083 00084 // Register to receive events when any buttons are clicked, including the A+B virtual button (both buttons at once). 00085 uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_EVT_ANY, onButton); 00086 uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_EVT_ANY, onButton); 00087 uBit.messageBus.listen(MICROBIT_ID_BUTTON_AB, MICROBIT_EVT_ANY, onButton); 00088 00089 // Also register for touch events on P0, P1 and P2. 00090 uBit.messageBus.listen(MICROBIT_ID_IO_P0, MICROBIT_EVT_ANY, onButton); 00091 uBit.messageBus.listen(MICROBIT_ID_IO_P1, MICROBIT_EVT_ANY, onButton); 00092 uBit.messageBus.listen(MICROBIT_ID_IO_P2, MICROBIT_EVT_ANY, onButton); 00093 00094 // Put the P0, P1 and P2 pins into touch sense mode. 00095 uBit.io.P0.isTouched(); 00096 uBit.io.P1.isTouched(); 00097 uBit.io.P2.isTouched(); 00098 00099 // We're done, so just enter a power efficient sleep while we wait for an event. 00100 while (1) 00101 uBit.sleep(10000); 00102 } 00103 00104 #endif
Generated on Thu Jul 14 2022 08:55:58 by
