-
Notifications
You must be signed in to change notification settings - Fork 0
/
states.cpp
145 lines (110 loc) · 2.97 KB
/
states.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "states.h"
#include "pomodoro.h"
#define WORK_MINUTES 25
#define WORK_SECONDS 0
#define BREAK_MINUTES 5
#define BREAK_SECONDS 0
#define MILLIS_IN_SECOND 1000
#define MILLIS_TO_BLINK 1000
// -------------------------------------------------------------------------
PomodoroState::PomodoroState(Pomodoro* pomodoro) {
this->pomodoro = pomodoro;
}
void PomodoroState::buttonPressed() {
}
void PomodoroState::time(int millis) {
}
void PomodoroState::enter() {
}
// -------------------------------------------------------------------------
Initialize::Initialize(Pomodoro* pomodoro) : PomodoroState(pomodoro) {
}
void Initialize::enter() {
pomodoro->offMain();
pomodoro->offButtonLed();
pomodoro->show();
}
void Initialize::buttonPressed() {
pomodoro->prepare();
}
// -------------------------------------------------------------------------
Prepare::Prepare(Pomodoro* pomodoro) : PomodoroState(pomodoro) {
}
void Prepare::enter() {
pomodoro->show(WORK_MINUTES, WORK_SECONDS);
}
void Prepare::buttonPressed() {
pomodoro->work();
}
// -------------------------------------------------------------------------
void AbstractTimedPomodoroState::init(int minutes, int seconds) {
this->millisCounter = 0;
this->minutes = minutes;
this->seconds = seconds;
}
bool AbstractTimedPomodoroState::elapsed(int millis) {
millisCounter += millis;
if (millisCounter >= MILLIS_IN_SECOND) {
millisCounter = 0;
seconds--;
if (seconds < 0) {
seconds = 59;
minutes--;
}
}
return minutes < 0;
}
AbstractTimedPomodoroState::AbstractTimedPomodoroState(Pomodoro* pomodoro) : PomodoroState(pomodoro) {
}
// -------------------------------------------------------------------------
Work::Work(Pomodoro* pomodoro) : AbstractTimedPomodoroState(pomodoro) {
}
void Work::enter() {
init(WORK_MINUTES, WORK_SECONDS);
pomodoro->onMain();
}
void Work::buttonPressed() {
pomodoro->initial();
}
void Work::time(int millis) {
if (elapsed(millis)) {
pomodoro->finished();
} else {
pomodoro->show(minutes, seconds);
}
}
// -------------------------------------------------------------------------
Finished::Finished(Pomodoro* pomodoro) : PomodoroState(pomodoro) {
}
void Finished::enter() {
millisCounter = 0;
pomodoro->show(BREAK_MINUTES, BREAK_SECONDS);
pomodoro->blinkMain();
}
void Finished::buttonPressed() {
pomodoro->intermission();
}
void Finished::time(int millis) {
millisCounter += millis;
if (millisCounter >= MILLIS_TO_BLINK) {
millisCounter = 0;
pomodoro->blinkMain();
}
}
// -------------------------------------------------------------------------
Intermission::Intermission(Pomodoro* pomodoro) : AbstractTimedPomodoroState(pomodoro) {
}
void Intermission::enter() {
init(BREAK_MINUTES, BREAK_SECONDS);
pomodoro->offMain();
}
void Intermission::buttonPressed() {
pomodoro->initial();
}
void Intermission::time(int millis) {
if (elapsed(millis)) {
pomodoro->initial();
} else {
pomodoro->show(minutes, seconds);
}
}