-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
129 lines (110 loc) · 3.13 KB
/
main.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
#include "MicroBit.h"
MicroBit uBit;
#define INTERVAL 60000 // [ms]
enum State {
IDLE,
RUNNING,
DONE,
PAUSE
};
static struct {
enum State state;
unsigned long t_last;
unsigned long t_actual;
int pixel; // actual pixel
} AppState;
struct Pixel {
int x;
int y;
};
struct Pixel getPixel(int index)
{
return (struct Pixel) {
.x = index / 5,
.y = index % 5
};
}
void beep()
{
uBit.io.P0.setAnalogValue(511);
uBit.io.P0.setAnalogPeriod(1);
uBit.sleep(90);
uBit.io.P0.setAnalogPeriod(0);
}
void beep3x()
{
beep();
uBit.sleep(1000);
beep();
uBit.sleep(1000);
beep();
}
void onButton(MicroBitEvent e)
{
if (e.value == MICROBIT_BUTTON_EVT_CLICK) {
if (e.source == MICROBIT_ID_BUTTON_B) {
if (AppState.state == IDLE) {
beep();
AppState.state = RUNNING;
} else if (AppState.state == RUNNING) {
beep();
AppState.state = PAUSE;
} else if (AppState.state == PAUSE) {
beep();
AppState.state = RUNNING;
}
} else if (e.source == MICROBIT_ID_BUTTON_A) {
if (AppState.state == PAUSE || AppState.state == DONE) {
beep();
uBit.display.clear();
AppState.t_last = 0;
AppState.t_actual = 0;
AppState.pixel = 0;
AppState.state = IDLE;
}
}
}
}
int main()
{
uBit.init();
uBit.display.setDisplayMode(DISPLAY_MODE_GREYSCALE);
uBit.display.scroll("Pomodoro", 75);
uBit.messageBus.listen(MICROBIT_ID_BUTTON_A, MICROBIT_EVT_ANY, onButton);
uBit.messageBus.listen(MICROBIT_ID_BUTTON_B, MICROBIT_EVT_ANY, onButton);
/* General timer */
unsigned long t_last = uBit.systemTime();
while(1) {
unsigned long t_actual = uBit.systemTime();
unsigned long t_dt = t_actual - t_last;
t_last = t_actual;
if (AppState.state == RUNNING) {
AppState.t_actual += t_dt;
/* New pixel */
if (AppState.t_actual - AppState.t_last > INTERVAL) {
struct Pixel p = getPixel(AppState.pixel);
uBit.display.image.setPixelValue(p.y, p.x, 255);
AppState.pixel++;
AppState.t_last = AppState.t_actual;
if (AppState.pixel == 25) {
AppState.state = DONE;
beep3x();
}
}
}
if (AppState.state != IDLE && AppState.state != DONE) {
int brightness = 0;
if (AppState.state != PAUSE)
brightness = t_actual % 2000 > 1000 ? 0 : 32; // ~= 1 sec
struct Pixel p = getPixel(AppState.pixel);
uBit.display.image.setPixelValue(p.y, p.x, brightness);
}
if (AppState.state == DONE) {
for (int i = 0; i < 25; i++) {
struct Pixel p = getPixel(i);
uBit.display.image.setPixelValue(p.y, p.x, t_actual % 1000 > 500 ? 0 : 255);
}
}
uBit.sleep(10); // [ms]
}
}