-
Notifications
You must be signed in to change notification settings - Fork 0
/
watchnlock.ino
559 lines (471 loc) · 13.7 KB
/
watchnlock.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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
#include <Arduino.h>
#include <TFT_eSPI.h>
#include <SPI.h>
#include <Wire.h>
#define USE_TFT_ESPI_LIBRARY
#include "lv_xiao_round_screen.h"
#include "I2C_BM8563.h"
// #include "NotoSansBold15.h"
#include "./assets.h"
I2C_BM8563 rtc(I2C_BM8563_DEFAULT_ADDRESS, Wire);
I2C_BM8563_TimeTypeDef timeStruct;
I2C_BM8563_DateTypeDef dateStruct;
TFT_eSprite face = TFT_eSprite(&tft);
#define COLOR_THEME 0xd6ba //
#define COLOR_MID 0x10A2
#define COLOR_LIGHT 0x3186
#define FACE_W 240
#define FACE_H 240
// Sampling frequency
#define NUM_ADC_SAMPLE 100
#define RP2040_VREF 3080
#define BATTERY_DEFICIT_VOL 500
#define BATTERY_FULL_VOL 950
const unsigned int MAX_MESSAGE_LENGTH = 120;
int32_t battery_level_percent(void)
{
int32_t mvolts = 0;
int32_t adc_raw = 0;
for(int8_t i=0; i<NUM_ADC_SAMPLE; i++){
adc_raw += analogRead(D0);
}
adc_raw /= NUM_ADC_SAMPLE;
mvolts = RP2040_VREF * adc_raw / (1<<12);
return adc_raw;
int32_t level = (mvolts - BATTERY_DEFICIT_VOL) * 100 / (BATTERY_FULL_VOL-BATTERY_DEFICIT_VOL); // 1850 ~ 2100
level = (level<0) ? 0 : ((level>100) ? 100 : level);
return level ;
}
int32_t getBat()
{
int lvl = battery_level_percent();
if (lvl < 10) {
return 0;
}
if (lvl < 30) {
return 1;
}
if (lvl < 60) {
return 2;
}
return 3;
}
// Time for next tick
uint32_t targetTime = 0;
lv_coord_t touchX, touchY;
// =========================================================================
// Setup
// =========================================================================
void setup() {
Serial.begin(115200);
Serial.println("Booting...");
pinMode(D7, INPUT_PULLUP);
pinMode(D6, OUTPUT);
pinMode(D0, INPUT);
tft.init();
tft.setRotation(0);
tft.fillScreen(TFT_BLACK);
face.createSprite(FACE_W, FACE_H);
// face.loadFont(NotoSansBold15);
Wire.begin();
rtc.begin();
syncTime();
renderFace();
analogReadResolution(12);
tft.writecommand(0x29);
}
// =========================================================================
// Loop
// =========================================================================
int lastTouch = -1;
void loop() {
if (chsc6x_is_pressed()) {
chsc6x_get_xy(&touchX, &touchY);
if (touchY > 180) {
lastTouch = 4;
}
if (lastTouch >= 0) {
lastTouch = 4;
}
}
if (lastTouch >= 0) {
lastTouch--;
}
if(lastTouch > 0){
face.fillSprite(TFT_WHITE);
face.pushSprite(0, 0, TFT_TRANSPARENT);
} else {
if (targetTime < millis()) {
targetTime = millis() + 1000;
syncTime();
renderFace();
}
}
if (Serial.available()) {
static char message[MAX_MESSAGE_LENGTH];
static unsigned int message_pos = 0;
while (Serial.available() > 0)
{
char inByte = Serial.read();
if ( inByte != '\n' && (message_pos < MAX_MESSAGE_LENGTH - 1) )
{
message[message_pos] = inByte;
message_pos++;
}
}
message[message_pos] = '\0';
Serial.printf("echo %s\n", message);
static char time[10];
if (prefix("SETMIN=", message)) {
rtc.getTime(&timeStruct);
Serial.println("SET MIN CMD");
strncpy(time, message+7, strlen(message));
timeStruct.minutes = atoi(time);
rtc.setTime(&timeStruct);
Serial.printf("CONFIRM MIN TO %i", atoi(time));
}
if (prefix("SETHRS=", message)) {
rtc.getTime(&timeStruct);
Serial.println("SET HRS CMD");
strncpy(time, message+7, strlen(message));
timeStruct.hours = atoi(time);
rtc.setTime(&timeStruct);
Serial.printf("CONFIRM HRS TO %i", atoi(time));
}
if (prefix("SETSEC=", message)) {
rtc.getTime(&timeStruct);
Serial.println("SET SEC CMD");
strncpy(time, message+7, strlen(message));
timeStruct.seconds = atoi(time);
rtc.setTime(&timeStruct);
Serial.printf("CONFIRM SEC TO %i", atoi(time));
}
if (prefix("SETDAY=", message)) {
Serial.println("SET DAY CMD");
rtc.getDate(&dateStruct);
strncpy(time, message+7, strlen(message));
dateStruct.date = atoi(time);
rtc.setDate(&dateStruct);
Serial.printf("CONFIRM DAY TO %i", atoi(time));
}
if (prefix("SETMON=", message)) {
Serial.println("SET MON CMD");
rtc.getDate(&dateStruct);
strncpy(time, message+7, strlen(message));
dateStruct.month = atoi(time);
rtc.setDate(&dateStruct);
Serial.printf("CONFIRM MON TO %i", atoi(time));
}
if (prefix("SETYRS=", message)) {
Serial.println("SET YRS CMD");
rtc.getDate(&dateStruct);
strncpy(time, message+7, strlen(message));
dateStruct.year = atoi(time);
rtc.setDate(&dateStruct);
Serial.printf("CONFIRM YRS TO %i", atoi(time));
}
if (prefix("GETDATE", message)) {
rtc.getDate(&dateStruct);
rtc.getTime(&timeStruct);
Serial.printf("%04d/%02d/%02d %02d:%02d:%02d\n",
dateStruct.year,
dateStruct.month,
dateStruct.date,
timeStruct.hours,
timeStruct.minutes,
timeStruct.seconds
);
}
if (prefix("GETMOON", message)) {
rtc.getDate(&dateStruct);
rtc.getTime(&timeStruct);
Serial.printf("Moon: %i\n", getMoon());
}
if (prefix("GETBAT", message)) {
int bat = battery_level_percent();
Serial.printf("Battery: %i\n", bat);
}
syncTime();
message_pos = 0;
}
}
bool prefix(const char *pre, const char *str)
{
return strncmp(pre, str, strlen(pre)) == 0;
}
static void renderFace() {
face.fillSprite(TFT_BLACK);
int x=0;
int y=0;
int i=0;
int p=0;
int dStart = 5;
int mStart = dStart+6;
int bStart = mStart+7;
int moon = getMoon();
moon += 3;
moon %= 8;
int bat = getBat();
int trinum = (dateStruct.date * moon) % 20;
for (x=0; x<watchface_bg_width; x++) {
for (y=0; y<watchface_bg_height; y++) {
i = (y*watchface_bg_width) + x;
p = pgm_read_byte_near(watchface_bg_pixels + i);
switch (p) {
case 1:
face.drawPixel(x, y, COLOR_LIGHT);
break;
case 2:
face.drawPixel(x, y, COLOR_MID);
break;
case 3: // AM
if (timeStruct.hours <= 12) {
face.drawPixel(x, y, COLOR_THEME);
} else {
face.drawPixel(x, y, COLOR_MID);
}
break;
case 4: // THEME
face.drawPixel(x, y, COLOR_THEME);
break;
case 19:
if (trinum == 0) {
face.drawPixel(x, y, COLOR_THEME);
}
break;
default:
if (p >= dStart && p <= mStart) { // Weekdays
if (dateStruct.weekDay == (p-dStart)) {
face.drawPixel(x, y, COLOR_THEME);
} else {
face.drawPixel(x, y, COLOR_MID);
}
}
if (p > mStart && p <= bStart) {
if (moon < 4) {
if (p > mStart + 4 - moon) {
face.drawPixel(x, y, COLOR_THEME);
}
} else {
if (p > mStart+(moon-4)) {
face.drawPixel(x, y, COLOR_THEME);
}
}
}
if (p > bStart) {
if (p < bStart+bat) {
face.drawPixel(x, y, COLOR_THEME);
} else {
face.drawPixel(x, y, COLOR_MID);
}
}
}
}
}
int numX = 11;
int numY = 85;
int numSpace = 34;
int offset = numX;
for (int i=0; i<8; i++) {
int displayNum;
int hours = timeStruct.hours % 12;
if (hours == 0) {
hours = 12;
}
switch (i) {
case 0:
displayNum = hours / 10;
if (displayNum == 0) {
displayNum = -1;
}
break;
case 1:
displayNum = hours % 10;
break;
case 3:
displayNum = timeStruct.minutes / 10;
break;
case 4:
displayNum = timeStruct.minutes % 10;
break;
case 6:
displayNum = timeStruct.seconds / 10;
break;
case 7:
displayNum = timeStruct.seconds % 10;
break;
}
if (i == 2 || i == 5) {
offset += 9;
} else {
drawSevenSegment(displayNum, offset, numY);
offset += numSpace;
}
}
face.pushSprite(0, 0, TFT_TRANSPARENT);
}
const uint8_t sevenSegmentNumbers[70] = {
1, 1, 1, 0, 1, 1, 1, // 0
0, 0, 1, 0, 0, 1, 0, // 1
1, 0, 1, 1, 1, 0, 1, // 2
1, 0, 1, 1, 0, 1, 1, // 3
0, 1, 1, 1, 0, 1, 0, // 4
1, 1, 0, 1, 0, 1, 1, // 5
1, 1, 0, 1, 1, 1, 1, // 6
1, 0, 1, 0, 0, 1, 0, // 7
1, 1, 1, 1, 1, 1, 1, // 8
1, 1, 1, 1, 0, 1, 1, // 9
};
static void drawSevenSegment(int16_t n, int16_t x, int16_t y) {
int digitalWidth = 24;
int digitalHeight = 28;
int digitalX = x;
int digitalY = y;
uint32_t onColor = COLOR_THEME;
uint32_t offColor = COLOR_MID;
if (n == -1) {
onColor = offColor;
n = 0;
}
drawHorizontalSegment(digitalX + 4, digitalY, digitalWidth, sevenSegmentNumbers[(n * 7) + 0] ? onColor : offColor);
drawVerticalSegment(digitalX, digitalY + 4, digitalHeight, sevenSegmentNumbers[(n * 7) + 1] ? onColor : offColor);
drawVerticalSegment(digitalX + digitalWidth + 3, digitalY + 4, digitalHeight, sevenSegmentNumbers[(n * 7) + 2] ? onColor : offColor);
drawHorizontalSegment(digitalX + 4, digitalY + digitalHeight + 3, digitalWidth, sevenSegmentNumbers[(n * 7) + 3] ? onColor : offColor);
drawVerticalSegment(digitalX, digitalY + 4 + digitalHeight + 3, digitalHeight, sevenSegmentNumbers[(n * 7) + 4] ? onColor : offColor);
drawVerticalSegment(digitalX + digitalWidth + 3, digitalY + 4 + digitalHeight + 3, digitalHeight, sevenSegmentNumbers[(n * 7) + 5] ? onColor : offColor);
drawHorizontalSegment(digitalX + 4, digitalY + digitalHeight + 3 + digitalHeight + 3, digitalWidth, sevenSegmentNumbers[(n * 7) + 6] ? onColor : offColor);
}
#define TRIANGLE_TOP_LENGTH 15
const uint8_t triangleTop[TRIANGLE_TOP_LENGTH] = {
0, 0, 1, 0, 0,
0, 1, 1, 1, 0,
1, 1, 1, 1, 1
};
static void drawHorizontalSegment(int16_t x, int16_t y, int16_t w, uint32_t color) {
int16_t h = 5;
int16_t area = w*h;
for (int16_t xi = 0; xi < w; xi++) {
for (int16_t yi = 0; yi < h; yi++) {
int16_t pixel = (xi * h) + yi;
int16_t tx = x+xi;
int16_t ty = y+yi;
if (pixel < TRIANGLE_TOP_LENGTH) {
if (triangleTop[pixel] == 1) {
face.drawPixel(tx, ty, color);
}
}
else if (area-pixel <= TRIANGLE_TOP_LENGTH) {
pixel = area-pixel-1;
if (triangleTop[pixel] == 1) {
face.drawPixel(tx, ty, color);
}
} else {
face.drawPixel(tx, ty, color);
}
}
}
}
static void drawVerticalSegment(int16_t x, int16_t y, int16_t h, uint32_t color) {
int16_t w = 5;
int16_t area = w*h;
for (int16_t xi = 0; xi < w; xi++) {
for (int16_t yi = 0; yi < h; yi++) {
int16_t pixel = (yi * w) + xi;
int16_t tx = x+xi;
int16_t ty = y+yi;
if (pixel < TRIANGLE_TOP_LENGTH) {
if (triangleTop[pixel] == 1) {
face.drawPixel(tx, ty, color);
}
}
else if (area-pixel <= TRIANGLE_TOP_LENGTH) {
pixel = area-pixel-1;
if (triangleTop[pixel] == 1) {
face.drawPixel(tx, ty, color);
}
} else {
face.drawPixel(tx, ty, color);
}
}
}
}
int calculateMoonPhase(int year, int month, int day) {
// Constants for the calculation
int knownNewMoonYear = 2000;
int knownNewMoonMonth = 1; // January
int knownNewMoonDay = 6; // 6th January 2000
int daysInLunarCycle = 29; // Average lunar cycle length in days
// Calculate the number of days from the known new moon date to the given date
int totalDays = 0;
// Calculate days from the known new moon year to the given year
for (int y = knownNewMoonYear; y < year; y++) {
totalDays += (y % 4 == 0 && (y % 100 != 0 || y % 400 == 0)) ? 366 : 365; // leap year check
}
// Calculate days from the known new moon month to the given month in the given year
for (int m = knownNewMoonMonth; m < month; m++) {
if (m == 2) {
totalDays += (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? 29 : 28; // February
} else if (m == 4 || m == 6 || m == 9 || m == 11) {
totalDays += 30; // April, June, September, November
} else {
totalDays += 31; // January, March, May, July, August, October, December
}
}
// Add the days in the given month
totalDays += (day - knownNewMoonDay);
// Calculate the phase
int phase = (totalDays % daysInLunarCycle + daysInLunarCycle) % daysInLunarCycle;
return phase; // Phase ranges from 0 to 28
}
int getMoon() {
int phase = calculateMoonPhase(dateStruct.year, dateStruct.month, dateStruct.date);
switch (phase) {
case 0:
case 1:
case 2:
case 3:
case 4:
return 0; // New Moon
break;
case 5:
case 6:
case 7:
return 1; // Waxing Crescent
break;
case 8:
case 9:
case 10:
return 2; // First Quarter
break;
case 11:
case 12:
case 13:
return 3; // Waxing Gibbous
break;
case 14:
return 4; // Full Moon
break;
case 15:
case 16:
case 17:
return 5; // Waning Gibbous
break;
case 18:
case 19:
case 20:
return 6; // Last Quarter
break;
case 21:
case 22:
case 23:
return 7; // Waning Crescent
break;
default:
return 0;
}
}
void syncTime(void){
targetTime = millis() + 100;
rtc.getTime(&timeStruct);
rtc.getDate(&dateStruct);
}