Add LED settings for potentially different hardware

Change-type: patch
Signed-off-by: Lorenzo Alberto Maria Ambrosi <lorenzothunder.ambrosi@gmail.com>
This commit is contained in:
Lorenzo Alberto Maria Ambrosi 2021-09-07 15:20:56 +02:00
parent 13159f93ee
commit dff2df4aab

View File

@ -29,13 +29,6 @@ import { observe, store } from './store';
const leds: Map<string, RGBLed> = new Map(); const leds: Map<string, RGBLed> = new Map();
const animator = new Animator([], 10); const animator = new Animator([], 10);
const red: Color = [0.78, 0, 0];
const green: Color = [0, 0.58, 0];
const blue: Color = [0, 0, 0.1];
const purple: Color = [0.7, 0, 0.78];
const white: Color = [0.7, 0.7, 0.7];
const black: Color = [0, 0, 0];
function createAnimationFunction( function createAnimationFunction(
intensityFunction: (t: number) => number, intensityFunction: (t: number) => number,
color: Color, color: Color,
@ -54,13 +47,27 @@ function one(_t: number) {
return 1; return 1;
} }
const blinkGreen = createAnimationFunction(blink, green); type LEDColors = {
const blinkPurple = createAnimationFunction(blink, purple); green: Color;
const staticRed = createAnimationFunction(one, red); purple: Color;
const staticGreen = createAnimationFunction(one, green); red: Color;
const staticBlue = createAnimationFunction(one, blue); blue: Color;
const staticWhite = createAnimationFunction(one, white); white: Color;
const staticBlack = createAnimationFunction(one, black); black: Color;
};
type LEDAnimationFunctions = {
blinkGreen: AnimationFunction;
blinkPurple: AnimationFunction;
staticRed: AnimationFunction;
staticGreen: AnimationFunction;
staticBlue: AnimationFunction;
staticWhite: AnimationFunction;
staticBlack: AnimationFunction;
};
let ledColors: LEDColors;
let ledAnimationFunctions: LEDAnimationFunctions;
interface LedsState { interface LedsState {
step: 'main' | 'flashing' | 'verifying' | 'finish'; step: 'main' | 'flashing' | 'verifying' | 'finish';
@ -132,31 +139,48 @@ export function updateLeds({
if (sourceDrive !== undefined) { if (sourceDrive !== undefined) {
if (plugged.has(sourceDrive)) { if (plugged.has(sourceDrive)) {
plugged.delete(sourceDrive); plugged.delete(sourceDrive);
mapping.push(setLeds(staticBlue, new Set([sourceDrive]))); mapping.push(
setLeds(ledAnimationFunctions.staticBlue, new Set([sourceDrive])),
);
} }
} }
if (step === 'main') { if (step === 'main') {
mapping.push( mapping.push(
setLeds(staticBlack, new Set([...unplugged, ...plugged])), setLeds(
setLeds(staticWhite, new Set([...selectedOk, ...selectedFailed])), ledAnimationFunctions.staticBlack,
new Set([...unplugged, ...plugged]),
),
setLeds(
ledAnimationFunctions.staticWhite,
new Set([...selectedOk, ...selectedFailed]),
),
); );
} else if (step === 'flashing') { } else if (step === 'flashing') {
mapping.push( mapping.push(
setLeds(staticBlack, new Set([...unplugged, ...plugged])), setLeds(
setLeds(blinkPurple, selectedOk), ledAnimationFunctions.staticBlack,
setLeds(staticRed, selectedFailed), new Set([...unplugged, ...plugged]),
),
setLeds(ledAnimationFunctions.blinkPurple, selectedOk),
setLeds(ledAnimationFunctions.staticRed, selectedFailed),
); );
} else if (step === 'verifying') { } else if (step === 'verifying') {
mapping.push( mapping.push(
setLeds(staticBlack, new Set([...unplugged, ...plugged])), setLeds(
setLeds(blinkGreen, selectedOk), ledAnimationFunctions.staticBlack,
setLeds(staticRed, selectedFailed), new Set([...unplugged, ...plugged]),
),
setLeds(ledAnimationFunctions.blinkGreen, selectedOk),
setLeds(ledAnimationFunctions.staticRed, selectedFailed),
); );
} else if (step === 'finish') { } else if (step === 'finish') {
mapping.push( mapping.push(
setLeds(staticBlack, new Set([...unplugged, ...plugged])), setLeds(
setLeds(staticGreen, selectedOk), ledAnimationFunctions.staticBlack,
setLeds(staticRed, selectedFailed), new Set([...unplugged, ...plugged]),
),
setLeds(ledAnimationFunctions.staticGreen, selectedOk),
setLeds(ledAnimationFunctions.staticRed, selectedFailed),
); );
} }
animator.mapping = mapping; animator.mapping = mapping;
@ -221,6 +245,16 @@ export async function init(): Promise<void> {
for (const [drivePath, ledsNames] of Object.entries(ledsMapping)) { for (const [drivePath, ledsNames] of Object.entries(ledsMapping)) {
leds.set('/dev/disk/by-path/' + drivePath, new RGBLed(ledsNames)); leds.set('/dev/disk/by-path/' + drivePath, new RGBLed(ledsNames));
} }
observe(_.debounce(stateObserver, 1000, { maxWait: 1000 }));
} }
ledColors = (await settings.get('ledColors')) || {};
ledAnimationFunctions = {
blinkGreen: createAnimationFunction(blink, ledColors['green']),
blinkPurple: createAnimationFunction(blink, ledColors['purple']),
staticRed: createAnimationFunction(one, ledColors['red']),
staticGreen: createAnimationFunction(one, ledColors['green']),
staticBlue: createAnimationFunction(one, ledColors['blue']),
staticWhite: createAnimationFunction(one, ledColors['white']),
staticBlack: createAnimationFunction(one, ledColors['black']),
};
observe(_.debounce(stateObserver, 1000, { maxWait: 1000 }));
} }