-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgps_handler.ino
104 lines (90 loc) · 2.71 KB
/
gps_handler.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
//#define DEBUG
#define GPS_CLOCK_SYNC_RATE (60ll * 60 * 1000)
static bool time_sync = false;
void GPSSetup() {
#ifdef DEBUG
Serial.begin(115200);
Serial.println("Starting GPS/SPI!");
#endif
pinMode(ublox_CSn_PIN, OUTPUT);
pinMode(ublox_RESETn_PIN, OUTPUT);
digitalWrite(ublox_RESETn_PIN, LOW); // reset
SPI.begin();
digitalWrite(ublox_RESETn_PIN, HIGH); // not in reset
}
void printDateTime(time_t t, const char *tz)
{
char buf[32];
char m[4]; // temporary storage for month string (DateStrings.cpp uses shared buffer)
strcpy(m, monthShortStr(month(t)));
sprintf(buf, "%.2d:%.2d:%.2d %s %.2d %s %d %s",
hour(t), minute(t), second(t), dayShortStr(weekday(t)), day(t), m, year(t), tz);
Serial.println(buf);
}
static time_t GPSMakeTime(NeoGPS::time_t &dt)
{
#ifdef DEBUG
char buf[64];
sprintf(buf, "maketime %.2d:%.2d:%.2d, %.2d %.2d %.2d",
dt.hours, dt.minutes, dt.seconds, dt.date, dt.month, dt.year);
Serial.println(buf);
#endif
tmElements_t tm;
tm.Year = dt.full_year() - 1970;
tm.Month = dt.month;
tm.Day = dt.date;
tm.Hour = dt.hours;
tm.Minute = dt.minutes;
tm.Second = dt.seconds;
return makeTime(tm);
}
void GPSLoop() {
static unsigned long last_update = 0;
byte in_byte = 0x0;
uint8_t data;
for(int i = 0; i < 20; i++)
{
/* Read SPI byte */
digitalWrite(ublox_CSn_PIN, LOW);
in_byte = SPI.transfer(0XFF);
digitalWrite(ublox_CSn_PIN, HIGH);
/* Decode NMEA */
if (gps.decode(in_byte) == NMEAGPS::DECODE_COMPLETED) {
fix_data = gps.fix();
#ifdef DEBUG
Serial.print("[*] FIX: time valid: ");
Serial.print(fix_data.valid.time);
Serial.print(" date valid: ");
Serial.print(fix_data.valid.date);
Serial.print(" time: ");
Serial.print(fix_data.dateTime.hours);
Serial.print(":");
Serial.print(fix_data.dateTime.minutes);
Serial.print(" lat: ");
Serial.print(fix_data.latitude());
Serial.print(" lon: ");
Serial.print(fix_data.longitude());
Serial.print("\r\n");
#endif
/* Sync clock */
if (fix_data.valid.time && fix_data.valid.date) {
/* Only update if > 1 hour since last update */
if ((!time_sync) || (millis() > last_update + GPS_CLOCK_SYNC_RATE))
{
#ifdef DEBUG
Serial.println("Setting time from GPS!");
#endif
last_update = millis();
time_sync = true;
time_t gpstime = GPSMakeTime(fix_data.dateTime);
setTime(myTimeZone.toLocal(gpstime, &tcr));
printDateTime(gpstime, tcr -> abbrev);
}
}
}
}
}
bool GPSIsTimeSynced(void)
{
return time_sync;
}