Last active
September 21, 2025 17:12
-
-
Save cleishm/49dc80d8685026fad551d74644305d8a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/main/radio.cpp b/main/radio.cpp | |
index 27893f0..06ae8b1 100644 | |
--- a/main/radio.cpp | |
+++ b/main/radio.cpp | |
@@ -4,10 +4,9 @@ | |
#include "radio.h" | |
namespace { | |
- RTC_DATA_ATTR std::optional<SPISettings> kSpi; | |
- RTC_DATA_ATTR std::optional<ArduinoHal> kHal; | |
- RTC_DATA_ATTR std::optional<Module> kModule; | |
- RTC_DATA_ATTR std::optional<SX1262> kRadio; | |
+ // Consider making these a members of OOK or Radio instead of global? | |
+ Module kModule; // You aren't really supposed to directly access Module members | |
+ SX1262 kRadio; | |
} | |
OOK::OOK(uint32_t bitUsDuration, float freq, int8_t minPower, int8_t maxPower) | |
@@ -15,14 +14,12 @@ OOK::OOK(uint32_t bitUsDuration, float freq, int8_t minPower, int8_t maxPower) | |
, mMinPower(minPower) | |
, mMaxPower(maxPower) | |
{ | |
- if (!kRadio) | |
- return; | |
- if (int16_t err = kRadio->beginFSK(freq)) { | |
+ if (int16_t err = kRadio.beginFSK(freq)) { | |
ESP_LOGE("Radio", "OOK error: %d", err); | |
} | |
- kRadio->setFrequency(freq); | |
- kRadio->transmitDirect(); | |
- kRadio->setOutputPower(mMinPower); // Initially set to low | |
+ kRadio.setFrequency(freq); | |
+ kRadio.transmitDirect(); | |
+ kRadio.setOutputPower(mMinPower); // Initially set to low | |
} | |
OOK::~OOK() { | |
Radio::sleep(); | |
@@ -37,61 +34,53 @@ void Signal::BasicOOK::send() const { | |
} | |
int16_t OOK::setOutputPowerFast(int8_t power) { | |
- if (auto state = kRadio->checkOutputPower(power, NULL)) | |
+ if (auto state = kRadio.checkOutputPower(power, NULL)) | |
return state; | |
const uint8_t data[] = { static_cast<uint8_t>(power), RADIOLIB_SX126X_PA_RAMP_10U }; | |
- return kModule->SPIwriteStream(RADIOLIB_SX126X_CMD_SET_TX_PARAMS, data, 2); | |
+ // Unsure why you need to go directly to the SPI interface here instead of using | |
+ // the setOutputPower method on the SX1262 class? | |
+ return kModule.SPIwriteStream(RADIOLIB_SX126X_CMD_SET_TX_PARAMS, data, 2); | |
} | |
void OOK::transmit(std::vector<uint8_t> seq){ | |
- if (!kRadio) | |
- return; | |
- RadioLibTime_t start = kHal->micros(); | |
+ // You don't need to use RadioLib HAL functions - you know the hardware is esp32 | |
+ RadioLibTime_t start = kModule.hal->micros(); | |
for (size_t byteIdx = 0; byteIdx < seq.size(); ++byteIdx) { | |
uint8_t byte = seq[byteIdx]; | |
for (int bitIdx = 7; bitIdx >= 0; --bitIdx) { | |
bool bit = (byte >> bitIdx) & 0x01; | |
setOutputPowerFast(bit ? mMaxPower : mMinPower); | |
- kModule->waitForMicroseconds(start, mBitUsDuration); | |
+ // Call your own wait function here instead so you don't need to access the Module | |
+ // interface directly? | |
+ kModule.waitForMicroseconds(start, mBitUsDuration); | |
start += mBitUsDuration; | |
} | |
} | |
- kRadio->setOutputPower(mMinPower); | |
+ kRadio.setOutputPower(mMinPower); | |
} | |
-Radio::Radio() { | |
+// Adding a `wokeFromDeepSleep` param. If you can't add this and pass in the | |
+// correct value, you could keep a `resetModule` boolean in RTC memory, with a true | |
+// value that you set to false. | |
+Radio::Radio(bool wokeFromDeepSleep) { | |
if constexpr (!HW::kHasLora) { | |
return; | |
} | |
- if (!kRadio) { | |
- // Construct the RadioLib objects just once in RTC mem | |
- kSpi.emplace(10'000'000, MSBFIRST, SPI_MODE0); | |
- kHal.emplace(SPI, *kSpi); | |
- kModule.emplace(&kHal.value(), HW::Lora::Cs, HW::Lora::Dio1, HW::Lora::Res, HW::Lora::Busy); | |
- kRadio.emplace(&kModule.value()); | |
- // kRadio->XTAL = true; | |
- kRadio->begin(434.0, 125.0, 9, 7, 0x12, 10, 8, 0, false); | |
- sleep(); | |
- // sendSignal(kSignals[0].mSequences[0].second); | |
- } else { | |
- kHal->pinMode(kModule->getIrq(), kHal->GpioModeInput); | |
- kHal->pinMode(kModule->getGpio(), kHal->GpioModeInput); | |
- kModule->init(); | |
- } | |
+ kModule = Module(HW::Lora::Cs, HW::Lora::Dio1, HW::Lora::Res, HW::Lora::Busy); | |
+ kRadio = kModule.value(); | |
+ kRadio.begin(434.0, 125.0, 9, 7, 0x12, kPowerLevel, 8, 0, false, !wokeFromDeepSleep); | |
+ // you seem to immediately put the radio to sleep on first init, so I kept this here | |
+ sleep(); | |
+ | |
+ // Unsure why this was here, but leaving it alone | |
rtc_gpio_hold_dis((gpio_num_t)HW::Lora::Cs); | |
} | |
void Radio::startReceive() | |
{ | |
- if (!kRadio) | |
- return; | |
- | |
// ESP_LOGE("lora", "listening"); | |
- // If we did a deep sleep, we need to call again begin() to reset the module | |
- kRadio->begin(434.0, 125.0, 9, 7, 0x12, 10, 8, 0, false); | |
- | |
- kRadio->startReceive(); // 4.65mA @ 3.3V | |
+ kRadio.startReceive(); // 4.65mA @ 3.3V | |
} | |
void Radio::sendSignal(const Signal::Sequence& seq) { | |
@@ -104,27 +93,21 @@ void Radio::sendSignal(const Signal::Sequence& seq) { | |
void Radio::sleep() | |
{ | |
- if (!kRadio) | |
- return; | |
- | |
- kRadio->sleep(false); // ~0.3 uA @ 3.3V | |
+ kRadio.sleep(false); // ~0.3 uA @ 3.3V | |
// kRadio->sleep(); // ~0.7 uA @3.3V | |
} | |
void Radio::readLoraPck() { | |
- if (!kRadio) | |
- return; | |
- | |
String data; | |
- auto status = kRadio->readData(data); | |
+ auto status = kRadio.readData(data); | |
if (status != RADIOLIB_ERR_NONE) { | |
ESP_LOGE("Radio", "receive error: %d", status); | |
} else { | |
// Fill the rest of parameters | |
mPck = Radio::Pck{.mData = data.c_str(), | |
- .mSNR = kRadio->getSNR(), | |
- .mRSSI = kRadio->getRSSI(), | |
- .mFreqError = kRadio->getFrequencyError()}; | |
+ .mSNR = kRadio.getSNR(), | |
+ .mRSSI = kRadio.getRSSI(), | |
+ .mFreqError = kRadio.getFrequencyError()}; | |
} | |
sleep(); | |
} | |
\ No newline at end of file |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment