This simple program displays either "A" or "B" on LED matrix when you press A/B button built-on micro:bit and any compatible devices (such as chibi:bit)

Dependencies:   microbit

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /******************************************************************************
00002 main.cpp
00003 
00004 Tatsuya Iwai @ greysound.com
00005 Original Creation Date: Aug 12, 2016
00006 https://github.com/sparkfun/LSM9DS1_Breakout
00007 
00008 This simple program tests built-in "A" and "B" button on micro:bit or any
00009 compatible devices (such as "chibi:bit") shows button label on LED matrix
00010 when either button is pressed.
00011 
00012 Distributed as-is; no warranty is given.
00013 ******************************************************************************/
00014 
00015 #include "MicroBit.h"
00016 
00017 // Objects --------------------------------------------------------------------
00018 MicroBitMessageBus bus;
00019 MicroBitButton buttonA(MICROBIT_PIN_BUTTON_A, MICROBIT_ID_BUTTON_A);
00020 MicroBitButton buttonB(MICROBIT_PIN_BUTTON_B, MICROBIT_ID_BUTTON_B);
00021 MicroBitDisplay display;
00022 
00023 // Function prototypes --------------------------------------------------------
00024 void onPressed(MicroBitEvent e);
00025 
00026 // Main  ----------------------------------------------------------------------
00027 int main()
00028 {
00029     scheduler_init(bus);
00030 
00031     bus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onPressed);
00032     bus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onPressed);
00033 
00034     while(1)
00035         fiber_sleep(1000);
00036 }
00037 
00038 // Functions ------------------------------------------------------------------
00039 void onPressed(MicroBitEvent e)
00040 {
00041     if (e.source == MICROBIT_ID_BUTTON_A)
00042         display.scroll("A");
00043 
00044     if (e.source == MICROBIT_ID_BUTTON_B)
00045         display.scroll("B");
00046 }