Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
from microbit import spi, sleep, pin16
import array
import gc
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")
MaxFree = 0
MyLoRa = LoRa()
while True:
MaxFree = max(MaxFree, gc.mem_free())
print(gc.mem_free(), MaxFree)
print("Send message")
MyLoRa.send_text("$$Hello World!\n")
print("Sent")
sleep(2000)
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment