2016-11-29 21:35:26 -08:00
|
|
|
#include "pxt.h"
|
2016-03-29 17:11:17 -07:00
|
|
|
|
|
|
|
|
2016-03-10 14:01:04 -08:00
|
|
|
/**
|
|
|
|
* Provides access to basic micro:bit functionality.
|
|
|
|
*/
|
2018-10-01 14:39:51 -07:00
|
|
|
//% color=#1E90FF weight=116 icon="\uf00a"
|
2016-03-10 14:01:04 -08:00
|
|
|
namespace basic {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws an image on the LED screen.
|
2016-06-14 06:30:07 -07:00
|
|
|
* @param leds the pattern of LED to turn on/off
|
|
|
|
* @param interval time in milliseconds to pause after drawing
|
2016-03-10 14:01:04 -08:00
|
|
|
*/
|
2017-10-24 13:10:54 -07:00
|
|
|
//% help=basic/show-leds
|
2016-03-10 14:01:04 -08:00
|
|
|
//% weight=95 blockGap=8
|
2018-05-09 10:56:48 -07:00
|
|
|
//% imageLiteral=1 async
|
2016-03-10 14:01:04 -08:00
|
|
|
//% blockId=device_show_leds
|
2018-05-09 10:56:48 -07:00
|
|
|
//% block="show leds" icon="\uf00a"
|
|
|
|
//% parts="ledmatrix"
|
2018-05-29 23:55:58 +01:00
|
|
|
void showLeds(ImageLiteral_ leds, int interval = 400) {
|
2016-04-02 13:44:29 -07:00
|
|
|
uBit.display.print(MicroBitImage(imageBytes(leds)), 0, 0, 0, interval);
|
2016-03-29 17:11:17 -07:00
|
|
|
}
|
2016-03-10 14:01:04 -08: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 13:10:54 -07:00
|
|
|
//% help=basic/show-string
|
2018-09-26 16:27:32 -07:00
|
|
|
//% weight=87 blockGap=16
|
2017-10-24 13:10:54 -07:00
|
|
|
//% block="show|string %text"
|
2016-03-10 14:01:04 -08:00
|
|
|
//% async
|
|
|
|
//% blockId=device_print_message
|
2016-08-22 08:48:48 -07:00
|
|
|
//% parts="ledmatrix"
|
2018-08-02 16:30:02 -04:00
|
|
|
//% text.shadowOptions.toString=true
|
2018-05-29 23:55:58 +01:00
|
|
|
void showString(String text, int interval = 150) {
|
2017-05-12 14:01:19 -07:00
|
|
|
if (interval <= 0)
|
|
|
|
interval = 1;
|
2018-05-29 23:55:58 +01:00
|
|
|
int l = text ? text->length : 0;
|
2016-03-29 17:11:17 -07:00
|
|
|
if (l == 0) {
|
|
|
|
uBit.display.clear();
|
2016-04-19 11:52:44 -07:00
|
|
|
fiber_sleep(interval * 5);
|
2016-03-29 17:11:17 -07:00
|
|
|
} else if (l > 1) {
|
2018-05-29 23:55:58 +01:00
|
|
|
uBit.display.scroll(MSTR(text), interval);
|
2016-03-29 17:11:17 -07:00
|
|
|
} else {
|
2018-08-09 10:45:46 -04:00
|
|
|
uBit.display.printChar(text->data[0], interval * 5);
|
2016-03-29 17:11:17 -07:00
|
|
|
}
|
|
|
|
}
|
2016-03-10 14:01:04 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn off all LEDs
|
|
|
|
*/
|
2016-03-21 22:13:39 -07:00
|
|
|
//% help=basic/clear-screen weight=79
|
2017-01-05 07:01:50 -08:00
|
|
|
//% blockId=device_clear_display block="clear screen"
|
2016-08-22 08:48:48 -07:00
|
|
|
//% parts="ledmatrix"
|
2017-01-31 15:54:23 -08:00
|
|
|
//% advanced=true
|
2016-03-29 17:11:17 -07:00
|
|
|
void clearScreen() {
|
|
|
|
uBit.display.image.clear();
|
|
|
|
}
|
2016-03-10 14:01:04 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows a sequence of LED screens as an animation.
|
2016-06-14 06:30:07 -07:00
|
|
|
* @param leds pattern of LEDs to turn on/off
|
|
|
|
* @param interval time in milliseconds between each redraw
|
2016-03-10 14:01:04 -08:00
|
|
|
*/
|
2018-05-09 10:56:48 -07:00
|
|
|
//% help=basic/show-animation imageLiteral=1 async
|
2016-08-22 08:48:48 -07:00
|
|
|
//% parts="ledmatrix"
|
2018-05-29 23:55:58 +01:00
|
|
|
void showAnimation(ImageLiteral_ leds, int interval = 400) {
|
2017-05-29 23:01:31 -07:00
|
|
|
uBit.display.animate(MicroBitImage(imageBytes(leds)), interval, 5, 0, 0);
|
2016-03-29 17:11:17 -07:00
|
|
|
}
|
2016-03-10 14:01:04 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Draws an image on the LED screen.
|
2016-06-14 06:30:07 -07:00
|
|
|
* @param leds pattern of LEDs to turn on/off
|
2016-03-10 14:01:04 -08:00
|
|
|
*/
|
2016-04-01 21:26:06 -07:00
|
|
|
//% help=basic/plot-leds weight=80
|
2016-08-22 08:48:48 -07:00
|
|
|
//% parts="ledmatrix"
|
2018-05-29 23:55:58 +01:00
|
|
|
void plotLeds(ImageLiteral_ leds) {
|
2016-04-02 13:44:29 -07:00
|
|
|
MicroBitImage i(imageBytes(leds));
|
2016-03-29 17:11:17 -07:00
|
|
|
uBit.display.print(i, 0, 0, 0, 0);
|
|
|
|
}
|
|
|
|
|
2016-03-10 14:01:04 -08:00
|
|
|
/**
|
|
|
|
* Repeats the code forever in the background. On each iteration, allows other codes to run.
|
2016-06-14 06:30:07 -07:00
|
|
|
* @param body code to execute
|
2016-03-10 14:01:04 -08:00
|
|
|
*/
|
2018-09-26 16:27:32 -07:00
|
|
|
//% help=basic/forever weight=55 blockGap=16 blockAllowMultiple=1 afterOnStart=true
|
2016-03-29 17:11:17 -07:00
|
|
|
//% blockId=device_forever block="forever" icon="\uf01e"
|
|
|
|
void forever(Action a) {
|
2018-05-29 23:55:58 +01:00
|
|
|
runForever(a);
|
2016-03-29 17:11:17 -07:00
|
|
|
}
|
2016-03-10 14:01:04 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pause for the specified time in milliseconds
|
|
|
|
* @param ms how long to pause for, eg: 100, 200, 500, 1000, 2000
|
|
|
|
*/
|
2016-03-21 22:13:39 -07:00
|
|
|
//% help=basic/pause weight=54
|
2018-09-26 16:27:32 -07:00
|
|
|
//% async block="pause (ms) %pause" blockGap=16
|
2016-03-10 14:01:04 -08:00
|
|
|
//% blockId=device_pause icon="\uf110"
|
2018-10-04 09:22:45 -07:00
|
|
|
//% pause.shadow=timePicker
|
2016-03-29 17:11:17 -07:00
|
|
|
void pause(int ms) {
|
2016-04-19 11:52:44 -07:00
|
|
|
fiber_sleep(ms);
|
2016-03-29 17:11:17 -07:00
|
|
|
}
|
2018-05-09 10:56:48 -07:00
|
|
|
}
|