2016-11-30 06:55:37 +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.
|
|
|
|
*/
|
|
|
|
namespace basic {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-12-14 20:00:47 +01:00
|
|
|
//% help=basic/show-leds
|
2022-03-22 17:36:19 +01:00
|
|
|
//% weight=85 blockGap=8
|
2016-03-10 23:01:04 +01:00
|
|
|
//% imageLiteral=1 async
|
|
|
|
//% blockId=device_show_leds
|
2017-12-14 20:00:47 +01:00
|
|
|
//% block="show leds" icon="\uf00a"
|
2016-08-22 17:48:48 +02:00
|
|
|
//% parts="ledmatrix"
|
2022-03-22 17:36:19 +01:00
|
|
|
//% group="LED matrix"
|
2019-12-02 05:58:26 +01: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.
|
2019-12-02 05:58:26 +01:00
|
|
|
* @param text the text to scroll on the screen, eg: "hi!"
|
2022-03-22 17:36:19 +01:00
|
|
|
* @param interval how fast to shift characters; eg: 50, 100, 150, 200
|
2016-03-10 23:01:04 +01:00
|
|
|
*/
|
2017-12-14 20:00:47 +01:00
|
|
|
//% help=basic/show-string
|
2022-03-22 17:36:19 +01:00
|
|
|
//% weight=100 blockGap=16
|
|
|
|
//% block="show|string %text || in an interval of %interval ms"
|
2016-03-10 23:01:04 +01:00
|
|
|
//% async
|
|
|
|
//% blockId=device_print_message
|
2016-08-22 17:48:48 +02:00
|
|
|
//% parts="ledmatrix"
|
2019-12-02 05:58:26 +01:00
|
|
|
//% text.shadowOptions.toString=true
|
2022-03-22 17:36:19 +01:00
|
|
|
//% expandableArgumentMode="toggle"
|
2022-06-30 19:01:19 +02:00
|
|
|
//% interval.defl=150
|
2022-03-22 17:36:19 +01:00
|
|
|
//% group="LED matrix"
|
2022-06-30 19:01:19 +02:00
|
|
|
void showString(String text, int interval = 150) {
|
2017-12-14 20:00:47 +01:00
|
|
|
if (interval <= 0)
|
|
|
|
interval = 1;
|
2019-12-02 05:58:26 +01:00
|
|
|
int l = text ? text->getUTF8Size() : 0;
|
2016-03-30 02:11:17 +02:00
|
|
|
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) {
|
2019-12-02 05:58:26 +01:00
|
|
|
uBit.display.scroll(MSTR(text), interval);
|
2016-03-30 02:11:17 +02:00
|
|
|
} else {
|
2019-12-02 05:58:26 +01:00
|
|
|
uBit.display.printChar(text->getUTF8Data()[0], interval * 5);
|
2016-03-30 02:11:17 +02:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
*/
|
2022-03-22 17:36:19 +01:00
|
|
|
//% help=basic/show-animation weight=83 imageLiteral=1 async
|
2016-08-22 17:48:48 +02:00
|
|
|
//% parts="ledmatrix"
|
2022-03-22 17:36:19 +01:00
|
|
|
//% group="LED matrix"
|
2019-12-02 05:58:26 +01:00
|
|
|
void showAnimation(ImageLiteral_ leds, int interval = 400) {
|
2017-12-14 20:00:47 +01: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"
|
2022-03-22 17:36:19 +01:00
|
|
|
//% group="LED matrix"
|
2019-12-02 05:58:26 +01: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);
|
|
|
|
}
|
|
|
|
|
2022-03-22 17:36:19 +01:00
|
|
|
/**
|
|
|
|
* Turn off all LEDs
|
|
|
|
*/
|
|
|
|
//% help=basic/clear-screen weight=75
|
|
|
|
//% blockId=device_clear_display block="clear screen"
|
|
|
|
//% parts="ledmatrix"
|
|
|
|
//% group="LED matrix"
|
|
|
|
//% advanced=true
|
|
|
|
void clearScreen() {
|
|
|
|
uBit.display.image.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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
|
|
|
*/
|
2019-12-02 05:58:26 +01:00
|
|
|
//% help=basic/forever weight=55 blockGap=16 blockAllowMultiple=1 afterOnStart=true
|
2017-12-14 20:00:47 +01:00
|
|
|
//% blockId=device_forever block="forever" icon="\uf01e"
|
2022-03-22 17:36:19 +01:00
|
|
|
//% group="Control"
|
2016-03-30 02:11:17 +02:00
|
|
|
void forever(Action a) {
|
2019-12-02 05:58:26 +01:00
|
|
|
runForever(a);
|
2016-03-30 02:11:17 +02:00
|
|
|
}
|
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
|
|
|
|
*/
|
2022-03-22 17:36:19 +01:00
|
|
|
//% help=basic/pause weight=50
|
2019-12-02 05:58:26 +01:00
|
|
|
//% async block="pause (ms) %pause" blockGap=16
|
2017-12-14 20:00:47 +01:00
|
|
|
//% blockId=device_pause icon="\uf110"
|
2019-12-02 05:58:26 +01:00
|
|
|
//% pause.shadow=timePicker
|
2022-03-22 17:36:19 +01:00
|
|
|
//% group="Control"
|
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
|
|
|
}
|
2022-03-22 17:36:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the color on the build-in LED. Set to 0 to turn off.
|
|
|
|
*/
|
|
|
|
//% blockId=device_set_led_color
|
|
|
|
//% block="set led to %color=colorNumberPicker"
|
2022-04-26 19:28:42 +02:00
|
|
|
//% color.defl=0xff0000
|
2022-03-22 17:36:19 +01:00
|
|
|
//% weight=10
|
|
|
|
//% group="RGB LED"
|
|
|
|
void setLedColor(int color) {
|
|
|
|
if (!color) {
|
|
|
|
uBit.rgb.off();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int w = (color >> 24) & 0xFF;
|
|
|
|
int r = (color >> 16) & 0xFF;
|
|
|
|
int g = (color >> 8) & 0xFF;
|
|
|
|
int b = (color) & 0xFF;
|
|
|
|
|
|
|
|
uBit.rgb.setColour(r,g,b,w);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the color on the build-in LED. Set to 0 to turn off.
|
|
|
|
*/
|
|
|
|
//% blockId=device_turn_rgb_led_off block="turn build-in LED off"
|
|
|
|
//% weight=10
|
|
|
|
//% group="RGB LED"
|
|
|
|
//% advanced=true
|
|
|
|
void turnRgbLedOff() {
|
|
|
|
uBit.rgb.off();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-09-08 11:04:25 +02:00
|
|
|
}
|