-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbudik4.ino
361 lines (261 loc) · 8.89 KB
/
budik4.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
#include <SPI.h>
#include "LowPower.h"
#include "U8glib.h"
#include <RotaryEncoder.h>
#include <Wire.h>
#include "RTClib.h"
#include "PinChangeInterrupt.h"
// Encoder
#define ENC_CLK 10
#define ENC_DT 11
#define ENC_SW 12 // Button
static int encLastPosition = 0;
static int encCurrentPosition = 0;
static int encLastButtonPosition = 0;
static int encCurrentButtonPosition = 0;
static int encCurrentRotationMillis = 0;
static int encLastRotationMillis = 0;
static int isTick = 0;
RotaryEncoder *encoder = nullptr;
// Buzzer
#define BEEP 4
boolean isBuzzerShortBeep = false;
unsigned long buzzerAlarmBeepTimer = 0;
// I2C
#define I2C_SUPPLY_PIN 8
// I2C Display
#define DISPLAY_UPD_MILLIS 250
U8GLIB_SSD1306_128X32 u8g;
// I2C RTC
#define RTC_UPD_MILLIS 250
RTC_DS1307 clock;
DateTime clockLastReadingTime;
DateTime clockCurrentReadingTime;
int clockIncorrectReadingCount = 0; // I'm suspicious that on battery it can get stucked
// Alarm and Time
// #define TIME_SET_HOLD_BUTTON_MS 5000 // Hold button to set clock
#define ALARM_SET_HOLD_BUTTON_MS 3000
bool isButtonHoldStart = false;
bool isButtonHoldPersisting = false;
unsigned long buttonHoldPersistingTimer = 0;
bool isTimeSetActive = false;
bool isAlarmTimeSetActive = false;
bool isAlarmActivated = false;
bool isAlarmTriggered = false;
DateTime alarmTime = DateTime(2023, 0, 0, 0, 0, 0);
// Sleep
#define SLEEP_WAKEUP_TIME_MS 15000 // Must be at least 5ms so it will skip sleep
unsigned long sleepWakeupTimer = 0;
// Alarm
enum alarmStatusEnum {
NOT_SET,
POSTPONED,
BEEPING
};
int alarm0[] = { 12, 0 };
alarmStatusEnum alarmStatus = NOT_SET;
void setup() {
// Serial
Serial.begin(115200);
Serial.println("Alarm clock");
// Encoder
encoder = new RotaryEncoder(ENC_CLK, ENC_DT, RotaryEncoder::LatchMode::TWO03);
pinMode(ENC_SW, INPUT_PULLUP); // button
pinMode(ENC_CLK, INPUT_PULLUP); // When no-pullup is set then it drains 1mA instead 0f 0.2mA. I don't know why.
pinMode(ENC_DT, INPUT_PULLUP);
attachPCINT(digitalPinToPCINT(ENC_SW), onEncoderTurned, CHANGE);
attachPCINT(digitalPinToPCINT(ENC_CLK), onEncoderTurned, CHANGE);
attachPCINT(digitalPinToPCINT(ENC_DT), onEncoderTurned, CHANGE);
// Buzzer
pinMode(BEEP, OUTPUT);
// I2C
pinMode(I2C_SUPPLY_PIN, OUTPUT);
digitalWrite(I2C_SUPPLY_PIN, HIGH);
// I2C Display
u8g = U8GLIB_SSD1306_128X32(U8G_I2C_OPT_NONE);
// Je to potreba?
if (u8g.getMode() == U8G_MODE_R3G3B2)
u8g.setColorIndex(255); // white
else if (u8g.getMode() == U8G_MODE_GRAY2BIT)
u8g.setColorIndex(3); // max intensity
else if (u8g.getMode() == U8G_MODE_BW)
u8g.setColorIndex(1); // pixel on
// I2C RTC
clock.begin();
clockLastReadingTime = clock.now();
// Alarm and time
clock.adjust(DateTime(__DATE__, __TIME__)); // Set current computer time
alarmTime = DateTime(__DATE__, __TIME__);
}
void onEncoderTurned(void) {
encoder->tick();
isTick = 1;
}
int timer = millis();
unsigned int loopCount = 0;
void loop() {
// Encored
if (isTick) {
encCurrentPosition = encoder->getPosition();
encCurrentButtonPosition = !digitalRead(ENC_SW);
encCurrentRotationMillis = encoder->getMillisBetweenRotations();
isTick = 0; // Here can be concurrency issue with interrupt, but it is so rare
Serial.println("Button");
}
// Buzzer
// if (isAlarmTriggered && millis() - buzzerAlarmBeepTimer > 1000) {
// tone(BEEP, 20);
// buzzerAlarmBeepTimer = millis();
// }
// else {
// noTone(BEEP);
// }
// if(!isAlarmTriggered) {
// buzzerAlarmBeepTimer = millis();
// }
// Sleep
bool isReasonToNotSleep = encCurrentPosition != encLastPosition || encCurrentButtonPosition != encLastButtonPosition || isAlarmTriggered;
if (isReasonToNotSleep) {
sleepWakeupTimer = millis(); // reset wakeup timer
}
bool isSleepAgain = millis() - sleepWakeupTimer > SLEEP_WAKEUP_TIME_MS;
if (isSleepAgain) {
Serial.println("Power off");
// Off
digitalWrite(I2C_SUPPLY_PIN, LOW);
LowPower.powerDown(SLEEP_4S, ADC_OFF, BOD_OFF);
delay(20); // Rathr add delay because I think that RTC has problem sometimes
// Full wakeup
digitalWrite(I2C_SUPPLY_PIN, HIGH);
clock.begin();
u8g.begin();
return; // To encoder
}
// I2C RTC
if (loopCount % 500 == 0) {
DateTime read = clock.now();
boolean isIncorrect = abs(read.unixtime() - clockCurrentReadingTime.unixtime()) > 3600;
if (isIncorrect && clockIncorrectReadingCount < 5) { // Otherwise let's print incorrect value to display too see that RTC is screwed up
clockIncorrectReadingCount++;
return;
}
clockIncorrectReadingCount = 0;
clockCurrentReadingTime = read;
}
// Time set
// isButtonHoldStart = encCurrentButtonPosition == 1 && encLastButtonPosition == 0;
// if (isButtonHoldStart) {
// buttonHoldPersistingTimer = millis(); // Set timer
// isButtonHoldStart = false;
// isButtonHoldPersisting = true;
// } else if (encCurrentButtonPosition == 0) {
// isButtonHoldPersisting = false;
// } else if (isButtonHoldPersisting && millis() - buttonHoldPersistingTimer > ALARM_SET_HOLD_BUTTON_MS && millis() - buttonHoldPersistingTimer < TIME_SET_HOLD_BUTTON_MS) {
// isAlarmActivated = !isAlarmActivated; // Activate setted alarm
// isBuzzerShortBeep = true;
// } else if (isButtonHoldPersisting && millis() - buttonHoldPersistingTimer > TIME_SET_HOLD_BUTTON_MS) {
// isAlarmActivated = !isAlarmActivated;
// isTimeSetActive = true; // Enter Time editing mode
// isBuzzerShortBeep = true;
// }
// // Enter Alarm set mode
// if (isButtonHoldPersisting) {
// int diffMin = encCurrentPosition - encLastPosition;
// int rotationAvg = (encLastRotationMillis + encCurrentRotationMillis) / 2;
// if (rotationAvg < 20) {
// diffMin *= 3;
// }
// if (rotationAvg < 10) {
// diffMin *= 6;
// }
// alarmTime = DateTime(alarmTime.unixtime() - 60 * diffMin);
// }
// Alarm and Time
isTimeSetActive = encCurrentButtonPosition && encCurrentPosition != encLastPosition;
isAlarmTimeSetActive = !isTimeSetActive && encCurrentPosition != encLastPosition;
if (isTimeSetActive || (isAlarmTimeSetActive && !isAlarmActivated)) {
int diffMin = encCurrentPosition - encLastPosition;
int rotationAvg = (encLastRotationMillis + encCurrentRotationMillis) / 2;
if (rotationAvg < 20) {
diffMin *= 3;
}
if (rotationAvg < 10) {
diffMin *= 6;
}
// Time
if (isTimeSetActive) {
clockCurrentReadingTime = DateTime(clockCurrentReadingTime.unixtime() - 60 * diffMin);
clock.adjust(clockCurrentReadingTime);
}
// Alarm
else if (isAlarmTimeSetActive) {
alarmTime = DateTime(alarmTime.unixtime() - 60 * diffMin);
}
}
// Alarm
if(!encCurrentButtonPosition) {
buttonHoldPersistingTimer = millis();
}
else if(millis() - buttonHoldPersistingTimer > ALARM_SET_HOLD_BUTTON_MS) {
isAlarmActivated = !isAlarmActivated;
buttonHoldPersistingTimer = millis();
isAlarmTriggered = false;
}
if(isAlarmActivated && !isAlarmTriggered) {
isAlarmTriggered = clockCurrentReadingTime.hour() == alarmTime.hour() && clockCurrentReadingTime.minute() == alarmTime.minute();
}
if(!isAlarmActivated && isAlarmTriggered) {
isAlarmTriggered = false;
}
// Display
bool isDisplayUpdate = clockCurrentReadingTime.unixtime() != clockLastReadingTime.unixtime() || isReasonToNotSleep || isButtonHoldPersisting;
if (isDisplayUpdate) { // Pozor, otestovat co udela display
Serial.println("Display update.");
u8g.firstPage();
int failsafeCount = 0;
do {
String s;
if (!isAlarmTimeSetActive) {
u8g.setFont(u8g_font_osr29r);
u8g.setPrintPos(9, 31);
s = getTwoDecimalStr(clockCurrentReadingTime.hour()) + getDelimiterOrEmpty(clockCurrentReadingTime.second()) + getTwoDecimalStr(clockCurrentReadingTime.minute());
if (isAlarmActivated) {
s += "'";
}
} else {
u8g.setFont(u8g_font_osr21r);
u8g.setPrintPos(23, 23);
s = getTwoDecimalStr(alarmTime.hour()) + ":" + getTwoDecimalStr(alarmTime.minute());
}
// String s = String(encoder->getMillisBetweenRotations());
u8g.println(s);
if(failsafeCount++ > 100) {
break; // I'm suspicious that Arduino is getting stuck becouse of infinite loop here
}
} while (u8g.nextPage());
}
// Finalize
encLastPosition = encCurrentPosition;
encLastButtonPosition = encCurrentButtonPosition;
encLastRotationMillis = encCurrentRotationMillis;
clockLastReadingTime = clockCurrentReadingTime;
loopCount++;
}
String getTwoDecimalStr(int i) {
char out[5];
if (i < 10) {
snprintf(out, 5, "0%d", i);
} else {
snprintf(out, 5, "%d", i);
}
Serial.println(i);
Serial.println(out);
return out;
}
String getDelimiterOrEmpty(int seconds) {
if (seconds % 2 == 0) {
return " ";
} else {
return ":";
}
}