1
0
Fork 0

Merge pull request #33 from markmcnelis/binary-clock

add: simple binary clock
master
Gordon Williams 2019-11-13 10:26:14 +00:00 committed by GitHub
commit ee35ead580
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 126 additions and 0 deletions

View File

@ -443,6 +443,18 @@
{"name":"*scolor","url":"show-color-icon.js","evaluate":true} {"name":"*scolor","url":"show-color-icon.js","evaluate":true}
] ]
}, },
{ "id": "bclock",
"name": "Binary Clock",
"icon": "clock-binary.png",
"description": "A simple binary clock watch face",
"tags": "clock",
"type":"clock",
"storage": [
{"name":"+bclock","url":"clock-binary.json"},
{"name":"-bclock","url":"clock-binary.js"},
{"name":"*bclock","url":"clock-binary-icon.js","evaluate":true}
]
},
{ "id": "clotris", { "id": "clotris",
"name": "Clock-Tris", "name": "Clock-Tris",
"icon": "clock-tris.png", "icon": "clock-tris.png",

View File

@ -0,0 +1 @@
require("heatshrink").decompress(atob("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH8AAAAAAMGAAAAAAYDAAAAAAwBgAAAABgAwAAAABAAQAAAABAAQAAAABAAQAAAABAAQAAAABAAQAAAABgAwAAAAAwBgAAAAAYDAAAAAAMGAAAAAAH8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAH8AAAAAAP+AAAAAAf/AAAAAA//gAAAAB//wAAAAB//wAAAAB//wAAAAB//wAAAAB//wAAAAB//wAAAAB//wAAAAA//gAAAAAf/AAAAAAP+AAAAAAH8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"))

106
apps/clock-binary.js Normal file
View File

@ -0,0 +1,106 @@
(() => {
const canvasWidth = 290;
const numberOfColumns = 6;
const drawFullGrid = false;
const colpos = canvasWidth / numberOfColumns - 10;
const binSize = (canvasWidth / numberOfColumns) / 3;
const findBinary = target => {
return [
[0, 0, 0, 0], // 0
[1, 0, 0, 0], // 1
[0, 1, 0, 0], // 2
[1, 1, 0, 0], // 3
[0, 0, 1, 0], // 4
[1, 0, 1, 0], // 5
[0, 1, 1, 0], // 6
[1, 1, 1, 0], // 7
[0, 0, 0, 1], // 8
[1, 0, 0, 1], // 9
][target];
};
const getCurrentTime = () => {
const flattenArray = (array = []) => [].concat.apply([], array);
const format = number => {
const numberStr = number.toString();
return numberStr.length === 1 ? ["0", numberStr] : numberStr.split("");
};
const now = new Date();
return flattenArray([now.getHours(), now.getMinutes(), now.getSeconds()].map(format));
};
let prevFrame = [];
const drawColumn = (position = 0, column = [0, 0, 0, 0]) => {
const maxDotsPerColumn = [2, 4, 3, 4, 3, 4];
const columnPos = position * colpos;
let pos = colpos / 2 + 45;
const frame = column.reverse();
const drawDot = fn => g[fn]((columnPos + colpos / 2), pos, binSize);
for (let i = 0; i < frame.length; i += 1) {
if (i + maxDotsPerColumn[position] >= 4 || drawFullGrid) {
if (prevFrame && prevFrame[position] && prevFrame[position][i]) {
if (frame[i] !== prevFrame[position][i]) {
// subsequent draw
g.clearRect((columnPos + colpos / 2) - 15, pos - 15, (columnPos + colpos / 2) + 20, pos + 20);
if (frame[i]) {
drawDot('fillCircle');
} else {
drawDot('drawCircle');
}
}
} else {
// First draw
if (frame[i]) {
drawDot('fillCircle');
} else {
drawDot('drawCircle');
}
}
}
pos += colpos;
}
};
const drawClock = () => {
const data = getCurrentTime().map(findBinary);
for (let i = 0; i < data.length; i += 1) {
drawColumn(i, data[i]);
}
prevFrame = data;
};
// Themes
const drawTheme = (idx) => () => {
idx += 1;
const themes = [
[[0, 0, 0], [1, 1, 1]],
[[1, 1, 1], [0, 0, 0]],
[[0, 0, 0], [1, 0, 0]],
[[0, 0, 0], [0, 1, 0]],
[[0, 0, 0], [0, 0, 1]],
];
if (idx >= themes.length) idx = 0;
const color = themes[idx];
g.setBgColor.apply(g, color[0]);
g.setColor.apply(g, color[1]);
g.clear();
};
const nextTheme = drawTheme(0);
setWatch(() => {
prevFrame = [];
Bangle.beep();
nextTheme();
}, BTN1, { repeat: true });
Bangle.on('lcdPower', on => {
if (on) drawClock();
});
g.clear();
setInterval(() => { drawClock(); }, 1000);
})();

7
apps/clock-binary.json Normal file
View File

@ -0,0 +1,7 @@
{
"name":"Binary Clock",
"type":"clock",
"icon":"*bclock",
"src":"-bclock"
}

BIN
apps/clock-binary.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 743 B