2020-01-17 11:43:26 +00:00
|
|
|
// This ALWAYS runs at boot
|
|
|
|
E.setFlags({pretokenise:1});
|
2020-02-04 16:17:23 +00:00
|
|
|
// Load settings...
|
2020-02-28 11:44:25 +00:00
|
|
|
var s = require('Storage').readJSON('setting.json')||{};
|
2020-02-04 16:17:23 +00:00
|
|
|
if (s.ble!==false) {
|
|
|
|
if (s.HID) { // Humen interface device
|
|
|
|
Bangle.HID = E.toUint8Array(atob("BQEJBqEBhQIFBxngKecVACUBdQGVCIEClQF1CIEBlQV1AQUIGQEpBZEClQF1A5EBlQZ1CBUAJXMFBxkAKXOBAAkFFQAm/wB1CJUCsQLABQwJAaEBhQEVACUBdQGVAQm1gQIJtoECCbeBAgm4gQIJzYECCeKBAgnpgQIJ6oECwA=="));
|
|
|
|
NRF.setServices({}, {uart:true, hid:Bangle.HID});
|
|
|
|
}
|
|
|
|
}
|
2020-02-25 13:34:06 +00:00
|
|
|
if (s.blerepl===false) { // If not programmable, force terminal off Bluetooth
|
|
|
|
if (s.log) Terminal.setConsole(true); // if showing debug, force REPL onto terminal
|
2020-02-28 11:44:25 +00:00
|
|
|
else E.setConsole(null,{force:true}); // on new (2v05+) firmware we have E.setConsole which allows a 'null' console
|
2020-02-25 13:34:06 +00:00
|
|
|
} else {
|
|
|
|
if (s.log) Terminal.setConsole(); // if showing debug, put REPL on terminal (until connection)
|
|
|
|
else Bluetooth.setConsole(true); // else if no debug, force REPL to Bluetooth
|
|
|
|
}
|
2020-02-04 16:17:23 +00:00
|
|
|
// we just reset, so BLE should be on
|
|
|
|
if (s.ble===false) NRF.sleep();
|
|
|
|
// Set time, vibrate, beep, etc
|
|
|
|
if (!s.vibrate) Bangle.buzz=Promise.resolve;
|
|
|
|
if (!s.beep) Bangle.beep=Promise.resolve;
|
|
|
|
Bangle.setLCDTimeout(s.timeout);
|
|
|
|
if (!s.timeout) Bangle.setLCDPower(1);
|
|
|
|
E.setTimeZone(s.timezone);
|
|
|
|
delete s;
|
2020-02-12 10:48:14 +00:00
|
|
|
// check for alarms
|
|
|
|
function checkAlarm() {
|
2020-02-28 11:44:25 +00:00
|
|
|
var alarms = require('Storage').readJSON('alarm.json')||[];
|
2020-02-12 10:48:14 +00:00
|
|
|
var time = new Date();
|
|
|
|
var active = alarms.filter(a=>a.on&&(a.last!=time.getDate()));
|
|
|
|
if (active.length) {
|
|
|
|
active = active.sort((a,b)=>a.hr-b.hr);
|
|
|
|
var hr = time.getHours()+(time.getMinutes()/60);
|
2020-02-28 11:44:25 +00:00
|
|
|
if (!require('Storage').read("alarm.app.js")) {
|
2020-02-12 10:48:14 +00:00
|
|
|
console.log("No alarm app!");
|
2020-02-28 11:44:25 +00:00
|
|
|
require('Storage').write('alarm.json',"[]")
|
2020-02-12 10:48:14 +00:00
|
|
|
} else {
|
|
|
|
if (active[0].hr < hr) {
|
|
|
|
// fire alarm now
|
2020-02-28 11:44:25 +00:00
|
|
|
load("alarm.app.js");
|
2020-02-12 10:48:14 +00:00
|
|
|
} else {
|
|
|
|
// execute alarm at the correct time
|
|
|
|
setTimeout(function() {
|
2020-02-28 11:44:25 +00:00
|
|
|
load("alarm.app.js");
|
2020-02-12 10:48:14 +00:00
|
|
|
},3600000*(active[0].hr-hr));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-05 10:52:51 +00:00
|
|
|
// check to see if our clock is wrong - if it is use GPS time
|
|
|
|
if ((new Date()).getFullYear()==1970) {
|
2020-02-25 13:34:06 +00:00
|
|
|
//console.log("Searching for GPS time");
|
2020-02-05 10:52:51 +00:00
|
|
|
Bangle.on('GPS',function cb(g) {
|
|
|
|
Bangle.setGPSPower(0);
|
|
|
|
Bangle.removeListener("GPS",cb);
|
|
|
|
if (!g.time || (g.time.getFullYear()<2000) ||
|
|
|
|
(g.time.getFullYear()==2250)) {
|
2020-02-25 13:34:06 +00:00
|
|
|
//console.log("GPS receiver's time not set");
|
2020-02-05 10:52:51 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
setTime(g.time.getTime()/1000);
|
2020-02-25 13:34:06 +00:00
|
|
|
//console.log("GPS time",g.time.toString());
|
2020-02-12 10:48:14 +00:00
|
|
|
checkAlarm();
|
2020-02-05 10:52:51 +00:00
|
|
|
});
|
|
|
|
Bangle.setGPSPower(1);
|
2020-02-12 10:48:14 +00:00
|
|
|
} else checkAlarm();
|
|
|
|
delete checkAlarm;
|