Compare commits

..

8 commits

2 changed files with 60 additions and 11 deletions

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});
@ -19,22 +22,51 @@ let state = {
color: "red"
}
updateState();
updateColorState();
let updateStateInterval = setInterval(updateState, 1000);
let updateColorStateInterval = 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 = {
color: timing.color
};
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:
console.log("Nap time until :", state.napEnd);
if(Date.now() < state.napEnd) {
newColor = "red";
break;
} else {
state = { ...state,
napTime: false,
napEnd: 0,
};
}
case false:
console.log("NO NAP!");
let time = getTime();
let timing = timings.find(x => x.start <= time && x.end > time);
newColor = timing.color
}
if(newColor !== state.color) {
console.log("setting color to", newcolor);
state = { ...state,
color: newColor,
};
}
}
function render() {
switch(state.color) {
case "red":
@ -57,7 +89,7 @@ function render() {
}
function getBrightness(x) {
return Math.floor(breathingCurve(x, 6000, 100000, 1000000));
return Math.floor(breathingCurve(x, 8000, 250000, 700000));
}
function breathingCurve(x, interval = 3000, min, max) {
@ -71,7 +103,7 @@ function breathingCurve(x, interval = 3000, min, max) {
function splitAtMidnight(arr) {
let last = arr[arr.length -1];
arr.unshift({ ...last, start: 0});
arr[arr.length - 1].end = 0;
arr[arr.length - 1].end = 2400;
}
function getTime() {

17
sleeplight.service Normal file
View file

@ -0,0 +1,17 @@
[Unit]
Description=SleepLight (Controls the gpio pins to light LEDs at bedtime)
After=syslog.target
After=time-sync.target
[Service]
RestartSec=2s
Type=simple
User=root
Group=root
WorkingDirectory=/opt/sleeplight
ExecStart=/usr/local/bin/node ./runSchedule.js
Restart=always
[Install]
WantedBy=multi-user.target