From e1d89282efd53542ff836820a740ca61ae1bb7f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominic=20H=C3=B6glinger?= Date: Wed, 17 Sep 2025 18:50:11 +0200 Subject: [PATCH] Radio: Refactor RX/TX packages --- Drivers/SX126x/Source/Sx1262.cpp | 13 +++------ Drivers/SX126x/Source/Sx1262.h | 2 +- .../Include/Tactility/hal/radio/RadioDevice.h | 28 +++++++++---------- Tactility/Source/hal/radio/RadioDevice.cpp | 2 ++ 4 files changed, 21 insertions(+), 24 deletions(-) diff --git a/Drivers/SX126x/Source/Sx1262.cpp b/Drivers/SX126x/Source/Sx1262.cpp index d45cf6b3..e08fcde9 100644 --- a/Drivers/SX126x/Source/Sx1262.cpp +++ b/Drivers/SX126x/Source/Sx1262.cpp @@ -177,7 +177,7 @@ int32_t Sx1262::threadMain(const Modulation modulation) { if ((eventFlags & RADIO_TRANSMIT_QUEUED_BIT) && (getTxQueueSize() > 0)) { currentTx = popNextQueuedTx(); radio.standby(); - uint16_t rc = radio.startTransmit(currentTx.data.data(), currentTx.data.size()); + uint16_t rc = radio.startTransmit(currentTx.packet.data.data(), currentTx.packet.data.size()); if (rc == RADIOLIB_ERR_NONE) { exchangeState = TransmitWaiting; currentTx.callback(currentTx.id, TransmissionState::PendingTransmit); @@ -204,8 +204,8 @@ int32_t Sx1262::threadMain(const Modulation modulation) { } } else if (eventFlags & SX1262_DIO1_EVENT_BIT) { uint16_t rxSize = radio.getPacketLength(true); - uint8_t *dataBuffer = new uint8_t[rxSize]; - uint16_t rc = radio.readData(dataBuffer, rxSize); + std::vector data(rxSize); + uint16_t rc = radio.readData(data.data(), rxSize); if (rc != RADIOLIB_ERR_NONE) { TT_LOG_E(TAG, "Error receiving data, RadioLib returned %hi", rc); } else if(rxSize == 0) { @@ -214,11 +214,8 @@ int32_t Sx1262::threadMain(const Modulation modulation) { float rssi = radio.getRSSI(); float snr = radio.getSNR(); TT_LOG_I(TAG, "LoRa RX size=%d RSSI=%f SNR=%f", rxSize, rssi, snr); - std::string message((char*)dataBuffer, rxSize); - TT_LOG_I(TAG, "msg=%s", message.c_str()); auto rxPacket = tt::hal::radio::RxPacket { - .data = dataBuffer, - .size = rxSize, + .data = data, .rssi = rssi, .snr = snr }; @@ -226,8 +223,6 @@ int32_t Sx1262::threadMain(const Modulation modulation) { publishRx(rxPacket); } - delete[] dataBuffer; - // A delay is needed before a new command vTaskDelay(pdMS_TO_TICKS(100)); gpio_set_level(GPIO_NUM_9, 0); diff --git a/Drivers/SX126x/Source/Sx1262.h b/Drivers/SX126x/Source/Sx1262.h index 2c7e3d3e..bc2b49a8 100644 --- a/Drivers/SX126x/Source/Sx1262.h +++ b/Drivers/SX126x/Source/Sx1262.h @@ -38,7 +38,7 @@ private: RadiolibTactilityHal hal; Module radioModule; SX1262 radio; - TxPacket currentTx; + TxItem currentTx; ExchangeState exchangeState; int8_t power = 0; diff --git a/Tactility/Include/Tactility/hal/radio/RadioDevice.h b/Tactility/Include/Tactility/hal/radio/RadioDevice.h index 3d54e8b4..455fa3e2 100644 --- a/Tactility/Include/Tactility/hal/radio/RadioDevice.h +++ b/Tactility/Include/Tactility/hal/radio/RadioDevice.h @@ -13,19 +13,23 @@ namespace tt::hal::radio { struct RxPacket { - uint8_t const *data; - size_t const size; + std::vector data; float rssi; float snr; }; +struct TxPacket { + std::vector data; + uint32_t address; // FSK only +}; + class RadioDevice : public Device { public: - enum class Modulation { Fsk, - LoRa + LoRa, + LrFhss }; enum class Parameter { @@ -64,9 +68,9 @@ public: using TxStateCallback = std::function; protected: - struct TxPacket { + struct TxItem { TxId id; - std::vector data; + TxPacket packet; TxStateCallback callback; }; @@ -84,7 +88,7 @@ private: std::unique_ptr _Nullable thread; bool threadInterrupted = false; std::vector rxSubscriptions; - std::deque txQueue; + std::deque txQueue; TxId lastTxId = 0; RxSubscriptionId lastRxSubscriptionId = 0; @@ -108,7 +112,7 @@ protected: return size; } - TxPacket popNextQueuedTx() { + TxItem popNextQueuedTx() { auto lock = mutex.asScopedLock(); lock.lock(); @@ -136,15 +140,11 @@ public: bool start(const Modulation modulation); bool stop(); - TxId transmit(uint8_t *data, const size_t size, TxStateCallback callback) { - return transmit(std::vector(data, data + size), callback); - } - - TxId transmit(const std::vector& data, TxStateCallback callback) { + TxId transmit(const TxPacket& packet, TxStateCallback callback) { auto lock = mutex.asScopedLock(); lock.lock(); const auto txId = lastTxId; - txQueue.push_back(TxPacket{.id = txId, .data = data, .callback = callback}); + txQueue.push_back(TxItem{.id = txId, .packet = packet, .callback = callback}); callback(txId, TransmissionState::Queued); lastTxId++; getEventFlag().set(RADIO_TRANSMIT_QUEUED_BIT); diff --git a/Tactility/Source/hal/radio/RadioDevice.cpp b/Tactility/Source/hal/radio/RadioDevice.cpp index 62f06245..ec750ed8 100644 --- a/Tactility/Source/hal/radio/RadioDevice.cpp +++ b/Tactility/Source/hal/radio/RadioDevice.cpp @@ -100,6 +100,8 @@ const char* toString(RadioDevice::Modulation modulation) { return "FSK"; case LoRa: return "LoRa"; + case LrFhss: + return "LR-FHSS"; default: return "Unkown"; }