Skip to content

Commit 7df86aa

Browse files
Tinyu-Zhaolbuque
authored andcommitted
lib/units: GPSV11 latitude and longitude conversion degrees, minutes.
Signed-off-by: Tinyu <[email protected]>
1 parent 4f26906 commit 7df86aa

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

m5stack/libs/unit/gps_v11.py

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -283,8 +283,8 @@ def _decode_rmc(self, data: str):
283283
int(time_buf[2:4]),
284284
int(time_buf[4:6]),
285285
]
286-
self.latitude = gps_list[3] + gps_list[4]
287-
self.longitude = gps_list[5] + gps_list[6]
286+
self.latitude = self._convert_to_decimal(gps_list[3], gps_list[4])
287+
self.longitude = self._convert_to_decimal(gps_list[5], gps_list[6], False)
288288
self.speed_ground_knot = gps_list[7]
289289
self.corse_ground_degree = gps_list[8]
290290
data_buf = gps_list[9]
@@ -303,6 +303,33 @@ def _decode_rmc(self, data: str):
303303
buf = time.mktime(t)
304304
self.timestamp = buf
305305

306+
def _convert_to_decimal(self, degrees_minutes, direction, latitude: bool = True) -> float:
307+
"""
308+
note:
309+
en: Convert latitude or longitude from degrees minutes format to decimal format.
310+
311+
params:
312+
degrees_minutes:
313+
note: Latitude or Longitude in DDMM.MMMM format (e.g., "2242.10772").
314+
direction:
315+
note: Direction of the coordinate ("N", "S", "E", "W").
316+
latitude:
317+
note: True if latitude, False if longitude.
318+
319+
returns:
320+
note: The decimal value of the coordinate.
321+
"""
322+
if latitude:
323+
degrees = degrees_minutes[:2] # First two digits are degrees
324+
minutes = degrees_minutes[2:] # The rest are minutes
325+
else:
326+
degrees = degrees_minutes[:3]
327+
minutes = degrees_minutes[3:]
328+
decimal = int(degrees) + round(float(minutes) / 60.0, 6)
329+
if direction in ["S", "W"]:
330+
decimal = -decimal
331+
return decimal
332+
306333
def _decode_txt(self, data: str):
307334
"""
308335
note:

0 commit comments

Comments
 (0)