Compare commits

..

No commits in common. "ed29517098d447037cfc8e5fa827ec2a56f46f95" and "3e318754de4043ebf727f1d75108881963a39cbd" have entirely different histories.

2 changed files with 11 additions and 60 deletions

View file

@ -1,12 +1,9 @@
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});
@ -22,51 +19,22 @@ let state = {
color: "red" color: "red"
} }
updateColorState(); updateState();
let updateColorStateInterval = setInterval(updateColorState, 1000); let updateStateInterval = setInterval(updateState, 1000);
let renderInterval = setInterval(render, 50); let renderInterval = setInterval(render, 50);
napButton.on("alert", startNap); function updateState() {
let time = getTime();
function startNap(level, tick) { let timing = timings.find(x => x.start <= time && x.end > time);
state = { ...state, if(timing.color !== state.color) {
napTime: true, state = {
napEnd: Date.now() + NAP_DURATION color: timing.color
};
} }
} }
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() { function render() {
switch(state.color) { switch(state.color) {
case "red": case "red":
@ -89,7 +57,7 @@ function render() {
} }
function getBrightness(x) { function getBrightness(x) {
return Math.floor(breathingCurve(x, 8000, 250000, 700000)); return Math.floor(breathingCurve(x, 6000, 100000, 1000000));
} }
function breathingCurve(x, interval = 3000, min, max) { function breathingCurve(x, interval = 3000, min, max) {
@ -103,7 +71,7 @@ function breathingCurve(x, interval = 3000, min, max) {
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 = 0;
} }
function getTime() { function getTime() {

View file

@ -1,17 +0,0 @@
[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