Skip to content

Commit

Permalink
Add update_wday() and calculate day of week by using UBX-NAV-TIMEUTC
Browse files Browse the repository at this point in the history
  • Loading branch information
fenrir-naru committed Sep 18, 2024
1 parent 0f91e7f commit 8295828
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions firmware/gps.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
11 changes: 11 additions & 0 deletions firmware/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 2 additions & 0 deletions firmware/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

#include "main.h"
#include "type.h"
#include <time.h>

void wait_8n6clk(unsigned char i);
void _wait_us(unsigned int count);
Expand Down Expand Up @@ -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__ */

0 comments on commit 8295828

Please sign in to comment.