Skip to content

Commit 9e0684d

Browse files
committed
Update SX127x examples
1 parent 3e4d60c commit 9e0684d

File tree

6 files changed

+71
-74
lines changed

6 files changed

+71
-74
lines changed

examples/SX127x/driver_rx.py

+19-24
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 SX127x
5-
import RPi.GPIO
4+
from LoRaRF import SX127x, LoRaSpi, LoRaGpio
65
import 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
1618
frequency = 915000000
@@ -32,8 +34,8 @@
3234
# SyncWord setting
3335
syncword = 0x12
3436

37+
# Receive flag
3538
received = False
36-
intSet = False
3739

3840
def 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)

examples/SX127x/driver_tx.py

+19-24
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 SX127x
5-
import RPi.GPIO
4+
from LoRaRF import SX127x, LoRaSpi, LoRaGpio
65
import 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
1618
frequency = 915000000
@@ -33,8 +35,8 @@
3335
# SyncWord setting
3436
syncword = 0x12
3537

38+
# Transmit flag
3639
transmitted = False
37-
intSet = False
3840

3941
def checkTransmitDone(channel) :
4042
global transmitted
@@ -44,11 +46,6 @@ def settingFunction() :
4446

4547
print("-- SETTING FUNCTION --")
4648

47-
# SPI and Pin setting
48-
print("Setting pins")
49-
LoRa.setSpi(busId, csId)
50-
LoRa.setPins(resetPin, irqPin, txenPin, rxenPin)
51-
5249
# Reset RF module by setting resetPin to LOW and begin SPI communication
5350
LoRa.reset()
5451
version = LoRa.readRegister(LoRa.REG_VERSION)
@@ -133,16 +130,13 @@ def transmitFunction(message: list) :
133130
LoRa.writeRegister(LoRa.REG_DIO_MAPPING_1, LoRa.DIO0_TX_DONE)
134131
# Attach irqPin to DIO0
135132
print(f"Attach interrupt on IRQ pin")
136-
global intSet
137-
if not intSet :
138-
GPIO.setup(irqPin, GPIO.IN)
139-
GPIO.add_event_detect(irqPin, GPIO.RISING, callback=checkTransmitDone, bouncetime=100)
140-
intSet = True
133+
monitoring = Thread(target=irq.monitor, args=(checkTransmitDone, 0.1))
134+
monitoring.start()
141135

142136
# Set txen and rxen pin state for transmitting packet
143-
if txenPin != -1 and rxenPin != -1 :
144-
GPIO.output(txenPin, GPIO.HIGH)
145-
GPIO.output(rxenPin, GPIO.LOW)
137+
if txen != None and rxen != None :
138+
txen.output(LoRaGpio.HIGH)
139+
rxen.output(LoRaGpio.LOW)
146140

147141
# Transmit message
148142
print("Transmitting message...")
@@ -153,6 +147,7 @@ def transmitFunction(message: list) :
153147
print("Wait for TX done interrupt")
154148
global transmitted
155149
while not transmitted : pass
150+
monitoring.join()
156151
tTrans = (time.time() - tStart) * 1000
157152
# Clear transmit interrupt flag
158153
transmitted = False
@@ -165,8 +160,8 @@ def transmitFunction(message: list) :
165160
irqStat = LoRa.readRegister(LoRa.REG_IRQ_FLAGS)
166161
LoRa.writeRegister(LoRa.REG_IRQ_FLAGS, 0xFF)
167162
print("Clear IRQ status")
168-
if txenPin != -1 :
169-
GPIO.output(txenPin, GPIO.LOW)
163+
if txen != None :
164+
txen.output(LoRaGpio.LOW)
170165

171166
# return interrupt status
172167
return irqStat

examples/SX127x/receiver.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
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 SX127x
4+
from LoRaRF import SX127x, 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; irqPin = -1; txenPin = -1; rxenPin = -1
11-
LoRa = SX127x()
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+
LoRa = SX127x(spi, cs, reset)
1213
print("Begin LoRa radio")
13-
if not LoRa.begin(busId, csId, resetPin, irqPin, txenPin, rxenPin) :
14+
if not LoRa.begin() :
1415
raise Exception("Something wrong, can't begin LoRa radio")
1516

1617
# Set frequency to 915 Mhz

examples/SX127x/receiver_callback.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 SX127x
4+
from LoRaRF import SX127x, 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; irqPin = 26; txenPin = -1; rxenPin = -1
10-
LoRa = SX127x()
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+
irq = LoRaGpio(0, 17)
13+
LoRa = SX127x(spi, cs, reset, irq)
1114
print("Begin LoRa radio")
12-
if not LoRa.begin(busId, csId, resetPin, irqPin, txenPin, rxenPin) :
15+
if not LoRa.begin() :
1316
raise Exception("Something wrong, can't begin LoRa radio")
1417

1518
# Set frequency to 915 Mhz

examples/SX127x/receiver_timeout.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
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 SX127x
4+
from LoRaRF import SX127x, 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; irqPin = -1; txenPin = -1; rxenPin = -1
10-
LoRa = SX127x()
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+
LoRa = SX127x(spi, cs, reset)
1113
print("Begin LoRa radio")
12-
if not LoRa.begin(busId, csId, resetPin, irqPin, txenPin, rxenPin) :
14+
if not LoRa.begin() :
1315
raise Exception("Something wrong, can't begin LoRa radio")
1416

1517
# Set frequency to 915 Mhz

examples/SX127x/transmitter.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
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 SX127x
4+
from LoRaRF import SX127x, 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; irqPin = -1; txenPin = -1; rxenPin = -1
11-
LoRa = SX127x()
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+
LoRa = SX127x(spi, cs, reset)
1213
print("Begin LoRa radio")
13-
if not LoRa.begin(busId, csId, resetPin, irqPin, txenPin, rxenPin) :
14+
if not LoRa.begin() :
1415
raise Exception("Something wrong, can't begin LoRa radio")
1516

1617
# Set frequency to 915 Mhz

0 commit comments

Comments
 (0)