11import os , sys
22currentdir = os .path .dirname (os .path .realpath (__file__ ))
33sys .path .append (os .path .dirname (os .path .dirname (currentdir )))
4- from LoRaRF import SX127x
5- import RPi .GPIO
4+ from LoRaRF import SX127x , LoRaSpi , LoRaGpio
65import time
6+ from threading import Thread
77
8- LoRa = SX127x ()
9- GPIO = RPi .GPIO
10-
11- # Pin setting
12- busId = 1 ; csId = 0
13- resetPin = 22 ; irqPin = 26 ; txenPin = - 1 ; rxenPin = - 1
8+ # Begin LoRa radio with connected SPI bus and IO pins (cs, reset and busy) on GPIO
9+ spi = LoRaSpi (0 , 0 )
10+ cs = LoRaGpio (0 , 8 )
11+ reset = LoRaGpio (0 , 24 )
12+ irq = LoRaGpio (0 , 17 )
13+ txen = LoRaGpio (0 , 5 )
14+ rxen = LoRaGpio (0 , 25 )
15+ LoRa = SX127x (spi , cs , reset )
1416
1517# RF frequency setting
1618frequency = 915000000
3234# SyncWord setting
3335syncword = 0x12
3436
37+ # Receive flag
3538received = False
36- intSet = False
3739
3840def checkReceiveDone (channel ) :
3941 global received
@@ -43,11 +45,6 @@ def settingFunction() :
4345
4446 print ("-- SETTING FUNCTION --" )
4547
46- # SPI and Pin setting
47- print ("Setting pins" )
48- LoRa .setSpi (busId , csId )
49- LoRa .setPins (resetPin , irqPin , txenPin , rxenPin )
50-
5148 # Reset RF module by setting resetPin to LOW and begin SPI communication
5249 LoRa .reset ()
5350 version = LoRa .readRegister (LoRa .REG_VERSION )
@@ -115,16 +112,13 @@ def receiveFunction(message: list) :
115112 LoRa .writeRegister (LoRa .REG_DIO_MAPPING_1 , LoRa .DIO0_RX_DONE )
116113 # Attach irqPin to DIO0
117114 print (f"Attach interrupt on IRQ pin" )
118- global intSet
119- if not intSet :
120- GPIO .setup (irqPin , GPIO .IN )
121- GPIO .add_event_detect (irqPin , GPIO .RISING , callback = checkReceiveDone , bouncetime = 100 )
122- intSet = True
115+ monitoring = Thread (target = irq .monitor , args = (checkReceiveDone , 0.1 ))
116+ monitoring .start ()
123117
124118 # Set txen and rxen pin state for receiving packet
125- if txenPin != - 1 and rxenPin != - 1 :
126- GPIO .output (txenPin , GPIO .LOW )
127- GPIO .output (rxenPin , GPIO .HIGH )
119+ if txen != None and rxen != None :
120+ txen .output (LoRaGpio .LOW )
121+ rxen .output (LoRaGpio .HIGH )
128122
129123 # Receive message
130124 print ("Receiving message..." )
@@ -134,6 +128,7 @@ def receiveFunction(message: list) :
134128 print ("Wait for RX done interrupt" )
135129 global received
136130 while not received : pass
131+ monitoring .join ()
137132 # Clear transmit interrupt flag
138133 received = False
139134 print ("Receive done" )
@@ -146,8 +141,8 @@ def receiveFunction(message: list) :
146141 irqStat = LoRa .readRegister (LoRa .REG_IRQ_FLAGS )
147142 LoRa .writeRegister (LoRa .REG_IRQ_FLAGS , 0xFF )
148143 print ("Clear IRQ status" )
149- if rxenPin != - 1 :
150- GPIO .output (rxenPin , GPIO .LOW )
144+ if rxen != None :
145+ rxen .output (LoRaGpio .LOW )
151146
152147 # Get FIFO address of received message and configure address pointer
153148 reg = LoRa .readRegister (LoRa .REG_FIFO_RX_CURRENT_ADDR )
0 commit comments