-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsource.js
More file actions
126 lines (110 loc) · 3.47 KB
/
Copy pathsource.js
File metadata and controls
126 lines (110 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
class LEDClockBits {
constructor() {
this._history = null;
this._hi = null;
this._lo = null;
this._count = 0;
}
set count(value) {
if (value < 0 || value > 60) {
throw new Error('invalid value');
}
this._count = value;
}
get count() {
return this._count;
}
getOutput() {
const hi = Math.floor(this.count / 10)
const lo = this.count % 10;
return [
hi.toString(2).padStart(4, '0').split('').map(v => v === '1'),
lo.toString(2).padStart(4, '0').split('').map(v => v === '1'),
]
}
bindLEDs(hi, lo) {
this._hi = hi;
this._lo = lo;
}
showOutputOnLED() {
if (!this._hi || !this._lo) {
throw new Error('missing output LEDs');
}
const [hi, lo] = this.getOutput();
const fingerprint = JSON.stringify([hi, lo]);
if (fingerprint === this._history) {
return;
}
this._history = fingerprint;
const switchLED = (dom, on) => {
if (on) {
dom.classList.add('on');
dom.classList.remove('off');
} else {
// cloud be off from previous state, and calling off again,
// causes animation trouble.
if (dom.classList.contains('on')) {
dom.classList.add('off');
setTimeout(() => {
dom.classList.remove('on');
dom.classList.remove('off');
// exit 50ms before css animation, to prevent "blink"
}, 450);
}
}
}
for (let i = 0; i < 4; i++) {
switchLED(this._hi[i], hi[i]);
switchLED(this._lo[i], lo[i]);
}
}
}
const globals = {
hmsLEDS: null
}
function getSegmentLEDs(segment) {
const hiLo = [[], []];
for (let x = 0; x < 2; x++) {
for (let y = 0; y < 4; y++) {
hiLo[x].push(document.getElementById(`px_${y}_${x + segment * 2}`));
}
}
return hiLo;
}
function parseAndDisplayTimeLED(ts) {
const date = new Date(ts);
with (globals) {
hmsLEDS[0].count = date.getHours()
hmsLEDS[1].count = date.getMinutes();
hmsLEDS[2].count = date.getSeconds();
hmsLEDS.forEach(led => led.showOutputOnLED())
}
const ms = date.getMilliseconds();
const dt = ms / 1000 * 2 * Math.PI;
const dy = Math.floor(Math.sin(dt) * 10);
const dx = Math.floor(Math.cos(dt) * 10);
document.body.style.boxShadow = `${dx}px ${dy}px 10px 1px #36752d2e`;
}
function render() {
parseAndDisplayTimeLED(Date.now());
requestAnimationFrame(render);
}
function main() {
setTimeout(() => {
const electron = require('electron');
const { ipcRenderer } = electron;
const msgTerminate = () => ipcRenderer.send('terminate', true);
// something nasty happens, when windows launches screensaver.
// this event gets fired multiple times.
document.addEventListener('keydown', msgTerminate);
document.addEventListener('mousedown', msgTerminate);
document.addEventListener('mousemove', msgTerminate);
}, 1000);
with (globals) {
hmsLEDS = [new LEDClockBits(), new LEDClockBits(), new LEDClockBits()];
for (let i = 0; i < 3; i++) {
hmsLEDS[i].bindLEDs(...getSegmentLEDs(i))
}
}
render();
}