pxt-jacdac/led/client.ts

471 строка
16 KiB
TypeScript
Исходник Постоянная ссылка Обычный вид История

2021-05-05 04:37:06 +03:00
namespace modules {
//% fixedInstances
//% blockGap=8
2021-05-05 04:37:06 +03:00
export class LedClient extends jacdac.Client {
private _dirty = false
private _localPixels: Buffer
private _autoShow = true
private _autoShowUnsub: () => void
2022-04-23 03:34:24 +03:00
private readonly _pixels: jacdac.RegisterClient<[Buffer]>
private readonly _brightness: jacdac.RegisterClient<[number]>
private readonly _actualBrightness: jacdac.RegisterClient<[number]>
private readonly _numPixels: jacdac.RegisterClient<[number]>
private readonly _numColumns: jacdac.RegisterClient<[number]>
private readonly _maxPower: jacdac.RegisterClient<[number]>
private readonly _ledsPerPixel: jacdac.RegisterClient<[number]>
private readonly _waveLength: jacdac.RegisterClient<[number]>
private readonly _luminousIntensity: jacdac.RegisterClient<[number]>
private readonly _variant: jacdac.RegisterClient<[jacdac.LedVariant]>
2021-05-05 04:37:06 +03:00
constructor(role: string) {
2021-06-19 03:20:03 +03:00
super(jacdac.SRV_LED, role)
2021-05-05 04:37:06 +03:00
2022-04-23 03:34:24 +03:00
this._pixels = this.addRegister<[Buffer]>(
jacdac.LedReg.Pixels,
jacdac.LedRegPack.Pixels
)
this._brightness = this.addRegister<[number]>(
jacdac.LedReg.Brightness,
jacdac.LedRegPack.Brightness
)
this._actualBrightness = this.addRegister<[number]>(
jacdac.LedReg.ActualBrightness,
jacdac.LedRegPack.ActualBrightness
)
this._numPixels = this.addRegister<[number]>(
jacdac.LedReg.NumPixels,
jacdac.LedRegPack.NumPixels,
jacdac.RegisterClientFlags.Const
)
this._numColumns = this.addRegister<[number]>(
jacdac.LedReg.NumColumns,
jacdac.LedRegPack.NumColumns,
jacdac.RegisterClientFlags.Optional |
jacdac.RegisterClientFlags.Const
)
this._maxPower = this.addRegister<[number]>(
jacdac.LedReg.MaxPower,
jacdac.LedRegPack.MaxPower,
jacdac.RegisterClientFlags.Optional
)
this._ledsPerPixel = this.addRegister<[number]>(
jacdac.LedReg.LedsPerPixel,
jacdac.LedRegPack.LedsPerPixel,
jacdac.RegisterClientFlags.Optional |
jacdac.RegisterClientFlags.Const
)
this._waveLength = this.addRegister<[number]>(
jacdac.LedReg.WaveLength,
jacdac.LedRegPack.WaveLength,
jacdac.RegisterClientFlags.Optional |
jacdac.RegisterClientFlags.Const
)
this._luminousIntensity = this.addRegister<[number]>(
jacdac.LedReg.LuminousIntensity,
jacdac.LedRegPack.LuminousIntensity,
jacdac.RegisterClientFlags.Optional |
jacdac.RegisterClientFlags.Const
)
this._variant = this.addRegister<[jacdac.LedVariant]>(
jacdac.LedReg.Variant,
jacdac.LedRegPack.Variant,
jacdac.RegisterClientFlags.Optional |
jacdac.RegisterClientFlags.Const
)
2022-08-09 21:46:55 +03:00
// render again when number of pixels known
this._numPixels.on(jacdac.REPORT_UPDATE, () => this.show())
// maximum size (may be reduced on call to show)
this._localPixels = Buffer.create(
jacdac.CONST_LED_MAX_PIXELS_LENGTH * 3
2021-06-19 03:20:03 +03:00
)
2021-05-05 04:37:06 +03:00
}
2021-10-08 17:29:00 +03:00
/**
2022-04-23 03:34:24 +03:00
* Set the luminosity of the strip.
* At `0` the power to the strip is completely shut down.
*/
2021-10-08 17:29:00 +03:00
//% callInDebugger
2022-05-19 00:01:00 +03:00
//% group="LED"
//% block="%led brightness"
//% blockId=jacdac_led_brightness___get
//% weight=98
brightness(): number {
2022-04-23 03:34:24 +03:00
this.start()
const values = this._brightness.pauseUntilValues() as any[]
return values[0] * 100
2021-10-08 17:29:00 +03:00
}
2021-05-05 04:37:06 +03:00
/**
2022-04-23 03:34:24 +03:00
* Set the luminosity of the strip.
* At `0` the power to the strip is completely shut down.
*/
2022-05-19 00:01:00 +03:00
//% group="LED"
//% blockId=jacdac_led_brightness___set
//% block="set %led brightness to %value"
//% weight=97
//% value.min=0
//% value.max=100
//% value.defl=0.05
setBrightness(value: number) {
2022-04-23 03:34:24 +03:00
this.start()
const values = this._brightness.values as any[]
values[0] = value / 100
this._brightness.values = values as [number]
}
/**
2022-04-23 03:34:24 +03:00
* This is the luminosity actually applied to the strip.
* May be lower than `brightness` if power-limited by the `max_power` register.
* It will rise slowly (few seconds) back to `brightness` is limits are no longer required.
*/
2021-05-05 04:37:06 +03:00
//% callInDebugger
2022-05-19 00:01:00 +03:00
//% group="LED"
//% weight=96
actualBrightness(): number {
2022-04-23 03:34:24 +03:00
this.start()
const values = this._actualBrightness.pauseUntilValues() as any[]
return values[0] * 100
2021-05-05 04:37:06 +03:00
}
/**
2022-04-23 03:34:24 +03:00
* Specifies the number of pixels in the strip.
*/
2021-05-05 04:37:06 +03:00
//% callInDebugger
2022-05-19 00:01:00 +03:00
//% group="LED"
//% weight=95
2022-05-17 18:14:23 +03:00
//% blockId=jacdac_led_num_pixels
//% block="%led number of pixels"
numPixels(): number {
2022-04-23 03:34:24 +03:00
this.start()
const values = this._numPixels.pauseUntilValues() as any[]
return values[0]
2021-05-05 04:37:06 +03:00
}
/**
2022-04-23 03:34:24 +03:00
* If the LED pixel strip is a matrix, specifies the number of columns.
*/
2021-05-05 04:37:06 +03:00
//% callInDebugger
2022-05-19 00:01:00 +03:00
//% group="LED"
//% weight=94
numColumns(): number {
2022-04-23 03:34:24 +03:00
this.start()
const values = this._numColumns.pauseUntilValues() as any[]
return values[0]
2021-05-05 04:37:06 +03:00
}
/**
2022-04-23 03:34:24 +03:00
* Limit the power drawn by the light-strip (and controller).
*/
2021-05-05 04:37:06 +03:00
//% callInDebugger
2022-05-19 00:01:00 +03:00
//% group="LED"
//% weight=93
2021-05-05 04:37:06 +03:00
maxPower(): number {
2022-04-23 03:34:24 +03:00
this.start()
const values = this._maxPower.pauseUntilValues() as any[]
return values[0]
2021-05-05 04:37:06 +03:00
}
/**
2022-04-23 03:34:24 +03:00
* Limit the power drawn by the light-strip (and controller).
*/
2022-05-19 00:01:00 +03:00
//% group="LED"
//% weight=92
//% value.min=0
//% value.max=65535
//% value.defl=200
2021-05-05 04:37:06 +03:00
setMaxPower(value: number) {
2022-04-23 03:34:24 +03:00
this.start()
const values = this._maxPower.values as any[]
values[0] = value
this._maxPower.values = values as [number]
2021-05-05 04:37:06 +03:00
}
/**
2022-04-23 03:34:24 +03:00
* If known, specifies the number of LEDs in parallel on this device.
* The actual number of LEDs is `num_pixels * leds_per_pixel`.
*/
2021-05-05 04:37:06 +03:00
//% callInDebugger
2022-05-19 00:01:00 +03:00
//% group="LED"
//% weight=91
ledsPerPixel(): number {
2022-04-23 03:34:24 +03:00
this.start()
const values = this._ledsPerPixel.pauseUntilValues() as any[]
return values[0]
2021-05-05 04:37:06 +03:00
}
/**
2022-04-23 03:34:24 +03:00
* If monochrome LED, specifies the wave length of the LED.
* Register is missing for RGB LEDs.
*/
2021-05-05 04:37:06 +03:00
//% callInDebugger
2022-05-19 00:01:00 +03:00
//% group="LED"
//% weight=90
2021-05-05 04:37:06 +03:00
waveLength(): number {
2022-04-23 03:34:24 +03:00
this.start()
const values = this._waveLength.pauseUntilValues() as any[]
return values[0]
2021-05-05 04:37:06 +03:00
}
/**
2022-04-23 03:34:24 +03:00
* The luminous intensity of all the LEDs, at full brightness, in micro candella.
*/
2021-05-05 04:37:06 +03:00
//% callInDebugger
2022-05-19 00:01:00 +03:00
//% group="LED"
//% weight=89
2021-05-05 04:37:06 +03:00
luminousIntensity(): number {
2022-04-23 03:34:24 +03:00
this.start()
const values = this._luminousIntensity.pauseUntilValues() as any[]
return values[0]
2021-05-05 04:37:06 +03:00
}
/**
2022-04-23 03:34:24 +03:00
* Specifies the shape of the light strip.
*/
2021-05-05 04:37:06 +03:00
//% callInDebugger
2022-05-19 00:01:00 +03:00
//% group="LED"
//% weight=88
2021-05-05 04:37:06 +03:00
variant(): jacdac.LedVariant {
2022-04-23 03:34:24 +03:00
this.start()
const values = this._variant.pauseUntilValues() as any[]
return values[0]
}
/**
* Gets the pixel color buffer, where every pixel color is encoded as a 24 bit RGB color.
*/
//% callInDebugger
2022-05-19 00:01:00 +03:00
//% group="LED"
//% weight=98
pixels(): Buffer {
2021-06-19 03:20:03 +03:00
this.start()
const values = this._pixels.pauseUntilValues() as any[]
2021-06-19 03:20:03 +03:00
return values[0]
2022-04-23 03:34:24 +03:00
}
/**
* Sets the local pixel color buffer, where every pixel color is encoded as a 24 bit RGB color.
*/
//% callInDebugger
2022-05-19 00:01:00 +03:00
//% group="LED"
//% weight=98
setPixels(pixels: Buffer) {
if (!pixels) return
this._localPixels = pixels
this.setDirty()
2021-05-05 04:37:06 +03:00
}
/**
* Turn on/off the ability to automatically show changes. If false, the user must call 'show'.
* @param value
2021-06-19 03:20:03 +03:00
*/
2022-04-23 03:34:24 +03:00
setAutoShow(value: boolean) {
this._autoShow = !!value
if (this._autoShow) this.show()
else this.stopAutoShow()
}
private startAutoShow() {
if (this._autoShowUnsub) return
this._autoShowUnsub = jacdac.bus.subscribeRefresh(() =>
this.refresh()
2021-06-19 03:20:03 +03:00
)
this.on(jacdac.DISCONNECT, () => this.stopAutoShow())
this.show()
}
private stopAutoShow() {
const unsub = this._autoShowUnsub
this._autoShowUnsub = undefined
if (unsub) unsub()
}
private setDirty() {
if (!this._dirty) {
this._dirty = true
if (!this._autoShowUnsub) this.startAutoShow()
}
}
private refresh() {
if (this._dirty) {
this.show()
}
2022-04-23 03:34:24 +03:00
}
/**
* Sends the local pixel buffer to device immediately, instead of waiting for the rendering loop
*/
//% callInDebugger
2022-05-19 00:01:00 +03:00
//% group="LED"
//% weight=98
show() {
this.start()
const numPixels = this.numPixels()
2022-08-16 21:26:59 +03:00
if (
!isNaN(numPixels) &&
numPixels > 0 &&
numPixels * 3 !== this._localPixels.length
) {
// create a new buffer of the correct length and copy over
const newBuf = Buffer.create(numPixels * 3)
newBuf.write(0, this._localPixels)
this._localPixels = newBuf
}
this._pixels.values = [this._localPixels] as [Buffer]
2022-08-09 21:46:55 +03:00
this._dirty = isNaN(numPixels)
}
/**
* Set a single 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
*/
2022-06-07 21:36:10 +03:00
//% blockId="jacdac_leddisplay_set_pixel_color" block="set %display color at pixel %index to %rgb=colorNumberPicker"
//% weight=81 blockGap=8
2022-05-19 00:01:00 +03:00
//% group="LED"
setPixelColor(index: number, rgb: number) {
index = index | 0
const pixels = this._localPixels
if (!pixels) return
if (index >= 0 && (index + 1) * 3 <= pixels.length) {
const r = (rgb >> 16) & 0xff
const g = (rgb >> 8) & 0xff
const b = rgb & 0xff
const k = index * 3
if (
pixels[k] != r ||
pixels[k + 1] != g ||
pixels[k + 2] != b
) {
pixels[k] = r
pixels[k + 1] = g
pixels[k + 2] = b
this.setDirty()
}
}
}
/**
* Set all of the pixels on the strip to one RGB color.
* @param rgb RGB color of the LED
*/
2022-06-07 21:36:10 +03:00
//% blockId="jacdac_leddisplay_set_strip_color" block="set %display all to %rgb=colorNumberPicker"
//% weight=80 blockGap=8
2022-05-19 00:01:00 +03:00
//% group="LED"
setAll(rgb: number) {
const pixels = this._localPixels
if (!pixels) return
const r = (rgb >> 16) & 0xff
const g = (rgb >> 8) & 0xff
const b = (rgb >> 0) & 0xff
let dirty = this._dirty
for (let i = 0; i + 2 < pixels.length; i += 3) {
dirty =
dirty ||
pixels[i] != r ||
pixels[i + 1] != g ||
pixels[i + 2] != b
pixels[i] = r
pixels[i + 1] = g
pixels[i + 2] = b
}
if (dirty) this.setDirty()
}
2022-06-07 21:36:10 +03:00
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()
2022-06-08 03:56:15 +03:00
for (let cv = 0, i = 0; cv < v && i < n; ++i) {
const b = Math.idiv(i * 0xff, n - 1)
2022-06-07 21:36:10 +03:00
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.
* @param offset number of pixels to shift forward, eg: 1
*/
//% blockId="jacdac_leddisplay_shift" block="shift %display pixels by %offset" blockGap=8
//% weight=40
2022-05-19 00:01:00 +03:00
//% group="LED"
shift(offset = 1): void {
offset = offset >> 0
if (!offset) return
const stride = 3
const pixels = this._localPixels
if (!pixels) return
pixels.shift(-offset * stride)
this.setDirty()
}
/**
* Rotate LEDs forward.
* You need to call ``show`` to make the changes visible.
* @param offset number of pixels to rotate forward, eg: 1
*/
//% blockId="jacdac_leddisplay_rotate" block="rotate %display pixels by %offset" blockGap=8
//% weight=39
rotate(offset = 1): void {
offset = offset >> 0
if (!offset) return
const stride = 3
const pixels = this._localPixels
if (!pixels) return
pixels.rotate(-offset * stride)
this.setDirty()
2021-06-19 03:20:03 +03:00
}
2022-06-07 21:36:10 +03:00
private clear() {
const pixels = this._localPixels
if (!pixels) return
pixels.fill(0, 0, pixels.length)
}
2021-05-05 04:37:06 +03:00
}
//% fixedInstance whenUsed
export const led1 = new LedClient("led 1")
2021-06-19 03:20:03 +03:00
}