Quick fixes and performance improvement (#3)

* Some performance improvement updates. Suspends timer when LCD is off, and resume when it comes back.
pull/26/head
Akinboyewa Akindolani 2019-11-12 14:32:07 +00:00 committed by GitHub
parent d6179b955f
commit b384eeb084
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 100 additions and 65 deletions

View File

@ -1,67 +1,102 @@
g.clear();
var minuteDate = new Date();
var secondDate = new Date();
function seconds(angle, r) {
var a = angle*Math.PI/180;
var x = 120+Math.sin(a)*r;
var y = 120-Math.cos(a)*r;
g.fillRect(x-1,y-1,x+1,y+1);
}
function hand(angle, r1,r2) {
var a = angle*Math.PI/180;
var r3 = 3;
var p = Math.PI/2;
g.fillPoly([
120+Math.sin(a)*r1,
120-Math.cos(a)*r1,
120+Math.sin(a+p)*r3,
120-Math.cos(a+p)*r3,
120+Math.sin(a)*r2,
120-Math.cos(a)*r2,
120+Math.sin(a-p)*r3,
120-Math.cos(a-p)*r3]);
}
function drawAll() {
(function(){
g.clear();
g.setColor(0,0,0.6);
for (var i=0;i<60;i++)
seconds(360*i/60, 90);
secondDate = minuteDate = new Date();
onSecond();
onMinute();
}
function onSecond() {
g.setColor(0,0,0.6);
seconds(360*secondDate.getSeconds()/60, 90);
g.setColor(1,0,0);
secondDate = new Date();
seconds(360*secondDate.getSeconds()/60, 90);
g.setColor(1,1,1);
}
function onMinute() {
g.setColor(0,0,0);
hand(360*minuteDate.getHours()/12, -10, 50);
hand(360*minuteDate.getMinutes()/60, -10, 82);
oldMinute = new Date();
g.setColor(1,1,1);
hand(360*minuteDate.getHours()/12, -10, 50);
hand(360*minuteDate.getMinutes()/60, -10, 82);
}
setInterval(onSecond,1000);
setInterval(onMinute,60*1000);
drawAll();
Bangle.on('lcdPower',function(on) {
if (on) {
g.clear();
drawAll();
drawWidgets();
const p = Math.PI/2;
const PRad = Math.PI/180;
let intervalRefMin = null;
let intervalRefSec = null;
let minuteDate = new Date();
let secondDate = new Date();
function seconds(angle, r) {
const a = angle*PRad;
const x = 120+Math.sin(a)*r;
const y = 120-Math.cos(a)*r;
g.fillRect(x-1,y-1,x+1,y+1);
}
});
function hand(angle, r1,r2) {
const a = angle*PRad;
const r3 = 3;
g.fillPoly([
120+Math.sin(a)*r1,
120-Math.cos(a)*r1,
120+Math.sin(a+p)*r3,
120-Math.cos(a+p)*r3,
120+Math.sin(a)*r2,
120-Math.cos(a)*r2,
120+Math.sin(a-p)*r3,
120-Math.cos(a-p)*r3]);
}
function drawAll() {
g.clear();
g.setColor(0,0,0.6);
for (let i=0;i<60;i++)
seconds(360*i/60, 90);
secondDate = minuteDate = new Date();
onSecond();
onMinute();
}
function onSecond() {
g.setColor(0,0,0.6);
seconds(360*secondDate.getSeconds()/60, 90);
g.setColor(1,0,0);
secondDate = new Date();
seconds(360*secondDate.getSeconds()/60, 90);
g.setColor(1,1,1);
}
function onMinute() {
g.setColor(0,0,0);
hand(360*minuteDate.getHours()/12, -10, 50);
hand(360*minuteDate.getMinutes()/60, -10, 82);
minuteDate = new Date();
g.setColor(1,1,1);
hand(360*minuteDate.getHours()/12, -10, 50);
hand(360*minuteDate.getMinutes()/60, -10, 82);
if(minuteDate.getHours() >= 0 && minuteDate.getMinutes() === 0) {
Bangle.buzz();
}
}
function clearTimers() {
if(intervalRefMin) {clearInterval(intervalRefMin);}
if(intervalRefSec) {clearInterval(intervalRefSec);}
}
function startTimers() {
minuteDate = new Date();
secondDate = new Date();
intervalRefSec = setInterval(onSecond,1000);
intervalRefMin = setInterval(onMinute,60*1000);
drawAll();
}
startTimers();
Bangle.on('lcdPower',function(on) {
if (on) {
g.clear();
startTimers();
drawWidgets();
}else {
clearTimers();
}
});
Bangle.on('gesture',function(gesture){
if (gesture && !Bangle.isLCDOn()) {
clearTimers();
g.clear();
startTimers();
drawWidgets();
Bangle.setLCDTimeout(30);
Bangle.setLCDPower(true);
}
});
})();