Created
November 7, 2017 17:56
-
-
Save marcopeg/3635f6d8a1054f2c02e986bfefedd5b2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is no elegant code but it works and now I go for a beer :-) | |
let timeLeft = 0; | |
function beep(freq, time, callback) { | |
var ll = require("NRF52LL"); | |
// set up D0 and D1 as outputs | |
digitalWrite(D0,0); | |
digitalWrite(D1,0); | |
// create two 'toggle' tasks, one for each pin | |
var t0 = ll.gpiote(6, {type:"task",pin:D0,lo2hi:1,hi2lo:1,initialState:0}); | |
var t1 = ll.gpiote(7, {type:"task",pin:D1,lo2hi:1,hi2lo:1,initialState:1}); | |
// create a timer that counts up to a value depending on the frequency | |
var tmr = ll.timer(3,{cc:[500000/freq],cc0clear:1}); | |
// use two PPI to trigger toggle events | |
ll.ppiEnable(6, tmr.eCompare[0], t0.tOut); | |
ll.ppiEnable(7, tmr.eCompare[0], t1.tOut); | |
// Manually trigger a task to start the timer | |
poke32(tmr.tClear,1); | |
poke32(tmr.tStart,1); | |
setTimeout(function() { | |
poke32(tmr.tStop,1); | |
ll.ppiDisable(6); | |
ll.ppiDisable(7); | |
if (callback) callback(); | |
}, time); | |
} | |
// LEDS FLIPPER | |
let ledsTimeout = null; | |
const leds = [ LED1, LED2, LED3, LED4, LED5, LED6 ]; | |
const loopLeds = (index, dur) => { | |
if (ledsTimeout) { | |
clearTimeout(ledsTimeout); | |
ledsTimeout = null; | |
} | |
leds.forEach(led => digitalWrite(led, 0)); | |
digitalWrite(leds[index], 1); | |
ledsTimeout = setTimeout(() => loopLeds((index === 5) ? 0 : index + 1, dur), dur); | |
}; | |
const stopLeds = () => { | |
if (ledsTimeout) { | |
clearTimeout(ledsTimeout); | |
ledsTimeout = null; | |
} | |
leds.forEach(led => digitalWrite(led, 0)); | |
}; | |
// LEDS FLIPPER | |
// SOUND FLIPPER | |
let soundTimeout = null; | |
let soundIsRunning = false; | |
const loopSound = (freq, sound, pause) => { | |
soundIsRunning = true; | |
if (soundTimeout) { | |
clearTimeout(soundTimeout); | |
soundTimeout = null; | |
} | |
beep(freq, sound, () => { | |
if (soundIsRunning) { | |
soundTimeout = setTimeout(() => loopSound(freq, sound, pause), pause); | |
} | |
}); | |
}; | |
const stopSound = () => { | |
soundIsRunning = false; | |
if (soundTimeout) { | |
clearTimeout(soundTimeout); | |
soundTimeout = null; | |
} | |
}; | |
// SOUND FLIPPER | |
const zeroPad = n => n >= 10 ? n : `0${n}`; | |
const getTimeStr = date => { | |
return `${zeroPad(date.getHours())}:${zeroPad(date.getMinutes())}`; | |
}; | |
const writeCenter = (txt, offsetY) => { | |
const txtW = g.stringWidth(txt); | |
const txtX = (g.getWidth() - txtW) / 2; | |
g.drawString(txt, txtX, offsetY); | |
}; | |
const drawSmallClock = () => { | |
const now = new Date(); | |
const h = zeroPad(now.getHours()); | |
const m = zeroPad(now.getMinutes()); | |
const s = zeroPad(now.getSeconds()); | |
g.setFontBitmap(); | |
writeCenter(`${h}:${m}:${s}`, 1); | |
}; | |
const drawBigClock = () => { | |
const now = new Date(); | |
const h = zeroPad(now.getHours()); | |
const m = zeroPad(now.getMinutes()); | |
const s = zeroPad(now.getSeconds()); | |
g.setFontVector(18); | |
writeCenter(`${h}:${m}:${s}`, 20); | |
}; | |
function msToTime(duration) { | |
var milliseconds = parseInt((duration%1000)/100) | |
, seconds = parseInt((duration/1000)%60) | |
, minutes = parseInt((duration/(1000*60))%60) | |
, hours = parseInt((duration/(1000*60*60))%24); | |
hours = (hours < 10) ? "0" + hours : hours; | |
minutes = (minutes < 10) ? "0" + minutes : minutes; | |
seconds = (seconds < 10) ? "0" + seconds : seconds; | |
return hours + ":" + minutes + ":" + seconds; | |
} | |
const drawRemainingTime = () => { | |
g.setFontVector(18); | |
writeCenter(`${msToTime(timeLeft)}`, 20); | |
}; | |
const paint = () => { | |
g.clear(); | |
if (timeLeft === 0) { | |
drawBigClock(); | |
} else { | |
drawSmallClock(); | |
drawRemainingTime(); | |
} | |
g.setFontBitmap(); | |
writeCenter('marcopeg.com @thepeg', 55); | |
g.flip(); | |
}; | |
const increment = 1000; | |
setWatch(() => { | |
timeLeft += increment; | |
paint(); | |
}, BTNL, { repeat: true, edge: 'rising', debounce: 50 }); | |
setWatch(() => { | |
timeLeft += increment * 15; | |
paint(); | |
}, BTNU, { repeat: true, edge: 'rising', debounce: 50 }); | |
setWatch(() => { | |
timeLeft += increment * 30; | |
paint(); | |
}, BTNR, { repeat: true, edge: 'rising', debounce: 50 }); | |
setWatch(() => { | |
timeLeft += increment * 60; | |
paint(); | |
}, BTND, { repeat: true, edge: 'rising', debounce: 50 }); | |
setWatch(() => { | |
timeLeft += increment * 60; | |
paint(); | |
}, BTNB, { repeat: true, edge: 'rising', debounce: 50 }); | |
setWatch(() => { | |
stopLeds(); | |
stopSound(); | |
timeLeft = 0; | |
paint(); | |
}, BTNA, { repeat: true, edge: 'rising', debounce: 50 }); | |
setInterval(paint, 500); | |
let lastClock = Date.now(); | |
setInterval(() => { | |
let was = timeLeft; | |
timeLeft -= Math.round(Date.now() - lastClock); | |
lastClock = Date.now(); | |
if (timeLeft < 0) timeLeft = 0; | |
if (was > 0 && timeLeft === 0) { | |
loopLeds(1, 250); | |
loopSound(1000, 250, 200); | |
} | |
}, 500); | |
//loopLeds(1, 250); | |
//setTimeout(() => stopLeds(), 3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment