Added button for nap time
This commit is contained in:
parent
1d046e3ce7
commit
9e35f65520
1 changed files with 35 additions and 6 deletions
|
@ -1,9 +1,12 @@
|
||||||
const Gpio = require("pigpio").Gpio;
|
const Gpio = require("pigpio").Gpio;
|
||||||
const schedule = require("./schedule.json");
|
const schedule = require("./schedule.json");
|
||||||
const HZ = 40000;
|
const HZ = 40000;
|
||||||
|
const NAP_DURATION = 100 * 60 * 1000;
|
||||||
|
|
||||||
let ledRed = new Gpio(12, {mode: Gpio.OUTPUT});
|
let ledRed = new Gpio(12, {mode: Gpio.OUTPUT});
|
||||||
let ledGreen = new Gpio(13, {mode: Gpio.OUTPUT});
|
let ledGreen = new Gpio(13, {mode: Gpio.OUTPUT});
|
||||||
|
let napButton = new Gpio(6, {mode: Gpio.INPUT, pullUpDown: Gpio.PUD_UP, alert: true });
|
||||||
|
napButton.glitchFilter(10000);
|
||||||
|
|
||||||
let timings = schedule.times.reduce((result, time) => {
|
let timings = schedule.times.reduce((result, time) => {
|
||||||
result.push({start: time.time, color: time.color});
|
result.push({start: time.time, color: time.color});
|
||||||
|
@ -21,18 +24,44 @@ let state = {
|
||||||
|
|
||||||
updateState();
|
updateState();
|
||||||
|
|
||||||
let updateStateInterval = setInterval(updateState, 1000);
|
let updateStateInterval = setInterval(updateColorState, 1000);
|
||||||
|
|
||||||
let renderInterval = setInterval(render, 50);
|
let renderInterval = setInterval(render, 50);
|
||||||
|
|
||||||
function updateState() {
|
napButton.on("alert", startNap);
|
||||||
let time = getTime();
|
|
||||||
let timing = timings.find(x => x.start <= time && x.end > time);
|
function startNap(level, tick) {
|
||||||
if(timing.color !== state.color) {
|
state = { ...state,
|
||||||
state = {
|
napTime: true,
|
||||||
|
napEnd: Date.now() + NAP_DURATION
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function updateColorState() {
|
||||||
|
let newColor = state.color;
|
||||||
|
switch(state.napTime) {
|
||||||
|
case true:
|
||||||
|
if(Date.now() < state.napEnd) {
|
||||||
|
newColor = "red";
|
||||||
|
break;
|
||||||
|
} else {
|
||||||
|
state = { ...state,
|
||||||
|
napTime: false,
|
||||||
|
napEnd: 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
case false:
|
||||||
|
let time = getTime();
|
||||||
|
let timing = timings.find(x => x.start <= time && x.end > time);
|
||||||
|
newColor = timing.color
|
||||||
|
}
|
||||||
|
if(newColor !== state.color) {
|
||||||
|
state = { ...state,
|
||||||
color: timing.color
|
color: timing.color
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function render() {
|
function render() {
|
||||||
|
|
Loading…
Reference in a new issue