Added some debugging
This commit is contained in:
parent
ed29517098
commit
13f16e0b18
1 changed files with 47 additions and 9 deletions
|
@ -1,13 +1,19 @@
|
||||||
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;
|
const NAP_DURATION = 100 * 60 * 1000;
|
||||||
|
const DEBUG = !!process.env["DEBUG"];
|
||||||
|
|
||||||
|
//Let's setup our connections to our physical hardware devices.
|
||||||
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 });
|
let napButton = new Gpio(6, {mode: Gpio.INPUT, pullUpDown: Gpio.PUD_UP, alert: true });
|
||||||
napButton.glitchFilter(10000);
|
napButton.glitchFilter(10000);
|
||||||
|
|
||||||
|
//Make an easily searchable array so we can find what color it should
|
||||||
|
//be using at any given time.
|
||||||
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});
|
||||||
return result;
|
return result;
|
||||||
|
@ -16,21 +22,33 @@ let timings = schedule.times.reduce((result, time) => {
|
||||||
return timing;
|
return timing;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//We need to split the last scheduled interval at midnight
|
||||||
|
//(and fill it in for the start of the day)
|
||||||
splitAtMidnight(timings);
|
splitAtMidnight(timings);
|
||||||
|
|
||||||
|
//Initialize State
|
||||||
let state = {
|
let state = {
|
||||||
color: "red"
|
color: "red",
|
||||||
|
napTime: false,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//Make sure the color is set correctly in state for the current time before we start.
|
||||||
|
//It will automatically update every seconds, but it might be wrong for a second.
|
||||||
updateColorState();
|
updateColorState();
|
||||||
|
|
||||||
let updateColorStateInterval = setInterval(updateColorState, 1000);
|
|
||||||
|
|
||||||
|
//Set intervals to update LED lights.
|
||||||
|
//********
|
||||||
|
let updateColorStateInterval = setInterval(updateColorState, 1000);
|
||||||
let renderInterval = setInterval(render, 50);
|
let renderInterval = setInterval(render, 50);
|
||||||
|
let debugInterval = DEBUG && setInterval(logLEDStatus, 2000);
|
||||||
|
//********
|
||||||
|
|
||||||
|
|
||||||
napButton.on("alert", startNap);
|
napButton.on("alert", startNap);
|
||||||
|
|
||||||
function startNap(level, tick) {
|
function startNap(level, tick) {
|
||||||
|
console.log("Button alert!:");
|
||||||
state = { ...state,
|
state = { ...state,
|
||||||
napTime: true,
|
napTime: true,
|
||||||
napEnd: Date.now() + NAP_DURATION
|
napEnd: Date.now() + NAP_DURATION
|
||||||
|
@ -59,7 +77,7 @@ function updateColorState() {
|
||||||
newColor = timing.color
|
newColor = timing.color
|
||||||
}
|
}
|
||||||
if(newColor !== state.color) {
|
if(newColor !== state.color) {
|
||||||
console.log("setting color to", newcolor);
|
console.log("setting color state to", newcolor);
|
||||||
|
|
||||||
state = { ...state,
|
state = { ...state,
|
||||||
color: newColor,
|
color: newColor,
|
||||||
|
@ -70,16 +88,21 @@ function updateColorState() {
|
||||||
function render() {
|
function render() {
|
||||||
switch(state.color) {
|
switch(state.color) {
|
||||||
case "red":
|
case "red":
|
||||||
if(ledGreen.digitalRead()) {
|
if(ledGreen.digitalRead()) {
|
||||||
|
DEBUG && console.log("Turning off Green LED.");
|
||||||
return ledGreen.digitalWrite(0);
|
return ledGreen.digitalWrite(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DEBUG && !ledRed.getPwmDutyCycle() && console.log("Turning on Red LED");
|
||||||
return ledRed.hardwarePwmWrite(HZ, getBrightness(Date.now()));
|
return ledRed.hardwarePwmWrite(HZ, getBrightness(Date.now()));
|
||||||
break;
|
break;
|
||||||
case "green":
|
case "green":
|
||||||
if(ledRed.digitalRead()) {
|
if(ledRed.getPwmDutyCycle()) {
|
||||||
|
DEBUG && console.log("Turning off Red LED.");
|
||||||
return ledRed.digitalWrite(0);
|
return ledRed.digitalWrite(0);
|
||||||
}
|
}
|
||||||
if(!ledGreen.digitalRead()) {
|
if(!ledGreen.digitalRead()) {
|
||||||
|
DEBUG && console.log("Turning on Green LED.");
|
||||||
return ledGreen.digitalWrite(1);
|
return ledGreen.digitalWrite(1);
|
||||||
}
|
}
|
||||||
//Commenting out. Probably don't need breathing on green leds
|
//Commenting out. Probably don't need breathing on green leds
|
||||||
|
@ -88,10 +111,20 @@ function render() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function getBrightness(x) {
|
function logLEDStatus() {
|
||||||
return Math.floor(breathingCurve(x, 8000, 250000, 700000));
|
console.log({
|
||||||
|
red: ledRed.getPwmDutyCycle(),
|
||||||
|
green: ledGreen.digitalRead(),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//get brightness for time value for LED pulse breathing
|
||||||
|
function getBrightness(time) {
|
||||||
|
//These values should be configurable, maybe?
|
||||||
|
return Math.floor(breathingCurve(time, 6500, 200000, 800000));
|
||||||
|
}
|
||||||
|
|
||||||
|
//algorithm for pulse breathing LEDs
|
||||||
function breathingCurve(x, interval = 3000, min, max) {
|
function breathingCurve(x, interval = 3000, min, max) {
|
||||||
let t = x * Math.PI/(interval/2);
|
let t = x * Math.PI/(interval/2);
|
||||||
let y = Math.sin(t + Math.sin(t) * 0.2);
|
let y = Math.sin(t + Math.sin(t) * 0.2);
|
||||||
|
@ -100,12 +133,17 @@ function breathingCurve(x, interval = 3000, min, max) {
|
||||||
return y * (max - min) + min;
|
return y * (max - min) + min;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//split the last time interval at midnight and add the remaining to the
|
||||||
|
//beginning of the the array.
|
||||||
function splitAtMidnight(arr) {
|
function splitAtMidnight(arr) {
|
||||||
let last = arr[arr.length -1];
|
let last = arr[arr.length -1];
|
||||||
arr.unshift({ ...last, start: 0});
|
arr.unshift({ ...last, start: 0});
|
||||||
arr[arr.length - 1].end = 2400;
|
arr[arr.length - 1].end = 2400;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//get current time in military style time. This format is used for ease of
|
||||||
|
//human input in conviguration file. I should have converted to Date when read
|
||||||
|
//from configuration.. oh well, I'll fix that later.
|
||||||
function getTime() {
|
function getTime() {
|
||||||
let date = new Date(Date.now());
|
let date = new Date(Date.now());
|
||||||
return (date.getHours() * 100) + date.getMinutes() + 1
|
return (date.getHours() * 100) + date.getMinutes() + 1
|
||||||
|
|
Loading…
Reference in a new issue