2022-04-04 14:49:45 +00:00
|
|
|
Sched: Scheduling library for alarms and timers
|
|
|
|
====================================
|
|
|
|
|
|
|
|
This provides boot code, a library and tools for alarms and timers.
|
|
|
|
|
|
|
|
Other apps can use this to provide alarm functionality.
|
|
|
|
|
|
|
|
App
|
|
|
|
---
|
|
|
|
|
2022-04-20 12:13:33 +00:00
|
|
|
The **Alarms & Timers** app allows you to add/modify any running alarms and timers.
|
2022-04-04 14:49:45 +00:00
|
|
|
|
2022-04-20 12:13:33 +00:00
|
|
|
Global Settings
|
|
|
|
---------------
|
|
|
|
|
|
|
|
- `Unlock at Buzz` - If `Yes` the alarm/timer will unlock the watch
|
2024-02-28 22:35:06 +00:00
|
|
|
- `Delete Expired Timers` - Default for whether expired timers are removed after firing.
|
2022-04-20 12:13:33 +00:00
|
|
|
- `Default Auto Snooze` - Default _Auto Snooze_ value for newly created alarms (_Alarms_ only)
|
|
|
|
- `Default Snooze` - Default _Snooze_ value for newly created alarms/timers
|
2024-02-28 22:35:11 +00:00
|
|
|
- `Buzz Count` - The number of buzzes before the watch goes silent, or "forever" to buzz until stopped.
|
2022-04-20 12:13:33 +00:00
|
|
|
- `Buzz Interval` - The interval between one buzz and the next
|
|
|
|
- `Default Alarm/Timer Pattern` - Default vibration pattern for newly created alarms/timers
|
2022-04-04 14:49:45 +00:00
|
|
|
|
|
|
|
Internals / Library
|
|
|
|
-------------------
|
|
|
|
|
|
|
|
Alarms are stored in an array in `sched.json`, and take the form:
|
|
|
|
|
|
|
|
```
|
|
|
|
{
|
|
|
|
id : "mytimer", // optional ID for this alarm/timer, so apps can easily find *their* timers
|
2022-04-06 10:45:14 +00:00
|
|
|
appid : "myappid", // optional app ID for alarms that you set/use for your app
|
2022-04-04 14:49:45 +00:00
|
|
|
on : true, // is the alarm enabled?
|
|
|
|
t : 23400000, // Time of day since midnight in ms (if a timer, this is set automatically when timer starts)
|
|
|
|
dow : 0b1111111, // Binary encoding for days of the week to run alarm on
|
|
|
|
// SUN = 1
|
|
|
|
// MON = 2
|
|
|
|
// TUE = 4
|
|
|
|
// WED = 8
|
|
|
|
// THU = 16
|
|
|
|
// FRI = 32
|
2022-04-24 14:25:36 +00:00
|
|
|
// SAT = 64
|
2022-04-04 14:49:45 +00:00
|
|
|
|
|
|
|
date : "2022-04-04", // OPTIONAL date for the alarm, in YYYY-MM-DD format
|
|
|
|
// eg (new Date()).toISOString().substr(0,10)
|
2022-04-06 10:45:14 +00:00
|
|
|
msg : "Eat food", // message to display.
|
2022-04-07 17:47:45 +00:00
|
|
|
last : 0, // last day of the month we alarmed on - so we don't alarm twice in one day! (No change from 0 on timers)
|
2023-01-09 04:04:13 +00:00
|
|
|
rp : true, // repeat the alarm every day? If date is given, pass an object instead of a boolean,
|
|
|
|
// e.g. repeat every 2 months: { interval: "month", num: 2 }.
|
|
|
|
// Supported intervals: day, week, month, year
|
2022-04-04 14:49:45 +00:00
|
|
|
vibrate : "...", // OPTIONAL pattern of '.', '-' and ' ' to use for when buzzing out this alarm (defaults to '..' if not set)
|
|
|
|
hidden : false, // OPTIONAL if false, the widget should not show an icon for this alarm
|
|
|
|
as : false, // auto snooze
|
|
|
|
timer : 5*60*1000, // OPTIONAL - if set, this is a timer and it's the time in ms
|
2022-06-11 13:36:48 +00:00
|
|
|
del : false, // OPTIONAL - if true, delete the timer after expiration
|
2022-04-04 14:49:45 +00:00
|
|
|
js : "load('myapp.js')" // OPTIONAL - a JS command to execute when the alarm activates (*instead* of loading 'sched.js')
|
|
|
|
// when this code is run, you're responsible for setting alarm.on=false (or removing the alarm)
|
2022-04-06 10:45:14 +00:00
|
|
|
data : { ... } // OPTIONAL - your app can store custom data in here if needed (don't store a lot of data here)
|
2022-04-04 14:49:45 +00:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
The [`sched` library](https://github.com/espruino/BangleApps/blob/master/apps/sched/lib.js) contains
|
|
|
|
a few helpful functions for getting/setting alarms and timers, but is intentionally sparse so as not to
|
|
|
|
use too much RAM.
|
|
|
|
|
|
|
|
It can be used as follows:
|
|
|
|
|
|
|
|
```
|
2022-04-20 12:12:46 +00:00
|
|
|
// Get a new alarm with default values
|
|
|
|
let alarm = require("sched").newDefaultAlarm();
|
|
|
|
|
|
|
|
// Get a new timer with default values
|
|
|
|
let timer = require("sched").newDefaultTimer();
|
|
|
|
|
2022-09-09 08:37:10 +00:00
|
|
|
// Add/update an existing alarm (using fields from the object shown above)
|
|
|
|
require("sched").setAlarm("mytimer", { // as a timer
|
2022-04-04 14:49:45 +00:00
|
|
|
msg : "Wake up",
|
2022-04-20 12:12:46 +00:00
|
|
|
timer : 10 * 60 * 1000 // 10 minutes
|
2022-04-04 14:49:45 +00:00
|
|
|
});
|
2022-09-09 08:37:10 +00:00
|
|
|
require("sched").setAlarm("myalarm", { // as an alarm
|
|
|
|
msg : "Wake up",
|
|
|
|
t : 9 * 3600000 // 9 o'clock (in ms)
|
|
|
|
});
|
|
|
|
require("sched").setAlarm("mydayalarm", { // as an alarm on a date
|
|
|
|
msg : "Wake up",
|
|
|
|
date : "2022-04-04",
|
|
|
|
t : 9 * 3600000 // 9 o'clock (in ms)
|
|
|
|
});
|
|
|
|
|
2022-04-04 14:49:45 +00:00
|
|
|
// Ensure the widget and alarm timer updates to schedule the new alarm properly
|
|
|
|
require("sched").reload();
|
|
|
|
|
|
|
|
// Get the time to the next alarm for us
|
2022-04-20 12:12:46 +00:00
|
|
|
let timeToNext = require("sched").getTimeToAlarm(require("sched").getAlarm("mytimer"));
|
|
|
|
// timeToNext === undefined if no alarm or alarm disabled
|
2022-04-04 14:49:45 +00:00
|
|
|
|
2022-04-20 12:12:46 +00:00
|
|
|
// Delete an alarm
|
2022-04-04 14:49:45 +00:00
|
|
|
require("sched").setAlarm("mytimer", undefined);
|
2022-04-20 12:12:46 +00:00
|
|
|
// Reload after deleting
|
2022-04-04 14:49:45 +00:00
|
|
|
require("sched").reload();
|
|
|
|
|
|
|
|
// Or add an alarm that runs your own code - in this case
|
|
|
|
// loading the settings app. The alarm will not be removed/stopped
|
|
|
|
// automatically.
|
|
|
|
require("sched").setAlarm("customrunner", {
|
2022-04-06 10:45:14 +00:00
|
|
|
appid : "myapp",
|
2022-04-04 14:49:45 +00:00
|
|
|
js : "load('setting.app.js')",
|
2022-04-20 12:12:46 +00:00
|
|
|
timer : 1 * 60 * 1000 // 1 minute
|
2022-04-04 14:49:45 +00:00
|
|
|
});
|
2022-04-06 10:45:14 +00:00
|
|
|
|
|
|
|
// If you have been specifying `appid` you can also find any alarms that
|
|
|
|
// your app has created with the following:
|
2022-04-20 12:12:46 +00:00
|
|
|
require("sched").getAlarms().filter(a => a.appid == "myapp");
|
|
|
|
|
|
|
|
// Get the scheduler settings
|
|
|
|
let settings = require("sched").getSettings();
|
2022-04-04 14:49:45 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
If your app requires alarms, you can specify that the alarms app needs to
|
2022-04-06 10:45:14 +00:00
|
|
|
be installed by adding `"dependencies": {"scheduler":"type"},` to your app's
|
|
|
|
metadata.
|