Skip to content

Commit

Permalink
Provide an easy way to run code at boot time espruino#163
Browse files Browse the repository at this point in the history
  • Loading branch information
gfwilliams committed Mar 31, 2020
1 parent dd0e3c8 commit e2247b2
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 25 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ easily distinguish between file types, we use the following:

* `stuff.info` is JSON that describes an app - this is auto-generated by the App Loader
* `stuff.img` is an image
* `stuff.app.js` is JS code
* `stuff.app.js` is JS code for applications
* `stuff.wid.js` is JS code for widgets
* `stuff.boot.js` is JS code that automatically gets run at boot time
* `stuff.json` is used for JSON settings for an app

## Developing your own app
Expand Down
5 changes: 3 additions & 2 deletions apps.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{ "id": "boot",
"name": "Bootloader",
"icon": "bootloader.png",
"version":"0.12",
"version":"0.13",
"description": "This is needed by Bangle.js to automatically load the clock, menu, widgets and settings",
"tags": "tool,system",
"type":"bootloader",
Expand Down Expand Up @@ -106,11 +106,12 @@
"name": "Default Alarm",
"shortName":"Alarms",
"icon": "app.png",
"version":"0.04",
"version":"0.05",
"description": "Set and respond to alarms",
"tags": "tool,alarm,widget",
"storage": [
{"name":"alarm.app.js","url":"app.js"},
{"name":"alarm.boot.js","url":"boot.js"},
{"name":"alarm.js","url":"alarm.js"},
{"name":"alarm.json","content":"[]"},
{"name":"alarm.img","url":"app-icon.js","evaluate":true},
Expand Down
1 change: 1 addition & 0 deletions apps/alarm/ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
0.02: Fix issues with alarm scheduling
0.03: More alarm scheduling issues
0.04: Tweaks for variable size widget system
0.05: Add alarm.boot.js and move code from the bootloader
24 changes: 24 additions & 0 deletions apps/alarm/boot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// check for alarms
(function() {
var alarms = require('Storage').readJSON('alarm.json',1)||[];
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)+(time.getSeconds()/3600);
if (!require('Storage').read("alarm.js")) {
console.log("No alarm app!");
require('Storage').write('alarm.json',"[]")
} else {
var t = 3600000*(active[0].hr-hr);
if (t<1000) t=1000;
/* execute alarm at the correct time. We avoid execing immediately
since this code will get called AGAIN when alarm.js is loaded. alarm.js
will then clearInterval() to get rid of this call so it can proceed
normally. */
setTimeout(function() {
load("alarm.js");
},t);
}
}
})()
2 changes: 2 additions & 0 deletions apps/boot/ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
If Debug info is set to 'show' don't move to Terminal if connected!
0.11: Added vibrate as beep workaround
0.12: Add an event on BTN2 to open launcher when no clock detected (fix #147)
0.13: Now automatically load *.boot.js at startup
Move alarm code into alarm.boot.js
26 changes: 4 additions & 22 deletions apps/boot/boot0.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,7 @@ E.setTimeZone(s.timezone);
delete s;
// stop users doing bad things!
global.save = function() { throw new Error("You can't use save() on Bangle.js without overwriting the bootloader!"); }
// check for alarms
var alarms = require('Storage').readJSON('alarm.json',1)||[];
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)+(time.getSeconds()/3600);
if (!require('Storage').read("alarm.js")) {
console.log("No alarm app!");
require('Storage').write('alarm.json',"[]")
} else {
var t = 3600000*(active[0].hr-hr);
if (t<1000) t=1000;
/* execute alarm at the correct time. We avoid execing immediately
since this code will get called AGAIN when alarm.js is loaded. alarm.js
will then clearInterval() to get rid of this call so it can proceed
normally. */
setTimeout(function() {
load("alarm.js");
},t);
}
}
// Load *.boot.js files
var clockApps = require('Storage').list(/\.boot\.js/).map(bootFile=>{
eval(require('Storage').read(bootFile));
});

0 comments on commit e2247b2

Please sign in to comment.