1
1
import os , sys
2
2
currentdir = os .path .dirname (os .path .realpath (__file__ ))
3
3
sys .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
6
5
import 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 )
13
17
14
18
# TCXO control setting
15
19
dio3Voltage = LoRa .DIO3_OUTPUT_1_8
41
45
42
46
# Receive flag
43
47
received = False
44
- intSet = False
45
48
46
- def checkReceiveDone (channel ) :
49
+ def checkReceiveDone () :
47
50
global received
48
51
received = True
49
52
50
53
def settingFunction () :
51
54
52
55
print ("-- SETTING FUNCTION --" )
53
56
54
- # SPI and GPIO Pins setting
55
- print ("Setting pins" )
56
- LoRa .setSpi (busId , csId )
57
- LoRa .setPins (resetPin , busyPin , irqPin , txenPin , rxenPin )
58
-
59
57
# Reset RF module by setting resetPin to LOW and begin SPI communication
60
58
print ("Resetting RF module" )
61
59
LoRa .reset ()
@@ -112,16 +110,13 @@ def receiveFunction(message: list, timeout: int) -> int :
112
110
LoRa .setDioIrqParams (mask , mask , LoRa .IRQ_NONE , LoRa .IRQ_NONE )
113
111
# Attach irqPin to DIO1
114
112
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 ()
120
115
121
116
# 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 )
125
120
126
121
# Calculate timeout (timeout duration = timeout * 15.625 us)
127
122
tOut = timeout * 64
@@ -133,15 +128,16 @@ def receiveFunction(message: list, timeout: int) -> int :
133
128
print ("Wait for RX done interrupt" )
134
129
global received
135
130
while not received : pass
131
+ monitoring .join ()
136
132
# Clear transmit interrupt flag
137
133
received = False
138
134
139
135
# Clear the interrupt status
140
136
irqStat = LoRa .getIrqStatus ()
141
137
print ("Clear IRQ status" )
142
138
LoRa .clearIrqStatus (irqStat )
143
- if rxenPin != - 1 :
144
- GPIO .output (rxenPin , GPIO .LOW )
139
+ if rxen != None :
140
+ rxen .output (LoRaGpio .LOW )
145
141
146
142
# Exit function if timeout reached
147
143
if irqStat & LoRa .IRQ_TIMEOUT :
0 commit comments