Skip to content

Commit 59d15e0

Browse files
committed
Resolve issue #125 . Update Epoch and LS examples.
1 parent e3d2c01 commit 59d15e0

File tree

4 files changed

+90
-72
lines changed

4 files changed

+90
-72
lines changed

Diff for: examples/Example24_GetUnixEpochAndMicros/Example24_GetUnixEpochAndMicros.ino

+18-18
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ void setup()
5454
myGNSS.setI2COutput(COM_TYPE_UBX); //Set the I2C port to output UBX only (turn off NMEA noise)
5555
//myGNSS.saveConfiguration(); //Optional: Save the current settings to flash and BBR
5656

57-
Serial.println("Compare Unix Epoch given with reference one from https://www.epochconverter.com/");
57+
Serial.println(F("Compare Unix Epoch given with reference one from https://www.epochconverter.com/"));
5858

5959
}
6060

@@ -70,48 +70,48 @@ void loop()
7070

7171
uint32_t us; //microseconds returned by getUnixEpoch()
7272
uint32_t epoch = myGNSS.getUnixEpoch();
73-
Serial.print("Unix Epoch rounded: ");
73+
Serial.print(F("Unix Epoch rounded: "));
7474
Serial.print(epoch, DEC);
7575
epoch = myGNSS.getUnixEpoch(us);
76-
Serial.print(" Exact Unix Epoch: ");
76+
Serial.print(F(" Exact Unix Epoch: "));
7777
Serial.print(epoch, DEC);
78-
Serial.print(" micros: ");
78+
Serial.print(F(" micros: "));
7979
Serial.println(us, DEC);
8080

8181
Serial.print(myGNSS.getYear());
82-
Serial.print("-");
82+
Serial.print(F("-"));
8383
Serial.print(myGNSS.getMonth());
84-
Serial.print("-");
84+
Serial.print(F("-"));
8585
Serial.print(myGNSS.getDay());
86-
Serial.print(" ");
86+
Serial.print(F(" "));
8787
Serial.print(myGNSS.getHour());
88-
Serial.print(":");
88+
Serial.print(F(":"));
8989
Serial.print(myGNSS.getMinute());
90-
Serial.print(":");
90+
Serial.print(F(":"));
9191
Serial.print(myGNSS.getSecond());
9292

93-
Serial.print(" Time is ");
93+
Serial.print(F(" Time is "));
9494
if (myGNSS.getTimeFullyResolved() == false)
9595
{
96-
Serial.print("not fully resolved but ");
96+
Serial.print(F("not fully resolved but "));
9797
} else {
98-
Serial.print("fully resolved and ");
98+
Serial.print(F("fully resolved and "));
9999
}
100100
if (myGNSS.getTimeValid() == false)
101101
{
102-
Serial.print("not ");
102+
Serial.print(F("not "));
103103
}
104-
Serial.print("valid ");
104+
Serial.print(F("valid "));
105105
if (myGNSS.getConfirmedTime() == false)
106106
{
107-
Serial.print("but not ");
107+
Serial.print(F("but not "));
108108
} else {
109-
Serial.print("and ");
109+
Serial.print(F("and "));
110110
}
111-
Serial.print("confirmed");
111+
Serial.print(F("confirmed"));
112112

113113
byte SIV = myGNSS.getSIV();
114114
Serial.print(F(" SIV: "));
115115
Serial.println(SIV);
116116
}
117-
}
117+
}

Diff for: examples/Example28_GetLeapSecondInfo/Example28_GetLeapSecondInfo.ino

+30-22
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ void setup()
5959
myGNSS.setI2COutput(COM_TYPE_UBX); //Set the I2C port to output UBX only (turn off NMEA noise)
6060
myGNSS.saveConfiguration(); //Optional: Save the current settings to flash and BBR
6161

62-
Serial.println("Compare Unix Epoch given with reference one from https://www.epochconverter.com/");
62+
Serial.println(F("Compare Unix Epoch given with reference one from https://www.epochconverter.com/"));
6363

6464
}
6565

@@ -75,69 +75,77 @@ void loop()
7575

7676
uint32_t us; //microseconds returned by getUnixEpoch()
7777
uint32_t epoch = myGNSS.getUnixEpoch();
78-
Serial.print("Unix Epoch rounded: ");
78+
Serial.print(F("Unix Epoch rounded: "));
7979
Serial.print(epoch, DEC);
8080
epoch = myGNSS.getUnixEpoch(us);
81-
Serial.print(" Exact Unix Epoch: ");
81+
Serial.print(F(" Exact Unix Epoch: "));
8282
Serial.print(epoch, DEC);
83-
Serial.print(" micros: ");
83+
Serial.print(F(" micros: "));
8484
Serial.println(us, DEC);
8585
int32_t timeToLeapSecEvent;
8686
ntp_LI_e leapIndicator = (ntp_LI_e)myGNSS.getLeapIndicator(timeToLeapSecEvent);
87-
Serial.print("NTP LI: ");
87+
Serial.print(F("NTP LI: "));
8888
Serial.print(leapIndicator, DEC);
8989
switch (leapIndicator){
9090
case LI_NO_WARNING:
91-
Serial.print(" - No event scheduled");
91+
Serial.print(F(" - No event scheduled"));
9292
break;
9393
case LI_LAST_MINUTE_61_SEC:
94-
Serial.print(" - last minute will end at 23:60");
94+
Serial.print(F(" - last minute will end at 23:60"));
9595
break;
9696
case LI_LAST_MINUTE_59_SEC:
97-
Serial.print(" - last minute will end at 23:58");
97+
Serial.print(F(" - last minute will end at 23:58"));
9898
break;
9999
case LI_ALARM_CONDITION:
100100
default:
101-
Serial.print(" - Unknown (clock not synchronized)");
101+
Serial.print(F(" - Unknown (clock not synchronized)"));
102102
break;
103103
}
104-
Serial.print(". Time to the next leap second event: ");
105-
Serial.println(timeToLeapSecEvent, DEC);
104+
if (timeToLeapSecEvent < 0)
105+
{
106+
Serial.print(F(". Time since the last leap second event: "));
107+
Serial.println(timeToLeapSecEvent * -1, DEC);
108+
}
109+
else
110+
{
111+
Serial.print(F(". Time to the next leap second event: "));
112+
Serial.println(timeToLeapSecEvent, DEC);
113+
}
106114

107115
sfe_ublox_ls_src_e leapSecSource;
108-
Serial.print("Leap seconds since GPS Epoch (Jan 6th, 1980): ");
116+
Serial.print(F("Leap seconds since GPS Epoch (Jan 6th, 1980): "));
109117
Serial.print(myGNSS.getCurrentLeapSeconds(leapSecSource), DEC);
110118
switch (leapSecSource){
111119
case SFE_UBLOX_LS_SRC_DEFAULT:
112-
Serial.print(" - hardcoded");
120+
Serial.print(F(" - hardcoded"));
113121
break;
114122
case SFE_UBLOX_LS_SRC_GLONASS:
115-
Serial.print(" - derived from GPS and GLONASS time difference");
123+
Serial.print(F(" - derived from GPS and GLONASS time difference"));
116124
break;
117125
case SFE_UBLOX_LS_SRC_GPS:
118-
Serial.print(" - according to GPS");
126+
Serial.print(F(" - according to GPS"));
119127
break;
120128
case SFE_UBLOX_LS_SRC_SBAS:
121-
Serial.print(" - according to SBAS");
129+
Serial.print(F(" - according to SBAS"));
122130
break;
123131
case SFE_UBLOX_LS_SRC_BEIDOU:
124-
Serial.print(" - according to BeiDou");
132+
Serial.print(F(" - according to BeiDou"));
125133
break;
126134
case SFE_UBLOX_LS_SRC_GALILEO:
127-
Serial.print(" - according to Galileo");
135+
Serial.print(F(" - according to Galileo"));
128136
break;
129137
case SFE_UBLOX_LS_SRC_AIDED:
130-
Serial.print(" - last minute will end at 23:58");
138+
Serial.print(F(" - last minute will end at 23:58"));
131139
break;
132140
case SFE_UBLOX_LS_SRC_CONFIGURED:
133-
Serial.print(" - as configured)");
141+
Serial.print(F(" - as configured)"));
134142
break;
135143
case SFE_UBLOX_LS_SRC_UNKNOWN:
136144
default:
137-
Serial.print(" - source unknown");
145+
Serial.print(F(" - source unknown"));
138146
break;
139147
}
140148
Serial.println();
141149
}
142150
Serial.println();
143-
}
151+
}

Diff for: src/SparkFun_u-blox_GNSS_Arduino_Library.cpp

+26-26
Original file line numberDiff line numberDiff line change
@@ -16102,7 +16102,7 @@ int32_t SFE_UBLOX_GNSS::getNanosecond(uint16_t maxWait)
1610216102
return (packetUBXNAVPVT->data.nano);
1610316103
}
1610416104

16105-
// Get the current Unix epoch time rounded up to the nearest second
16105+
// Get the current Unix epoch time rounded to the nearest second
1610616106
uint32_t SFE_UBLOX_GNSS::getUnixEpoch(uint16_t maxWait)
1610716107
{
1610816108
if (packetUBXNAVPVT == NULL)
@@ -16119,16 +16119,16 @@ uint32_t SFE_UBLOX_GNSS::getUnixEpoch(uint16_t maxWait)
1611916119
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.min = false;
1612016120
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.sec = false;
1612116121
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.all = false;
16122-
// assemble time elements into time_t - credits to Thomas Roell @ https://github.com/GrumpyOldPizza
16123-
uint32_t t = ((((((((uint32_t)packetUBXNAVPVT->data.year - 1970) * 365) + ((((uint32_t)packetUBXNAVPVT->data.year - 1970) + 3) / 4)) +
16124-
DAYS_SINCE_MONTH[((uint32_t)packetUBXNAVPVT->data.year - 1970) & 3][(uint32_t)packetUBXNAVPVT->data.month] +
16125-
((uint32_t)packetUBXNAVPVT->data.day - 1)) *
16126-
24 +
16127-
(uint32_t)packetUBXNAVPVT->data.hour) *
16128-
60 +
16129-
(uint32_t)packetUBXNAVPVT->data.min) *
16130-
60 +
16131-
(uint32_t)packetUBXNAVPVT->data.sec);
16122+
uint32_t t = SFE_UBLOX_DAYS_FROM_1970_TO_2020; // Jan 1st 2020 as days from Jan 1st 1970
16123+
t += (uint32_t)SFE_UBLOX_DAYS_SINCE_2020[packetUBXNAVPVT->data.year - 2020]; // Add on the number of days since 2020
16124+
t += (uint32_t)SFE_UBLOX_DAYS_SINCE_MONTH[packetUBXNAVPVT->data.year % 4 == 0 ? 0 : 1][packetUBXNAVPVT->data.month - 1]; // Add on the number of days since Jan 1st
16125+
t += (uint32_t)packetUBXNAVPVT->data.day - 1; // Add on the number of days since the 1st of the month
16126+
t *= 24; // Convert to hours
16127+
t += (uint32_t)packetUBXNAVPVT->data.hour; // Add on the hour
16128+
t *= 60; // Convert to minutes
16129+
t += (uint32_t)packetUBXNAVPVT->data.min; // Add on the minute
16130+
t *= 60; // Convert to seconds
16131+
t += (uint32_t)packetUBXNAVPVT->data.sec; // Add on the second
1613216132
return t;
1613316133
}
1613416134

@@ -16150,23 +16150,23 @@ uint32_t SFE_UBLOX_GNSS::getUnixEpoch(uint32_t &microsecond, uint16_t maxWait)
1615016150
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.sec = false;
1615116151
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.nano = false;
1615216152
packetUBXNAVPVT->moduleQueried.moduleQueried1.bits.all = false;
16153-
// assemble time elements into time_t - credits to Thomas Roell @ https://github.com/GrumpyOldPizza
16154-
uint32_t t = ((((((((uint32_t)packetUBXNAVPVT->data.year - 1970) * 365) + ((((uint32_t)packetUBXNAVPVT->data.year - 1970) + 3) / 4)) +
16155-
DAYS_SINCE_MONTH[((uint32_t)packetUBXNAVPVT->data.year - 1970) & 3][(uint32_t)packetUBXNAVPVT->data.month] +
16156-
((uint32_t)packetUBXNAVPVT->data.day - 1)) *
16157-
24 +
16158-
(uint32_t)packetUBXNAVPVT->data.hour) *
16159-
60 +
16160-
(uint32_t)packetUBXNAVPVT->data.min) *
16161-
60 +
16162-
(uint32_t)packetUBXNAVPVT->data.sec);
16163-
int32_t us = packetUBXNAVPVT->data.nano / 1000;
16164-
microsecond = (uint32_t)us;
16165-
// adjust t if nano is negative
16153+
uint32_t t = SFE_UBLOX_DAYS_FROM_1970_TO_2020; // Jan 1st 2020 as days from Jan 1st 1970
16154+
t += (uint32_t)SFE_UBLOX_DAYS_SINCE_2020[packetUBXNAVPVT->data.year - 2020]; // Add on the number of days since 2020
16155+
t += (uint32_t)SFE_UBLOX_DAYS_SINCE_MONTH[packetUBXNAVPVT->data.year % 4 == 0 ? 0 : 1][packetUBXNAVPVT->data.month - 1]; // Add on the number of days since Jan 1st
16156+
t += (uint32_t)packetUBXNAVPVT->data.day - 1; // Add on the number of days since the 1st of the month
16157+
t *= 24; // Convert to hours
16158+
t += (uint32_t)packetUBXNAVPVT->data.hour; // Add on the hour
16159+
t *= 60; // Convert to minutes
16160+
t += (uint32_t)packetUBXNAVPVT->data.min; // Add on the minute
16161+
t *= 60; // Convert to seconds
16162+
t += (uint32_t)packetUBXNAVPVT->data.sec; // Add on the second
16163+
int32_t us = packetUBXNAVPVT->data.nano / 1000; // Convert nanos to micros
16164+
microsecond = (uint32_t)us; // Could be -ve!
16165+
// Adjust t if nano is negative
1616616166
if (us < 0)
1616716167
{
16168-
microsecond = (uint32_t)(us + 1000000);
16169-
t--;
16168+
microsecond = (uint32_t)(us + 1000000); // Make nano +ve
16169+
t--; // Decrement t by 1 second
1617016170
}
1617116171
return t;
1617216172
}

Diff for: src/SparkFun_u-blox_GNSS_Arduino_Library.h

+16-6
Original file line numberDiff line numberDiff line change
@@ -622,12 +622,22 @@ typedef struct
622622
bool moduleQueried;
623623
} moduleSWVersion_t;
624624

625-
const uint16_t DAYS_SINCE_MONTH[4][16] =
626-
{
627-
{0, 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 335, 335, 335},
628-
{0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 334, 334, 334},
629-
{0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 334, 334, 334},
630-
{0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 334, 334, 334},
625+
const uint32_t SFE_UBLOX_DAYS_FROM_1970_TO_2020 = 18262; // Jan 1st 2020 Epoch = 1577836800 seconds
626+
const uint16_t SFE_UBLOX_DAYS_SINCE_2020[80] =
627+
{
628+
0, 366, 731, 1096, 1461, 1827, 2192, 2557, 2922, 3288,
629+
3653, 4018, 4383, 4749, 5114, 5479, 5844, 6210, 6575, 6940,
630+
7305, 7671, 8036, 8401, 8766, 9132, 9497, 9862, 10227, 10593,
631+
10958, 11323, 11688, 12054, 12419, 12784, 13149, 13515, 13880, 14245,
632+
14610, 14976, 15341, 15706, 16071, 16437, 16802, 17167, 17532, 17898,
633+
18263, 18628, 18993, 19359, 19724, 20089, 20454, 20820, 21185, 21550,
634+
21915, 22281, 22646, 23011, 23376, 23742, 24107, 24472, 24837, 25203,
635+
25568, 25933, 26298, 26664, 27029, 27394, 27759, 28125, 28490, 28855
636+
};
637+
const uint16_t SFE_UBLOX_DAYS_SINCE_MONTH[2][12] =
638+
{
639+
{0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335}, // Leap Year (Year % 4 == 0)
640+
{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334} // Normal Year
631641
};
632642

633643
class SFE_UBLOX_GNSS

0 commit comments

Comments
 (0)