fixed a lot of stuff, toggling bypass now works as expected

This commit is contained in:
Daniel Sommer 2022-03-25 01:10:50 +01:00
parent c5b5844233
commit be71257260
6 changed files with 70 additions and 77 deletions

2
.nvmrc
View file

@ -1 +1 @@
16
17

2
.vscode/launch.json vendored
View file

@ -3,7 +3,7 @@
"configurations": [
{
"type": "pwa-node",
"runtimeVersion": "16",
"runtimeVersion": "17",
"request": "launch",
"name": "api",
"skipFiles": [

View file

@ -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))
}
);
}

View file

@ -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
}

View file

@ -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
}

45
package-lock.json generated Normal file
View file

@ -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=="
}
}
}