From be71257260771eed3ed8130bf6b0cdc3c9513776 Mon Sep 17 00:00:00 2001 From: velvettear Date: Fri, 25 Mar 2022 01:10:50 +0100 Subject: [PATCH] fixed a lot of stuff, toggling bypass now works as expected --- .nvmrc | 2 +- .vscode/launch.json | 2 +- libs/blinky.js | 6 +++++- libs/cache.js | 49 +++------------------------------------------ libs/modep.js | 43 ++++++++++++++------------------------- package-lock.json | 45 +++++++++++++++++++++++++++++++++++++++++ 6 files changed, 70 insertions(+), 77 deletions(-) create mode 100644 package-lock.json diff --git a/.nvmrc b/.nvmrc index 19c7bdb..8e2afd3 100644 --- a/.nvmrc +++ b/.nvmrc @@ -1 +1 @@ -16 \ No newline at end of file +17 \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json index 06a306d..9537f2c 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -3,7 +3,7 @@ "configurations": [ { "type": "pwa-node", - "runtimeVersion": "16", + "runtimeVersion": "17", "request": "launch", "name": "api", "skipFiles": [ diff --git a/libs/blinky.js b/libs/blinky.js index 9cec2c0..2540b4b 100644 --- a/libs/blinky.js +++ b/libs/blinky.js @@ -3,6 +3,7 @@ const { CONTROL_BYPASS } = require('./constants.js'); const { httpPOST } = require('./util.js'); let enabled; +let isBypassActive; async function initialize() { enabled = global.config.blinky !== undefined && @@ -49,6 +50,9 @@ async function setPedalStatus(pedal) { if (!enabled || pedal === undefined || pedal.controls === undefined) { return; } + if (isBypassActive === undefined) { + isBypassActive = require('./modep.js').isBypassActive; + } for (let index = 0; index < pedal.controls.length; index++) { const bypass = pedal.controls[index]; if (bypass.name !== CONTROL_BYPASS) { @@ -61,7 +65,7 @@ async function setPedalStatus(pedal) { { blinkstick: "strip", index: pedal.id, - color: getPedalColor(pedal.name, (bypass.value === undefined || bypass.value === 0)) + color: getPedalColor(pedal.name, isBypassActive(bypass)) } ); } diff --git a/libs/cache.js b/libs/cache.js index d532d53..d6bd177 100644 --- a/libs/cache.js +++ b/libs/cache.js @@ -31,19 +31,12 @@ function clear() { } function getValue(key) { - let value = cache.get(key); - if (value === undefined) { - return undefined; - } - return cache.get(key).data; + return cache.get(key); } function setValue(key, value) { - if (!isActive) { - return; - } logger.debug('caching \'' + key + '\'...'); - cache.set(key, { data: value, timestamp: new Date().getTime() }); + cache.set(key, value); } function resetValue(key) { @@ -122,38 +115,6 @@ function setInfo(value) { setValue(constants.CACHE_INFO, value); } -function setBypassedPedal(pedalId) { - if (isPedalBypassed(pedalId)) { - return; - } - let bypassedPedals = cache.get(constants.CACHE_PEDALS_BYPASSED); - if (bypassedPedals === undefined) { - bypassedPedals = []; - } - bypassedPedals.push(pedalId); - cache.set(constants.CACHE_PEDALS_BYPASSED, bypassedPedals); -} - -function isPedalBypassed(pedalId) { - let bypassedPedals = cache.get(constants.CACHE_PEDALS_BYPASSED); - if (bypassedPedals === undefined) { - return; - } - return bypassedPedals.includes(pedalId); -} - -function removeBypassedPedal(pedalId) { - if (!isPedalBypassed(pedalId)) { - return; - } - let bypassedPedals = cache.get(constants.CACHE_PEDALS_BYPASSED); - bypassedPedals.splice(bypassedPedals.indexOf(pedalId), 1); -} - -function clearBypassedPedals() { - cache.delete(constants.CACHE_PEDALS_BYPASSED); -} - module.exports = { fill, clear, @@ -174,9 +135,5 @@ module.exports = { setCurrentPedals, getInfo, clearInfo, - setInfo, - setBypassedPedal, - isPedalBypassed, - removeBypassedPedal, - clearBypassedPedals + setInfo } \ No newline at end of file diff --git a/libs/modep.js b/libs/modep.js index afec0cf..b80402e 100644 --- a/libs/modep.js +++ b/libs/modep.js @@ -307,28 +307,28 @@ async function parseCurrentPedalboard(currentPedalboard) { function setControlByRequest(pedalId, requestParams) { return new Promise(function (resolve, reject) { let controlId = requestParams.get('id'); - if (controlId == undefined) { + if (controlId === undefined) { reject('could not handle POST - missing parameter \'id\'', 400); } let value = parseInt(requestParams.get('value')); - if (value == undefined) { + if (value === undefined) { reject('could not handle POST - missing parameter \'value\'', 400); } else if (Number.isNaN(value)) { reject('parameter \'value\' is not a number', 400); } getPedalControlById(pedalId, controlId) .then(function (control) { - resolve(setControl(control, value)); + resolve(setControl(pedalId, control, value)); }) .catch(reject); }); } -async function setControl(control, value) { +async function setControl(pedalId, control, value) { if (control === undefined || control.midi === undefined) { throw new Error('control \'' + control.name + '\' with id \'' + control.id + '\' has no midi bindings'); - } - control.value = await osc.send(control.midi.controller, control.midi.channel, value); + } + control.midi.value = await osc.send(control.midi.controller, control.midi.channel, value); } function setPedalboardById(pedalboardId) { @@ -370,16 +370,15 @@ function setPedalboard(pedalboard) { } function hasControlMidiBindings(control) { - return control !== undefined && control.midi !== undefined; + return control?.midi !== undefined; } -function isBypassed(control) { - return control.name === constants.CONTROL_BYPASS && control; +function isBypassActive(control) { + return (control.midi?.value === undefined && control.value >= 1) || control.midi?.value === 0; } function isPedalBypassed(pedal) { - const control = getBypassControlFromPedal(pedal); - return control.value === undefined || control.value === 0; + return isBypassActive(getBypassControlFromPedal(pedal)); } function getBypassControlFromPedal(pedal) { @@ -428,24 +427,12 @@ async function toggleBypass(pedalId) { } const pedal = await getCurrentPedalById(pedalId); const bypass = getBypassControlFromPedal(pedal); - const bypassState = cache.isPedalBypassed(pedal.id); let value = 0; - if (bypassState === true) { + if (isBypassActive(bypass)) { value = 127; - } else if (bypassState === false) { - value = 0; - } else if (bypassState === undefined) { - if (bypass.value !== undefined && bypass.value !== 0) { - value = 127; - } } try { - await setControl(bypass, value); - if (value === 0) { - cache.setBypassedPedal(pedal.id); - } else { - cache.removeBypassedPedal(pedal.id); - } + await setControl(pedalId, bypass, value); blinky.setPedalStatus(pedal); } catch (err) { throw new Error('could not toggle bypass for pedal ' + pedal.name + ' with id \'' + pedal.id + '\' > ' + err); @@ -466,8 +453,8 @@ module.exports = { setPedalboardById, setControlByRequest, toggleBypass, + isBypassActive, isPedalBypassed, - hasControlMidiBindings, - isPedalBypassed, - getBypassControlFromPedal + getBypassControlFromPedal, + hasControlMidiBindings } \ No newline at end of file diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..3e9a66f --- /dev/null +++ b/package-lock.json @@ -0,0 +1,45 @@ +{ + "name": "pbc", + "version": "0.0.1", + "lockfileVersion": 2, + "requires": true, + "packages": { + "": { + "name": "pbc", + "version": "0.0.1", + "license": "MIT", + "dependencies": { + "@frogcat/ttl2jsonld": "^0.0.6", + "moment": "^2.29.1" + } + }, + "node_modules/@frogcat/ttl2jsonld": { + "version": "0.0.6", + "resolved": "https://registry.npmjs.org/@frogcat/ttl2jsonld/-/ttl2jsonld-0.0.6.tgz", + "integrity": "sha512-KwZDRYfrYKtRS35OniszIwZWuuvqkXocpSXU7KQ/7fF2OLoe2Zg8oXQidgw/4xLgRuzoQIELcOgvWXk2W2f8Lg==", + "bin": { + "ttl2jsonld": "bin/cli.js" + } + }, + "node_modules/moment": { + "version": "2.29.1", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz", + "integrity": "sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==", + "engines": { + "node": "*" + } + } + }, + "dependencies": { + "@frogcat/ttl2jsonld": { + "version": "0.0.6", + "resolved": "https://registry.npmjs.org/@frogcat/ttl2jsonld/-/ttl2jsonld-0.0.6.tgz", + "integrity": "sha512-KwZDRYfrYKtRS35OniszIwZWuuvqkXocpSXU7KQ/7fF2OLoe2Zg8oXQidgw/4xLgRuzoQIELcOgvWXk2W2f8Lg==" + }, + "moment": { + "version": "2.29.1", + "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.1.tgz", + "integrity": "sha512-kHmoybcPV8Sqy59DwNDY3Jefr64lK/by/da0ViFcuA4DH0vQg5Q6Ze5VimxkfQNSC+Mls/Kx53s7TjP1RhFEDQ==" + } + } +}