Skip to content

Instantly share code, notes, and snippets.

@marcopeg
Last active November 6, 2017 16:50
Show Gist options
  • Save marcopeg/ae8d2921c7e596c34f40f73b1a31fe0f to your computer and use it in GitHub Desktop.
Save marcopeg/ae8d2921c7e596c34f40f73b1a31fe0f to your computer and use it in GitHub Desktop.
// Customize your badge
const line1 = 'Marco';
const line2 = '@thepeg marcopeg.com';
// Conf program
const schedule = [
{
start: new Date(2017, 10, 6, 14, 0),
end: new Date(2017, 10, 6, 15, 27),
title: 'Javascript everything',
},
{
start: new Date(2017, 10, 6, 16, 0),
end: new Date(2017, 10, 6, 17, 30),
title: 'The Streams Workshop',
},
{
start: new Date(2017, 10, 6, 19, 0),
end: new Date(2017, 10, 6, 23, 59, 59),
title: 'Netflix Party!',
},
{
start: new Date(2017, 10, 7, 9, 0),
end: new Date(2017, 10, 7, 9, 29, 59),
title: 'High Perf Js',
},
{
start: new Date(2017, 10, 7, 9, 30),
end: new Date(2017, 10, 7, 9, 59, 59),
title: 'Hop on Serverless',
},
{
start: new Date(2017, 10, 7, 10, 0),
end: new Date(2017, 10, 7, 10, 29, 59),
title: 'Cryptominoes',
},
{
start: new Date(2017, 10, 7, 11, 0),
end: new Date(2017, 10, 7, 11, 29, 59),
title: 'Memory Footprint',
},
{
start: new Date(2017, 10, 7, 11, 30),
end: new Date(2017, 10, 7, 11, 59, 59),
title: 'Back Pressure',
},
{
start: new Date(2017, 10, 7, 12, 0),
end: new Date(2017, 10, 7, 12, 29, 59),
title: 'Tiny Art',
},
{
start: new Date(2017, 10, 7, 13, 30),
end: new Date(2017, 10, 7, 15, 0, 0),
title: 'Crypto Wrks',
},
{
start: new Date(2017, 10, 7, 15, 30),
end: new Date(2017, 10, 7, 17, 29, 59),
title: 'Taming Unicorns',
}
];
let showProgram = false;
let btnaTimer = null;
const now = () => Date.now();
const getCEvt = () => {
return schedule
.filter(item => (
item.start.getTime() <= now()
&& item.end.getTime() >= now()
))
.shift();
};
const getNEvt = (limit) => {
return schedule
.filter(item => item.start.getTime() >= limit.getTime())
.shift();
};
const isNextClose = event => {
const i = event.start.getTime() - (new Date()).getTime();
return i < 60000;
};
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 drawEvent = (evt, offset) => {
g.setFontVector(10);
writeCenter(evt.title, offset);
g.setFontBitmap();
writeCenter(`${getTimeStr(evt.start)} - ${getTimeStr(evt.end)}`, offset + 14);
};
const drawClock = () => {
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 drawProgram = (curr, next) => {
if (curr) {
digitalWrite(LED3, 1);
digitalWrite(LED1, 0);
drawEvent(curr, 10);
} else {
digitalWrite(LED3, 0);
digitalWrite(LED1, 1);
}
if (next) {
const nofs = curr ? 0 : -15;
writeCenter('next:', 34 + nofs);
drawEvent(next, 40 + nofs);
}
};
const paint = () => {
g.clear();
// get events data
const curr = getCEvt();
let next = null;
if (curr) {
next = getNEvt(curr.end);
} else {
next = getNEvt(new Date());
}
// show relevant screens
if (showProgram) {
drawProgram(curr, next);
} else {
digitalWrite(LED1, 0);
// is next event approaching?
if (isNextClose(next)) {
digitalWrite(LED3, digitalRead(LED3) === 0 ? 1 : 0);
} else {
digitalWrite(LED3, 0);
}
g.setFontVector(18);
writeCenter(line1, 15);
g.setFontVector(9);
writeCenter(line2, 38);
}
drawClock();
g.flip();
};
// watch on BTNA
setWatch(() => {
showProgram = true;
paint();
}, BTNA, { repeat: true, edge: 'rising', debounce: 50 });
setWatch(() => {
showProgram = false;
paint();
}, BTNA, { repeat: true, edge: 'falling', debounce: 50 });
setInterval(paint, 250);
//paint();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment