Skip to content

Instantly share code, notes, and snippets.

@daveake
Created February 10, 2017 09:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daveake/4e0d2f6daa1d20e606a92905b3012fc9 to your computer and use it in GitHub Desktop.
Save daveake/4e0d2f6daa1d20e606a92905b3012fc9 to your computer and use it in GitHub Desktop.
from microbit import i2c, sleep
import gc
import math
class GPS(object):
"""
GPS using i2c
"""
def __init__(self):
pass
def GPSChecksumOK(self, Line):
Count = len(Line)
XOR = 0
for i in range(1, Count-4):
c = ord(Line[i])
XOR ^= c
return (Line[Count-4] == '*') and (Line[Count-3:Count-1] == hex(XOR)[2:4].upper())
def FixPosition(self, Position):
Position = Position / 100
MinutesSeconds = math.modf(Position)
return MinutesSeconds[1] + MinutesSeconds[0] * 5 / 3
def SendGPS(self, Bytes):
i2c.write(0x42,Bytes)
def ProcessLine(self, Line):
Position = None
if self.GPSChecksumOK(Line):
if Line[3:6] == "GGA":
Position = {'fix': 0}
# $GNGGA,213511.00,5157.01416,N,00232.65975,W,1,12,0.64,149.8,M,48.6,M,,*55
Fields = Line.split(',')
if Fields[1] != '':
Position['time'] = Fields[1][0:2] + ':' + Fields[1][2:4] + ':' + Fields[1][4:6]
if Fields[2] != '':
Position['lat'] = self.FixPosition(float(Fields[2]))
if Fields[3] == 'S':
Position['lat'] = -Position['lat']
Position['lon'] = self.FixPosition(float(Fields[4]))
if Fields[5] == 'W':
Position['lon'] = -Position['lon']
Position['alt'] = round(float(Fields[9]))
Position['fix'] = int(Fields[6])
Position['sats'] = int(Fields[7])
elif Line[3:6] == "RMC":
print("Disabling RMC")
setRMC = bytearray([0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x04, 0x40])
self.SendGPS(setRMC)
elif Line[3:6] == "GSV":
print("Disabling GSV")
setGSV = bytearray([0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x39])
self.SendGPS(setGSV)
elif Line[3:6] == "GLL":
print("Disabling GLL")
setGLL = bytearray([0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x01, 0x2B])
self.SendGPS(setGLL)
elif Line[3:6] == "GSA":
print("Disabling GSA")
setGSA = bytearray([0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x32])
self.SendGPS(setGSA)
elif Line[3:6] == "VTG":
print("Disabling VTG")
setVTG = bytearray([0xB5, 0x62, 0x06, 0x01, 0x08, 0x00, 0xF0, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x05, 0x47])
self.SendGPS(setVTG)
return Position
def GetGPSPosition(self):
Position = None
Line = ''
while not Position:
Byte = i2c.read(0x42,1)[0]
if Byte != 255:
Character = chr(Byte)
if Character == '$':
Line = Character
elif len(Line) > 90:
Line = ''
elif (Line != '') and (Character != '\r'):
Line = Line + Character
if Character == '\n':
Position = self.ProcessLine(Line)
Line = ''
return Position
sleep(1000)
print("GPS Test Program")
sleep(1000)
print("Read from UBlox GPS on i2c address 0x42")
print()
MaxFree = 0
while True:
MaxFree = max(MaxFree, gc.mem_free())
print(gc.mem_free(), MaxFree)
gps = GPS()
Position = gps.GetGPSPosition()
gps = None
if Position:
print(Position)
#if Position['sats'] >= 10:
# display.show('*')
#else:
# display.show(str(Position['sats']))
else:
print("No position")
#display.show('N')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment