Skip to content

Physical Button Can Stop an Alarm #3753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/sched/ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@
0.28: Added an icon for disabled events
0.29: Improve clkinfo startup time by 10ms
0.30: Fix possible bug in toggling an alarm to on, from clkinfo
0.31: Pressing BTN1 behaves the same as pressing Stop
1 change: 1 addition & 0 deletions apps/sched/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Global Settings
- `Buzz Count` - The number of buzzes before the watch goes silent, or "forever" to buzz until stopped.
- `Buzz Interval` - The interval between one buzz and the next
- `Default Alarm/Timer Pattern` - Default vibration pattern for newly created alarms/timers
- `BTN1 to Stop` - If `Yes` a button press will register the same as pressing `Stop`.

Internals / Library
-------------------
Expand Down
3 changes: 2 additions & 1 deletion apps/sched/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,8 @@ exports.getSettings = function () {
buzzCount: 10,
buzzIntervalMillis: 3000, // 3 seconds
defaultAlarmPattern: "::",
defaultTimerPattern: "::"
defaultTimerPattern: "::",
btnToStop: false
},
require("Storage").readJSON("sched.settings.json", true) || {}
);
Expand Down
2 changes: 1 addition & 1 deletion apps/sched/metadata.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "sched",
"name": "Scheduler",
"version": "0.30",
"version": "0.31",
"description": "Scheduling library for alarms and timers",
"icon": "app.png",
"type": "scheduler",
Expand Down
99 changes: 97 additions & 2 deletions apps/sched/sched.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,100 @@
// Chances are boot0.js got run already and scheduled *another*
// 'load(sched.js)' - so let's remove it first!

function showPromptBtnCancel(msg,options) {
if (!options) options={};
if (!options.buttons)
options.buttons = {"Yes":true,"No":false};
var btns = Object.keys(options.buttons);
var btnPos;
function draw(highlightedButton) {
g.reset().setFont("6x8:2").setFontAlign(0,-1);
var Y = Bangle.appRect.y;
var W = g.getWidth(), H = g.getHeight()-Y, FH=g.getFontHeight();
var titleLines = g.wrapString(options.title, W-2);
var msgLines = g.wrapString(msg||"", W-2);
var y = Y + (H + (titleLines.length - msgLines.length)*FH )/2 - 24;
if (options.img) {
var im = g.imageMetrics(options.img);
g.drawImage(options.img,(W-im.width)/2,y - im.height/2);
y += 4+im.height/2;
}
if (titleLines)
g.setColor(g.theme.fgH).setBgColor(g.theme.bgH).
clearRect(0,Y,W-1,Y+4+titleLines.length*FH).
drawString(titleLines.join("\n"),W/2,Y+2);
g.setColor(g.theme.fg).setBgColor(g.theme.bg).
drawString(msgLines.join("\n"),W/2,y);
y += msgLines.length*FH+32;

var buttonWidths = 0;
var buttonPadding = 24;
g.setFontAlign(0,0);
btns.forEach(btn=>buttonWidths += buttonPadding+g.stringWidth(btn));
if (buttonWidths>W) { // if they don't fit, use smaller font
g.setFont("6x8");
buttonWidths = 0;
btns.forEach(btn=>buttonWidths += buttonPadding+g.stringWidth(btn));
}
var x = (W-buttonWidths)/2;
btnPos = [];
btns.forEach((btn,idx)=>{
var w = g.stringWidth(btn);
x += (buttonPadding+w)/2;
var bw = 6+w/2;
var poly = [x-bw,y-16,
x+bw,y-16,
x+bw+4,y-12,
x+bw+4,y+12,
x+bw,y+16,
x-bw,y+16,
x-bw-4,y+12,
x-bw-4,y-12,
x-bw,y-16];
btnPos.push({x1:x-bw-buttonPadding/2, x2:x+bw+buttonPadding/2,
y1:y-30, y2:y+30,
poly: poly});
g.setColor(idx===highlightedButton ? g.theme.bgH : g.theme.bg2).fillPoly(poly).
setColor(idx===highlightedButton ? g.theme.fgH : g.theme.fg2).drawPoly(poly).drawString(btn,x,y+1);
x += (buttonPadding+w)/2;
});
Bangle.setLCDPower(1); // ensure screen is on
}
g.reset().clearRect(Bangle.appRect); // clear screen
if (!msg) {
Bangle.setUI(); // remove watches
return Promise.resolve();
}
draw();
return new Promise(resolve=>{
Bangle.setUI({mode:"custom", remove: options.remove, redraw: draw, back:options.back,
btn: () => { // Handle physical buttons explicitly
showPromptBtnCancel();
resolve(options.buttons.No);
},
touch:(_,e)=>{
btnPos.forEach((b,i)=>{
if (e.x > b.x1 && e.x < b.x2 &&
e.y > b.y1 && e.y < b.y2) {
draw(i); // highlighted button
g.flip(); // write to screen
showPromptBtnCancel(); // remove
resolve(options.buttons[btns[i]]);
}
});
}});
});
}

function showCustomPrompt(message, options, btnToStop) {
const BANGLEJS2 = process.env.HWVERSION==2;
if (BANGLEJS2 && btnToStop) {
return showPromptBtnCancel(message, options);
} else {
return E.showPrompt(message, options);
}
}

if (Bangle.SCHED) {
clearInterval(Bangle.SCHED);
delete Bangle.SCHED;
Expand All @@ -25,10 +120,10 @@ function showAlarm(alarm) {

let buzzCount = settings.buzzCount;

E.showPrompt(message, {
showCustomPrompt(message, {
title: alarm.timer ? /*LANG*/"TIMER!" : /*LANG*/"ALARM!",
buttons: { /*LANG*/"Snooze": true, /*LANG*/"Stop": false } // default is sleep so it'll come back in some mins
}).then(function (sleep) {
}, settings.btnToStop).then(function (sleep) {
buzzCount = 0;

if (sleep) {
Expand Down
18 changes: 15 additions & 3 deletions apps/sched/settings.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
(function (back) {
let settings = require("sched").getSettings();
const BANGLEJS2 = process.env.HWVERSION == 2;

E.showMenu({
let menu = {
"": { "title": /*LANG*/"Scheduler" },

"< Back": () => back(),
Expand Down Expand Up @@ -70,10 +71,21 @@
settings.defaultAlarmPattern = v;
require("sched").setSettings(settings);
}),

/*LANG*/"Default Timer Pattern": require("buzz_menu").pattern(settings.defaultTimerPattern, v => {
settings.defaultTimerPattern = v;
require("sched").setSettings(settings);
})
});
};

if (BANGLEJS2) {
menu[/*LANG*/"BTN1 to Stop"] = {
value: settings.btnToStop,
onchange: v => {
settings.btnToStop = v;
require("sched").setSettings(settings);
}
};
}

E.showMenu(menu);
})
1 change: 1 addition & 0 deletions typescript/types/sched.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ declare module Sched {
buzzIntervalMillis: number,
defaultAlarmPattern: string,
defaultTimerPattern: string,
btnToStop: boolean,
};

function getAlarms(): Sched[];
Expand Down