Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions adafruit_gps.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"""

import time
from datetime import datetime

from micropython import const

Expand Down Expand Up @@ -412,8 +413,8 @@ def has_3d_fix(self) -> bool:
return self.fix_quality_3d is not None and self.fix_quality_3d >= 2

@property
def datetime(self) -> Optional[time.struct_time]:
"""Return struct_time object to feed rtc.set_time_source() function"""
def datetime(self) -> Optional[datetime]:
"""Return datetime object to feed rtc.set_time_source() function"""
return self.timestamp_utc

@property
Expand Down Expand Up @@ -494,19 +495,24 @@ def _update_timestamp_utc(self, time_utc: str, date: Optional[str] = None) -> No
hours = int(time_utc[0:2])
mins = int(time_utc[2:4])
secs = int(time_utc[4:6])
milisecs = int(time_utc[7:10]) if len(time_utc) >= 10 and time_utc[7:10].isdigit() else 0
if date is None:
if self.timestamp_utc is None:
day, month, year = 0, 0, 0
return
else:
day = self.timestamp_utc.tm_mday
month = self.timestamp_utc.tm_mon
year = self.timestamp_utc.tm_year
day = self.timestamp_utc.day
month = self.timestamp_utc.month
year = self.timestamp_utc.year - 2000
else:
day = int(date[0:2])
month = int(date[2:4])
year = 2000 + int(date[4:6])
year = int(date[4:6])

self.timestamp_utc = time.struct_time((year, month, day, hours, mins, secs, 0, 0, -1))
self.timestamp_utc = datetime.strptime(
str("%d/%d/%d %d:%d:%d.%d" % (day, month, year, hours, mins, secs, milisecs)),
"%d/%m/%y %H:%M:%S.%f",
)

def _parse_vtg(self, data: List[str]) -> bool:
# VTG - Course Over Ground and Ground Speed
Expand Down
2 changes: 1 addition & 1 deletion examples/gps_displayio_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,6 @@

# Update the label.text property to change the text on the display
display_output_label.text = f"Timestamp (UTC): \
\n{t.tm_mday}/{t.tm_mon}/{t.tm_year} {t.tm_hour}:{t.tm_min:02}:{t.tm_sec:02}\
\n{t.day}/{t.month}/{t.year} {t.hour}:{t.minute:02}:{t.second:02}\
\nLat: {gps.latitude:.6f}\
\nLong: {gps.longitude:.6f}"
15 changes: 8 additions & 7 deletions examples/gps_simpletest.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,14 @@
# Print out details about the fix like location, date, etc.
print("=" * 40) # Print a separator line.
print(
"Fix timestamp: {}/{}/{} {:02}:{:02}:{:02}".format( # noqa: UP032
gps.timestamp_utc.tm_mon, # Grab parts of the time from the
gps.timestamp_utc.tm_mday, # struct_time object that holds
gps.timestamp_utc.tm_year, # the fix time. Note you might
gps.timestamp_utc.tm_hour, # not get all data like year, day,
gps.timestamp_utc.tm_min, # month!
gps.timestamp_utc.tm_sec,
"Fix timestamp: {}/{}/{} {:02}:{:02}:{:02}.{06}".format( # noqa: UP032
gps.timestamp_utc.month, # Grab parts of the time from the
gps.timestamp_utc.day, # struct_time object that holds
gps.timestamp_utc.year, # the fix time. Note you might
gps.timestamp_utc.hour, # not get all data like year, day,
gps.timestamp_utc.minute, # month!
gps.timestamp_utc.second,
gps.timestamp_utc.microsecond,
)
)
print(f"Latitude: {gps.latitude:.6f} degrees")
Expand Down
9 changes: 6 additions & 3 deletions examples/gps_time_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# time while there is powersource (ie coin cell battery)

import time
from datetime import datetime

import board
import busio
Expand All @@ -20,16 +21,18 @@
# gps = adafruit_gps.GPS_GtopI2C(i2c, debug=False) # Use I2C interface

gps.send_command(b"PMTK314,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0")
gps.send_command(b"PMTK220,1000")
gps.send_command(b"PMTK220,100")

print("Set GPS as time source")
rtc.set_time_source(gps)
the_rtc = rtc.RTC()


def _format_datetime(datetime):
date_part = f"{datetime.tm_mon:02}/{datetime.tm_mday:02}/{datetime.tm_year}"
time_part = f"{datetime.tm_hour:02}:{datetime.tm_min:02}:{datetime.tm_sec:02}"
date_part = f"{datetime.month:02}/{datetime.day:02}/{datetime.year}"
time_part = (
f"{datetime.hour:02}:{datetime.minute:02}:{datetime.second:02}.{datetime.microsecond:06}"
)
return f"{date_part} {time_part}"


Expand Down
Loading