BangleApps/apps/cliclockJS2Enhanced/app.js

159 lines
4.0 KiB
JavaScript
Raw Normal View History

2021-11-17 17:44:17 +00:00
var fontsize = g.getWidth()>200 ? 3 : 2;
var fontsizeTime = g.getWidth()>200 ? 4 : 4;
var fontheight = 10*fontsize;
var fontheightTime = 10*fontsizeTime;
var locale = require("locale");
var marginTop = 25;
2021-11-17 17:44:17 +00:00
var flag = false;
2021-11-20 16:32:34 +00:00
var storage = require('Storage');
const settings = storage.readJSON('setting.json',1) || { HID: false };
var sendHid, next, prev, toggle, up, down, profile;
var lasty = 0;
var lastx = 0;
if (settings.HID=="kbmedia") {
profile = 'Music';
sendHid = function (code, cb) {
try {
NRF.sendHIDReport([1,code], () => {
NRF.sendHIDReport([1,0], () => {
if (cb) cb();
});
});
} catch(e) {
print(e);
}
};
next = function (cb) { sendHid(0x01, cb); };
prev = function (cb) { sendHid(0x02, cb); };
toggle = function (cb) { sendHid(0x10, cb); };
up = function (cb) {sendHid(0x40, cb); };
down = function (cb) { sendHid(0x80, cb); };
} else {
E.showPrompt("Enable HID?",{title:"HID disabled"}).then(function(enable) {
if (enable) {
settings.HID = "kbmedia";
require("Storage").write('setting.json', settings);
setTimeout(load, 1000, "hidmsicswipe.app.js");
} else setTimeout(load, 1000);
});
}
if (next) {
setWatch(function(e) {
var len = e.time - e.lastTime;
E.showMessage('lock');
setTimeout(drawApp, 1000);
Bangle.setLocked(true);
}, BTN1, { edge:"falling",repeat:true,debounce:50});
Bangle.on('drag', function(e) {
2021-11-20 16:32:34 +00:00
if(!e.b){
console.log(lasty);
console.log(lastx);
if(lasty > 40){
writeLine('Down', 3);
// setTimeout(drawApp, 1000);
2021-11-22 16:05:57 +00:00
// Bluetooth.println(JSON.stringify({t:"music", n:"volumedown"}));
2021-11-20 16:32:34 +00:00
down(() => {});
}
else if(lasty < -40){
writeLine('Up', 3);
// setTimeout(drawApp, 1000);
2021-11-22 16:05:57 +00:00
//Bluetooth.println(JSON.stringify({t:"music", n:"volumeup"}));
2021-11-20 18:14:50 +00:00
2021-11-20 16:32:34 +00:00
up(() => {});
} else if(lastx < -40){
writeLine('Prev', 3);
// setTimeout(drawApp, 1000);
2021-11-22 16:05:57 +00:00
// Bluetooth.println(JSON.stringify({t:"music", n:"previous"}));
2021-11-20 16:32:34 +00:00
prev(() => {});
} else if(lastx > 40){
writeLine('Next', 3);
// setTimeout(drawApp, 1000);
2021-11-22 16:05:57 +00:00
// Bluetooth.println(JSON.stringify({t:"music", n:"next"}));
2021-11-20 16:32:34 +00:00
next(() => {});
} else if(lastx==0 && lasty==0){
writeLine('play/pause', 3);
//setTimeout(drawApp, 1000);
2021-11-22 16:05:57 +00:00
// Bluetooth.println(JSON.stringify({t:"music", n:"play"}));
2021-11-20 17:57:57 +00:00
2021-11-20 16:32:34 +00:00
toggle(() => {});
}
lastx = 0;
lasty = 0;
}
else{
lastx = lastx + e.dx;
lasty = lasty + e.dy;
}
});
2021-11-20 16:32:34 +00:00
}
2021-11-17 17:44:17 +00:00
let textCol = g.theme.dark ? "#0f0" : "#080";
function drawAll(){
updateTime();
updateRest(new Date());
}
function updateRest(now){
writeLine(locale.dow(now),1);
writeLine(locale.date(now,1),2);
}
function updateTime(){
if (!Bangle.isLCDOn()) return;
let now = new Date();
writeLine(locale.time(now,1),0);
2021-11-20 16:32:34 +00:00
writeLine(flag?" ":"_ ",3);
2021-11-17 17:44:17 +00:00
flag = !flag;
if(now.getMinutes() == 0)
updateRest(now);
}
function writeLineStart(line){
if (line==0){
g.drawString(">",0,marginTop+(line)*fontheight);
} else {
g.drawString(">",4,marginTop+(line-1)*fontheight + fontheightTime);
}
}
function writeLine(str,line){
if (line == 0){
var y = marginTop+line*fontheightTime;
g.setFont("6x8",fontsizeTime);
g.setColor(textCol).setFontAlign(-1,-1);
2021-11-17 19:04:16 +00:00
g.clearRect(0,y,((str.length+1)*40),y+fontheightTime-1);
2021-11-17 17:44:17 +00:00
writeLineStart(line);
g.drawString(str,25,y);
} else {
var y = marginTop+(line-1)*fontheight+fontheightTime;
g.setFont("6x8",fontsize);
g.setColor(textCol).setFontAlign(-1,-1);
2021-11-20 16:32:34 +00:00
g.clearRect(0,y,((str.length+10)*40),y+fontheightTime-1);
2021-11-17 17:44:17 +00:00
writeLineStart(line);
g.drawString(str,25,y);
}
}
g.clear();
2021-11-17 17:44:17 +00:00
Bangle.on('lcdPower',function(on) {
if (on) drawAll();
});
var click = setInterval(updateTime, 1000);
// Show launcher when button pressed
2021-11-17 21:26:29 +00:00
Bangle.setUI("clockupdown", btn=>{
drawAll(); // why do we redraw here??
2021-11-17 21:26:29 +00:00
});
Bangle.loadWidgets();
Bangle.drawWidgets();
drawAll();