Skip to content

Instantly share code, notes, and snippets.

@daveake
Created February 9, 2017 12:43
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/9e5c64c8bec1f5c844ead6ea7475061a to your computer and use it in GitHub Desktop.
Save daveake/9e5c64c8bec1f5c844ead6ea7475061a to your computer and use it in GitHub Desktop.
from microbit import *
import math
# Write your code here :-)
def GPSChecksumOK(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(Position):
Position = Position / 100
MinutesSeconds = math.modf(Position)
return MinutesSeconds[1] + MinutesSeconds[0] * 5 / 3
def SendGPS(Bytes):
i2c.write(0x42,Bytes)
def ProcessLine(Line):
Position = None
if 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'] = FixPosition(float(Fields[2]))
if Fields[3] == 'S':
Position['lat'] = -Position['lat']
Position['lon'] = FixPosition(float(Fields[4]))
if Fields[5] == 'W':
Position['lon'] = -Position['lon']
Position['alt'] = round(float(Fields[9]))
if Position['fix'] != int(Fields[6]):
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])
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])
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])
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])
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])
SendGPS(setVTG)
else:
print("Unknown NMEA sentence " + Line)
else:
print("Bad checksum")
return Position
def GetGPSPosition():
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 = ProcessLine(Line)
Line = ''
return Position
sleep(1000)
print("GPS Test Program")
sleep(1000)
print("Read from UBlox GPS on i2c address 0x42")
print()
while True:
Position = GetGPSPosition()
if Position:
print(Position)
if Position['sats'] >= 10:
display.show('*')
else:
display.show(str(Position['sats']))
else:
print("No position")
display.show('N')
from microbit import *
import array
class LoRa(object):
"""
Radio - LoRa. Single channel.
"""
def __init__(self, Frequency=434.450, Mode=1):
self.sending = False
self.Frequency = Frequency
self.currentMode = 0x81
self.Power = 0x88
pin16.write_digital(1) # Deselect CS
spi.init()
self.__writeRegister(0x41, 0x00) # REG_DIO_MAPPING_2
self.SetLoRaFrequency(Frequency)
self.SetStandardLoRaParameters(Mode)
def __readRegister(self, register):
data = bytearray([register & 0x7F, 0])
pin16.write_digital(0)
spi.write_readinto(data, data)
pin16.write_digital(1)
return data[1]
def __writeRegister(self, register, value):
pin16.write_digital(0)
spi.write(bytearray([register | 0x80, value]))
pin16.write_digital(1)
def __setMode(self, newMode):
if newMode != self.currentMode:
if newMode == 0x83: # RF98_MODE_TX
# TURN LNA OFF FOR TRANSMIT
self.__writeRegister(0x0C, 0x00) # REG_LNA, LNA_OFF_GAIN
# Set 10mW
self.__writeRegister(0x09, self.Power) # REG_PA_CONFIG
elif newMode == 0x85: # RF98_MODE_RX_CONTINUOUS
# PA Off
self.__writeRegister(0x09, 0x00) # REG_PA_CONFIG, PA_OFF_BOOST
# Max LNA Gain
self.__writeRegister(0x0C, 0x23) # REG_LNA, LNA_MAX_GAIN
self.__writeRegister(0x01, newMode) # REG_OPMODE
self.currentMode = newMode
if newMode != 0x80: # RF98_MODE_SLEEP
#while not self.DIO5.is_active:
# pass
# time.sleep(0.1)
pass
def SetLoRaFrequency(self, Frequency):
self.__setMode(0x81) # RF98_MODE_STANDBY
self.__setMode(0x80) # RF98_MODE_SLEEP
self.__writeRegister(0x01, 0x80) # REG_OPMODE
self.__setMode(0x81) # RF98_MODE_STANDBY
FrequencyValue = int((Frequency * 7110656) / 434)
self.__writeRegister(0x06, (FrequencyValue >> 16) & 0xFF)
self.__writeRegister(0x07, (FrequencyValue >> 8) & 0xFF)
self.__writeRegister(0x08, FrequencyValue & 0xFF)
def SetLoRaParameters(self, ImplicitOrExplicit, ErrorCoding, Bandwidth, SpreadingFactor, LowDataRateOptimize):
self.__writeRegister(0x1D, ImplicitOrExplicit | ErrorCoding | Bandwidth)
self.__writeRegister(0x1E, SpreadingFactor | 0x04)
self.__writeRegister(0x26, 0x04 | (0x08 if LowDataRateOptimize else 0))
self.__writeRegister(0x31, (self.__readRegister(0x31) & 0xF8) | (0x05 if (SpreadingFactor == 0x60) else 0x03))
self.__writeRegister(0x37, 0x0C if (SpreadingFactor == 0x60) else 0x0A)
self.PayloadLength = 255 if ImplicitOrExplicit else 0
self.__writeRegister(0x22, self.PayloadLength)
self.__writeRegister(0x1A, self.PayloadLength)
def SetStandardLoRaParameters(self, Mode):
if Mode == 0:
self.SetLoRaParameters(0x00, 0x08, 0x30, 0xB0, True)
elif Mode == 1:
self.SetLoRaParameters(0x01, 0x02, 0x30, 0x60, False)
elif Mode == 2:
self.SetLoRaParameters(0x00, 0x08, 0x60, 0x80, False)
def send_packet(self, packet):
self.sending = True
self.__setMode(0x81) # RF98_MODE_STANDBY)
# map DIO0 to TxDone
self.__writeRegister(0x40, 0x40) # REG_DIO_MAPPING_1
self.__writeRegister(0x0E, 0x00) # REG_FIFO_TX_BASE_AD
self.__writeRegister(0x0D, 0x00) # REG_FIFO_ADDR_PTR
# data = bytearray([0x80] + packet + [0]
pin16.write_digital(0)
# spi.write(data)
spi.write(bytearray([0x80]))
spi.write(packet)
spi.write(bytearray([0]))
pin16.write_digital(1)
self.__writeRegister(0x22, self.PayloadLength if self.PayloadLength else len(packet)) # REG_PAYLOAD_LENGTH
self.__setMode(0x83) # RF98_MODE_TX
def send_text(self, sentence):
self.send_packet(bytearray(sentence))
sleep(1000)
print("LoRa Test Program")
sleep(1000)
print("Create LoRa object")
MyLoRa = LoRa()
print("Send message")
MyLoRa.send_text("$$Hello World!\n")
print("Sent")
print()
@carlosperate
Copy link

carlosperate commented Feb 9, 2017

As mentioned on twitter, explicit imports should help.
Apart from that have a look at http://docs.micropython.org/en/v1.7/pyboard/library/gc.html to force the garbage collector.
There are a couple of functions there doing a lot of allocations, so try calling the collection immediately after those, see if that helps.
You can also print how much free memory you've got a different points, so that could help you track where you are running out of ram.

By using the filesystem you can import both files into a main.py file and there would be no need to merge them into one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment