Added button for nap time

This commit is contained in:
John Shaver 2018-12-12 18:17:58 -08:00
parent 1d046e3ce7
commit 9e35f65520

View file

@ -1,9 +1,12 @@
const Gpio = require("pigpio").Gpio;
const schedule = require("./schedule.json");
const HZ = 40000;
const NAP_DURATION = 100 * 60 * 1000;
let ledRed = new Gpio(12, {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) => {
result.push({start: time.time, color: time.color});
@ -21,18 +24,44 @@ let state = {
updateState();
let updateStateInterval = setInterval(updateState, 1000);
let updateStateInterval = setInterval(updateColorState, 1000);
let renderInterval = setInterval(render, 50);
function updateState() {
let time = getTime();
let timing = timings.find(x => x.start <= time && x.end > time);
if(timing.color !== state.color) {
state = {
napButton.on("alert", startNap);
function startNap(level, tick) {
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
};
}
}
function render() {