Skip to content

Commit b01901c

Browse files
authored
Merge pull request espruino#3678 from cbkerr/andark-fastload-queue
andark: queue redraws and enable fastload
2 parents 22078e8 + aea8ff9 commit b01901c

File tree

3 files changed

+60
-35
lines changed

3 files changed

+60
-35
lines changed

apps/andark/ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
0.05: Fix support for dark theme + support widgets +
66
add settings for widgets, order of drawing and hour hand length
77
0.06: Fix issue showing widgets when app is fast-loaded into from launcher with widgets disabled
8+
0.07: Enable fast loading and queue updates to the second

apps/andark/app.js

Lines changed: 58 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
{
12
const defaultSettings = {
23
loadWidgets : false,
34
textAboveHands : false,
@@ -25,18 +26,16 @@ const zahlpos=(function() {
2526
return z;
2627
})();
2728

28-
let unlock = false;
2929

30-
function zeiger(len,dia,tim){
30+
let zeiger = function(len,dia,tim){
3131
const x=c.x+ Math.cos(tim)*len/2,
3232
y=c.y + Math.sin(tim)*len/2,
3333
d={"d":3,"x":dia/2*Math.cos(tim+Math.PI/2),"y":dia/2*Math.sin(tim+Math.PI/2)},
3434
pol=[c.x-d.x,c.y-d.y,c.x+d.x,c.y+d.y,x+d.x,y+d.y,x-d.x,y-d.y];
3535
return pol;
36+
};
3637

37-
}
38-
39-
function drawHands(d) {
38+
let drawHands = function(d) {
4039
let m=d.getMinutes(), h=d.getHours(), s=d.getSeconds();
4140
g.setColor(1,1,1);
4241

@@ -61,9 +60,9 @@ function drawHands(d) {
6160
g.fillPoly(sekz,true);
6261
}
6362
g.fillCircle(c.x,c.y,4);
64-
}
63+
};
6564

66-
function drawText(d) {
65+
let drawText = function(d) {
6766
g.setFont("Vector",10);
6867
g.setBgColor(0,0,0);
6968
g.setColor(1,1,1);
@@ -74,19 +73,30 @@ function drawText(d) {
7473
g.setBgColor(1,0,0);
7574
}
7675
g.drawString(batStr, c.x, c.y+40, true);
77-
}
76+
};
7877

79-
function drawNumbers() {
78+
let drawNumbers = function() {
8079
//draws the numbers on the screen
8180
g.setFont("Vector",20);
8281
g.setColor(1,1,1);
8382
g.setBgColor(0,0,0);
8483
for(let i = 0;i<12;i++){
8584
g.drawString(zahlpos[i][0],zahlpos[i][1],zahlpos[i][2],true);
8685
}
87-
}
86+
};
87+
88+
let drawTimeout;
89+
let queueMillis = 1000;
8890

89-
function draw(){
91+
let queueDraw = function() {
92+
if (drawTimeout) clearTimeout(drawTimeout);
93+
drawTimeout = setTimeout(function() {
94+
drawTimeout = undefined;
95+
draw();
96+
}, queueMillis - (Date.now() % queueMillis));
97+
};
98+
99+
let draw = function(){
90100
// draw black rectangle in the middle to clear screen from scale and hands
91101
g.setColor(0,0,0);
92102
g.fillRect(10,10,2*c.x-10,2*c.x-10);
@@ -100,10 +110,11 @@ function draw(){
100110
} else {
101111
drawText(d); drawHands(d);
102112
}
103-
}
113+
queueDraw();
114+
};
104115

105116
//draws the scale once the app is startet
106-
function drawScale(){
117+
let drawScale = function(){
107118
// clear the screen
108119
g.setBgColor(0,0,0);
109120
g.clear();
@@ -117,36 +128,49 @@ function drawScale(){
117128
g.fillRect(10,10,2*c.x-10,2*c.x-10);
118129
g.setColor(1,1,1);
119130
}
120-
}
131+
};
121132

122133
//// main running sequence ////
123134

124135
// Show launcher when middle button pressed, and widgets that we're clock
125-
Bangle.setUI("clock");
136+
Bangle.setUI({
137+
mode: "clock",
138+
remove: function() {
139+
if (drawTimeout) clearTimeout(drawTimeout);
140+
drawTimeout = undefined;
141+
Bangle.removeListener('lcdPower', updateState);
142+
Bangle.removeListener('lock', updateState);
143+
require("widget_utils").show();
144+
}});
126145
// Load widgets if needed, and make them show swipeable
127146
if (settings.loadWidgets) {
128147
Bangle.loadWidgets();
129148
require("widget_utils").swipeOn();
130149
} else if (global.WIDGETS) require("widget_utils").hide();
131-
// Clear the screen once, at startup
132-
drawScale();
133-
draw();
134150

135-
let secondInterval = setInterval(draw, 1000);
151+
let updateState = function() {
152+
if (Bangle.isLCDOn()) {
153+
if (!Bangle.isLocked()) {
154+
queueMillis = 1000;
155+
unlock = true;
156+
} else {
157+
queueMillis = 60000;
158+
unlock = false;
159+
}
160+
draw();
161+
} else {
162+
if (drawTimeout) clearTimeout(drawTimeout);
163+
drawTimeout = undefined;
164+
}
165+
};
136166

137167
// Stop updates when LCD is off, restart when on
138-
Bangle.on('lcdPower',on=>{
139-
if (secondInterval) clearInterval(secondInterval);
140-
secondInterval = undefined;
141-
if (on) {
142-
secondInterval = setInterval(draw, 1000);
143-
draw(); // draw immediately
144-
}
145-
});
146-
Bangle.on('lock',on=>{
147-
unlock = !on;
148-
if (secondInterval) clearInterval(secondInterval);
149-
secondInterval = setInterval(draw, unlock ? 1000 : 60000);
150-
draw(); // draw immediately
151-
});
152-
Bangle.on('charging',on=>{draw();});
168+
Bangle.on('lcdPower', updateState);
169+
170+
Bangle.on('lock', updateState);
171+
172+
let unlock = true;
173+
updateState();
174+
drawScale();
175+
draw();
176+
}

apps/andark/metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{ "id": "andark",
22
"name": "Analog Dark",
33
"shortName":"AnDark",
4-
"version":"0.06",
4+
"version":"0.07",
55
"description": "analog clock face without disturbing widgets",
66
"icon": "andark_icon.png",
77
"type": "clock",

0 commit comments

Comments
 (0)