Skip to content

Commit d05929b

Browse files
author
Pierrick C
committed
Add datetime property to use GPS object as time source for rtc.set_time_source()
1 parent 6416856 commit d05929b

File tree

2 files changed

+47
-0
lines changed

2 files changed

+47
-0
lines changed

adafruit_gps.py

+5
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,11 @@ def has_fix(self):
133133
"""True if a current fix for location information is available."""
134134
return self.fix_quality is not None and self.fix_quality >= 1
135135

136+
@property
137+
def datetime(self):
138+
"""Return struct_time object to feed rtc.set_time_source() function"""
139+
return self.timestamp_utc
140+
136141
def _parse_sentence(self):
137142
# Parse any NMEA sentence that is available.
138143
# pylint: disable=len-as-condition

examples/time_source.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import busio, board, time, rtc
2+
import adafruit_gps
3+
4+
uart = busio.UART(board.TX, board.RX, baudrate = 9600, timeout=3000)
5+
6+
gps = adafruit_gps.GPS(uart, debug=False)
7+
gps.send_command(b'PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0')
8+
gps.send_command(b'PMTK220,1000')
9+
10+
print("Set GPS as time source")
11+
rtc.set_time_source(gps)
12+
13+
last_print = time.monotonic()
14+
while True:
15+
16+
gps.update()
17+
# Every second print out current time from GPS, RTC and time.localtime()
18+
current = time.monotonic()
19+
if current - last_print >= 1.0:
20+
last_print = current
21+
print('Fix timestamp: {:02}/{:02}/{} {:02}:{:02}:{:02}'.format(
22+
gps.timestamp_utc.tm_mon, # Grab parts of the time from the
23+
gps.timestamp_utc.tm_mday, # struct_time object that holds
24+
gps.timestamp_utc.tm_year, # the fix time. Note you might
25+
gps.timestamp_utc.tm_hour, # not get all data like year, day,
26+
gps.timestamp_utc.tm_min, # month!
27+
gps.timestamp_utc.tm_sec))
28+
print('RTC timestamp: {:02}/{:02}/{} {:02}:{:02}:{:02}'.format(
29+
rtc.RTC.datetime.tm_mon,
30+
rtc.RTC.datetime.tm_mday,
31+
rtc.RTC.datetime.tm_year,
32+
rtc.RTC.datetime.tm_hour,
33+
rtc.RTC.datetime.tm_min,
34+
rtc.RTC.datetime.tm_sec))
35+
local_time = time.localtime()
36+
print("Local time: {:02}/{:02}/{} {:02}:{:02}:{:02}".format(
37+
local_time.tm_mon,
38+
local_time.tm_mday,
39+
local_time.tm_year,
40+
local_time.tm_hour,
41+
local_time.tm_min,
42+
local_time.tm_sec))

0 commit comments

Comments
 (0)