Visačka na diskotéku
Zo stránky SensorWiki
Vyrobíme jednoduchú visačku na diskotéku. Po zapnutí sa na displeji zobrazí znak ✓, ktorým dáme na javo, že zariadenie je pripravené. Emocionálny stav nositeľa tejto visačky sa dá meniť tlačidlami. Po stlačení ľavého tlačidla 🅐 sa zobrazí na displeji veselý smajlík 😀, po stlačení 🅑 sa zobrazí smutný smajlík ☹ a po stlačení oboch 🅐+🅑 súčasne sa zobrazí "tajný symbol" ❤ pre toho pravého...
-
Prvý program...
-
...bude vyzerať v simulátore takto.
Visacka na diskoteku:
basic.showIcon(IconNames.Yes)
input.onButtonPressed(Button.A, function () {
basic.showIcon(IconNames.Happy)
})
input.onButtonPressed(Button.B, function () {
basic.showIcon(IconNames.Sad)
})
input.onButtonPressed(Button.AB, function () {
basic.showIcon(IconNames.Heart)
})
# MindPlus
# microbit
from microbit import *
def on_button_a():
while True:
if button_a.is_pressed():
yield callback_button_a()
else:
yield
def on_button_b():
while True:
if button_b.is_pressed() and not button_a.is_pressed():
yield callback_button_b()
else:
yield
# Event callback function
def callback_button_a():
global mind_n_myValue, mind_s_my_string_variable
display.show(Image("00000:09090:00000:90009:09990"))
def callback_button_b():
global mind_n_myValue, mind_s_my_string_variable
display.show(Image("00000:09090:00000:09990:90009"))
def callback_button_a():
global mind_n_myValue, mind_s_my_string_variable
display.show(Image("09090:99999:99999:09990:00900"))
func_button_a = on_button_a()
func_button_b = on_button_b()
display.show(Image("00000:00009:00090:90900:09000"))
while True:
next(func_button_a)
next(func_button_b)
/*!
* MindPlus
* microbit
*
*/
#include <Microbit_Matrix.h>
// Function declaration
void buttonACallback();
void buttonBCallback();
void buttonABCallback();
// Static constants
const uint8_t bbcBitmap[][5] = {
{B00000,B00001,B00010,B10100,B01000},
{B00000,B01010,B00000,B10001,B01110},
{B00000,B01010,B00000,B01110,B10001},
{B01010,B11111,B11111,B01110,B00100}
};
// Main program start
void setup() {
onEvent(ID_BUTTON_A, PRESS, buttonACallback);
onEvent(ID_BUTTON_B, PRESS, buttonBCallback);
onEvent(ID_BUTTON_AB, PRESS, buttonABCallback);
MMatrix.show(bbcBitmap[0]);
}
void loop() {
}
// Event callback function
void buttonACallback() {
MMatrix.show(bbcBitmap[1]);
}
void buttonBCallback() {
MMatrix.show(bbcBitmap[2]);
}
void buttonABCallback() {
MMatrix.show(bbcBitmap[3]);
}
- include "MicroBit.h"
MicroBit uBit;
MicroBitImage HEART("0,1,0,1,0 \n"
"1,1,1,1,1 \n" "1,1,1,1,1 \n" "0,1,1,1,0 \n" "0,0,1,0,0 \n");
MicroBitImage SMILEY_HAPPY("0,0,0,0,0\n 0,1,0,1,0\n 0,0,0,0,0\n 1,0,0,0,1\n 0,1,1,1,0\n"); MicroBitImage SMILEY_SAD("0,0,0,0,0\n 0,1,0,1,0\n 0,0,0,0,0\n 0,1,1,1,0\n 1,0,0,0,1\n"); MicroBitImage YES("0,0,0,0,0\n 0,0,0,0,1\n 0,0,0,1,0\n 1,0,1,0,0\n 0,1,0,0,0\n");
void onButtonA(MicroBitEvent e)
{
uBit.display.print(SMILEY_HAPPY);
}
void onButtonB(MicroBitEvent e) {
uBit.display.print(SMILEY_SAD);
}
void onButtonAB(MicroBitEvent e) {
uBit.display.print(HEART);
}
int main() {
uBit.init(); // setup // Register to receive events when any buttons are clicked, including the A+B virtual button (both buttons at once). uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_BUTTON_EVT_CLICK, onButtonA); uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_BUTTON_EVT_CLICK, onButtonB); uBit.messageBus.listen(MICROBIT_ID_BUTTON_AB, MICROBIT_BUTTON_EVT_CLICK, onButtonAB);
uBit.display.print(YES);
// We're done, so just enter a power efficient sleep while we wait for an event. while (1) uBit.sleep(10000);
}