-
Notifications
You must be signed in to change notification settings - Fork 708
Expand file tree
/
Copy pathbasic.cpp
More file actions
103 lines (95 loc) · 3.15 KB
/
basic.cpp
File metadata and controls
103 lines (95 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "pxt.h"
/**
* Provides access to basic micro:bit functionality.
*/
//% color=#1E90FF weight=116 icon="\uf00a"
namespace basic {
/**
* Draws an image on the LED screen.
* @param leds the pattern of LED to turn on/off.
* @param interval time in milliseconds to pause after drawing.
*/
//% help=basic/show-leds
//% weight=95 blockGap=8
//% imageLiteral=1 async
//% blockId=device_show_leds
//% block="show leds" icon="\uf00a"
//% parts="ledmatrix"
void showLeds(ImageLiteral_ leds, int interval = 400) {
uBit.display.print(MicroBitImage(imageBytes(leds)), 0, 0, 0, interval);
}
/**
* 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
*/
//% help=basic/show-string
//% weight=87 blockGap=16
//% block="show|string %text"
//% async
//% blockId=device_print_message
//% parts="ledmatrix"
//% text.shadowOptions.toString=true
void showString(String text, int interval = 150) {
if (interval <= 0)
interval = 1;
int l = text ? text->getUTF8Size() : 0;
if (l == 0) {
uBit.display.clear();
fiber_sleep(interval * 5);
} else if (l > 1) {
uBit.display.scroll(MSTR(text), interval);
} else {
uBit.display.printChar(text->getUTF8Data()[0], interval * 5);
}
}
/**
* Turn off all LEDs
*/
//% help=basic/clear-screen weight=79
//% blockId=device_clear_display block="clear screen"
//% parts="ledmatrix"
void clearScreen() {
uBit.display.image.clear();
}
/**
* Shows a sequence of LED screens as an animation.
* @param leds pattern of LEDs to turn on/off
* @param interval time in milliseconds between each redraw.
*/
//% help=basic/show-animation imageLiteral=1 async
//% parts="ledmatrix"
void showAnimation(ImageLiteral_ leds, int interval = 400) {
uBit.display.animate(MicroBitImage(imageBytes(leds)), interval, 5, 0, 0);
}
/**
* Draws an image on the LED screen.
* @param leds pattern of LEDs to turn on/off
*/
//% help=basic/plot-leds weight=80
//% parts="ledmatrix"
void plotLeds(ImageLiteral_ leds) {
MicroBitImage i(imageBytes(leds));
uBit.display.print(i, 0, 0, 0, 0);
}
/**
* Repeats the code forever in the background. On each iteration, allows other codes to run.
* @param body code to execute
*/
//% help=basic/forever weight=55 blockGap=16 blockAllowMultiple=1 afterOnStart=true
//% blockId=device_forever block="forever" icon="\uf01e"
void forever(Action a) {
runForever(a);
}
/**
* Pause for the specified time in milliseconds
* @param ms how long to pause for, eg: 100, 200, 500, 1000, 2000
*/
//% help=basic/pause weight=54
//% async block="pause (ms) %pause" blockGap=16
//% blockId=device_pause icon="\uf110"
//% pause.shadow=timePicker
void pause(int ms) {
fiber_sleep(ms);
}
}