From cdbee17f949295e042e89c1cf2baeec4655012f1 Mon Sep 17 00:00:00 2001 From: Alessandro Mauri Date: Sun, 20 Sep 2026 12:53:37 +0200 Subject: [PATCH] test fix lora configuration and transmission --- main.py | 48 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 43 insertions(+), 5 deletions(-) diff --git a/main.py b/main.py index c928d6c..6e34175 100644 --- a/main.py +++ b/main.py @@ -378,12 +378,40 @@ class LoRaGSStateMachine: # --- Hardware Interface Stubs --- # You will need to implement these using LoRaRF or your chosen SPI library - def lora_transmit_timeout(self, data: bytes, timeout_ms: int, force: bool) -> bool: - """Transmit packet over SPI to SX1262 and update self.last_tx_time.""" - # TODO: Implement via LoRaRF +# def lora_transmit_timeout(self, data: bytes, timeout_ms: int, force: bool) -> bool: +# """Transmit packet over SPI to SX1262 and update self.last_tx_time.""" +# # TODO: Implement via LoRaRF +# LoRa.beginPacket() +# LoRa.write(list(data), len(data)) +# LoRa.endPacket(timeout_ms * 64) +# self.last_tx_time = self.now_ms() +# return True + def lora_transmit_timeout( + self, + data: bytes, + timeout_ms: int, + force: bool + ) -> bool: + LoRa.beginPacket() LoRa.write(list(data), len(data)) - LoRa.endPacket(timeout_ms * 64) + + # timeout is already in milliseconds. + if not LoRa.endPacket(timeout_ms): + print("Failed to start LoRa TX") + return False + + # endPacket() is asynchronous. DO NOT enter RX until TX is done. + if not LoRa.wait(timeout_ms / 1000.0): + print("LoRa TX host timeout") + return False + + status = LoRa.status() + + if status != LoRa.STATUS_TX_DONE: + print(f"LoRa TX failed, status={status}") + return False + self.last_tx_time = self.now_ms() return True @@ -435,7 +463,17 @@ def main(): LoRa.setLoRaModulation(spreading_factor,EU868_BANDS[band].ch_bw_khz*1000, coding_rate, False) LoRa.setTxPower(10, LoRa.TX_POWER_SX1262) LoRa.setRxGain(LoRa.RX_GAIN_BOOSTED) - LoRa.setHeaderType(LoRa.HEADER_EXPLICIT) + #LoRa.setHeaderType(LoRa.HEADER_EXPLICIT) + LoRa.setLoRaPacket( + LoRa.HEADER_EXPLICIT, + 8, # preamble symbols: match RadioLib + 15, # sync/connect/accept packet length + True, # CRC enabled + False # normal IQ + ) + # Explicitly make the sync word identical on both radios. + LoRa.setSyncWord(0x12) + print("LoRa parameters set") state_machine = LoRaGSStateMachine(LORA_GS_ID, LORA_FC_ID)