BangleApps/apps/ha/ha.app.js

97 lines
2.3 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-24 06:33:14 +00:00
var icon = {
width : 48, height : 48, bpp : 1,
transparent : 0,
buffer : require("heatshrink").decompress(atob("AD8BwAFDg/gAocP+AFDj4FEn/8Aod//wFD/1+FAf4j+8AoMD+EPDAUH+OPAoUP+fPAoUfBYk/C4l/EYIwC//8n//FwIFEgYFD4EH+E8nkP8BdBAonjjk44/wj/nzk58/4gAFDF4PgCIMHAoPwhkwh4FB/EEkEfIIWAHwIFC4A+BAoXgg4FDL4IFDL4IFDLIYFkAEQA=="))
};
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-24 06:33:14 +00:00
g.setFont("Vector", h);
2022-06-23 18:06:53 +00:00
var action = actions[position];
var w = g.stringWidth(action);
2022-06-20 11:36:49 +00:00
2022-06-24 06:33:14 +00:00
g.setFontAlign(-1,-1);
g.setColor(g.theme.fg).drawImage(icon, 12, H/5-2);
g.drawString("Home", icon.width + 20, H/5);
g.drawString("Assistant", icon.width + 18, H/5+24);
2022-06-23 18:06:53 +00:00
2022-06-24 06:33:14 +00:00
g.setFontAlign(0,0);
var ypos = H/5*3+20;
2022-06-24 06:46:31 +00:00
g.drawRect(W/2-w/2-8, ypos-h/2-8, W/2+w/2+5, ypos+h/2+5);
g.fillRect(W/2-w/2-6, ypos-h/2-6, W/2+w/2+3, ypos+h/2+3);
2022-06-24 06:33:14 +00:00
g.setColor(g.theme.bg).drawString(action, W/2, ypos);
2022-06-20 11:36:49 +00:00
}
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();
});
2022-06-24 06:33:14 +00:00
// Send a startup trigger such that we could also execute
// an action when the app is started :)
Bluetooth.println(JSON.stringify({
t:"intent",
action:"com.espruino.gadgetbridge.banglejs.HA",
extra:{
trigger: "APP_STARTED"
}})
);
// Next load the widgets and draw the app
2022-06-20 11:36:49 +00:00
Bangle.loadWidgets();
2022-06-24 06:33:14 +00:00
Bangle.drawWidgets();
draw();
setWatch(_=>load(), BTN1);