Skip to content

Commit 3e4d60c

Browse files
committed
Update SX126x examples
1 parent 5637099 commit 3e4d60c

File tree

6 files changed

+79
-76
lines changed

6 files changed

+79
-76
lines changed

examples/SX126x/driver_rx.py

+21-25
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import os, sys
22
currentdir = os.path.dirname(os.path.realpath(__file__))
33
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
65
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)
1317

1418
# TCXO control setting
1519
dio3Voltage = LoRa.DIO3_OUTPUT_1_8
@@ -41,21 +45,15 @@
4145

4246
# Receive flag
4347
received = False
44-
intSet = False
4548

46-
def checkReceiveDone(channel) :
49+
def checkReceiveDone() :
4750
global received
4851
received = True
4952

5053
def 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 :

examples/SX126x/driver_tx.py

+21-25
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import os, sys
22
currentdir = os.path.dirname(os.path.realpath(__file__))
33
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
65
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)
1317

1418
# TCXO control setting
1519
dio3Voltage = LoRa.DIO3_OUTPUT_1_8
@@ -44,21 +48,15 @@
4448

4549
# Transmit flag
4650
transmitted = False
47-
intSet = False
4851

49-
def checkTransmitDone(channel) :
52+
def checkTransmitDone() :
5053
global transmitted
5154
transmitted = True
5255

5356
def settingFunction() :
5457

5558
print("-- SETTING FUNCTION --")
5659

57-
# SPI and GPIO Pins setting
58-
print("Setting pins")
59-
LoRa.setSpi(busId, csId)
60-
LoRa.setPins(resetPin, busyPin, irqPin, txenPin, rxenPin)
61-
6260
# Reset RF module by setting resetPin to LOW and begin SPI communication
6361
print("Resetting RF module")
6462
LoRa.reset()
@@ -131,15 +129,12 @@ def transmitFunction(message: list, timeout: int) -> int :
131129

132130
# Attach irqPin to DIO1
133131
print(f"Attach interrupt on IRQ pin")
134-
global intSet
135-
if not intSet :
136-
GPIO.setup(irqPin, GPIO.IN)
137-
GPIO.add_event_detect(irqPin, GPIO.RISING, callback=checkTransmitDone, bouncetime=100)
138-
intSet = True
132+
monitoring = Thread(target=irq.monitor, args=(checkTransmitDone, 0.1))
133+
monitoring.start()
139134
# Set rxen and txen pin state for transmitting packet
140-
if txenPin != -1 and rxenPin != -1 :
141-
GPIO.output(txenPin, GPIO.HIGH)
142-
GPIO.output(rxenPin, GPIO.LOW)
135+
if txen != None and rxen != None :
136+
txen.output(LoRaGpio.HIGH)
137+
rxen.output(LoRaGpio.LOW)
143138

144139
# Calculate timeout (timeout duration = timeout * 15.625 us)
145140
tOut = timeout * 64
@@ -153,6 +148,7 @@ def transmitFunction(message: list, timeout: int) -> int :
153148
print("Wait for TX done interrupt")
154149
global transmitted
155150
while not transmitted : pass
151+
monitoring.join()
156152
tTrans = time.time() - tStart
157153
# Clear transmit interrupt flag
158154
transmitted = False
@@ -165,8 +161,8 @@ def transmitFunction(message: list, timeout: int) -> int :
165161
irqStat = LoRa.getIrqStatus()
166162
print("Clear IRQ status")
167163
LoRa.clearIrqStatus(irqStat)
168-
if txenPin != -1 :
169-
GPIO.output(txenPin, GPIO.LOW)
164+
if txen != None :
165+
txen.output(LoRaGpio.LOW)
170166

171167
# Return interrupt status
172168
return irqStat

examples/SX126x/receiver.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import os, sys
22
currentdir = os.path.dirname(os.path.realpath(__file__))
33
sys.path.append(os.path.dirname(os.path.dirname(currentdir)))
4-
from LoRaRF import SX126x
4+
from LoRaRF import SX126x, LoRaSpi, LoRaGpio
55
import time
66

7-
# Begin LoRa radio and set NSS, reset, busy, IRQ, txen, and rxen pin with connected Raspberry Pi gpio pins
8-
# IRQ pin not used in this example (set to -1). Set txen and rxen pin to -1 if RF module doesn't have one
9-
busId = 1; csId = 0
10-
resetPin = 22; busyPin = 23; irqPin = -1; txenPin = 5; rxenPin = 25
11-
LoRa = SX126x()
7+
# Begin LoRa radio with connected SPI bus and IO pins (cs and reset) on GPIO
8+
# SPI is defined by bus ID and cs ID and IO pins defined by chip and offset number
9+
spi = LoRaSpi(0, 0)
10+
cs = LoRaGpio(0, 8)
11+
reset = LoRaGpio(0, 24)
12+
busy = LoRaGpio(0, 23)
13+
LoRa = SX126x(spi, cs, reset, busy)
1214
print("Begin LoRa radio")
13-
if not LoRa.begin(busId, csId, resetPin, busyPin, irqPin, txenPin, rxenPin) :
15+
if not LoRa.begin() :
1416
raise Exception("Something wrong, can't begin LoRa radio")
1517

1618
# Configure LoRa to use TCXO with DIO3 as control

examples/SX126x/receiver_continuous.py

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import os, sys
22
currentdir = os.path.dirname(os.path.realpath(__file__))
33
sys.path.append(os.path.dirname(os.path.dirname(currentdir)))
4-
from LoRaRF import SX126x
4+
from LoRaRF import SX126x, LoRaSpi, LoRaGpio
55
import time
66

7-
# Begin LoRa radio and set NSS, reset, busy, IRQ, txen, and rxen pin with connected Raspberry Pi gpio pins
8-
busId = 1; csId = 0
9-
resetPin = 22; busyPin = 23; irqPin = 26; txenPin = 5; rxenPin = 25
10-
LoRa = SX126x()
7+
# Begin LoRa radio with connected SPI bus and IO pins (cs and reset) on GPIO
8+
# SPI is defined by bus ID and cs ID and IO pins defined by chip and offset number
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+
LoRa = SX126x(spi, cs, reset, busy, irq)
1115
print("Begin LoRa radio")
12-
if not LoRa.begin(busId, csId, resetPin, busyPin, irqPin, txenPin, rxenPin) :
16+
if not LoRa.begin() :
1317
raise Exception("Something wrong, can't begin LoRa radio")
1418

1519
# Configure LoRa to use TCXO with DIO3 as control

examples/SX126x/receiver_listen.py

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
import os, sys
22
currentdir = os.path.dirname(os.path.realpath(__file__))
33
sys.path.append(os.path.dirname(os.path.dirname(currentdir)))
4-
from LoRaRF import SX126x
4+
from LoRaRF import SX126x, LoRaSpi, LoRaGpio
55
import time
66

7-
# Begin LoRa radio and set NSS, reset, busy, IRQ, txen, and rxen pin with connected Raspberry Pi gpio pins
8-
busId = 1; csId = 0
9-
resetPin = 22; busyPin = 23; irqPin = 26; txenPin = 5; rxenPin = 25
10-
LoRa = SX126x()
7+
# Begin LoRa radio with connected SPI bus and IO pins (cs and reset) on GPIO
8+
# SPI is defined by bus ID and cs ID and IO pins defined by chip and offset number
9+
spi = LoRaSpi(0, 0)
10+
cs = LoRaGpio(0, 8)
11+
reset = LoRaGpio(0, 24)
12+
busy = LoRaGpio(0, 23)
13+
LoRa = SX126x(spi, cs, reset, busy)
1114
print("Begin LoRa radio")
12-
if not LoRa.begin(busId, csId, resetPin, busyPin, irqPin, txenPin, rxenPin) :
15+
if not LoRa.begin() :
1316
raise Exception("Something wrong, can't begin LoRa radio")
1417

1518
# Configure LoRa to use TCXO with DIO3 as control

examples/SX126x/transmitter.py

+9-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import os, sys
22
currentdir = os.path.dirname(os.path.realpath(__file__))
33
sys.path.append(os.path.dirname(os.path.dirname(currentdir)))
4-
from LoRaRF import SX126x
4+
from LoRaRF import SX126x, LoRaSpi, LoRaGpio
55
import time
66

7-
# Begin LoRa radio and set NSS, reset, busy, IRQ, txen, and rxen pin with connected Raspberry Pi gpio pins
8-
# IRQ pin not used in this example (set to -1). Set txen and rxen pin to -1 if RF module doesn't have one
9-
busId = 1; csId = 0
10-
resetPin = 22; busyPin = 23; irqPin = -1; txenPin = 5; rxenPin = 25
11-
LoRa = SX126x()
7+
# Begin LoRa radio with connected SPI bus and IO pins (cs and reset) on GPIO
8+
# SPI is defined by bus ID and cs ID and IO pins defined by chip and offset number
9+
spi = LoRaSpi(0, 0)
10+
cs = LoRaGpio(0, 8)
11+
reset = LoRaGpio(0, 24)
12+
busy = LoRaGpio(0, 23)
13+
LoRa = SX126x(spi, cs, reset, busy)
1214
print("Begin LoRa radio")
13-
if not LoRa.begin(busId, csId, resetPin, busyPin, irqPin, txenPin, rxenPin) :
15+
if not LoRa.begin() :
1416
raise Exception("Something wrong, can't begin LoRa radio")
1517

1618
# Configure LoRa to use TCXO with DIO3 as control

0 commit comments

Comments
 (0)