From 82958281e202240cdb18a6626007ec30314a31db Mon Sep 17 00:00:00 2001 From: fenrir Date: Wed, 18 Sep 2024 10:00:32 +0900 Subject: [PATCH] Add update_wday() and calculate day of week by using UBX-NAV-TIMEUTC --- firmware/gps.c | 1 + firmware/util.c | 11 +++++++++++ firmware/util.h | 2 ++ 3 files changed, 14 insertions(+) diff --git a/firmware/gps.c b/firmware/gps.c index d8d120524..d804a0f68 100644 --- a/firmware/gps.c +++ b/firmware/gps.c @@ -544,6 +544,7 @@ static void make_packet(packet_t *packet){ gps_utc.tm_hour = buf.utc.hour; // Hours [0-23] gps_utc.tm_min = buf.utc.min; // Minutes [0-59] gps_utc.tm_sec = buf.utc.sec; // Seconds [0-60] + update_wday(&gps_utc); gps_utc_valid = TRUE; }else if((ubx_state.packet_type == AID_EPH) && (buf.svid <= UBX_GPS_MAX_ID)){ u32 mask = 1; diff --git a/firmware/util.c b/firmware/util.c index 6c1636873..37ceab271 100644 --- a/firmware/util.c +++ b/firmware/util.c @@ -199,3 +199,14 @@ long str2num(char *s, char **endptr){ if(endptr){*endptr = s;} return res; } + +void update_wday(struct tm *t){ + // Recalculate weekday + // @see https://en.wikipedia.org/wiki/Determination_of_the_day_of_the_week#Sakamoto's_methods + // modified base date from 1753/1/1(1=Monday) -> 1980/1/1(2=Tuesday) + // valid from 1980/1/1 to 2099/12/31 + u8 y = t->tm_year - 76; // y > 1980 + static const u8 tbl[] = {5, 1, 0, 3, 5, 1, 3, 6, 2, 4, 0, 2}; + if(t->tm_mon < 2){y -= 1;} + t->tm_wday = ((y + y/4 + tbl[t->tm_mon] + (u8)t->tm_mday) % 7); +} diff --git a/firmware/util.h b/firmware/util.h index 61d61ba0d..838bc6ea3 100644 --- a/firmware/util.h +++ b/firmware/util.h @@ -34,6 +34,7 @@ #include "main.h" #include "type.h" +#include void wait_8n6clk(unsigned char i); void _wait_us(unsigned int count); @@ -82,6 +83,7 @@ u16 swap_u16(u16 w); u16 crc16(u8 *buf, u8 size, u16 crc); long str2num(char *str, char **endptr); +void update_wday(struct tm *t); #endif /* __UTIL_H__ */