2016-11-30 06:35:26 +01:00
|
|
|
#include "pxt.h"
|
2016-03-30 02:11:17 +02:00
|
|
|
|
|
|
|
|
2016-03-10 23:01:04 +01:00
|
|
|
/**
|
|
|
|
* Provides access to basic micro:bit functionality.
|
|
|
|
*/
|
2017-01-05 16:01:50 +01:00
|
|
|
//% color=#0078D7 weight=100 icon="\uf00a"
|
2016-03-10 23:01:04 +01:00
|
|
|
namespace basic {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Scroll a number on the screen. If the number fits on the screen (i.e. is a single digit), do not scroll.
|
|
|
|
* @param interval speed of scroll; eg: 150, 100, 200, -100
|
|
|
|
*/
|
2016-03-22 06:13:39 +01:00
|
|
|
//% help=basic/show-number
|
2016-03-10 23:01:04 +01:00
|
|
|
//% weight=96
|
2017-01-05 16:01:50 +01:00
|
|
|
//% blockId=device_show_number block="show|number %number" blockGap=8
|
2016-03-10 23:01:04 +01:00
|
|
|
//% async
|
2016-08-22 17:48:48 +02:00
|
|
|
//% parts="ledmatrix"
|
2017-10-24 22:10:54 +02:00
|
|
|
void showNumber(int value, int interval = 150) {
|
2017-05-12 23:01:19 +02:00
|
|
|
if (interval <= 0)
|
|
|
|
interval = 1;
|
2016-03-30 02:11:17 +02:00
|
|
|
ManagedString t(value);
|
|
|
|
if (value < 0 || value >= 10) {
|
|
|
|
uBit.display.scroll(t, interval);
|
|
|
|
} else {
|
2016-07-29 00:04:56 +02:00
|
|
|
uBit.display.printChar(t.charAt(0), interval * 5);
|
2016-03-30 02:11:17 +02:00
|
|
|
}
|
|
|
|
}
|
2016-03-10 23:01:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws an image on the LED screen.
|
2016-06-14 15:30:07 +02:00
|
|
|
* @param leds the pattern of LED to turn on/off
|
|
|
|
* @param interval time in milliseconds to pause after drawing
|
2016-03-10 23:01:04 +01:00
|
|
|
*/
|
2017-10-24 22:10:54 +02:00
|
|
|
//% help=basic/show-leds
|
2016-03-10 23:01:04 +01:00
|
|
|
//% weight=95 blockGap=8
|
2018-04-21 19:25:43 +02:00
|
|
|
//% imageLiteral=0 async
|
|
|
|
//% leds.fieldEditor="matrix"
|
|
|
|
//% leds.fieldOptions.onParentBlock=true
|
|
|
|
//% leds.fieldOptions.decompileLiterals=true
|
2016-03-10 23:01:04 +01:00
|
|
|
//% blockId=device_show_leds
|
2018-04-21 19:25:43 +02:00
|
|
|
//% block="show leds| %leds" icon="\uf00a"
|
|
|
|
//% parts="ledmatrix" blockExternalInputs=1
|
2016-03-30 02:11:17 +02:00
|
|
|
void showLeds(ImageLiteral leds, int interval = 400) {
|
2016-04-02 22:44:29 +02:00
|
|
|
uBit.display.print(MicroBitImage(imageBytes(leds)), 0, 0, 0, interval);
|
2016-03-30 02:11:17 +02:00
|
|
|
}
|
2016-03-10 23:01:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Display text on the display, one character at a time. If the string fits on the screen (i.e. is one letter), does not scroll.
|
|
|
|
* @param text the text to scroll on the screen, eg: "Hello!"
|
|
|
|
* @param interval how fast to shift characters; eg: 150, 100, 200, -100
|
|
|
|
*/
|
2017-10-24 22:10:54 +02:00
|
|
|
//% help=basic/show-string
|
2016-03-10 23:01:04 +01:00
|
|
|
//% weight=87 blockGap=8
|
2017-10-24 22:10:54 +02:00
|
|
|
//% block="show|string %text"
|
2016-03-10 23:01:04 +01:00
|
|
|
//% async
|
|
|
|
//% blockId=device_print_message
|
2016-08-22 17:48:48 +02:00
|
|
|
//% parts="ledmatrix"
|
2016-03-30 02:11:17 +02:00
|
|
|
void showString(StringData *text, int interval = 150) {
|
2017-05-12 23:01:19 +02:00
|
|
|
if (interval <= 0)
|
|
|
|
interval = 1;
|
2016-03-30 02:11:17 +02:00
|
|
|
ManagedString s(text);
|
|
|
|
int l = s.length();
|
|
|
|
if (l == 0) {
|
|
|
|
uBit.display.clear();
|
2016-04-19 20:52:44 +02:00
|
|
|
fiber_sleep(interval * 5);
|
2016-03-30 02:11:17 +02:00
|
|
|
} else if (l > 1) {
|
|
|
|
uBit.display.scroll(s, interval);
|
|
|
|
} else {
|
|
|
|
uBit.display.print(s.charAt(0), interval * 5);
|
|
|
|
}
|
|
|
|
}
|
2016-03-10 23:01:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn off all LEDs
|
|
|
|
*/
|
2016-03-22 06:13:39 +01:00
|
|
|
//% help=basic/clear-screen weight=79
|
2017-01-05 16:01:50 +01:00
|
|
|
//% blockId=device_clear_display block="clear screen"
|
2016-08-22 17:48:48 +02:00
|
|
|
//% parts="ledmatrix"
|
2017-02-01 00:54:23 +01:00
|
|
|
//% advanced=true
|
2016-03-30 02:11:17 +02:00
|
|
|
void clearScreen() {
|
|
|
|
uBit.display.image.clear();
|
|
|
|
}
|
2016-03-10 23:01:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows a sequence of LED screens as an animation.
|
2016-06-14 15:30:07 +02:00
|
|
|
* @param leds pattern of LEDs to turn on/off
|
|
|
|
* @param interval time in milliseconds between each redraw
|
2016-03-10 23:01:04 +01:00
|
|
|
*/
|
2018-04-21 19:25:43 +02:00
|
|
|
//% help=basic/show-animation imageLiteral=0 async
|
|
|
|
//% leds.fieldEditor="matrix"
|
|
|
|
//% leds.fieldOptions.onParentBlock=true
|
|
|
|
//% leds.fieldOptions.decompileLiterals=true
|
2016-08-22 17:48:48 +02:00
|
|
|
//% parts="ledmatrix"
|
2016-03-30 02:11:17 +02:00
|
|
|
void showAnimation(ImageLiteral leds, int interval = 400) {
|
2017-05-30 08:01:31 +02:00
|
|
|
uBit.display.animate(MicroBitImage(imageBytes(leds)), interval, 5, 0, 0);
|
2016-03-30 02:11:17 +02:00
|
|
|
}
|
2016-03-10 23:01:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws an image on the LED screen.
|
2016-06-14 15:30:07 +02:00
|
|
|
* @param leds pattern of LEDs to turn on/off
|
2016-03-10 23:01:04 +01:00
|
|
|
*/
|
2016-04-02 06:26:06 +02:00
|
|
|
//% help=basic/plot-leds weight=80
|
2016-08-22 17:48:48 +02:00
|
|
|
//% parts="ledmatrix"
|
2016-03-30 02:11:17 +02:00
|
|
|
void plotLeds(ImageLiteral leds) {
|
2016-04-02 22:44:29 +02:00
|
|
|
MicroBitImage i(imageBytes(leds));
|
2016-03-30 02:11:17 +02:00
|
|
|
uBit.display.print(i, 0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void forever_stub(void *a) {
|
|
|
|
while (true) {
|
2016-04-02 22:44:29 +02:00
|
|
|
runAction0((Action)a);
|
2016-04-19 20:52:44 +02:00
|
|
|
fiber_sleep(20);
|
2016-03-30 02:11:17 +02:00
|
|
|
}
|
|
|
|
}
|
2016-03-10 23:01:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Repeats the code forever in the background. On each iteration, allows other codes to run.
|
2016-06-14 15:30:07 +02:00
|
|
|
* @param body code to execute
|
2016-03-10 23:01:04 +01:00
|
|
|
*/
|
2017-10-24 22:10:54 +02:00
|
|
|
//% help=basic/forever weight=55 blockGap=8 blockAllowMultiple=1 afterOnStart=true
|
2016-03-30 02:11:17 +02:00
|
|
|
//% blockId=device_forever block="forever" icon="\uf01e"
|
|
|
|
void forever(Action a) {
|
|
|
|
if (a != 0) {
|
|
|
|
incr(a);
|
|
|
|
create_fiber(forever_stub, (void*)a);
|
|
|
|
}
|
|
|
|
}
|
2016-03-10 23:01:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pause for the specified time in milliseconds
|
|
|
|
* @param ms how long to pause for, eg: 100, 200, 500, 1000, 2000
|
|
|
|
*/
|
2016-03-22 06:13:39 +01:00
|
|
|
//% help=basic/pause weight=54
|
2016-03-30 02:11:17 +02:00
|
|
|
//% async block="pause (ms) %pause"
|
2016-03-10 23:01:04 +01:00
|
|
|
//% blockId=device_pause icon="\uf110"
|
2016-03-30 02:11:17 +02:00
|
|
|
void pause(int ms) {
|
2016-04-19 20:52:44 +02:00
|
|
|
fiber_sleep(ms);
|
2016-03-30 02:11:17 +02:00
|
|
|
}
|
2016-03-10 23:01:04 +01:00
|
|
|
}
|