2019-11-27 08:14:48 +00:00
|
|
|
const storage = require("Storage");
|
|
|
|
|
2019-11-14 19:45:04 +00:00
|
|
|
const DEFAULT_TIME = 1500; // 25m
|
|
|
|
const TIME_BREAK = 300;
|
|
|
|
const STATES = {
|
2020-05-23 21:32:33 +00:00
|
|
|
INIT: 1,
|
|
|
|
STARTED: 2,
|
|
|
|
DONE: 3,
|
|
|
|
BREAK: 4
|
2019-11-14 19:45:04 +00:00
|
|
|
};
|
|
|
|
var counterInterval;
|
|
|
|
|
|
|
|
class State {
|
2021-11-18 14:43:23 +00:00
|
|
|
constructor (state, device) {
|
2020-05-23 21:32:33 +00:00
|
|
|
this.state = state;
|
2021-11-18 14:43:23 +00:00
|
|
|
this.device = device;
|
2020-05-23 21:32:33 +00:00
|
|
|
this.next = null;
|
|
|
|
}
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
setNext (next) {
|
|
|
|
this.next = next;
|
|
|
|
}
|
2019-11-27 09:00:15 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
setButtons () {}
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
clear () {
|
|
|
|
clearWatch();
|
|
|
|
g.clear();
|
|
|
|
g.setFontAlign(0, 0);
|
|
|
|
}
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
draw () {
|
|
|
|
g.clear();
|
|
|
|
}
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
init () { }
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
go (nextState) {
|
|
|
|
if (nextState) {
|
|
|
|
this.next = nextState;
|
2019-11-14 19:45:04 +00:00
|
|
|
}
|
2020-05-23 21:32:33 +00:00
|
|
|
|
|
|
|
this.clear();
|
|
|
|
this.init();
|
|
|
|
this.setButtons();
|
|
|
|
this.draw();
|
|
|
|
}
|
2019-11-14 19:45:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
class InitState extends State {
|
2021-11-18 14:43:23 +00:00
|
|
|
constructor (device) {
|
|
|
|
super(STATES.INIT, device);
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
this.timeCounter = parseInt(storage.read(".pomodo") || DEFAULT_TIME, 10);
|
|
|
|
}
|
2019-11-27 08:14:48 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
saveTime () {
|
|
|
|
storage.write('.pomodo', '' + this.timeCounter);
|
|
|
|
}
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
setButtons () {
|
2021-11-18 14:43:23 +00:00
|
|
|
this.device.setBTN1(() => {
|
2020-05-23 21:32:33 +00:00
|
|
|
if (this.timeCounter + 300 > 3599) {
|
|
|
|
this.timeCounter = 3599;
|
|
|
|
} else {
|
|
|
|
this.timeCounter += 300;
|
|
|
|
}
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
this.draw();
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2021-11-18 14:43:23 +00:00
|
|
|
});
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2021-11-18 14:43:23 +00:00
|
|
|
this.device.setBTN3(() => {
|
2020-05-23 21:32:33 +00:00
|
|
|
if (this.timeCounter - 300 > 0) {
|
|
|
|
this.timeCounter -= 300;
|
|
|
|
this.draw();
|
|
|
|
}
|
2021-11-18 14:43:23 +00:00
|
|
|
});
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2021-11-18 14:43:23 +00:00
|
|
|
this.device.setBTN4(() => {
|
2020-05-23 21:32:33 +00:00
|
|
|
if (this.timeCounter - 60 > 0) {
|
|
|
|
this.timeCounter -= 60;
|
|
|
|
this.draw();
|
|
|
|
}
|
2021-11-18 14:43:23 +00:00
|
|
|
});
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2021-11-18 14:43:23 +00:00
|
|
|
this.device.setBTN5(() => {
|
2020-05-23 21:32:33 +00:00
|
|
|
if (this.timeCounter + 60 > 3599) {
|
|
|
|
this.timeCounter = 3599;
|
|
|
|
} else {
|
|
|
|
this.timeCounter += 60;
|
|
|
|
}
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
this.draw();
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2021-11-18 14:43:23 +00:00
|
|
|
});
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2021-11-18 14:43:23 +00:00
|
|
|
this.device.setBTN2(() => {
|
2020-05-23 21:32:33 +00:00
|
|
|
this.saveTime();
|
2021-11-18 14:43:23 +00:00
|
|
|
const startedState = new StartedState(this.timeCounter, this.device);
|
2020-05-23 21:32:33 +00:00
|
|
|
|
|
|
|
this.setNext(startedState);
|
|
|
|
this.next.go();
|
2021-11-18 14:43:23 +00:00
|
|
|
});
|
2020-05-23 21:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
draw () {
|
|
|
|
g.clear();
|
|
|
|
g.setFontAlign(0, 0); // center font
|
|
|
|
g.setFont("Vector", 50); // vector font, 80px
|
|
|
|
drawCounter(this.timeCounter);
|
|
|
|
}
|
2019-11-14 19:45:04 +00:00
|
|
|
}
|
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
class StartedState extends State {
|
2021-11-18 14:43:23 +00:00
|
|
|
constructor (timeCounter, buttons) {
|
|
|
|
super(STATES.STARTED, buttons);
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
this.timeCounter = timeCounter;
|
|
|
|
}
|
|
|
|
|
|
|
|
draw () {
|
2021-11-18 14:09:00 +00:00
|
|
|
drawCounter(this.timeCounter, g.getWidth() / 2, g.getHeight() / 2);
|
2020-05-23 21:32:33 +00:00
|
|
|
}
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
init () {
|
|
|
|
function countDown () {
|
|
|
|
this.timeCounter--;
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
// Out of time
|
|
|
|
if (this.timeCounter <= 0) {
|
|
|
|
clearInterval(counterInterval);
|
|
|
|
counterInterval = undefined;
|
2019-11-14 19:45:04 +00:00
|
|
|
this.next.go();
|
2020-05-23 21:32:33 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.draw();
|
2019-11-14 19:45:04 +00:00
|
|
|
}
|
2020-05-23 21:32:33 +00:00
|
|
|
|
2021-11-18 14:43:23 +00:00
|
|
|
const doneState = new DoneState(this.device);
|
2020-05-23 21:32:33 +00:00
|
|
|
this.setNext(doneState);
|
|
|
|
counterInterval = setInterval(countDown.bind(this), 1000);
|
|
|
|
}
|
2019-11-14 19:45:04 +00:00
|
|
|
}
|
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
class BreakState extends State {
|
2021-11-18 14:43:23 +00:00
|
|
|
constructor (buttons) {
|
|
|
|
super(STATES.BREAK, buttons);
|
2020-05-23 21:32:33 +00:00
|
|
|
}
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
draw () {
|
|
|
|
g.setFontAlign(0, 0);
|
|
|
|
}
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
init () {
|
2021-11-18 14:43:23 +00:00
|
|
|
const startedState = new StartedState(TIME_BREAK, this.device);
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
this.setNext(startedState);
|
|
|
|
this.next.go();
|
|
|
|
}
|
|
|
|
}
|
2021-11-18 14:43:23 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
class DoneState extends State {
|
2021-11-18 14:43:23 +00:00
|
|
|
constructor (device) {
|
|
|
|
super(STATES.DONE, device);
|
2020-05-23 21:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setButtons () {
|
2021-11-18 14:43:23 +00:00
|
|
|
this.device.setBTN1(() => {
|
|
|
|
});
|
2020-05-23 21:32:33 +00:00
|
|
|
|
2021-11-18 14:43:23 +00:00
|
|
|
this.device.setBTN3(() => {
|
|
|
|
});
|
2020-05-23 21:32:33 +00:00
|
|
|
|
2021-11-18 14:43:23 +00:00
|
|
|
this.device.setBTN2(() => {
|
|
|
|
});
|
2020-05-23 21:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
draw () {
|
|
|
|
g.clear();
|
2021-11-18 15:27:09 +00:00
|
|
|
E.showPrompt("You are a hero!", {
|
|
|
|
buttons : {"AGAIN":1,"BREAK":2}
|
|
|
|
}).then((v) => {
|
|
|
|
var nextSate = (v == 1
|
|
|
|
? new InitState(this.device)
|
|
|
|
: new BreakState(this.device));
|
|
|
|
clearTimeout(this.timeout);
|
|
|
|
nextSate.go();
|
|
|
|
});
|
2020-05-23 21:32:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
init () {
|
|
|
|
|
|
|
|
function buzz () {
|
|
|
|
Bangle.buzz();
|
|
|
|
Bangle.beep(200, 4000)
|
|
|
|
.then(() => new Promise(resolve => setTimeout(resolve, 50)))
|
|
|
|
.then(() => Bangle.beep(200, 3000))
|
|
|
|
.then(() => new Promise(resolve => setTimeout(resolve, 200)))
|
|
|
|
.then(() => Bangle.beep(200, 3000))
|
|
|
|
.then(() => new Promise(resolve => setTimeout(resolve, 300)))
|
|
|
|
.then(() => {
|
|
|
|
Bangle.beep(200, 3000);
|
|
|
|
Bangle.buzz()
|
|
|
|
});
|
2019-11-14 19:45:04 +00:00
|
|
|
}
|
2020-05-23 21:32:33 +00:00
|
|
|
|
|
|
|
buzz();
|
|
|
|
// again, 10 secs later
|
|
|
|
this.timeout = setTimeout(buzz.bind(this), 10000);
|
|
|
|
}
|
2019-11-14 19:45:04 +00:00
|
|
|
}
|
|
|
|
|
2021-11-18 14:43:23 +00:00
|
|
|
class Bangle1 {
|
|
|
|
setBTN1(callback) {
|
|
|
|
setWatch(callback, BTN1, { repeat: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
setBTN2(callback) {
|
|
|
|
setWatch(callback, BTN2, { repeat: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
setBTN3(callback) {
|
|
|
|
setWatch(callback, BTN3, { repeat: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
setBTN4(callback) {
|
|
|
|
setWatch(callback, BTN4, { repeat: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
setBTN5(callback) {
|
|
|
|
setWatch(callback, BTN5, { repeat: true });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class Bangle2 {
|
2021-11-18 15:40:11 +00:00
|
|
|
setBTN1(callback) {
|
|
|
|
Bangle.on('touch', function(zone, e) {
|
|
|
|
if (e.y < g.getHeight() / 2) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-11-18 14:43:23 +00:00
|
|
|
|
|
|
|
setBTN2(callback) {
|
|
|
|
setWatch(callback, BTN1, { repeat: true });
|
|
|
|
}
|
|
|
|
|
2021-11-18 15:40:11 +00:00
|
|
|
setBTN3(callback) {
|
|
|
|
Bangle.on('touch', function(zone, e) {
|
|
|
|
if (e.y > g.getHeight() / 2) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2021-11-18 14:43:23 +00:00
|
|
|
|
|
|
|
setBTN4(callback) { }
|
|
|
|
|
|
|
|
setBTN5(callback) { }
|
|
|
|
}
|
|
|
|
|
2019-11-14 19:45:04 +00:00
|
|
|
function drawCounter (currentValue, x, y) {
|
2020-05-23 21:32:33 +00:00
|
|
|
if (currentValue < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2021-11-18 14:09:00 +00:00
|
|
|
x = x || g.getWidth() / 2;
|
|
|
|
y = y || g.getHeight() / 2;
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
let minutes = 0;
|
|
|
|
let seconds = 0;
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
if (currentValue >= 60) {
|
|
|
|
minutes = Math.floor(currentValue / 60);
|
|
|
|
seconds = currentValue % 60;
|
|
|
|
} else {
|
|
|
|
seconds = currentValue;
|
|
|
|
}
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
let minutesString = '' + minutes;
|
|
|
|
let secondsString = '' + seconds;
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
if (minutes < 10) {
|
|
|
|
minutesString = '0' + minutes;
|
|
|
|
}
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
if (seconds < 10) {
|
|
|
|
secondsString = '0' + seconds;
|
|
|
|
}
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
g.clear();
|
|
|
|
g.drawString(minutesString + ':' + secondsString, x, y);
|
2019-11-14 19:45:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function init () {
|
2021-11-18 14:43:23 +00:00
|
|
|
device = (process.env.HWVERSION==1
|
|
|
|
? new Bangle1()
|
|
|
|
: new Bangle2());
|
|
|
|
const initState = new InitState(device);
|
2020-05-23 21:32:33 +00:00
|
|
|
initState.go();
|
2019-11-14 19:45:04 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 16:25:31 +00:00
|
|
|
init();
|