From e1fc5c0996bda7a4940fb0458655374e69b255c4 Mon Sep 17 00:00:00 2001 From: Noah Howard <3317164+nh-99@users.noreply.github.com> Date: Mon, 23 May 2022 09:15:04 -0400 Subject: [PATCH] Make screen updates more efficient --- apps/mandelbrotclock/mandelbrotclock.js | 29 ++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/apps/mandelbrotclock/mandelbrotclock.js b/apps/mandelbrotclock/mandelbrotclock.js index d46dae782..ffc50e9bd 100644 --- a/apps/mandelbrotclock/mandelbrotclock.js +++ b/apps/mandelbrotclock/mandelbrotclock.js @@ -13,6 +13,18 @@ const mandelbrotBmp = { ), }; +// timeout used to update every minute +var drawTimeout; + +// schedule a draw for the next minute +function queueDraw() { + if (drawTimeout) clearTimeout(drawTimeout); + drawTimeout = setTimeout(function() { + drawTimeout = undefined; + draw(); + }, 60000 - (Date.now() % 60000)); +} + function draw() { g.drawImage(mandelbrotBmp); // work out how to display the current time @@ -27,10 +39,25 @@ function draw() { g.drawString(timeStr, 70, 58, false); g.setFont("Vector", 20); g.drawString(dateStr, 70, 88, false); + + queueDraw(); } +// Clear the screen once, at startup g.clear(); // draw immediately at first draw(); -var secondInterval = setInterval(draw, 1000); + +// Stop updates when LCD is off, restart when on +Bangle.on('lcdPower',on=>{ + if (on) { + draw(); // draw immediately, queue redraw + } else { // stop draw timer + if (drawTimeout) clearTimeout(drawTimeout); + drawTimeout = undefined; + } +}); + +// Show launcher when middle button pressed +Bangle.setUI("clock");