Browse Source
Handles setting RTC based on the compile date/time (assuming you'll compile + upload immediately) and tracking the last time set in eeprom to avoid resetting every bootmaster
Adam Pippin
4 years ago
2 changed files with 204 additions and 0 deletions
@ -0,0 +1,202 @@ |
|||
#include <M5StickC.h> |
|||
#include "Kernel.h" |
|||
#include "EAT.h" |
|||
|
|||
RTC_TimeTypeDef time_to_rtc(char* time); |
|||
RTC_DateTypeDef date_to_rtc(char* date); |
|||
void redraw(RTC_TimeTypeDef time); |
|||
|
|||
int last_minute = -1; |
|||
|
|||
int Clock(int pid, unsigned int signal) |
|||
{ |
|||
if (signal & SIGNAL_START) |
|||
{ |
|||
Kernel_signal_mask(pid, SIGNAL_START | SIGNAL_STOP | SIGNAL_TICK | SIGNAL_REDRAW); |
|||
|
|||
RTC_TimeTypeDef compile_time = time_to_rtc(__TIME__); |
|||
RTC_DateTypeDef compile_date = date_to_rtc(__DATE__); |
|||
|
|||
if (EAT_allocate(2, 6) || |
|||
EAT_read(2, 0) != compile_time.Hours || |
|||
EAT_read(2, 1) != compile_time.Minutes || |
|||
EAT_read(2, 2) != compile_time.Seconds || |
|||
EAT_read(2, 3) != (compile_date.Year & 0xFF) || |
|||
EAT_read(2, 4) != compile_date.Month || |
|||
EAT_read(2, 5) != compile_date.Date) |
|||
{ |
|||
M5.Rtc.SetTime(&compile_time); |
|||
M5.Rtc.SetData(&compile_date); |
|||
|
|||
EAT_write(2, 0, compile_time.Hours); |
|||
EAT_write(2, 1, compile_time.Minutes); |
|||
EAT_write(2, 2, compile_time.Seconds); |
|||
EAT_write(2, 3, compile_date.Year & 0xFF); |
|||
EAT_write(2, 4, compile_date.Month); |
|||
EAT_write(2, 5, compile_date.Date); |
|||
} |
|||
Serial.begin(115200); |
|||
delay(30); |
|||
} |
|||
|
|||
if (signal & SIGNAL_STOP) |
|||
{ |
|||
return 255; |
|||
} |
|||
|
|||
if (signal & SIGNAL_TICK || signal & SIGNAL_REDRAW) |
|||
{ |
|||
RTC_TimeTypeDef time; |
|||
M5.Rtc.GetTime(&time); |
|||
|
|||
if (signal & SIGNAL_TICK) // && time.Minutes != last_minute)
|
|||
{ |
|||
Kernel_signal(SIGNAL_REDRAW); |
|||
} |
|||
if (signal & SIGNAL_REDRAW) |
|||
{ |
|||
Serial.println("Redraw!"); |
|||
redraw(time); |
|||
} |
|||
} |
|||
|
|||
return 0; |
|||
} |
|||
|
|||
void redraw(RTC_TimeTypeDef time) |
|||
{ |
|||
M5.Lcd.setCursor(0, 40); |
|||
char time_str[9]; |
|||
sprintf(time_str, "%02d:%02d:%02d", time.Hours, time.Minutes, time.Seconds); |
|||
M5.Lcd.print(time_str); |
|||
} |
|||
|
|||
RTC_TimeTypeDef time_to_rtc(char* time) |
|||
{ |
|||
RTC_TimeTypeDef time_ret; |
|||
int buffer_idx = 0; |
|||
char buffer[3]; |
|||
char* current; |
|||
int state = 0; |
|||
|
|||
for (current = time; ; current++) |
|||
{ |
|||
if (*current == ':' || *current == '\0') |
|||
{ |
|||
buffer[buffer_idx++] = '\0'; |
|||
int next = atoi(buffer); |
|||
switch (state++) |
|||
{ |
|||
case 0: |
|||
time_ret.Hours = next; |
|||
break; |
|||
case 1: |
|||
time_ret.Minutes = next; |
|||
break; |
|||
case 2: |
|||
time_ret.Seconds = next; |
|||
break; |
|||
} |
|||
buffer_idx = 0; |
|||
} |
|||
else |
|||
{ |
|||
buffer[buffer_idx++] = *current; |
|||
} |
|||
|
|||
if (*current == '\0') |
|||
{ |
|||
break; |
|||
} |
|||
} |
|||
|
|||
return time_ret; |
|||
} |
|||
|
|||
RTC_DateTypeDef date_to_rtc(char* date) |
|||
{ |
|||
RTC_DateTypeDef date_ret; |
|||
int buffer_idx = 0; |
|||
char buffer[5]; |
|||
char* current; |
|||
int state = 0; |
|||
|
|||
for (current = date; ; current++) |
|||
{ |
|||
if (*current == ' ' || *current == '\0') |
|||
{ |
|||
buffer[buffer_idx++] = '\0'; |
|||
switch (state++) |
|||
{ |
|||
case 0: // month
|
|||
if (strcmp(buffer, "Jan") == 0) |
|||
{ |
|||
date_ret.Month = 1; |
|||
} |
|||
else if (strcmp(buffer, "Feb") == 0) |
|||
{ |
|||
date_ret.Month = 2; |
|||
} |
|||
else if (strcmp(buffer, "Mar") == 0) |
|||
{ |
|||
date_ret.Month = 3; |
|||
} |
|||
else if (strcmp(buffer, "Apr") == 0) |
|||
{ |
|||
date_ret.Month = 4; |
|||
} |
|||
else if (strcmp(buffer, "May") == 0) |
|||
{ |
|||
date_ret.Month = 5; |
|||
} |
|||
else if (strcmp(buffer, "Jun") == 0) |
|||
{ |
|||
date_ret.Month = 6; |
|||
} |
|||
else if (strcmp(buffer, "Jul") == 0) |
|||
{ |
|||
date_ret.Month = 7; |
|||
} |
|||
else if (strcmp(buffer, "Aug") == 0) |
|||
{ |
|||
date_ret.Month = 8; |
|||
} |
|||
else if (strcmp(buffer, "Sep") == 0) |
|||
{ |
|||
date_ret.Month = 9; |
|||
} |
|||
else if (strcmp(buffer, "Oct") == 0) |
|||
{ |
|||
date_ret.Month = 10; |
|||
} |
|||
else if (strcmp(buffer, "Nov") == 0) |
|||
{ |
|||
date_ret.Month = 11; |
|||
} |
|||
else if (strcmp(buffer, "Dec") == 0) |
|||
{ |
|||
date_ret.Month = 12; |
|||
} |
|||
break; |
|||
case 1: // day
|
|||
date_ret.Date = atoi(buffer); |
|||
break; |
|||
case 2: // year
|
|||
date_ret.Year = atoi(buffer); |
|||
break; |
|||
} |
|||
buffer_idx = 0; |
|||
} |
|||
else |
|||
{ |
|||
buffer[buffer_idx++] = *current; |
|||
} |
|||
|
|||
if (*current == '\0') |
|||
{ |
|||
break; |
|||
} |
|||
} |
|||
|
|||
return date_ret; |
|||
} |
@ -0,0 +1,2 @@ |
|||
|
|||
int Clock(int pid, unsigned int signal); |
Loading…
Reference in new issue