-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathm5StickC-QlockTwo.ino
405 lines (359 loc) · 11 KB
/
m5StickC-QlockTwo.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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include <M5StickC.h>
#include <WiFi.h>
#include "time.h"
// WiFi credentials
char const *ssid = "ssid";
char const *password = "password";
char const *ntpServer = "pool.ntp.org";
double vbat = 0.0;
int discharge, charge;
char const *weekdays[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
// define what timezone you are in
int timeZone = 8 * 3600; // 8 for UTC/GMT+08:00, https://blog.csdn.net/Naisu_kun/article/details/115627629
// mode
enum Mode
{
Qlock = 0,
Simple,
ScreenOff,
};
Mode currentMode;
long screenOnTime;
long lastRefreshTime;
RTC_TimeTypeDef RTC_TimeStruct;
RTC_DateTypeDef RTC_DateStruct;
uint8_t bat_3[] =
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
0x00, 0x6e, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xdb, 0x00, 0xff,
0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
0x00, 0xdb, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
0x00, 0xb7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xdb,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
};
uint8_t bat_2[] =
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
0x00, 0x6e, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xdb, 0x00, 0xff,
0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
0x00, 0xdb, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
0x00, 0xb7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xdb,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
};
uint8_t bat_1[] =
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
0x00, 0x6e, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xdb, 0x00, 0xff,
0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
0x00, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00,
0x00, 0xb7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0xdb,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff,
};
int QLOCK_GREY = M5.Lcd.color565(40, 40, 40);
void buttons_code()
{
// Button A switch the mode
if (M5.BtnA.wasPressed())
{
switchMode();
}
// Button B doing a time resync if pressed for 2 sec
if (M5.BtnB.pressedFor(2000))
{
timeSync();
}
}
void turnOffScreen()
{
M5.Lcd.writecommand(ST7735_DISPOFF);
M5.Axp.ScreenBreath(0); // Brightness: 7 ~ 16
}
void turnOnScreen()
{
M5.Lcd.writecommand(ST7735_DISPON);
M5.Axp.ScreenBreath(16);
}
void switchMode()
{
currentMode = Mode((currentMode + 1) % 3);
if (currentMode == ScreenOff)
{
turnOffScreen();
}
else
{
turnOnScreen();
screenOnTime = millis();
M5.Lcd.fillScreen(BLACK);
}
}
// Printing time to LCD
void drawSimpleClock()
{
batteryStatus();
M5.Lcd.setRotation(1);
M5.Lcd.setCursor(40, 0, 2);
M5.Lcd.println("Simple Clock");
if (millis() >= lastRefreshTime + 1000)
{
M5.Lcd.setTextSize(2);
M5.Rtc.GetTime(&RTC_TimeStruct);
M5.Rtc.GetData(&RTC_DateStruct);
M5.Lcd.setCursor(27, 20);
M5.Lcd.printf("%02d:%02d:%02d", RTC_TimeStruct.Hours, RTC_TimeStruct.Minutes, RTC_TimeStruct.Seconds);
M5.Lcd.setCursor(27, 55);
M5.Lcd.setTextSize(1);
M5.Lcd.printf("%04d-%02d-%02d %s", RTC_DateStruct.Year, RTC_DateStruct.Month, RTC_DateStruct.Date, weekdays[RTC_DateStruct.WeekDay]);
}
}
void drawQlock()
{
M5.Lcd.setRotation(0);
M5.Rtc.GetTime(&RTC_TimeStruct);
M5.Rtc.GetData(&RTC_DateStruct);
drawQlockPanel(RTC_TimeStruct.Hours, RTC_TimeStruct.Minutes);
M5.Lcd.setCursor(6, 130);
M5.Lcd.setTextSize(1);
M5.Lcd.printf("%02d-%02d %s", RTC_DateStruct.Month, RTC_DateStruct.Date, weekdays[RTC_DateStruct.WeekDay]);
}
// Syncing time from NTP Server
void timeSync()
{
M5.Lcd.setRotation(1);
M5.Lcd.setTextSize(1);
Serial.println("Syncing Time");
Serial.printf("Connecting to %s ", ssid);
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(20, 15);
M5.Lcd.println("connecting WiFi");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println(" CONNECTED");
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(20, 15);
M5.Lcd.println("Connected");
// Set ntp time to local
configTime(timeZone, 0, ntpServer);
// Get local time
struct tm timeInfo;
if (getLocalTime(&timeInfo))
{
// Set RTC time
RTC_TimeTypeDef TimeStruct;
TimeStruct.Hours = timeInfo.tm_hour;
TimeStruct.Minutes = timeInfo.tm_min;
TimeStruct.Seconds = timeInfo.tm_sec;
M5.Rtc.SetTime(&TimeStruct);
RTC_DateTypeDef DateStruct;
DateStruct.WeekDay = timeInfo.tm_wday;
DateStruct.Month = timeInfo.tm_mon + 1;
DateStruct.Date = timeInfo.tm_mday;
DateStruct.Year = timeInfo.tm_year + 1900;
M5.Rtc.SetData(&DateStruct);
Serial.println("Time now matching NTP");
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(20, 15);
M5.Lcd.println("S Y N C");
delay(500);
M5.Lcd.fillScreen(BLACK);
M5.Lcd.setCursor(40, 0, 2);
M5.Lcd.println("Simple Clock");
}
}
const char *clockPanel = "ITLISASAMPM"
"ACQUARTERDC"
"TWENTYFIVEX"
"HALFSTENFTO"
"PASTERUNINE"
"ONESIXTHREE"
"FOURFIVETWO"
"EIGHTELEVEN"
"SEVENTWELVE"
"TENSEOCLOCK";
const int R = 10;
const int C = 11;
const int INT_SIZE = 32;
typedef struct
{
char *word;
uint32_t encoding[4];
} Word;
// 110 characters in total, 4 int32_t needed
Word itis = {(char *)"IT IS", {0x1B, 0, 0, 0}};
Word am = {(char *)"AM", {0x180, 0, 0, 0}};
Word pm = {(char *)"PM", {0x600, 0, 0, 0}};
Word quarter = {(char *)"A QUARTER", {0xFE800, 0, 0, 0}};
Word twenty = {(char *)"TWENTY", {0xFC00000, 0, 0, 0}};
Word mFive = {(char *)"FIVE", {0xF0000000, 0, 0, 0}};
Word half = {(char *)"HALF", {0, 0x1E, 0, 0}};
Word mTen = {(char *)"TEN", {0, 0x1C0, 0, 0}};
Word to = {(char *)"TO", {0, 0xC00, 0, 0}};
Word past = {(char *)"PAST", {0, 0xF000, 0, 0}};
Word nine = {(char *)"NINE", {0, 0x780000, 0, 0}};
Word one = {(char *)"ONE", {0, 0x3800000, 0, 0}};
Word six = {(char *)"SIX", {0, 0x1C000000, 0, 0}};
Word three = {(char *)"THREE", {0, 0xE0000000, 0x3, 0}};
Word four = {(char *)"SIX", {0, 0, 0x3C, 0}};
Word hFive = {(char *)"FIVE", {0, 0, 0x3C0, 0}};
Word two = {(char *)"TWO", {0, 0, 0x1C00, 0}};
Word eight = {(char *)"EIGHT", {0, 0, 0x3E000, 0}};
Word eleven = {(char *)"ELEVEN", {0, 0, 0xFC0000, 0}};
Word seven = {(char *)"SEVEN", {0, 0, 0x1F000000, 0}};
Word twelve = {(char *)"TWELVE", {0, 0, 0xE0000000, 0x7}};
Word hTen = {(char *)"TEN", {0, 0, 0, 0x38}};
Word oclock = {(char *)"O'CLOCK", {0, 0, 0, 0x3F00}};
Word hourWords[] = {twelve, one, two, three, four, hFive, six, seven, eight, nine, hTen, eleven};
int lastUpdateTime = 0;
void drawQlockMinuteDot(int dm) // 0 ~ 4
{
for (int i = 0; i < 4; i++)
{
int xc = 14 + i * 16;
M5.Lcd.drawLine(xc - 1, 120, xc + 1, 120, i < dm ? WHITE : QLOCK_GREY);
}
}
void drawQlockPanel(int h, int m)
{
Word words[10] = {itis};
int p = 1;
words[p++] = h < 12 ? am : pm;
int approxM = m / 5 * 5;
if (approxM == 0)
{
// do nothing, o'clock
}
else if (approxM == 5 || approxM == 55)
{
words[p++] = mFive;
}
else if (approxM == 10 || approxM == 50)
{
words[p++] = mTen;
}
else if (approxM == 15 || approxM == 45)
{
words[p++] = quarter;
}
else if (approxM == 20 || approxM == 40)
{
words[p++] = twenty;
}
else if (approxM == 25 || approxM == 35)
{
words[p++] = twenty;
words[p++] = mFive;
}
else // (approxM == 30)
{
words[p++] = half;
}
if (approxM > 0)
{
if (approxM < 30)
{
words[p++] = past;
}
else
{
words[p++] = to;
h += 1;
}
words[p++] = hourWords[h % 12];
}
else
{
words[p++] = hourWords[h % 12];
words[p++] = oclock;
}
int len = p;
p = 0;
uint32_t mask[4] = {0, 0, 0, 0};
while (p <= len)
{
for (int i = 0; i < 4; i++)
{
mask[i] |= words[p].encoding[i];
}
p++;
}
int intSize = sizeof(*mask);
for (int i = 0, y = 20; i < R; i++, y += 9)
{
for (int j = 0, x = 0; j < C; j++, x += 7)
{
int pos = i * C + j;
int p = pos / INT_SIZE;
int b = pos % INT_SIZE;
int color = mask[p] & (1 << b) ? WHITE : QLOCK_GREY;
M5.Lcd.drawChar(x, y, clockPanel[pos], color, BLACK, 1);
if (pos == 104)
{
M5.Lcd.drawLine(x + 6, y - 1, x + 5, y, color); // o'clock
}
}
}
drawQlockMinuteDot(m - approxM);
}
void batterySaver()
{
if (millis() > screenOnTime + 10 * 1000 && currentMode != ScreenOff)
{
turnOffScreen();
currentMode = ScreenOff;
}
}
void batteryStatus()
{
vbat = M5.Axp.GetVbatData() * 1.1 / 1000;
discharge = M5.Axp.GetIdischargeData() / 2;
if (vbat >= 4)
{
M5.Lcd.pushImage(145, 1, 14, 8, bat_3);
}
else if (vbat >= 3.7)
{
M5.Lcd.pushImage(145, 1, 14, 8, bat_2);
}
else if (vbat < 3.7)
{
M5.Lcd.pushImage(145, 1, 14, 8, bat_1);
}
else
{
}
// M5.Lcd.setTextColor(TFT_YELLOW);
// M5.Lcd.setCursor(140, 12);
// M5.Lcd.setTextSize(1);
// M5.Lcd.println(discharge);
}
void setup()
{
M5.begin();
M5.Lcd.fillScreen(BLACK);
// timeSync(); //uncomment if you want to have a timesync everytime you turn device on (if no WIFI is avail mostly bad)
screenOnTime = millis();
lastRefreshTime = millis();
}
void loop()
{
M5.update();
buttons_code();
if (currentMode == Simple)
{
drawSimpleClock();
}
if (currentMode == Qlock)
{
drawQlock();
}
batterySaver();
// TODO: low battery flashlight,
// battery: https://github.com/m5stack/m5-docs/blob/master/docs/zh_CN/api/axp192_m5stickc.md
}