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 SX127x
5
- import RPi .GPIO
4
+ from LoRaRF import SX127x , LoRaSpi , LoRaGpio
6
5
import time
6
+ from threading import Thread
7
7
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 )
14
16
15
17
# RF frequency setting
16
18
frequency = 915000000
32
34
# SyncWord setting
33
35
syncword = 0x12
34
36
37
+ # Receive flag
35
38
received = False
36
- intSet = False
37
39
38
40
def checkReceiveDone (channel ) :
39
41
global received
@@ -43,11 +45,6 @@ def settingFunction() :
43
45
44
46
print ("-- SETTING FUNCTION --" )
45
47
46
- # SPI and Pin setting
47
- print ("Setting pins" )
48
- LoRa .setSpi (busId , csId )
49
- LoRa .setPins (resetPin , irqPin , txenPin , rxenPin )
50
-
51
48
# Reset RF module by setting resetPin to LOW and begin SPI communication
52
49
LoRa .reset ()
53
50
version = LoRa .readRegister (LoRa .REG_VERSION )
@@ -115,16 +112,13 @@ def receiveFunction(message: list) :
115
112
LoRa .writeRegister (LoRa .REG_DIO_MAPPING_1 , LoRa .DIO0_RX_DONE )
116
113
# Attach irqPin to DIO0
117
114
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 ()
123
117
124
118
# 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 )
128
122
129
123
# Receive message
130
124
print ("Receiving message..." )
@@ -134,6 +128,7 @@ def receiveFunction(message: list) :
134
128
print ("Wait for RX done interrupt" )
135
129
global received
136
130
while not received : pass
131
+ monitoring .join ()
137
132
# Clear transmit interrupt flag
138
133
received = False
139
134
print ("Receive done" )
@@ -146,8 +141,8 @@ def receiveFunction(message: list) :
146
141
irqStat = LoRa .readRegister (LoRa .REG_IRQ_FLAGS )
147
142
LoRa .writeRegister (LoRa .REG_IRQ_FLAGS , 0xFF )
148
143
print ("Clear IRQ status" )
149
- if rxenPin != - 1 :
150
- GPIO .output (rxenPin , GPIO .LOW )
144
+ if rxen != None :
145
+ rxen .output (LoRaGpio .LOW )
151
146
152
147
# Get FIFO address of received message and configure address pointer
153
148
reg = LoRa .readRegister (LoRa .REG_FIFO_RX_CURRENT_ADDR )
0 commit comments