Use Adapter to generalize Buttons beween devices

master
Adam Schmalhofer 2021-11-18 15:43:23 +01:00
parent bc813feaa3
commit 3dba1342e8
1 changed files with 72 additions and 31 deletions

View File

@ -11,8 +11,9 @@ const STATES = {
var counterInterval; var counterInterval;
class State { class State {
constructor (state) { constructor (state, device) {
this.state = state; this.state = state;
this.device = device;
this.next = null; this.next = null;
} }
@ -47,8 +48,8 @@ class State {
} }
class InitState extends State { class InitState extends State {
constructor (time) { constructor (device) {
super(STATES.INIT); super(STATES.INIT, device);
this.timeCounter = parseInt(storage.read(".pomodo") || DEFAULT_TIME, 10); this.timeCounter = parseInt(storage.read(".pomodo") || DEFAULT_TIME, 10);
} }
@ -58,7 +59,7 @@ class InitState extends State {
} }
setButtons () { setButtons () {
setWatch(() => { this.device.setBTN1(() => {
if (this.timeCounter + 300 > 3599) { if (this.timeCounter + 300 > 3599) {
this.timeCounter = 3599; this.timeCounter = 3599;
} else { } else {
@ -67,23 +68,23 @@ class InitState extends State {
this.draw(); this.draw();
}, BTN1, { repeat: true }); });
setWatch(() => { this.device.setBTN3(() => {
if (this.timeCounter - 300 > 0) { if (this.timeCounter - 300 > 0) {
this.timeCounter -= 300; this.timeCounter -= 300;
this.draw(); this.draw();
} }
}, BTN3, { repeat: true }); });
setWatch(() => { this.device.setBTN4(() => {
if (this.timeCounter - 60 > 0) { if (this.timeCounter - 60 > 0) {
this.timeCounter -= 60; this.timeCounter -= 60;
this.draw(); this.draw();
} }
}, BTN4, { repeat: true }); });
setWatch(() => { this.device.setBTN5(() => {
if (this.timeCounter + 60 > 3599) { if (this.timeCounter + 60 > 3599) {
this.timeCounter = 3599; this.timeCounter = 3599;
} else { } else {
@ -92,15 +93,15 @@ class InitState extends State {
this.draw(); this.draw();
}, BTN5, { repeat: true }); });
setWatch(() => { this.device.setBTN2(() => {
this.saveTime(); this.saveTime();
const startedState = new StartedState(this.timeCounter); const startedState = new StartedState(this.timeCounter, this.device);
this.setNext(startedState); this.setNext(startedState);
this.next.go(); this.next.go();
}, BTN2, { repeat: true }); });
} }
draw () { draw () {
@ -112,8 +113,8 @@ class InitState extends State {
} }
class StartedState extends State { class StartedState extends State {
constructor (timeCounter) { constructor (timeCounter, buttons) {
super(STATES.STARTED); super(STATES.STARTED, buttons);
this.timeCounter = timeCounter; this.timeCounter = timeCounter;
} }
@ -137,15 +138,15 @@ class StartedState extends State {
this.draw(); this.draw();
} }
const doneState = new DoneState(); const doneState = new DoneState(this.device);
this.setNext(doneState); this.setNext(doneState);
counterInterval = setInterval(countDown.bind(this), 1000); counterInterval = setInterval(countDown.bind(this), 1000);
} }
} }
class BreakState extends State { class BreakState extends State {
constructor () { constructor (buttons) {
super(STATES.BREAK); super(STATES.BREAK, buttons);
} }
draw () { draw () {
@ -153,32 +154,33 @@ class BreakState extends State {
} }
init () { init () {
const startedState = new StartedState(TIME_BREAK); const startedState = new StartedState(TIME_BREAK, this.device);
this.setNext(startedState); this.setNext(startedState);
this.next.go(); this.next.go();
} }
} }
class DoneState extends State { class DoneState extends State {
constructor () { constructor (device) {
super(STATES.DONE); super(STATES.DONE, device);
} }
setButtons () { setButtons () {
setWatch(() => { this.device.setBTN1(() => {
const initState = new InitState(); const initState = new InitState(this.device);
clearTimeout(this.timeout); clearTimeout(this.timeout);
initState.go(); initState.go();
}, BTN1, { repeat: true }); });
setWatch(() => { this.device.setBTN3(() => {
const breakState = new BreakState(); const breakState = new BreakState(this.device);
clearTimeout(this.timeout); clearTimeout(this.timeout);
breakState.go(); breakState.go();
}, BTN3, { repeat: true }); });
setWatch(() => { this.device.setBTN2(() => {
}, BTN2, { repeat: true }); });
} }
draw () { draw () {
@ -215,6 +217,42 @@ class DoneState extends State {
} }
} }
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 {
setBTN1(callback) { }
setBTN2(callback) {
setWatch(callback, BTN1, { repeat: true });
}
setBTN3(callback) { }
setBTN4(callback) { }
setBTN5(callback) { }
}
function drawCounter (currentValue, x, y) { function drawCounter (currentValue, x, y) {
if (currentValue < 0) { if (currentValue < 0) {
return; return;
@ -249,7 +287,10 @@ function drawCounter (currentValue, x, y) {
} }
function init () { function init () {
const initState = new InitState(); device = (process.env.HWVERSION==1
? new Bangle1()
: new Bangle2());
const initState = new InitState(device);
initState.go(); initState.go();
} }