2016-11-30 06:55:37 +01:00
|
|
|
#include "pxt.h"
|
2016-04-02 06:52:25 +02:00
|
|
|
|
|
|
|
enum class DisplayMode_ {
|
|
|
|
//% block="black and white"
|
2019-12-02 05:58:26 +01:00
|
|
|
BlackAndWhite = DISPLAY_MODE_BLACK_AND_WHITE,
|
|
|
|
//% blockHidden=true
|
2016-04-02 06:52:25 +02:00
|
|
|
BackAndWhite = DISPLAY_MODE_BLACK_AND_WHITE,
|
|
|
|
//% block="greyscale"
|
|
|
|
Greyscale = DISPLAY_MODE_GREYSCALE,
|
|
|
|
// TODO DISPLAY_MODE_BLACK_AND_WHITE_LIGHT_SENSE
|
|
|
|
};
|
|
|
|
|
2017-01-20 05:55:31 +01:00
|
|
|
//% color=#8169E6 weight=35 icon="\uf205"
|
2016-04-02 06:52:25 +02:00
|
|
|
namespace led {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn on the specified LED using x, y coordinates (x is horizontal, y is vertical). (0,0) is upper left.
|
2017-12-14 20:00:47 +01:00
|
|
|
* @param x the horizontal coordinate of the LED starting at 0
|
|
|
|
* @param y the vertical coordinate of the LED starting at 0
|
2016-04-02 06:52:25 +02:00
|
|
|
*/
|
2016-04-02 06:53:50 +02:00
|
|
|
//% help=led/plot weight=78
|
2017-01-20 05:55:31 +01:00
|
|
|
//% blockId=device_plot block="plot|x %x|y %y" blockGap=8
|
2016-08-22 17:48:48 +02:00
|
|
|
//% parts="ledmatrix"
|
2017-12-14 20:00:47 +01:00
|
|
|
//% x.min=0 x.max=4 y.min=0 y.max=4
|
2019-12-02 05:58:26 +01:00
|
|
|
//% x.fieldOptions.precision=1 y.fieldOptions.precision=1
|
2016-10-11 22:48:25 +02:00
|
|
|
void plot(int x, int y) {
|
2017-12-14 20:00:47 +01:00
|
|
|
uBit.display.image.setPixelValue(x, y, 0xff);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn on the specified LED with specific brightness using x, y coordinates (x is horizontal, y is vertical). (0,0) is upper left.
|
|
|
|
* @param x the horizontal coordinate of the LED starting at 0
|
|
|
|
* @param y the vertical coordinate of the LED starting at 0
|
|
|
|
* @param brightness the brightness from 0 (off) to 255 (bright), eg:255
|
|
|
|
*/
|
|
|
|
//% help=led/plot-brightness weight=78
|
|
|
|
//% blockId=device_plot_brightness block="plot|x %x|y %y|brightness %brightness" blockGap=8
|
|
|
|
//% parts="ledmatrix"
|
|
|
|
//% x.min=0 x.max=4 y.min=0 y.max=4 brightness.min=0 brightness.max=255
|
2019-12-02 05:58:26 +01:00
|
|
|
//% x.fieldOptions.precision=1 y.fieldOptions.precision=1
|
2017-12-14 20:00:47 +01:00
|
|
|
//% advanced=true
|
|
|
|
void plotBrightness(int x, int y, int brightness) {
|
|
|
|
brightness = max(0, min(0xff, brightness));
|
|
|
|
// enable greyscale as needed
|
|
|
|
if (brightness != 0 && brightness != 0xff && uBit.display.getDisplayMode() != DISPLAY_MODE_GREYSCALE)
|
|
|
|
uBit.display.setDisplayMode(DISPLAY_MODE_GREYSCALE);
|
|
|
|
uBit.display.image.setPixelValue(x, y, brightness);
|
2016-04-02 06:52:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn off the specified LED using x, y coordinates (x is horizontal, y is vertical). (0,0) is upper left.
|
2019-12-02 05:58:26 +01:00
|
|
|
* @param x the horizontal coordinate of the LED
|
|
|
|
* @param y the vertical coordinate of the LED
|
2016-04-02 06:52:25 +02:00
|
|
|
*/
|
2016-04-02 06:53:50 +02:00
|
|
|
//% help=led/unplot weight=77
|
2017-01-20 05:55:31 +01:00
|
|
|
//% blockId=device_unplot block="unplot|x %x|y %y" blockGap=8
|
2016-08-22 17:48:48 +02:00
|
|
|
//% parts="ledmatrix"
|
2017-12-14 20:00:47 +01:00
|
|
|
//% x.min=0 x.max=4 y.min=0 y.max=4
|
2019-12-02 05:58:26 +01:00
|
|
|
//% x.fieldOptions.precision=1 y.fieldOptions.precision=1
|
2016-10-11 22:48:25 +02:00
|
|
|
void unplot(int x, int y) {
|
2016-04-02 06:52:25 +02:00
|
|
|
uBit.display.image.setPixelValue(x, y, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-09-08 11:04:25 +02:00
|
|
|
* Get the brightness state of the specified LED using x, y coordinates. (0,0) is upper left.
|
2019-12-02 05:58:26 +01:00
|
|
|
* @param x the horizontal coordinate of the LED
|
|
|
|
* @param y the vertical coordinate of the LED
|
2016-04-02 06:52:25 +02:00
|
|
|
*/
|
2020-09-08 11:04:25 +02:00
|
|
|
//% help=led/point-brightness weight=76
|
|
|
|
//% blockId=device_point_brightness block="point|x %x|y %y brightness"
|
2016-08-22 17:48:48 +02:00
|
|
|
//% parts="ledmatrix"
|
2017-12-14 20:00:47 +01:00
|
|
|
//% x.min=0 x.max=4 y.min=0 y.max=4
|
2019-12-02 05:58:26 +01:00
|
|
|
//% x.fieldOptions.precision=1 y.fieldOptions.precision=1
|
2020-09-08 11:04:25 +02:00
|
|
|
//% advanced=true
|
|
|
|
int pointBrightness(int x, int y) {
|
|
|
|
return uBit.display.image.getPixelValue(x, y);
|
2016-04-02 06:52:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the screen brightness from 0 (off) to 255 (full bright).
|
|
|
|
*/
|
2016-04-02 06:53:50 +02:00
|
|
|
//% help=led/brightness weight=60
|
2017-01-20 05:55:31 +01:00
|
|
|
//% blockId=device_get_brightness block="brightness" blockGap=8
|
2016-08-22 17:48:48 +02:00
|
|
|
//% parts="ledmatrix"
|
2016-10-11 22:48:25 +02:00
|
|
|
//% advanced=true
|
2016-04-02 06:52:25 +02:00
|
|
|
int brightness() {
|
|
|
|
return uBit.display.getBrightness();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the screen brightness from 0 (off) to 255 (full bright).
|
|
|
|
* @param value the brightness value, eg:255, 127, 0
|
|
|
|
*/
|
2016-04-02 06:53:50 +02:00
|
|
|
//% help=led/set-brightness weight=59
|
2017-01-20 05:55:31 +01:00
|
|
|
//% blockId=device_set_brightness block="set brightness %value"
|
2016-08-22 17:48:48 +02:00
|
|
|
//% parts="ledmatrix"
|
2016-10-11 22:48:25 +02:00
|
|
|
//% advanced=true
|
2017-12-14 20:00:47 +01:00
|
|
|
//% value.min=0 value.max=255
|
2016-10-11 22:48:25 +02:00
|
|
|
void setBrightness(int value) {
|
2016-04-02 06:52:25 +02:00
|
|
|
uBit.display.setBrightness(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Cancels the current animation and clears other pending animations.
|
|
|
|
*/
|
2016-04-02 06:53:50 +02:00
|
|
|
//% weight=50 help=led/stop-animation
|
2017-01-20 05:55:31 +01:00
|
|
|
//% blockId=device_stop_animation block="stop animation"
|
2016-08-22 17:48:48 +02:00
|
|
|
//% parts="ledmatrix"
|
2016-10-11 22:48:25 +02:00
|
|
|
//% advanced=true
|
|
|
|
void stopAnimation() {
|
2016-04-02 06:52:25 +02:00
|
|
|
uBit.display.stopAnimation();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sets the display mode between black and white and greyscale for rendering LEDs.
|
2016-10-28 17:34:04 +02:00
|
|
|
* @param mode mode the display mode in which the screen operates
|
2016-04-02 06:52:25 +02:00
|
|
|
*/
|
2016-08-10 22:10:40 +02:00
|
|
|
//% weight=1 help=led/set-display-mode
|
2019-12-02 05:58:26 +01:00
|
|
|
//% parts="ledmatrix" advanced=true weight=1
|
|
|
|
//% blockId="led_set_display_mode" block="set display mode $mode"
|
2016-10-11 22:48:25 +02:00
|
|
|
void setDisplayMode(DisplayMode_ mode) {
|
2016-04-02 06:52:25 +02:00
|
|
|
uBit.display.setDisplayMode((DisplayMode)mode);
|
|
|
|
}
|
|
|
|
|
2017-12-14 19:34:32 +01:00
|
|
|
/**
|
|
|
|
* Gets the current display mode
|
|
|
|
*/
|
|
|
|
//% weight=1 parts="ledmatrix" advanced=true
|
|
|
|
DisplayMode_ displayMode() {
|
|
|
|
return (DisplayMode_)uBit.display.getDisplayMode();
|
|
|
|
}
|
|
|
|
|
2016-10-28 17:34:04 +02:00
|
|
|
/**
|
2019-12-02 05:58:26 +01:00
|
|
|
* Turns on or off the display
|
2016-10-28 17:34:04 +02:00
|
|
|
*/
|
2017-12-14 20:00:47 +01:00
|
|
|
//% help=led/enable blockId=device_led_enable block="led enable %on"
|
2016-10-28 17:34:04 +02:00
|
|
|
//% advanced=true parts="ledmatrix"
|
|
|
|
void enable(bool on) {
|
|
|
|
if (on) uBit.display.enable();
|
|
|
|
else uBit.display.disable();
|
|
|
|
}
|
|
|
|
|
2016-04-02 06:52:25 +02:00
|
|
|
/**
|
|
|
|
* Takes a screenshot of the LED screen and returns an image.
|
|
|
|
*/
|
2016-04-02 06:53:50 +02:00
|
|
|
//% help=led/screenshot
|
2016-08-22 17:48:48 +02:00
|
|
|
//% parts="ledmatrix"
|
2016-04-02 06:52:25 +02:00
|
|
|
Image screenshot() {
|
2019-12-02 05:58:26 +01:00
|
|
|
auto d = uBit.display.screenShot().leakData();
|
|
|
|
auto r = NEW_GC(RefMImage, d);
|
|
|
|
d->decr();
|
|
|
|
return r;
|
2016-04-02 06:52:25 +02:00
|
|
|
/*
|
|
|
|
let Image img;
|
|
|
|
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;
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|