From 2747330ada7718c1e028cc4f37c93c3f26a903bc Mon Sep 17 00:00:00 2001 From: Michael Schmidt Date: Sat, 2 Jan 2021 00:08:25 +0100 Subject: [PATCH] Add support for rotary switches --- js/behringer-model-d.js | 2 +- js/front-panel.js | 23 +++++++++++++++-------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/js/behringer-model-d.js b/js/behringer-model-d.js index 7a7e12f..8abe592 100644 --- a/js/behringer-model-d.js +++ b/js/behringer-model-d.js @@ -62,7 +62,7 @@ const KNOBS = [ ] const KNOB_TYPES = { large: {radius: 55, limit: 304}, - medium: {radius: 45, limit: 160}, + medium: {radius: 45, limit: 160, positions: 6}, small: {radius: 30, limit: 300}, } const TOGGLES = [ diff --git a/js/front-panel.js b/js/front-panel.js index bf36582..2d6eb6f 100644 --- a/js/front-panel.js +++ b/js/front-panel.js @@ -312,8 +312,8 @@ class FrontPanel { this.turning = true; this.turnKnobIdx = knob.idx; this.turnPreviousY = pos.y; - this.turnOldValue = knob.value; - this.turnNewValue = knob.value; + this.turnOldValue = this.knobValues[knob.idx]; + this.turnNewValue = this.knobValues[knob.idx]; this.requestRedraw() } }) @@ -359,12 +359,19 @@ class FrontPanel { } if (this.turning) { - const knob = this.knobs[this.turnKnobIdx] - const knobType= this.knobTypes[knob.type] - this.knobValues[this.turnKnobIdx] += (this.turnPreviousY - pos.y) * 2 - this.knobValues[this.turnKnobIdx] = Math.max(this.knobValues[this.turnKnobIdx], 0) - this.knobValues[this.turnKnobIdx] = Math.min(this.knobValues[this.turnKnobIdx], knobType.limit) - this.turnPreviousY = pos.y + const knob = this.knobs[this.turnKnobIdx]; + const knobType = this.knobTypes[knob.type]; + let value = this.knobValues[this.turnKnobIdx] + (this.turnPreviousY - pos.y) * 2; + this.turnNewValue += (this.turnPreviousY - pos.y) * 2; + + if (knobType.positions !== undefined) { + const step = (knobType.limit / (knobType.positions - 1)); + value = Math.round(this.turnNewValue / step) * step; + } + + this.knobValues[this.turnKnobIdx] = Math.max(value, 0); + this.knobValues[this.turnKnobIdx] = Math.min(this.knobValues[this.turnKnobIdx], knobType.limit); + this.turnPreviousY = pos.y; // console.log(this.knobValues[this.turnKnobIdx]) } // e.preventDefault()