-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdigitalClock.ino
296 lines (225 loc) · 6.47 KB
/
digitalClock.ino
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/* Project by: Abtin Ortgoli
Course: CSCE 3612
*/
#include <TFT_eSPI.h>
#include <SPI.h>
#include"LIS3DHTR.h"
#define LCD_BACKLIGHT (72Ul)
#define TFT_GREY 0x5AEB
#define BUZZER_DURATION 100
#define BLUE_LED_DURATION 100
#define ACCLRM_PROCESS_INTERVAL 100
#define INTERRUPT_DEBOUCE 100
#define LCD_SLEEP_TIMEOUT 5000
#define ACCLRM_SLEEP_THRESHOLD 0.05
#define ACCLRM_X_WAKE_THRESHOLD_MIN 0.39
#define ACCLRM_X_WAKE_THRESHOLD_MAX 0.48
TFT_eSPI tft = TFT_eSPI();
LIS3DHTR<TwoWire> acclrm;
uint32_t digital_clock_should_update_at = 0;
uint32_t acclrm_values_should_processed_at = 0;
uint32_t lcd_backlight_should_turn_off_at = 0;
uint32_t buzzing_started_at = 0;
uint32_t blue_led_turned_on_at = 0;
uint32_t wio_a_interrupted_at = 0;
uint32_t wio_b_interrupted_at = 0;
double acclrm_x_last = 0;
double acclrm_y_last = 0;
bool should_force_redraw_digital_clock = false;
static uint8_t conv2d(const char* p);
uint8_t hh = conv2d(__TIME__);
uint8_t mm = conv2d(__TIME__ + 3);
uint8_t ss = conv2d(__TIME__ + 6);
// set the time to anything desire
//uint8_t hh = 00;
//uint8_t mm = 00;
//uint8_t ss = 00;
unsigned int colour = 0;
void setup(void) {
Serial.begin(9600);
tft.init();
tft.setRotation(3);
tft.setTextSize(6);
tft.setTextColor(TFT_BLACK, TFT_GREEN);
tft.fillScreen(TFT_GREEN);
tft.setTextColor(TFT_BLACK);
acclrm.begin(Wire1);
if (!acclrm) {
while(1);
}
acclrm.setOutputDataRate(LIS3DHTR_DATARATE_25HZ);
acclrm.setFullScaleRange(LIS3DHTR_RANGE_2G);
digital_clock_should_update_at = millis() + 1000;
pinMode(LED_BUILTIN, OUTPUT);
pinMode(WIO_BUZZER, OUTPUT);
pinMode(WIO_KEY_A, INPUT);
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(LCD_BACKLIGHT, HIGH);
attachInterrupt(digitalPinToInterrupt(WIO_KEY_A), wio_key_a_handler, RISING);
attachInterrupt(digitalPinToInterrupt(WIO_KEY_B), wio_key_b_handler, RISING);
}
void wio_key_a_handler() {
if (wio_a_interrupted_at + INTERRUPT_DEBOUCE < millis()) {
wio_a_interrupted_at = millis();
should_force_redraw_digital_clock = true;
change_minute();
turn_buzzer_on();
}
}
void wio_key_b_handler() {
if (wio_b_interrupted_at + INTERRUPT_DEBOUCE < millis()) {
wio_b_interrupted_at = millis();
should_force_redraw_digital_clock = true;
change_hour();
turn_buzzer_on();
}
}
void loop() {
if (should_force_redraw_digital_clock == true) {
should_force_redraw_digital_clock = false;
draw_digital_clock_v2();
}
watch_buzzer();
watch_blue_led();
watch_lcd();
process_acclrm_values();
update_and_draw_digital_clock();
}
void watch_buzzer() {
if (buzzing_started_at == 0) {
return;
}
if (buzzing_started_at + BUZZER_DURATION < millis()) {
turn_buzzer_off();
}
}
void watch_blue_led() {
if (blue_led_turned_on_at == 0) {
return;
}
if (blue_led_turned_on_at + BLUE_LED_DURATION < millis()) {
turn_blue_led_off();
}
}
void watch_lcd() {
if (lcd_backlight_should_turn_off_at == 0) {
return;
}
if (lcd_backlight_should_turn_off_at < millis()) {
lcd_turn_off();
}
}
void turn_blue_led_on() {
blue_led_turned_on_at = millis();
digitalWrite(LED_BUILTIN, HIGH);
}
void turn_blue_led_off() {
blue_led_turned_on_at = 0;
digitalWrite(LED_BUILTIN, LOW);
}
void turn_buzzer_on() {
buzzing_started_at = millis();
analogWrite(WIO_BUZZER, 128);
}
void turn_buzzer_off() {
buzzing_started_at = 0;
analogWrite(WIO_BUZZER, 0);
}
void schedule_lcd_turn_off() {
lcd_backlight_should_turn_off_at = millis() + LCD_SLEEP_TIMEOUT;
}
void cancel_schedule_lcd_turn_off() {
lcd_backlight_should_turn_off_at = 0;
}
void lcd_turn_on() {
digitalWrite(LCD_BACKLIGHT, HIGH);
}
void lcd_turn_off() {
digitalWrite(LCD_BACKLIGHT, LOW);
}
bool isLcdOn() {
return digitalRead(LCD_BACKLIGHT) == HIGH;
}
bool isLcdOffScheduled() {
return lcd_backlight_should_turn_off_at != 0;
}
void process_acclrm_values() {
if (acclrm_values_should_processed_at < millis()) {
acclrm_values_should_processed_at = millis() + ACCLRM_PROCESS_INTERVAL;
double acclrm_x_current = acclrm.getAccelerationX();
double acclrm_y_current = acclrm.getAccelerationY();
double acclrm_x_delta = abs(acclrm_x_current - acclrm_x_last);
double acclrm_y_delta = abs(acclrm_y_current - acclrm_y_last);
if (acclrm_x_current > ACCLRM_X_WAKE_THRESHOLD_MIN && acclrm_x_current < ACCLRM_X_WAKE_THRESHOLD_MAX) {
if (isLcdOffScheduled()) {
cancel_schedule_lcd_turn_off();
}
if (!isLcdOn()) {
lcd_turn_on();
}
} else if (acclrm_x_delta < ACCLRM_SLEEP_THRESHOLD && acclrm_y_delta < ACCLRM_SLEEP_THRESHOLD) {
if (isLcdOn() && !isLcdOffScheduled()) {
schedule_lcd_turn_off();
}
}
acclrm_x_last = acclrm_x_current;
acclrm_y_last = acclrm_y_current;
}
}
void change_hour() {
hh++;
if (hh > 23) {
hh = 0;
}
}
void change_minute() {
mm++;
if (mm > 59) {
mm = 0;
}
}
void update_and_draw_digital_clock() {
if (digital_clock_should_update_at < millis()) {
digital_clock_should_update_at = millis() + 1000;
turn_blue_led_on();
ss++;
if (ss == 60) {
ss = 0;
mm++;
if (mm > 59) {
mm = 0;
hh++;
if (hh > 23) {
hh = 0;
}
}
}
draw_digital_clock_v2();
}
}
void draw_digital_clock_v2() {
tft.fillScreen(TFT_GREEN);
tft.setCursor(18,97.6);
if (hh < 10) {
tft.print("0");
}
tft.printf("%d", hh);
tft.print(":");
if (mm < 10) {
tft.print("0");
}
tft.printf("%d", mm);
tft.print(":");
if (ss < 10) {
tft.print("0");
}
tft.printf("%d", ss);
}
// function to extract numbers from compile time string
static uint8_t conv2d(const char* p) {
uint8_t v = 0;
if ('0' <= *p && *p <= '9') {
v = *p - '0';
}
return 10 * v + *++p - '0';
}