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 {
|
2020-05-23 21:32:33 +00:00
|
|
|
constructor (state) {
|
|
|
|
this.state = state;
|
|
|
|
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 {
|
2020-05-23 21:32:33 +00:00
|
|
|
constructor (time) {
|
|
|
|
super(STATES.INIT);
|
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 () {
|
|
|
|
setWatch(() => {
|
|
|
|
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
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
}, BTN1, { repeat: true });
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
setWatch(() => {
|
|
|
|
if (this.timeCounter - 300 > 0) {
|
|
|
|
this.timeCounter -= 300;
|
|
|
|
this.draw();
|
|
|
|
}
|
|
|
|
}, BTN3, { repeat: true });
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
setWatch(() => {
|
|
|
|
if (this.timeCounter - 60 > 0) {
|
|
|
|
this.timeCounter -= 60;
|
|
|
|
this.draw();
|
|
|
|
}
|
|
|
|
}, BTN4, { repeat: true });
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
setWatch(() => {
|
|
|
|
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
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
}, BTN5, { repeat: true });
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
setWatch(() => {
|
|
|
|
this.saveTime();
|
|
|
|
const startedState = new StartedState(this.timeCounter);
|
|
|
|
|
|
|
|
this.setNext(startedState);
|
|
|
|
this.next.go();
|
|
|
|
}, BTN2, { repeat: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
constructor (timeCounter) {
|
|
|
|
super(STATES.STARTED);
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
this.timeCounter = timeCounter;
|
|
|
|
}
|
|
|
|
|
|
|
|
draw () {
|
|
|
|
drawCounter(this.timeCounter, 120, 120);
|
|
|
|
}
|
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
|
|
|
|
|
|
|
const doneState = new DoneState();
|
|
|
|
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 {
|
|
|
|
constructor () {
|
|
|
|
super(STATES.BREAK);
|
|
|
|
}
|
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 () {
|
|
|
|
const startedState = new StartedState(TIME_BREAK);
|
2019-11-14 19:45:04 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
this.setNext(startedState);
|
|
|
|
this.next.go();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
class DoneState extends State {
|
|
|
|
constructor () {
|
|
|
|
super(STATES.DONE);
|
|
|
|
}
|
|
|
|
|
|
|
|
setButtons () {
|
|
|
|
setWatch(() => {
|
|
|
|
const initState = new InitState();
|
|
|
|
clearTimeout(this.timeout);
|
|
|
|
initState.go();
|
|
|
|
}, BTN1, { repeat: true });
|
|
|
|
|
|
|
|
setWatch(() => {
|
|
|
|
const breakState = new BreakState();
|
|
|
|
clearTimeout(this.timeout);
|
|
|
|
breakState.go();
|
|
|
|
}, BTN3, { repeat: true });
|
|
|
|
|
|
|
|
setWatch(() => {
|
|
|
|
}, BTN2, { repeat: true });
|
|
|
|
}
|
|
|
|
|
|
|
|
draw () {
|
|
|
|
g.clear();
|
|
|
|
g.setFont("6x8", 2);
|
|
|
|
g.setFontAlign(0, 0, 3);
|
|
|
|
g.drawString("AGAIN", 230, 50);
|
|
|
|
g.drawString("BREAK", 230, 190);
|
|
|
|
g.setFont("Vector", 45);
|
|
|
|
g.setFontAlign(-1, -1);
|
|
|
|
|
|
|
|
g.drawString('You\nare\na\nhero!', 50, 40);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
x = x || 120;
|
|
|
|
y = y || 120;
|
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 () {
|
2020-05-23 21:32:33 +00:00
|
|
|
const initState = new InitState();
|
|
|
|
initState.go();
|
2019-11-14 19:45:04 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 16:25:31 +00:00
|
|
|
init();
|