Radio: Refactor RX/TX packages

This commit is contained in:
Dominic Höglinger 2025-09-17 18:50:11 +02:00
parent 82cb276350
commit fe68a83aaa
4 changed files with 21 additions and 24 deletions

View File

@ -177,7 +177,7 @@ int32_t Sx1262::threadMain(const Modulation modulation) {
if ((eventFlags & RADIO_TRANSMIT_QUEUED_BIT) && (getTxQueueSize() > 0)) { if ((eventFlags & RADIO_TRANSMIT_QUEUED_BIT) && (getTxQueueSize() > 0)) {
currentTx = popNextQueuedTx(); currentTx = popNextQueuedTx();
radio.standby(); 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) { if (rc == RADIOLIB_ERR_NONE) {
exchangeState = TransmitWaiting; exchangeState = TransmitWaiting;
currentTx.callback(currentTx.id, TransmissionState::PendingTransmit); currentTx.callback(currentTx.id, TransmissionState::PendingTransmit);
@ -204,8 +204,8 @@ int32_t Sx1262::threadMain(const Modulation modulation) {
} }
} else if (eventFlags & SX1262_DIO1_EVENT_BIT) { } else if (eventFlags & SX1262_DIO1_EVENT_BIT) {
uint16_t rxSize = radio.getPacketLength(true); uint16_t rxSize = radio.getPacketLength(true);
uint8_t *dataBuffer = new uint8_t[rxSize]; std::vector<uint8_t> data(rxSize);
uint16_t rc = radio.readData(dataBuffer, rxSize); uint16_t rc = radio.readData(data.data(), rxSize);
if (rc != RADIOLIB_ERR_NONE) { if (rc != RADIOLIB_ERR_NONE) {
TT_LOG_E(TAG, "Error receiving data, RadioLib returned %hi", rc); TT_LOG_E(TAG, "Error receiving data, RadioLib returned %hi", rc);
} else if(rxSize == 0) { } else if(rxSize == 0) {
@ -214,11 +214,8 @@ int32_t Sx1262::threadMain(const Modulation modulation) {
float rssi = radio.getRSSI(); float rssi = radio.getRSSI();
float snr = radio.getSNR(); float snr = radio.getSNR();
TT_LOG_I(TAG, "LoRa RX size=%d RSSI=%f SNR=%f", rxSize, rssi, snr); 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 { auto rxPacket = tt::hal::radio::RxPacket {
.data = dataBuffer, .data = data,
.size = rxSize,
.rssi = rssi, .rssi = rssi,
.snr = snr .snr = snr
}; };
@ -226,8 +223,6 @@ int32_t Sx1262::threadMain(const Modulation modulation) {
publishRx(rxPacket); publishRx(rxPacket);
} }
delete[] dataBuffer;
// A delay is needed before a new command // A delay is needed before a new command
vTaskDelay(pdMS_TO_TICKS(100)); vTaskDelay(pdMS_TO_TICKS(100));
gpio_set_level(GPIO_NUM_9, 0); gpio_set_level(GPIO_NUM_9, 0);

View File

@ -38,7 +38,7 @@ private:
RadiolibTactilityHal hal; RadiolibTactilityHal hal;
Module radioModule; Module radioModule;
SX1262 radio; SX1262 radio;
TxPacket currentTx; TxItem currentTx;
ExchangeState exchangeState; ExchangeState exchangeState;
int8_t power = 0; int8_t power = 0;

View File

@ -13,19 +13,23 @@
namespace tt::hal::radio { namespace tt::hal::radio {
struct RxPacket { struct RxPacket {
uint8_t const *data; std::vector<uint8_t> data;
size_t const size;
float rssi; float rssi;
float snr; float snr;
}; };
struct TxPacket {
std::vector<uint8_t> data;
uint32_t address; // FSK only
};
class RadioDevice : public Device { class RadioDevice : public Device {
public: public:
enum class Modulation { enum class Modulation {
Fsk, Fsk,
LoRa LoRa,
LrFhss
}; };
enum class Parameter { enum class Parameter {
@ -64,9 +68,9 @@ public:
using TxStateCallback = std::function<void(TxId id, TransmissionState state)>; using TxStateCallback = std::function<void(TxId id, TransmissionState state)>;
protected: protected:
struct TxPacket { struct TxItem {
TxId id; TxId id;
std::vector<uint8_t> data; TxPacket packet;
TxStateCallback callback; TxStateCallback callback;
}; };
@ -84,7 +88,7 @@ private:
std::unique_ptr<Thread> _Nullable thread; std::unique_ptr<Thread> _Nullable thread;
bool threadInterrupted = false; bool threadInterrupted = false;
std::vector<RxSubscription> rxSubscriptions; std::vector<RxSubscription> rxSubscriptions;
std::deque<TxPacket> txQueue; std::deque<TxItem> txQueue;
TxId lastTxId = 0; TxId lastTxId = 0;
RxSubscriptionId lastRxSubscriptionId = 0; RxSubscriptionId lastRxSubscriptionId = 0;
@ -108,7 +112,7 @@ protected:
return size; return size;
} }
TxPacket popNextQueuedTx() { TxItem popNextQueuedTx() {
auto lock = mutex.asScopedLock(); auto lock = mutex.asScopedLock();
lock.lock(); lock.lock();
@ -136,15 +140,11 @@ public:
bool start(const Modulation modulation); bool start(const Modulation modulation);
bool stop(); bool stop();
TxId transmit(uint8_t *data, const size_t size, TxStateCallback callback) { TxId transmit(const TxPacket& packet, TxStateCallback callback) {
return transmit(std::vector<uint8_t>(data, data + size), callback);
}
TxId transmit(const std::vector<uint8_t>& data, TxStateCallback callback) {
auto lock = mutex.asScopedLock(); auto lock = mutex.asScopedLock();
lock.lock(); lock.lock();
const auto txId = lastTxId; 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); callback(txId, TransmissionState::Queued);
lastTxId++; lastTxId++;
getEventFlag().set(RADIO_TRANSMIT_QUEUED_BIT); getEventFlag().set(RADIO_TRANSMIT_QUEUED_BIT);

View File

@ -100,6 +100,8 @@ const char* toString(RadioDevice::Modulation modulation) {
return "FSK"; return "FSK";
case LoRa: case LoRa:
return "LoRa"; return "LoRa";
case LrFhss:
return "LR-FHSS";
default: default:
return "Unkown"; return "Unkown";
} }