diff --git a/apps/widswatchbeats/metadata.json b/apps/widswatchbeats/metadata.json new file mode 100644 index 000000000..5c8229966 --- /dev/null +++ b/apps/widswatchbeats/metadata.json @@ -0,0 +1,13 @@ +{ + "id": "widswatchbeats", + "name": "Swatch Internet Time Widget", + "icon": "widget-icon.png", + "type": "widget", + "version": "0.01", + "description": "Displays the current .beat (e.g. @500 for midday)", + "tags": "widget,time,swatch,internet,beat,.beat,clock", + "supports": ["BANGLEJS","BANGLEJS2"], + "storage": [ + {"name": "widswatchbeats.wid.js","url": "widget.js"} + ] +} diff --git a/apps/widswatchbeats/widget-icon.png b/apps/widswatchbeats/widget-icon.png new file mode 100644 index 000000000..726a57825 Binary files /dev/null and b/apps/widswatchbeats/widget-icon.png differ diff --git a/apps/widswatchbeats/widget.js b/apps/widswatchbeats/widget.js new file mode 100644 index 000000000..7f2427c74 --- /dev/null +++ b/apps/widswatchbeats/widget.js @@ -0,0 +1,41 @@ +(function() { + const WIDTH = 50; + const SEC_PER_BEAT = 86.4; + + let drawTimeout; + + function getSecondsSinceMidnight() { + const now = new Date(); + return now.getHours() * 3600 + now.getMinutes() * 60 + now.getSeconds(); + } + + function queueDraw() { + if (drawTimeout) clearTimeout(drawTimeout); + const nextSecond = SEC_PER_BEAT - (getSecondsSinceMidnight() % SEC_PER_BEAT); + drawTimeout = setTimeout(function() { + drawTimeout = undefined; + WIDGETS.widswatchbeats.draw(); + }, nextSecond * 1000 + 1); // Add one ms to ensure we're past the beat + } + + function draw() { + const now = new Date(); + const seconds = now.getHours() * 3600 + now.getMinutes() * 60 + now.getSeconds(); + const beats = Math.floor(seconds / SEC_PER_BEAT); + const beatsString = '@' + beats.toString().padStart(3, '0'); + + g.reset(); + g.setFontAlign(0, 0); + g.clearRect(this.x, this.y, this.x + WIDTH, this.y+22); + g.setFont("6x8", 2); + g.drawString(beatsString, this.x+WIDTH/2, this.y+12); + queueDraw(); + } + + WIDGETS.widswatchbeats = { + area: "tl", + width: WIDTH, + draw + }; + +})();