From e7c1915076082c9464d69f5387de2dc46364b715 Mon Sep 17 00:00:00 2001 From: Peli de Halleux Date: Mon, 7 Aug 2017 11:47:43 -0700 Subject: [PATCH] support for led.plotBrightness (#494) * support for led.plotBrightness * fixing c++ build * fixed blockid --- docs/reference/led.md | 3 +- docs/reference/led/plotBrightness.md | 69 ++++++++++++++++++++++ libs/core/_locales/core-jsdoc-strings.json | 8 ++- libs/core/_locales/core-strings.json | 1 + libs/core/led.cpp | 25 +++++++- libs/core/shims.d.ts | 17 +++++- sim/state/ledmatrix.ts | 11 +++- 7 files changed, 125 insertions(+), 9 deletions(-) create mode 100644 docs/reference/led/plotBrightness.md diff --git a/docs/reference/led.md b/docs/reference/led.md index 117aff0d..8338b867 100644 --- a/docs/reference/led.md +++ b/docs/reference/led.md @@ -22,4 +22,5 @@ led.enable(false) ### See Also -[plot](/reference/led/plot), [unplot](/reference/led/unplot), [point](/reference/led/point), [brightness](/reference/led/brightness), [setBrightness](/reference/led/set-brightness), [stopAnimation](/reference/led/stop-animation), [plotBarGraph](/reference/led/plot-bar-graph), [fadeIn](/reference/led/fade-in), [fadeOut](/reference/led/fade-out), [plotAll](/reference/led/plot-all), [screenshot](/reference/led/screenshot), [toggle](/reference/led/toggle), [toggleAll](/reference/led/toggle-all), [setDisplayMode](/reference/led/set-display-mode), [enabled](/reference/led/enable) +[plot](/reference/led/plot), [unplot](/reference/led/unplot), [point](/reference/led/point), [brightness](/reference/led/brightness), [setBrightness](/reference/led/set-brightness), [stopAnimation](/reference/led/stop-animation), [plotBarGraph](/reference/led/plot-bar-graph), [fadeIn](/reference/led/fade-in), [fadeOut](/reference/led/fade-out), [plotAll](/reference/led/plot-all), [screenshot](/reference/led/screenshot), [toggle](/reference/led/toggle), [toggleAll](/reference/led/toggle-all), [setDisplayMode](/reference/led/set-display-mode), [enabled](/reference/led/enable), +[plotBrightness](/reference/led/plotBrightness), diff --git a/docs/reference/led/plotBrightness.md b/docs/reference/led/plotBrightness.md new file mode 100644 index 00000000..286f4200 --- /dev/null +++ b/docs/reference/led/plotBrightness.md @@ -0,0 +1,69 @@ +# Plot Brightness + +Turn on a LED light with a specific brightness on the [LED screen](/device/screen). + +```sig +led.plotBrightness(0,0, 128); +``` + +## ~hint + +Use [unplot](/reference/led/unplot) to turn **off** an LED. + +## ~ + +### Parameters + +* ``x`` is a [number](/types/number) that means the + horizontal spot on the LED screen (from left to right: 0, 1, 2, 3, + or 4) +* ``y`` is a [number](/types/number) that means the vertical + spot on the LED screen (from top to bottom: 0, 1, 2, 3, or 4) +* ``brightness` is a [number](/types/number) that represents the brightness of the LED, from 0 (off) to 255 (full brightness) + +If a parameter is [out of bounds](/reference/out-of-bounds) (a value +other than 0 to 4), then this function will do nothing. + +### ~hint + +The LED screen is a solid square of LEDs with five LEDs on each side. +To learn more about how you number the LEDs with ``x`` and ``y`` +coordinates, see [LED screen](/device/screen). + +### ~ + +### Example: One LED + +This program turns on the bottom right LED at 50% brightness + +```blocks +led.plot(4, 4, 128) +``` + + +### Example: Square + +This program uses a [for loop](/blocks/loops/for) +and the `plotBrightness` function +to make a square around the edges of the LED screen. + +```blocks +for (let i = 0; i < 5; i++) { + led.plot(0, i, 64) + led.plot(4, i, 128) + led.plot(i, 0, 172) + led.plot(i, 4, 255) + basic.pause(500) +} +``` + +### ~hint + +Use the [point](/reference/led/point) function to find out if an LED is +on or off. + +### ~ + +### See also + +[plot](/reference/led/plot), [unplot](/reference/led/unplot), [point](/reference/led/point), [LED screen](/device/screen) diff --git a/libs/core/_locales/core-jsdoc-strings.json b/libs/core/_locales/core-jsdoc-strings.json index 6c53cc1a..55dd2f34 100644 --- a/libs/core/_locales/core-jsdoc-strings.json +++ b/libs/core/_locales/core-jsdoc-strings.json @@ -156,8 +156,12 @@ "led.plotBarGraph": "Displays a vertical bar graph based on the `value` and `high` value.\nIf `high` is 0, the chart gets adjusted automatically.", "led.plotBarGraph|param|high": "maximum value. If 0, maximum value adjusted automatically, eg: 0", "led.plotBarGraph|param|value": "current value to plot", - "led.plot|param|x": "TODO", - "led.plot|param|y": "TODO", + "led.plotBrightness": "Turn on the specified LED with specific brightness using x, y coordinates (x is horizontal, y is vertical). (0,0) is upper left.", + "led.plotBrightness|param|brightness": "the brightness from 0 (off) to 255 (bright), eg:255", + "led.plotBrightness|param|x": "the horizontal coordinate of the LED starting at 0", + "led.plotBrightness|param|y": "the vertical coordinate of the LED starting at 0", + "led.plot|param|x": "the horizontal coordinate of the LED starting at 0", + "led.plot|param|y": "the vertical coordinate of the LED starting at 0", "led.point": "Get the on/off state of the specified LED using x, y coordinates. (0,0) is upper left.", "led.point|param|x": "TODO", "led.point|param|y": "TODO", diff --git a/libs/core/_locales/core-strings.json b/libs/core/_locales/core-strings.json index 99d516bd..9640a34b 100644 --- a/libs/core/_locales/core-strings.json +++ b/libs/core/_locales/core-strings.json @@ -264,6 +264,7 @@ "led.brightness|block": "brightness", "led.enable|block": "led enable %on", "led.plotBarGraph|block": "plot bar graph of %value |up to %high", + "led.plotBrightness|block": "plot|x %x|y %y|value %brightness", "led.plot|block": "plot|x %x|y %y", "led.point|block": "point|x %x|y %y", "led.setBrightness|block": "set brightness %value", diff --git a/libs/core/led.cpp b/libs/core/led.cpp index 33bc77ee..ea560626 100644 --- a/libs/core/led.cpp +++ b/libs/core/led.cpp @@ -13,15 +13,34 @@ 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 + * @param x the horizontal coordinate of the LED starting at 0 + * @param y the vertical coordinate of the LED starting at 0 */ //% help=led/plot weight=78 //% blockId=device_plot block="plot|x %x|y %y" blockGap=8 //% parts="ledmatrix" //% x.min=0 x.max=4 y.min=0 y.max=4 void plot(int x, int y) { - uBit.display.image.setPixelValue(x, y, 1); + 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 weight=78 + //% blockId=device_plot_brightness block="plot|x %x|y %y|value %brightness" blockGap=8 + //% parts="ledmatrix" + //% x.min=0 x.max=4 y.min=0 y.max=4 brightness.min=0 brightness.max=255 + //% 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); } /** diff --git a/libs/core/shims.d.ts b/libs/core/shims.d.ts index 4155db25..b9e8c82e 100644 --- a/libs/core/shims.d.ts +++ b/libs/core/shims.d.ts @@ -441,8 +441,8 @@ declare 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 + * @param x the horizontal coordinate of the LED starting at 0 + * @param y the vertical coordinate of the LED starting at 0 */ //% help=led/plot weight=78 //% blockId=device_plot block="plot|x %x|y %y" blockGap=8 @@ -450,6 +450,19 @@ declare namespace led { //% x.min=0 x.max=4 y.min=0 y.max=4 shim=led::plot function plot(x: number, y: number): void; + /** + * 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 weight=78 + //% blockId=device_plot_brightness block="plot|x %x|y %y|value %brightness" blockGap=8 + //% parts="ledmatrix" + //% x.min=0 x.max=4 y.min=0 y.max=4 brightness.min=0 brightness.max=255 + //% advanced=true shim=led::plotBrightness + function plotBrightness(x: number, y: number, brightness: 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 diff --git a/sim/state/ledmatrix.ts b/sim/state/ledmatrix.ts index fa4639de..7c62600d 100644 --- a/sim/state/ledmatrix.ts +++ b/sim/state/ledmatrix.ts @@ -273,7 +273,16 @@ namespace pxsim.basic { namespace pxsim.led { export function plot(x: number, y: number) { - board().ledMatrixState.image.set(x, y, 255); + board().ledMatrixState.image.set(x, y, 0xff); + runtime.queueDisplayUpdate() + } + + export function plotBrightness(x: number, y: number, brightness: number) { + const state = board().ledMatrixState; + brightness = Math.max(0, Math.min(0xff, brightness)); + if (brightness != 0 && brightness != 0xff && state.displayMode != DisplayMode.greyscale) + state.displayMode = DisplayMode.greyscale; + state.image.set(x, y, brightness); runtime.queueDisplayUpdate() }