Tami Farber / microbit-4-pushLED

Dependencies:   microbit

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.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 MicroBit uBit;
00028 //MicroBitImage field("0,0,0,0,0\n0,0,0,0,0\n0,0,0,0,0\n0,0,0,0,0\n0,0,0,0,0\n");
00029 int x = 0;
00030 int y = 0;
00031 int b = 255;
00032 
00033 
00034 void iniDisplay(){
00035     x = uBit.random(5);
00036     y = uBit.random(5);
00037     uBit.display.image.setPixelValue(x,y,b);
00038 }
00039 
00040 void onButtonA(MicroBitEvent e){
00041     if( x == 0){
00042         x = 4;
00043     } else {
00044         x -= 1;
00045     }
00046     uBit.display.image.clear();
00047     uBit.display.image.setPixelValue(x,y,b);
00048 }
00049 
00050 void onButtonB(MicroBitEvent e){
00051     if( x == 4){
00052         x = 0;
00053     } else {
00054         x += 1;
00055     }
00056     uBit.display.image.clear();
00057     uBit.display.image.setPixelValue(x,y,b);
00058 }
00059 
00060 void onButtonAB(MicroBitEvent e){
00061     if( y == 0){
00062         y = 4;
00063     } else {
00064         y -= 1;
00065     }
00066     uBit.display.image.clear();
00067     uBit.display.image.setPixelValue(x,y,b);
00068 }
00069     
00070     
00071 
00072 int main()
00073 {
00074     // Initialise the micro:bit runtime.
00075     uBit.init();
00076     uBit.display.setDisplayMode(DISPLAY_MODE_GREYSCALE);
00077     iniDisplay();
00078     
00079     uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
00080     uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
00081     uBit.messageBus.listen(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK, onButtonAB);
00082 
00083     
00084 
00085     // If main exits, there may still be other fibers running or registered event handlers etc.
00086     // Simply release this fiber, which will mean we enter the scheduler. Worse case, we then
00087     // sit in the idle task forever, in a power efficient sleep.
00088     release_fiber();
00089 }
00090