BangleApps/apps/ha/ha.app.js

72 lines
1.4 KiB
JavaScript
Raw Normal View History

2022-06-23 18:06:53 +00:00
var storage = require("Storage");
2022-06-20 11:36:49 +00:00
var W = g.getWidth(), H = g.getHeight();
var position=0;
2022-06-23 16:51:37 +00:00
2022-06-23 18:06:53 +00:00
// Try to read custom actions, otherwise use default
var actions = [
"No Actions",
];
2022-06-23 16:51:37 +00:00
2022-06-23 18:06:53 +00:00
try{
2022-06-23 18:32:39 +00:00
actions = storage.read("ha.trigger.txt").split(",");
2022-06-23 18:06:53 +00:00
} catch(e) {
// In case there are no user actions yet, we show the default...
2022-06-23 16:51:37 +00:00
}
2022-06-20 11:36:49 +00:00
function draw() {
g.reset().clearRect(Bangle.appRect);
2022-06-23 18:32:39 +00:00
var h = 22;
2022-06-23 18:06:53 +00:00
g.setFont("Vector", h).setFontAlign(0,0);
var action = actions[position];
var w = g.stringWidth(action);
2022-06-20 11:36:49 +00:00
2022-06-23 18:06:53 +00:00
g.fillRect(W/2-w/2-8, H/2-h/2-8, W/2+w/2+2, H/2+h/2+2);
g.setColor(g.theme.bg).drawString(action, W/2, H/2);
2022-06-20 11:36:49 +00:00
}
draw();
setWatch(_=>load(), BTN1);
Bangle.on('touch', function(btn, e){
var left = parseInt(g.getWidth() * 0.3);
var right = g.getWidth() - left;
var isLeft = e.x < left;
var isRight = e.x > right;
if(isRight){
2022-06-23 18:06:53 +00:00
Bangle.buzz(40, 0.6);
2022-06-20 11:36:49 +00:00
position += 1;
2022-06-23 18:06:53 +00:00
position = position >= actions.length ? 0 : position;
2022-06-20 11:36:49 +00:00
}
if(isLeft){
2022-06-23 18:06:53 +00:00
Bangle.buzz(40, 0.6);
2022-06-20 11:36:49 +00:00
position -= 1;
2022-06-23 18:06:53 +00:00
position = position < 0 ? actions.length-1 : position;
2022-06-20 11:36:49 +00:00
}
if(!isRight && !isLeft){
2022-06-23 18:06:53 +00:00
Bangle.buzz(80, 0.6).then(()=>{
2022-06-23 18:23:30 +00:00
Bluetooth.println(JSON.stringify({
t:"intent",
action:"com.espruino.gadgetbridge.banglejs.HA",
extra:{
trigger: actions[position]
}})
);
2022-06-23 18:06:53 +00:00
setTimeout(()=>{
Bangle.buzz(80, 0.6);
}, 250);
});
2022-06-20 11:36:49 +00:00
}
draw();
});
Bangle.loadWidgets();
Bangle.drawWidgets();