MicroBit project problem #4 Display 1 dot randomly in the LED matrix. Button A moves led left, Button B moves led right, and Buttons A+B pressed together move the led up the matrix. If the active led reaches an edge, the next button click wraps it around to the other side of the matrix.

Dependencies:   microbit

Committer:
tsfarber
Date:
Sat Nov 30 02:01:52 2019 +0000
Revision:
0:f046525e6f7c
tested and verified;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tsfarber 0:f046525e6f7c 1 /*
tsfarber 0:f046525e6f7c 2 The MIT License (MIT)
tsfarber 0:f046525e6f7c 3
tsfarber 0:f046525e6f7c 4 Copyright (c) 2016 British Broadcasting Corporation.
tsfarber 0:f046525e6f7c 5 This software is provided by Lancaster University by arrangement with the BBC.
tsfarber 0:f046525e6f7c 6
tsfarber 0:f046525e6f7c 7 Permission is hereby granted, free of charge, to any person obtaining a
tsfarber 0:f046525e6f7c 8 copy of this software and associated documentation files (the "Software"),
tsfarber 0:f046525e6f7c 9 to deal in the Software without restriction, including without limitation
tsfarber 0:f046525e6f7c 10 the rights to use, copy, modify, merge, publish, distribute, sublicense,
tsfarber 0:f046525e6f7c 11 and/or sell copies of the Software, and to permit persons to whom the
tsfarber 0:f046525e6f7c 12 Software is furnished to do so, subject to the following conditions:
tsfarber 0:f046525e6f7c 13
tsfarber 0:f046525e6f7c 14 The above copyright notice and this permission notice shall be included in
tsfarber 0:f046525e6f7c 15 all copies or substantial portions of the Software.
tsfarber 0:f046525e6f7c 16
tsfarber 0:f046525e6f7c 17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
tsfarber 0:f046525e6f7c 18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
tsfarber 0:f046525e6f7c 19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
tsfarber 0:f046525e6f7c 20 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
tsfarber 0:f046525e6f7c 21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
tsfarber 0:f046525e6f7c 22 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
tsfarber 0:f046525e6f7c 23 DEALINGS IN THE SOFTWARE.
tsfarber 0:f046525e6f7c 24 */
tsfarber 0:f046525e6f7c 25
tsfarber 0:f046525e6f7c 26 #include "MicroBit.h"
tsfarber 0:f046525e6f7c 27 MicroBit uBit;
tsfarber 0:f046525e6f7c 28 //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");
tsfarber 0:f046525e6f7c 29 int x = 0;
tsfarber 0:f046525e6f7c 30 int y = 0;
tsfarber 0:f046525e6f7c 31 int b = 255;
tsfarber 0:f046525e6f7c 32
tsfarber 0:f046525e6f7c 33
tsfarber 0:f046525e6f7c 34 void iniDisplay(){
tsfarber 0:f046525e6f7c 35 x = uBit.random(5);
tsfarber 0:f046525e6f7c 36 y = uBit.random(5);
tsfarber 0:f046525e6f7c 37 uBit.display.image.setPixelValue(x,y,b);
tsfarber 0:f046525e6f7c 38 }
tsfarber 0:f046525e6f7c 39
tsfarber 0:f046525e6f7c 40 void onButtonA(MicroBitEvent e){
tsfarber 0:f046525e6f7c 41 if( x == 0){
tsfarber 0:f046525e6f7c 42 x = 4;
tsfarber 0:f046525e6f7c 43 } else {
tsfarber 0:f046525e6f7c 44 x -= 1;
tsfarber 0:f046525e6f7c 45 }
tsfarber 0:f046525e6f7c 46 uBit.display.image.clear();
tsfarber 0:f046525e6f7c 47 uBit.display.image.setPixelValue(x,y,b);
tsfarber 0:f046525e6f7c 48 }
tsfarber 0:f046525e6f7c 49
tsfarber 0:f046525e6f7c 50 void onButtonB(MicroBitEvent e){
tsfarber 0:f046525e6f7c 51 if( x == 4){
tsfarber 0:f046525e6f7c 52 x = 0;
tsfarber 0:f046525e6f7c 53 } else {
tsfarber 0:f046525e6f7c 54 x += 1;
tsfarber 0:f046525e6f7c 55 }
tsfarber 0:f046525e6f7c 56 uBit.display.image.clear();
tsfarber 0:f046525e6f7c 57 uBit.display.image.setPixelValue(x,y,b);
tsfarber 0:f046525e6f7c 58 }
tsfarber 0:f046525e6f7c 59
tsfarber 0:f046525e6f7c 60 void onButtonAB(MicroBitEvent e){
tsfarber 0:f046525e6f7c 61 if( y == 0){
tsfarber 0:f046525e6f7c 62 y = 4;
tsfarber 0:f046525e6f7c 63 } else {
tsfarber 0:f046525e6f7c 64 y -= 1;
tsfarber 0:f046525e6f7c 65 }
tsfarber 0:f046525e6f7c 66 uBit.display.image.clear();
tsfarber 0:f046525e6f7c 67 uBit.display.image.setPixelValue(x,y,b);
tsfarber 0:f046525e6f7c 68 }
tsfarber 0:f046525e6f7c 69
tsfarber 0:f046525e6f7c 70
tsfarber 0:f046525e6f7c 71
tsfarber 0:f046525e6f7c 72 int main()
tsfarber 0:f046525e6f7c 73 {
tsfarber 0:f046525e6f7c 74 // Initialise the micro:bit runtime.
tsfarber 0:f046525e6f7c 75 uBit.init();
tsfarber 0:f046525e6f7c 76 uBit.display.setDisplayMode(DISPLAY_MODE_GREYSCALE);
tsfarber 0:f046525e6f7c 77 iniDisplay();
tsfarber 0:f046525e6f7c 78
tsfarber 0:f046525e6f7c 79 uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA);
tsfarber 0:f046525e6f7c 80 uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB);
tsfarber 0:f046525e6f7c 81 uBit.messageBus.listen(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK, onButtonAB);
tsfarber 0:f046525e6f7c 82
tsfarber 0:f046525e6f7c 83
tsfarber 0:f046525e6f7c 84
tsfarber 0:f046525e6f7c 85 // If main exits, there may still be other fibers running or registered event handlers etc.
tsfarber 0:f046525e6f7c 86 // Simply release this fiber, which will mean we enter the scheduler. Worse case, we then
tsfarber 0:f046525e6f7c 87 // sit in the idle task forever, in a power efficient sleep.
tsfarber 0:f046525e6f7c 88 release_fiber();
tsfarber 0:f046525e6f7c 89 }
tsfarber 0:f046525e6f7c 90