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 SX126x
5- import RPi .GPIO
4+ from LoRaRF import SX126x , LoRaSpi , LoRaGpio
65import time
7-
8- busId = 1 ; csId = 0
9- resetPin = 22 ; busyPin = 23 ; irqPin = 26 ; txenPin = 5 ; rxenPin = 25
10-
11- LoRa = SX126x ()
12- GPIO = RPi .GPIO
6+ from threading import Thread
7+
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+ busy = LoRaGpio (0 , 23 )
13+ irq = LoRaGpio (0 , 17 )
14+ txen = LoRaGpio (0 , 5 )
15+ rxen = LoRaGpio (0 , 25 )
16+ LoRa = SX126x (spi , cs , reset , busy )
1317
1418# TCXO control setting
1519dio3Voltage = LoRa .DIO3_OUTPUT_1_8
4145
4246# Receive flag
4347received = False
44- intSet = False
4548
46- def checkReceiveDone (channel ) :
49+ def checkReceiveDone () :
4750 global received
4851 received = True
4952
5053def settingFunction () :
5154
5255 print ("-- SETTING FUNCTION --" )
5356
54- # SPI and GPIO Pins setting
55- print ("Setting pins" )
56- LoRa .setSpi (busId , csId )
57- LoRa .setPins (resetPin , busyPin , irqPin , txenPin , rxenPin )
58-
5957 # Reset RF module by setting resetPin to LOW and begin SPI communication
6058 print ("Resetting RF module" )
6159 LoRa .reset ()
@@ -112,16 +110,13 @@ def receiveFunction(message: list, timeout: int) -> int :
112110 LoRa .setDioIrqParams (mask , mask , LoRa .IRQ_NONE , LoRa .IRQ_NONE )
113111 # Attach irqPin to DIO1
114112 print (f"Attach interrupt on IRQ pin" )
115- global intSet
116- if not intSet :
117- GPIO .setup (irqPin , GPIO .IN )
118- GPIO .add_event_detect (irqPin , GPIO .RISING , callback = checkReceiveDone , bouncetime = 100 )
119- intSet = True
113+ monitoring = Thread (target = irq .monitor , args = (checkReceiveDone , 0.1 ))
114+ monitoring .start ()
120115
121116 # Set rxen and txen pin state for receiving packet
122- if txenPin != - 1 and rxenPin != - 1 :
123- GPIO .output (txenPin , GPIO .LOW )
124- GPIO .output (rxenPin , GPIO .HIGH )
117+ if txen != None and rxen != None :
118+ txen .output (LoRaGpio .LOW )
119+ rxen .output (LoRaGpio .HIGH )
125120
126121 # Calculate timeout (timeout duration = timeout * 15.625 us)
127122 tOut = timeout * 64
@@ -133,15 +128,16 @@ def receiveFunction(message: list, timeout: int) -> int :
133128 print ("Wait for RX done interrupt" )
134129 global received
135130 while not received : pass
131+ monitoring .join ()
136132 # Clear transmit interrupt flag
137133 received = False
138134
139135 # Clear the interrupt status
140136 irqStat = LoRa .getIrqStatus ()
141137 print ("Clear IRQ status" )
142138 LoRa .clearIrqStatus (irqStat )
143- if rxenPin != - 1 :
144- GPIO .output (rxenPin , GPIO .LOW )
139+ if rxen != None :
140+ rxen .output (LoRaGpio .LOW )
145141
146142 # Exit function if timeout reached
147143 if irqStat & LoRa .IRQ_TIMEOUT :
0 commit comments