pxt-calliope/libs/microbit/led.ts

133 lines
3.5 KiB
TypeScript
Raw Normal View History

2016-04-06 00:59:25 +02:00
/**
* Control of the LED screen.
*/
2016-03-10 23:01:04 +01:00
//% color=3 weight=35
namespace led {
// what's the current high value
let barGraphHigh = 0;
// when was the current high value recorded
let barGraphHighLast = 0;
2016-05-17 01:24:44 +02:00
2016-03-10 23:01:04 +01:00
/**
* Displays a vertical bar graph based on the `value` and `high` value.
* If `high` is 0, the chart gets adjusted automatically.
2016-03-10 23:01:04 +01:00
* @param value current value to plot
2016-04-22 09:14:57 +02:00
* @param high maximum value. If 0, maximum value adjusted automatically, eg: 0
2016-03-10 23:01:04 +01:00
*/
2016-03-22 06:13:39 +01:00
//% help=/led/plot-bar-graph weight=20
2016-03-10 23:01:04 +01:00
//% blockId=device_plot_bar_graph block="plot bar graph of %value |up to %high" icon="\uf080" blockExternalInputs=true
2016-05-17 01:24:44 +02:00
export function plotBarGraph(value: number, high: number): void {
let now = input.runningTime();
serial.writeString(value.toString() + "\r\n");
value = Math.abs(value);
2016-05-17 01:24:44 +02:00
if (high != 0) barGraphHigh = high;
else if (value > barGraphHigh || now - barGraphHighLast > 5000) {
barGraphHigh = value;
barGraphHighLast = now;
}
2016-05-17 01:24:44 +02:00
barGraphHigh = Math.max(barGraphHigh, 16);
2016-05-17 01:24:44 +02:00
let v = (value * 15) / barGraphHigh;
2016-03-10 23:01:04 +01:00
let k = 0;
2016-05-17 01:24:44 +02:00
for (let y = 4; y >= 0; --y) {
2016-03-10 23:01:04 +01:00
for (let x = 0; x < 3; ++x) {
if (k > v) {
2016-05-17 01:24:44 +02:00
unplot(2 - x, y);
unplot(2 + x, y);
2016-03-10 23:01:04 +01:00
} else {
2016-05-17 01:24:44 +02:00
plot(2 - x, y);
plot(2 + x, y);
2016-03-10 23:01:04 +01:00
}
++k;
}
2016-05-17 01:24:44 +02:00
}
2016-03-10 23:01:04 +01:00
}
2016-05-17 01:24:44 +02:00
2016-03-10 23:01:04 +01:00
/**
* Toggles a particular pixel
* @param x TODO
* @param y TODO
*/
2016-03-22 06:13:39 +01:00
//% help=led/toggle
2016-03-10 23:01:04 +01:00
export function toggle(x: number, y: number): void {
if (led.point(x, y)) {
led.unplot(x, y);
} else {
led.plot(x, y);
}
}
/**
* Turns all LEDS on
*/
2016-03-22 06:13:39 +01:00
//% help=led/plot-all
2016-03-10 23:01:04 +01:00
export function plotAll(): void {
for (let i = 0; i < 5; i++) {
for (let j = 0; j < 5; j++) {
led.plot(i, j);
}
}
}
/**
* Inverts the current LED display
*/
2016-03-22 06:13:39 +01:00
//% help=led/toggle-all
2016-03-10 23:01:04 +01:00
export function toggleAll(): void {
for (let i = 0; i < 5; i++) {
for (let j = 0; j < 5; j++) {
led.toggle(i, j);
}
}
}
/**
* Fades in the screen display.
* @param ms TODO
*/
2016-03-22 06:13:39 +01:00
//% help=led/fade-in
2016-03-10 23:01:04 +01:00
export function fadeIn(ms: number = 700): void {
if (ms < 20) {
led.setBrightness(255);
return;
}
let dt = 50;
let brightness = led.brightness();
let start = input.runningTime();
let elapsed = 0;
while (elapsed < ms) {
led.setBrightness(brightness + ((255 - brightness) * elapsed) / ms);
basic.pause(dt);
elapsed = input.runningTime() - start;
}
led.setBrightness(255);
}
/**
* Fades out the screen brightness.
* @param ms TODO
*/
2016-03-22 06:13:39 +01:00
//% help=led/fade-out
2016-03-10 23:01:04 +01:00
export function fadeOut(ms: number = 700): void {
if (ms < 20) {
led.setBrightness(0);
return;
}
let brightness = led.brightness();
let dt = 50;
let start = input.runningTime();
let elapsed = 0;
while (elapsed < ms) {
led.setBrightness(brightness - (brightness * elapsed) / ms);
basic.pause(dt);
elapsed = input.runningTime() - start;
}
led.setBrightness(0);
}
}