Skip to content

Commit

Permalink
fix: clear status light state after 2 announce cycles
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Jul 17, 2023
1 parent c32b44e commit ce86a4e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/jdom/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -772,6 +772,9 @@ export class JDDevice extends JDNode {
this.bus.emit(CHANGE)
this.emit(CHANGE)
}

// update status led
this.statusLight?.processAnnouncement()
}

/**
Expand Down Expand Up @@ -851,7 +854,7 @@ export class JDDevice extends JDNode {
pkt.isCommand &&
pkt.serviceCommand == ControlCmd.SetStatusLight
)
pkt.device.statusLight?.handlePacket(pkt)
pkt.device.statusLight?.processPacket(pkt)
}

/**
Expand Down
16 changes: 15 additions & 1 deletion src/jdom/ledcontroller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ function trgbToValues(trgb: number) {

export class LEDController extends JDEventSource {
private _color: number
private _announces = 0

constructor(
public readonly service: JDService,
Expand All @@ -30,6 +31,8 @@ export class LEDController extends JDEventSource {
async setColor(color: number) {
if (color !== this._color) {
this._color = color
this._announces = 0

if (this._color !== undefined) {
const data = jdpack(
ControlCmdPack.SetStatusLight,
Expand All @@ -51,7 +54,18 @@ export class LEDController extends JDEventSource {
}
}

handlePacket(pkt: Packet) {
processAnnouncement() {
if (this._color === undefined) return
this._announces++
if (this._announces > 2) {
// jacdac will blink at least once per announce cycle
this._color = undefined
this._announces = 0
this.emit(CHANGE)
}
}

processPacket(pkt: Packet) {
const [toRed, toGreen, toBlue] = jdunpack<
[number, number, number, number]
>(pkt.data, ControlCmdPack.SetStatusLight)
Expand Down

0 comments on commit ce86a4e

Please sign in to comment.