Skip to content

Commit

Permalink
add plotBarGraph to led client
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Jun 7, 2022
1 parent 2e76e41 commit 36a9b98
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 21 deletions.
62 changes: 59 additions & 3 deletions led/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ namespace modules {
* You need to call ``show`` to make the changes visible.
* @param rgb RGB color of the LED
*/
//% blockId="jacdac_leddisplay_set_pixel_color" block="set %display color at %index pixels to %rgb=colorNumberPicker"
//% blockId="jacdac_leddisplay_set_pixel_color" block="set %display color at pixel %index to %rgb=colorNumberPicker"
//% weight=81 blockGap=8
//% group="LED"
setPixelColor(index: number, rgb: number) {
Expand All @@ -340,10 +340,9 @@ namespace modules {

/**
* Set all of the pixels on the strip to one RGB color.
* You need to call ``show`` to make the changes visible.
* @param rgb RGB color of the LED
*/
//% blockId="jacdac_leddisplay_set_strip_color" block="set %display all pixels to %rgb=colorNumberPicker"
//% blockId="jacdac_leddisplay_set_strip_color" block="set %display all to %rgb=colorNumberPicker"
//% weight=80 blockGap=8
//% group="LED"
setAll(rgb: number) {
Expand All @@ -366,6 +365,57 @@ namespace modules {
if (dirty) this.setDirty()
}

private _barGraphHigh = 0
private _barGraphHighLast = 0
/**
* Displays a vertical bar graph based on the `value` and `high` value.
* If `high` is 0, the chart gets adjusted automatically.
* @param value current value to plot
* @param high maximum value, eg: 255
*/
//% weight=84
//% blockId=jacdac_led_show_bar_graph block="plot %strip bar graph of $value||up to $high"
plotBarGraph(value: number, high?: number): void {
if (isNaN(value)) {
this.clear()
this.setDirty()
return
}

const n = this.numPixels()
const pixels = this._localPixels
if (!pixels || n <= 0) return

value = Math.abs(value)
const now = control.millis()
// auto-scale "high" is not provided
if (high > 0) {
this._barGraphHigh = high
} else if (
value > this._barGraphHigh ||
now - this._barGraphHighLast > 10000
) {
this._barGraphHigh = value
this._barGraphHighLast = now
}

// normalize lack of data to 0..1
if (this._barGraphHigh < 16 * Number.EPSILON) this._barGraphHigh = 1

// normalize value to 0..1
const v = value / this._barGraphHigh
const dv = 1 / n
const n1 = n - 1
this.clear()
for(let cv = 0, i = 0; cv < v && i < n; ++i) {
const b = Math.idiv(i * 0xff, n - 1);
pixels[i * 3] = b
pixels[i * 3 + 2] = 0xff - b
cv += dv
}
this.setDirty()
}

/**
* Shift LEDs forward and clear with zeros.
* You need to call ``show`` to make the changes visible.
Expand Down Expand Up @@ -400,6 +450,12 @@ namespace modules {
pixels.rotate(-offset * stride)
this.setDirty()
}

private clear() {
const pixels = this._localPixels
if (!pixels) return
pixels.fill(0, 0, pixels.length)
}
}

//% fixedInstance whenUsed
Expand Down
19 changes: 7 additions & 12 deletions led/pxt.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@
"name": "jacdac-led",
"version": "0.10.56",
"description": "A controller for 1 or more monochrome or RGB LEDs connected in parallel.",
"files": [
"constants.ts",
"client.ts"
],
"testFiles": [
"test.ts"
],
"supportedTargets": [
"arcade",
"microbit",
"maker"
],
"files": ["constants.ts", "client.ts"],
"testFiles": ["test.microbit.ts"],
"supportedTargets": ["arcade", "microbit", "maker"],
"fileDependencies": {
"test.microbit.ts": "target:microbit"
},
"dependencies": {
"core": "*",
"color": "*",
"jacdac": "github:microsoft/pxt-jacdac"
}
}
11 changes: 11 additions & 0 deletions led/test.microbit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
forever(() => {
led.toggle(0, 0)
if (input.buttonIsPressed(Button.A)) {
modules.led1.setAll(0xff0f00)
pause(500)
modules.led1.setAll(0x00000f)
pause(500)
} else {
modules.led1.plotBarGraph(input.acceleration(Dimension.X))
}
})
6 changes: 0 additions & 6 deletions led/test.ts

This file was deleted.

0 comments on commit 36a9b98

Please sign in to comment.