Cache preamble SF to avoid redundant setPreambleLength() on every TX

This commit is contained in:
overkillfpv
2026-03-07 12:48:22 +11:00
parent a61add2e6a
commit 6fd8c28522
2 changed files with 9 additions and 3 deletions

View File

@@ -26,7 +26,8 @@ void setFlag(void) {
void RadioLibWrapper::begin() {
_radio->setPacketReceivedAction(setFlag); // this is also SentComplete interrupt
_radio->setPreambleLength(getSpreadingFactor() <= 8 ? 32 : 16); // longer preamble for lower SF improves reliability
_preamble_sf = getSpreadingFactor();
_radio->setPreambleLength(_preamble_sf <= 8 ? 32 : 16); // longer preamble for lower SF improves reliability
state = STATE_IDLE;
if (_board->getStartupReason() == BD_STARTUP_RX_PACKET) { // received a LoRa packet (while in deep sleep)
@@ -143,7 +144,11 @@ uint32_t RadioLibWrapper::getEstAirtimeFor(int len_bytes) {
bool RadioLibWrapper::startSendRaw(const uint8_t* bytes, int len) {
_board->onBeforeTransmit();
_radio->setPreambleLength(getSpreadingFactor() <= 8 ? 32 : 16); // keep preamble in sync with current SF
uint8_t sf = getSpreadingFactor();
if (sf != _preamble_sf) {
_preamble_sf = sf;
_radio->setPreambleLength(sf <= 8 ? 32 : 16); // update preamble when SF has changed
}
int err = _radio->startTransmit((uint8_t *) bytes, len);
if (err == RADIOLIB_ERR_NONE) {
state = STATE_TX_WAIT;

View File

@@ -11,6 +11,7 @@ protected:
int16_t _noise_floor, _threshold;
uint16_t _num_floor_samples;
int32_t _floor_sample_sum;
uint8_t _preamble_sf;
void idle();
void startRecv();
@@ -19,7 +20,7 @@ protected:
virtual void doResetAGC();
public:
RadioLibWrapper(PhysicalLayer& radio, mesh::MainBoard& board) : _radio(&radio), _board(&board) { n_recv = n_sent = 0; }
RadioLibWrapper(PhysicalLayer& radio, mesh::MainBoard& board) : _radio(&radio), _board(&board), _preamble_sf(0) { n_recv = n_sent = 0; }
void begin() override;
virtual void powerOff() { _radio->sleep(); }