-
Notifications
You must be signed in to change notification settings - Fork 0
/
m5atom-pomodoro.cpp
125 lines (98 loc) · 2.28 KB
/
m5atom-pomodoro.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
#include <Adafruit_NeoPixel.h>
#include <Arduino.h>
#include <color.h>
#define LED_PIN (27)
#define BUTTON_PIN (39)
#define COLS (5)
#define ROWS (5)
#define ON_DELAY_THRESHHOLD (10)
#define POMO_WORK_MS (47 * 60 * 1000)
#define POMO_BREAK_MS (13 * 60 * 1000)
#define POMO_CYCLE_MS (POMO_WORK_MS + POMO_BREAK_MS)
#define POMO_ALERT_FLASH_ON_MS (20)
#define POMO_ALERT_FLASH_OFF_MS (80)
#define POMO_ALERT_FLASHES (4)
#define POMO_ALERT_BETWEEN_FLASHES_MS (400)
#define POMO_ALERT_REPETITIONS (10)
Adafruit_NeoPixel strip(COLS * ROWS, LED_PIN);
bool was_working;
uint32_t t0;
int flash_color[3];
int flashes;
int flash_groups;
int on_delay;
void setup(void)
{
was_working = true;
t0 = 0;
flashes = 0;
flash_groups = 0;
on_delay = 0;
strip.begin();
pinMode(BUTTON_PIN, INPUT_PULLUP);
}
void loop(void)
{
/* debounce */
if (digitalRead(BUTTON_PIN) == LOW) {
on_delay++;
} else if (on_delay > 0) {
on_delay--;
}
if (flashes) {
flashes--;
/* all on */
strip.clear();
for (int i = 0; i < ROWS * COLS; i++)
strip.setPixelColor(i, flash_color[0], flash_color[1], flash_color[2]);
strip.show();
delay(POMO_ALERT_FLASH_ON_MS);
/* all off */
strip.clear();
strip.show();
delay(POMO_ALERT_FLASH_OFF_MS);
if (!flashes)
delay(POMO_ALERT_BETWEEN_FLASHES_MS);
return;
}
if (flash_groups) {
if (flash_groups > 0)
flash_groups--;
flashes = POMO_ALERT_FLASHES + 1;
return;
}
uint32_t t_raw = millis();
/* button pressed: switch break/work */
if (on_delay >= ON_DELAY_THRESHHOLD) {
on_delay = 0;
t0 = t_raw;
if (was_working) {
t0 -= POMO_WORK_MS;
}
was_working = !was_working;
}
uint32_t t = (t_raw - t0) % POMO_CYCLE_MS;
const bool working = t < POMO_WORK_MS;
if (was_working != working) {
was_working = working;
flash_groups = POMO_ALERT_REPETITIONS;
flash_color[0] = working ? 60 : 0;
flash_color[1] = 20;
flash_color[2] = working ? 0 : 60;
return;
}
uint8_t completion = working
? (ROWS * COLS) * t / POMO_WORK_MS
: (ROWS * COLS) - 1 - (ROWS * COLS) * (t - POMO_WORK_MS) / POMO_BREAK_MS;
strip.clear();
for (int c = 0; c < COLS; c++) {
for (int r = 0; r < ROWS; r++) {
int i = r + c * ROWS;
if (i <= completion) {
strip.setPixelColor(i, working ? 20 : 0, 6, working ? 0 : 20);
}
}
}
strip.show();
delay(10);
}