Added some debugging

This commit is contained in:
John Shaver 2018-12-13 22:21:23 -08:00
parent ed29517098
commit 13f16e0b18
1 changed files with 47 additions and 9 deletions

View File

@ -1,13 +1,19 @@
const Gpio = require("pigpio").Gpio;
const schedule = require("./schedule.json");
const HZ = 40000;
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 ledGreen = new Gpio(13, {mode: Gpio.OUTPUT});
let napButton = new Gpio(6, {mode: Gpio.INPUT, pullUpDown: Gpio.PUD_UP, alert: true });
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) => {
result.push({start: time.time, color: time.color});
return result;
@ -16,21 +22,33 @@ let timings = schedule.times.reduce((result, time) => {
return timing;
});
//We need to split the last scheduled interval at midnight
//(and fill it in for the start of the day)
splitAtMidnight(timings);
//Initialize 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();
let updateColorStateInterval = setInterval(updateColorState, 1000);
//Set intervals to update LED lights.
//********
let updateColorStateInterval = setInterval(updateColorState, 1000);
let renderInterval = setInterval(render, 50);
let debugInterval = DEBUG && setInterval(logLEDStatus, 2000);
//********
napButton.on("alert", startNap);
function startNap(level, tick) {
console.log("Button alert!:");
state = { ...state,
napTime: true,
napEnd: Date.now() + NAP_DURATION
@ -59,7 +77,7 @@ function updateColorState() {
newColor = timing.color
}
if(newColor !== state.color) {
console.log("setting color to", newcolor);
console.log("setting color state to", newcolor);
state = { ...state,
color: newColor,
@ -70,16 +88,21 @@ function updateColorState() {
function render() {
switch(state.color) {
case "red":
if(ledGreen.digitalRead()) {
if(ledGreen.digitalRead()) {
DEBUG && console.log("Turning off Green LED.");
return ledGreen.digitalWrite(0);
}
}
DEBUG && !ledRed.getPwmDutyCycle() && console.log("Turning on Red LED");
return ledRed.hardwarePwmWrite(HZ, getBrightness(Date.now()));
break;
case "green":
if(ledRed.digitalRead()) {
if(ledRed.getPwmDutyCycle()) {
DEBUG && console.log("Turning off Red LED.");
return ledRed.digitalWrite(0);
}
if(!ledGreen.digitalRead()) {
if(!ledGreen.digitalRead()) {
DEBUG && console.log("Turning on Green LED.");
return ledGreen.digitalWrite(1);
}
//Commenting out. Probably don't need breathing on green leds
@ -88,10 +111,20 @@ function render() {
}
}
function getBrightness(x) {
return Math.floor(breathingCurve(x, 8000, 250000, 700000));
function logLEDStatus() {
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) {
let t = x * Math.PI/(interval/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;
}
//split the last time interval at midnight and add the remaining to the
//beginning of the the array.
function splitAtMidnight(arr) {
let last = arr[arr.length -1];
arr.unshift({ ...last, start: 0});
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() {
let date = new Date(Date.now());
return (date.getHours() * 100) + date.getMinutes() + 1