Skip to content

Commit 5637099

Browse files
committed
Implement gpiod event for TX and RX interrupt
1 parent 7da122d commit 5637099

File tree

3 files changed

+74
-8
lines changed

3 files changed

+74
-8
lines changed

LoRaRF/SX126x.py

+23-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from .base import LoRaSpi, LoRaGpio, BaseLoRa
22
from typing import Optional
33
import time
4+
from threading import Thread
45

56
class SX126x(BaseLoRa) :
67
"""Class for SX1261/62/68 and LLCC68 LoRa chipsets from Semtech"""
@@ -639,7 +640,12 @@ def endPacket(self, timeout: int = TX_SINGLE) -> bool :
639640
self._transmitTime = time.time()
640641

641642
# set operation status to wait and attach TX interrupt handler
642-
643+
if self._irq != None :
644+
if isinstance(self._monitoring, Thread):
645+
self._monitoring.join()
646+
to = self._irqTimeout/1000 if timeout == self.TX_SINGLE else timeout/1000
647+
self._monitoring = Thread(target=self._irq.monitor, args=(self._interruptTx, to))
648+
self._monitoring.start()
643649
return True
644650

645651
def write(self, data, length: int = 0) :
@@ -700,7 +706,16 @@ def request(self, timeout: int = RX_SINGLE) -> bool :
700706
self.setRx(rxTimeout)
701707

702708
# set operation status to wait and attach RX interrupt handler
703-
709+
if self._irq != None :
710+
if isinstance(self._monitoring, Thread):
711+
self._monitoring.join()
712+
to = self._irqTimeout/1000 if timeout == self.RX_SINGLE else timeout/1000
713+
if timeout == self.RX_CONTINUOUS:
714+
self._monitoring = Thread(target=self._irq.monitor_continuous, args=(self._interruptRxContinuous, to))
715+
self._monitoring.setDaemon(True)
716+
else:
717+
self._monitoring = Thread(target=self._irq.monitor, args=(self._interruptRx, to))
718+
self._monitoring.start()
704719
return True
705720

706721
def listen(self, rxPeriod: int, sleepPeriod: int) -> bool :
@@ -731,7 +746,12 @@ def listen(self, rxPeriod: int, sleepPeriod: int) -> bool :
731746
self.setRxDutyCycle(rxPeriod, sleepPeriod)
732747

733748
# set operation status to wait and attach RX interrupt handler
734-
749+
if self._irq != None :
750+
if isinstance(self._monitoring, Thread):
751+
self._monitoring.join()
752+
to = self._irqTimeout/1000 if rxPeriod == self.RX_SINGLE else rxPeriod/1000
753+
self._monitoring = Thread(target=self._irq.monitor, args=(self._interruptRx, to))
754+
self._monitoring.start()
735755
return True
736756

737757
def available(self) -> int :

LoRaRF/SX127x.py

+24-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from .base import LoRaSpi, LoRaGpio, BaseLoRa
22
from typing import Optional
33
import time
4+
from threading import Thread
45

56
class SX127x(BaseLoRa) :
67
"""Class for SX1276/77/78/79 LoRa chipsets from Semtech"""
@@ -468,7 +469,13 @@ def endPacket(self, timeout: int = 0) -> bool :
468469
self._transmitTime = time.time()
469470

470471
# set TX done interrupt on DIO0 and attach TX interrupt handler
471-
472+
if self._irq != None :
473+
self.writeRegister(self.REG_DIO_MAPPING_1, self.DIO0_TX_DONE)
474+
if isinstance(self._monitoring, Thread):
475+
self._monitoring.join()
476+
to = self._irqTimeout/1000 if timeout == 0 else timeout/1000
477+
self._monitoring = Thread(target=self._irq.monitor, args=(self._interruptTx, to))
478+
self._monitoring.start()
472479
return True
473480

474481
def write(self, data, length: int = 0) :
@@ -539,7 +546,17 @@ def request(self, timeout: int = 0) -> bool :
539546
self.writeRegister(self.REG_OP_MODE, self._modem | rxMode)
540547

541548
# set RX done interrupt on DIO0 and attach RX interrupt handler
542-
549+
if self._irq != None :
550+
self.writeRegister(self.REG_DIO_MAPPING_1, self.DIO0_RX_DONE)
551+
if isinstance(self._monitoring, Thread):
552+
self._monitoring.join()
553+
to = self._irqTimeout/1000 if timeout == 0 else timeout/1000
554+
if timeout == self.RX_CONTINUOUS:
555+
self._monitoring = Thread(target=self._irq.monitor_continuous, args=(self._interruptRxContinuous, to))
556+
self._monitoring.setDaemon(True)
557+
else:
558+
self._monitoring = Thread(target=self._irq.monitor, args=(self._interruptRx, to))
559+
self._monitoring.start()
543560
return True
544561

545562
def available(self) :
@@ -779,14 +796,16 @@ def writeRegister(self, address: int, data: int) :
779796

780797
self._transfer(address | 0x80, data)
781798

782-
def readRegister(self, address: int) ->int:
799+
def readRegister(self, address: int) -> int :
783800

784801
return self._transfer(address & 0x7F, 0x00)
785802

786-
def _transfer(self, address: int, data: int) ->int:
803+
def _transfer(self, address: int, data: int) -> int :
787804

788805
buf = [address, data]
789-
feedback = self._spi.xfer2(buf)
806+
self._cs.output(LoRaGpio.LOW)
807+
feedback = self._spi.transfer(buf)
808+
self._cs.output(LoRaGpio.HIGH)
790809
if (len(feedback) == 2) :
791810
return int(feedback[1])
792811
return -1

LoRaRF/base.py

+27
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,33 @@ def input(self) -> int:
5555
chip.close()
5656
return value
5757

58+
def monitor(self, callback, timeout: float):
59+
seconds = int(timeout)
60+
chip = gpiod.Chip(self.chip)
61+
line = chip.get_line(self.offset)
62+
try:
63+
line.request(consumer="LoRaGpio", type=gpiod.LINE_REQ_EV_RISING_EDGE)
64+
if line.event_wait(seconds, int((timeout - seconds) * 1000000000)):
65+
callback()
66+
except: return
67+
finally:
68+
line.release()
69+
chip.close()
70+
71+
def monitor_continuous(self, callback, timeout: float):
72+
seconds = int(timeout)
73+
while True:
74+
chip = gpiod.Chip(self.chip)
75+
line = chip.get_line(self.offset)
76+
try:
77+
line.request(consumer="LoRaGpio", type=gpiod.LINE_REQ_EV_RISING_EDGE)
78+
if line.event_wait(seconds, int((timeout - seconds) * 1000000000)):
79+
callback()
80+
except: continue
81+
finally:
82+
line.release()
83+
chip.close()
84+
5885

5986
class BaseLoRa :
6087

0 commit comments

Comments
 (0)