Radio: Refactor RX/TX packages
This commit is contained in:
parent
24e33368b2
commit
e1d89282ef
@ -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<uint8_t> 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);
|
||||
|
||||
@ -38,7 +38,7 @@ private:
|
||||
RadiolibTactilityHal hal;
|
||||
Module radioModule;
|
||||
SX1262 radio;
|
||||
TxPacket currentTx;
|
||||
TxItem currentTx;
|
||||
ExchangeState exchangeState;
|
||||
|
||||
int8_t power = 0;
|
||||
|
||||
@ -13,19 +13,23 @@
|
||||
namespace tt::hal::radio {
|
||||
|
||||
struct RxPacket {
|
||||
uint8_t const *data;
|
||||
size_t const size;
|
||||
std::vector<uint8_t> data;
|
||||
float rssi;
|
||||
float snr;
|
||||
};
|
||||
|
||||
struct TxPacket {
|
||||
std::vector<uint8_t> 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<void(TxId id, TransmissionState state)>;
|
||||
|
||||
protected:
|
||||
struct TxPacket {
|
||||
struct TxItem {
|
||||
TxId id;
|
||||
std::vector<uint8_t> data;
|
||||
TxPacket packet;
|
||||
TxStateCallback callback;
|
||||
};
|
||||
|
||||
@ -84,7 +88,7 @@ private:
|
||||
std::unique_ptr<Thread> _Nullable thread;
|
||||
bool threadInterrupted = false;
|
||||
std::vector<RxSubscription> rxSubscriptions;
|
||||
std::deque<TxPacket> txQueue;
|
||||
std::deque<TxItem> 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<uint8_t>(data, data + size), callback);
|
||||
}
|
||||
|
||||
TxId transmit(const std::vector<uint8_t>& 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);
|
||||
|
||||
@ -100,6 +100,8 @@ const char* toString(RadioDevice::Modulation modulation) {
|
||||
return "FSK";
|
||||
case LoRa:
|
||||
return "LoRa";
|
||||
case LrFhss:
|
||||
return "LR-FHSS";
|
||||
default:
|
||||
return "Unkown";
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user