pxt-calliope/libs/microbit/led.ts

207 lines
5.9 KiB
TypeScript
Raw Normal View History

2016-03-10 23:01:04 +01:00
enum DisplayMode {
//% block="black and white"
BackAndWhite = 0,
//% block="greyscale"
Greyscale = 1,
2016-03-10 23:01:04 +01:00
}
//% color=3 weight=35
namespace led {
/**
* Turn on the specified LED using x, y coordinates (x is horizontal, y is vertical). (0,0) is upper left.
* @param x TODO
* @param y TODO
*/
2016-03-22 06:13:39 +01:00
//% help=led/plot weight=78 shim=micro_bit::plot
2016-03-10 23:01:04 +01:00
//% blockId=device_plot block="plot|x %x|y %y" icon="\uf205" blockGap=8
export function plot(x: number, y: number): void { }
/**
* Turn off the specified LED using x, y coordinates (x is horizontal, y is vertical). (0,0) is upper left.
* @param x TODO
* @param y TODO
*/
2016-03-22 06:13:39 +01:00
//% help=led/unplot weight=77 shim=micro_bit::unPlot
2016-03-10 23:01:04 +01:00
//% blockId=device_unplot block="unplot|x %x|y %y" icon="\uf204" blockGap=8
export function unplot(x: number, y: number): void { }
/**
* Get the on/off state of the specified LED using x, y coordinates. (0,0) is upper left.
* @param x TODO
* @param y TODO
*/
2016-03-22 06:13:39 +01:00
//% help=led/point weight=76 shim=micro_bit::point
2016-03-10 23:01:04 +01:00
//% blockId=device_point block="point|x %x|y %y" icon="\uf10c"
export function point(x: number, y: number): boolean {
return false;
}
/**
* Get the screen brightness from 0 (off) to 255 (full bright).
*/
2016-03-22 06:13:39 +01:00
//% help=led/brightness weight=60 shim=micro_bit::getBrightness
2016-03-10 23:01:04 +01:00
//% blockId=device_get_brightness block="brightness" icon="\uf042" blockGap=8
export function brightness(): number {
return 0;
}
/**
* Set the screen brightness from 0 (off) to 255 (full bright).
* @param value the brightness value, eg:255, 127, 0
*/
2016-03-22 06:13:39 +01:00
//% help=led/set-brightness weight=59 shim=micro_bit::setBrightness
2016-03-10 23:01:04 +01:00
//% blockId=device_set_brightness block="set brightness %value" icon="\uf042"
export function setBrightness(value: number): void { }
/**
* Cancels the current animation and clears other pending animations.
*/
2016-03-22 06:13:39 +01:00
//% weight=50 shim=uBit.display.stopAnimation help=led/stop-animation
2016-03-10 23:01:04 +01:00
//% blockId=device_stop_animation block="stop animation" icon="\uf04d"
export function stopAnimation(): void { }
/**
* Displays a vertical bar graph based on the ``value`` and ``high`` value.
* @param value current value to plot
* @param high maximum value, eg: 1023, 255
*/
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-03-22 00:49:21 +01:00
export function plotBarGraph(value: number, high: number): void {
2016-03-10 23:01:04 +01:00
2016-03-16 22:14:49 +01:00
writeString(value.toString() + "\r\n");
2016-03-10 23:01:04 +01:00
let v = Math.abs((value * 15) / high);
let k = 0;
for(let y = 4; y >= 0; --y) {
for (let x = 0; x < 3; ++x) {
if (k > v) {
unplot(2-x,y);
unplot(2+x,y);
} else {
plot(2-x, y);
plot(2+x, y);
}
++k;
}
}
}
/**
* Writes a string to serial
*/
//% shim=micro_bit::serialSendString
function writeString(text: string): void { }
/**
* Sets the display mode between black and white and greyscale for rendering LEDs.
* @param mode TODO
*/
2016-03-22 06:13:39 +01:00
//% shim=micro_bit::setDisplayMode weight=1 help=/led/set-display-mode
2016-03-10 23:01:04 +01:00
export function setDisplayMode(mode: DisplayMode): void { }
/**
* 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);
}
/**
* Takes a screenshot of the LED screen and returns an image.
*/
2016-03-22 06:13:39 +01:00
//% shim=uBit.display.screenShot help=led/screenshot
2016-04-02 04:55:51 +02:00
export function screenshot(): Image {
2016-03-10 23:01:04 +01:00
/*
let img: Image;
img = image.createImage("");
for (let i = 0; i < 5; i++) {
for (let j = 0; j < 5; j++) {
if (led.point(i, j)) {
img.setPixel(i, j, true);
}
}
}
return img;
*/
return null;
}
}