mclock: Improve performance, attempt to remove occasional glitch when LCD on (fix #279)
parent
899eaee98e
commit
bf5bf91967
|
|
@ -108,7 +108,7 @@
|
||||||
{ "id": "mclock",
|
{ "id": "mclock",
|
||||||
"name": "Morphing Clock",
|
"name": "Morphing Clock",
|
||||||
"icon": "clock-morphing.png",
|
"icon": "clock-morphing.png",
|
||||||
"version":"0.03",
|
"version":"0.04",
|
||||||
"description": "7 segment clock that morphs between minutes and hours",
|
"description": "7 segment clock that morphs between minutes and hours",
|
||||||
"tags": "clock",
|
"tags": "clock",
|
||||||
"type":"clock",
|
"type":"clock",
|
||||||
|
|
|
||||||
|
|
@ -1,2 +1,3 @@
|
||||||
0.02: Modified for use with new bootloader and firmware
|
0.02: Modified for use with new bootloader and firmware
|
||||||
0.03: Added Locale based date
|
0.03: Added Locale based date
|
||||||
|
0.04: Improve performance, attempt to remove occasional glitch when LCD on (fix #279)
|
||||||
|
|
|
||||||
|
|
@ -1,14 +1,15 @@
|
||||||
var locale = require("locale");
|
var locale = require("locale");
|
||||||
|
var CHARW = 34; // how tall are digits?
|
||||||
|
var CHARP = 2; // how chunky are digits?
|
||||||
|
var Y = 50; // start height
|
||||||
// Offscreen buffer
|
// Offscreen buffer
|
||||||
var buf = Graphics.createArrayBuffer(240,86,1,{msb:true});
|
var buf = Graphics.createArrayBuffer(CHARW+CHARP*2,CHARW*2 + CHARP*2,1,{msb:true});
|
||||||
function flip() {
|
var bufimg = {width:buf.getWidth(),height:buf.getHeight(),buffer:buf.buffer};
|
||||||
g.setColor(1,1,1);
|
|
||||||
g.drawImage({width:buf.getWidth(),height:buf.getHeight(),buffer:buf.buffer},0,50);
|
|
||||||
}
|
|
||||||
// The last time that we displayed
|
// The last time that we displayed
|
||||||
var lastTime = " ";
|
var lastTime = " ";
|
||||||
// If animating, this is the interval's id
|
// If animating, this is the interval's id
|
||||||
var animInterval;
|
var animInterval;
|
||||||
|
var timeInterval;
|
||||||
|
|
||||||
/* Get array of lines from digit d to d+1.
|
/* Get array of lines from digit d to d+1.
|
||||||
n is the amount (0..1)
|
n is the amount (0..1)
|
||||||
|
|
@ -49,7 +50,7 @@ const DIGITS = {
|
||||||
[0,1,1,1],
|
[0,1,1,1],
|
||||||
[1,1,1,2],
|
[1,1,1,2],
|
||||||
[1-n,2,1,2]],
|
[1-n,2,1,2]],
|
||||||
"5": (n,maxFive)=>maxFive ? [ // 5 -> 0
|
"5to0": n=>[ // 5 -> 0
|
||||||
[0,0,0,1],
|
[0,0,0,1],
|
||||||
[0,0,1,0],
|
[0,0,1,0],
|
||||||
[n,1,1,1],
|
[n,1,1,1],
|
||||||
|
|
@ -57,7 +58,8 @@ const DIGITS = {
|
||||||
[0,2,1,2],
|
[0,2,1,2],
|
||||||
[0,2,0,2],
|
[0,2,0,2],
|
||||||
[1,1-n,1,1],
|
[1,1-n,1,1],
|
||||||
[0,1,0,1+n]] : [ // 5 -> 6
|
[0,1,0,1+n]],
|
||||||
|
"5to6": n=>[ // 5 -> 6
|
||||||
[0,0,0,1],
|
[0,0,0,1],
|
||||||
[0,0,1,0],
|
[0,0,1,0],
|
||||||
[0,1,1,1],
|
[0,1,1,1],
|
||||||
|
|
@ -109,19 +111,18 @@ const DIGITS = {
|
||||||
|
|
||||||
/* Draw a transition between lastText and thisText.
|
/* Draw a transition between lastText and thisText.
|
||||||
'n' is the amount - 0..1 */
|
'n' is the amount - 0..1 */
|
||||||
function draw(lastText,thisText,n) {
|
function drawDigits(lastText,thisText,n) {
|
||||||
buf.clear();
|
const p = CHARP; // padding around digits
|
||||||
var x = 1; // x offset
|
const s = CHARW; // character size
|
||||||
const p = 2; // padding around digits
|
var x = 0; // x offset
|
||||||
var y = p; // y offset
|
g.reset();
|
||||||
const s = 34; // character size
|
|
||||||
for (var i=0;i<lastText.length;i++) {
|
for (var i=0;i<lastText.length;i++) {
|
||||||
var lastCh = lastText[i];
|
var lastCh = lastText[i];
|
||||||
var thisCh = thisText[i];
|
var thisCh = thisText[i];
|
||||||
if (thisCh==":") x-=4;
|
if (thisCh==":") x-=4;
|
||||||
|
if (lastCh!=thisCh) {
|
||||||
var ch, chn = n;
|
var ch, chn = n;
|
||||||
if (lastCh!==undefined &&
|
if ((thisCh-1==lastCh ||
|
||||||
(thisCh-1==lastCh ||
|
|
||||||
(thisCh==0 && lastCh==5) ||
|
(thisCh==0 && lastCh==5) ||
|
||||||
(thisCh==0 && lastCh==9)))
|
(thisCh==0 && lastCh==9)))
|
||||||
ch = lastCh;
|
ch = lastCh;
|
||||||
|
|
@ -129,39 +130,47 @@ function draw(lastText,thisText,n) {
|
||||||
ch = thisCh;
|
ch = thisCh;
|
||||||
chn = 0;
|
chn = 0;
|
||||||
}
|
}
|
||||||
var l = DIGITS[ch](chn,lastCh==5 && thisCh==0);
|
buf.clear();
|
||||||
|
if (ch=="5") ch = (lastCh==5 && thisCh==0)?"5to0":"5to6";
|
||||||
|
var l = DIGITS[ch](chn);
|
||||||
l.forEach(c=>{
|
l.forEach(c=>{
|
||||||
if (c[0]!=c[2]) // horiz
|
if (c[0]!=c[2]) // horiz
|
||||||
buf.fillRect(x+c[0]*s,y+c[1]*s-p,x+c[2]*s,y+c[3]*s+p);
|
buf.fillRect(p+c[0]*s,c[1]*s,p+c[2]*s,2*p+c[3]*s);
|
||||||
else if (c[1]!=c[3]) // vert
|
else if (c[1]!=c[3]) // vert
|
||||||
buf.fillRect(x+c[0]*s-p,y+c[1]*s,x+c[2]*s+p,y+c[3]*s);
|
buf.fillRect(c[0]*s,p+c[1]*s,2*p+c[2]*s,p+c[3]*s);
|
||||||
});
|
});
|
||||||
|
g.drawImage(bufimg,x,Y);
|
||||||
|
}
|
||||||
if (thisCh==":") x-=4;
|
if (thisCh==":") x-=4;
|
||||||
x+=s+p+7;
|
x+=s+p+7;
|
||||||
}
|
}
|
||||||
y += 2*s;
|
}
|
||||||
|
function drawSeconds() {
|
||||||
|
var x = (CHARW + CHARP + 6)*5;
|
||||||
|
var y = Y + 2*CHARW + CHARP;
|
||||||
var d = new Date();
|
var d = new Date();
|
||||||
buf.setFont("6x8");
|
g.reset();
|
||||||
buf.setFontAlign(-1,-1);
|
g.setFont("6x8");
|
||||||
buf.drawString(("0"+d.getSeconds()).substr(-2), x, y-8);
|
g.setFontAlign(-1,-1);
|
||||||
|
g.drawString(("0"+d.getSeconds()).substr(-2), x, y-8, true);
|
||||||
// date
|
// date
|
||||||
buf.setFontAlign(0,-1);
|
g.setFontAlign(0,-1);
|
||||||
var date = locale.date(d,false);
|
var date = locale.date(d,false);
|
||||||
buf.drawString(date, buf.getWidth()/2, y+8);
|
g.drawString(date, g.getWidth()/2, y+8, true);
|
||||||
flip();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Show the current time, and animate if needed */
|
/* Show the current time, and animate if needed */
|
||||||
function showTime() {
|
function showTime() {
|
||||||
if (!Bangle.isLCDOn()) return;
|
|
||||||
if (animInterval) return; // in animation - quit
|
if (animInterval) return; // in animation - quit
|
||||||
var d = new Date();
|
var d = new Date();
|
||||||
var t = (" "+d.getHours()).substr(-2)+":"+
|
var t = (" "+d.getHours()).substr(-2)+":"+
|
||||||
("0"+d.getMinutes()).substr(-2);
|
("0"+d.getMinutes()).substr(-2);
|
||||||
var l = lastTime;
|
var l = lastTime;
|
||||||
// same - don't animate
|
// same - don't animate
|
||||||
if (t==l) {
|
if (t==l || l==" ") {
|
||||||
draw(t,l,0);
|
drawDigits(l,t,0);
|
||||||
|
drawSeconds();
|
||||||
|
lastTime = t;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var n = 0;
|
var n = 0;
|
||||||
|
|
@ -170,23 +179,35 @@ function showTime() {
|
||||||
if (n>=1) {
|
if (n>=1) {
|
||||||
n=1;
|
n=1;
|
||||||
clearInterval(animInterval);
|
clearInterval(animInterval);
|
||||||
animInterval=0;
|
animInterval = undefined;
|
||||||
}
|
}
|
||||||
draw(l,t,n);
|
drawDigits(l,t,n);
|
||||||
}, 20);
|
}, 20);
|
||||||
lastTime = t;
|
lastTime = t;
|
||||||
}
|
}
|
||||||
|
|
||||||
Bangle.on('lcdPower',function(on) {
|
Bangle.on('lcdPower',function(on) {
|
||||||
if (on)
|
if (animInterval) {
|
||||||
|
clearInterval(animInterval);
|
||||||
|
animInterval = undefined;
|
||||||
|
}
|
||||||
|
if (timeInterval) {
|
||||||
|
clearInterval(timeInterval);
|
||||||
|
timeInterval = undefined;
|
||||||
|
}
|
||||||
|
if (on) {
|
||||||
showTime();
|
showTime();
|
||||||
|
timeInterval = setInterval(showTime, 1000);
|
||||||
|
} else {
|
||||||
|
lastTime = " ";
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
g.clear();
|
g.clear();
|
||||||
Bangle.loadWidgets();
|
Bangle.loadWidgets();
|
||||||
Bangle.drawWidgets();
|
Bangle.drawWidgets();
|
||||||
// Update time once a second
|
// Update time once a second
|
||||||
setInterval(showTime, 1000);
|
timeInterval = setInterval(showTime, 1000);
|
||||||
showTime();
|
showTime();
|
||||||
|
|
||||||
// Show launcher when middle button pressed
|
// Show launcher when middle button pressed
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue