diff --git a/tools/microbit-jukebox/rotaryencoder.ts b/tools/microbit-jukebox/rotaryencoder.ts index 8fb97d3c4..0e9b86e31 100644 --- a/tools/microbit-jukebox/rotaryencoder.ts +++ b/tools/microbit-jukebox/rotaryencoder.ts @@ -1,16 +1,11 @@ machine.addClientFactory(jacdac.SRV_ROTARY_ENCODER, (devid, srvid, options) => { + const clicks = 12 const client = new modules.RotaryEncoderClient(`${devid}:${srvid}`) - let currentPosition = client.position() - if (!currentPosition) currentPosition = 0 - client.onPositionChanged(() => { - let clicks = client.clicksPerTurn() - clicks = clicks ? clicks : 24 - const newPosition = client.position() - machine.microbit.emit( - currentPosition < newPosition ? machine.EVENT_A : machine.EVENT_B - ) - currentPosition = newPosition - machine.plot(newPosition % clicks, clicks, options) + let currentPosition = 0 + client.onPositionChanged(delta => { + machine.microbit.emit(delta > 0 ? machine.EVENT_A : machine.EVENT_B) + currentPosition += delta + machine.plot(currentPosition % clicks, clicks, options) }) return client })