Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
lumapu committed Apr 25, 2024
1 parent b291ad1 commit d75ba1b
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 41 deletions.
28 changes: 8 additions & 20 deletions src/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,40 +33,33 @@ void app::setup() {
resetSystem();
esp_task_wdt_reset();

mSettings.setup();
mSettings.getPtr(mConfig);
mSettings.setup(mConfig);
ah::Scheduler::setup(mConfig->inst.startWithoutTime);
DPRINT(DBG_INFO, F("Settings valid: "));
DSERIAL.flush();
if (mSettings.getValid())
DBGPRINTLN(F("true"));
else
DBGPRINTLN(F("false"));
DBGPRINTLN(mConfig->valid ? F("true") : F("false"));

esp_task_wdt_reset();

mNrfRadio.setup(&mConfig->serial.debug, &mConfig->serial.privacyLog, &mConfig->serial.printWholeTrace, &mConfig->nrf);
#if defined(ESP32)
if(mConfig->cmt.enabled) {
mCmtRadio.setup(&mConfig->serial.debug, &mConfig->serial.privacyLog, &mConfig->serial.printWholeTrace, mConfig->cmt.pinSclk, mConfig->cmt.pinSdio, mConfig->cmt.pinCsb, mConfig->cmt.pinFcsb, mConfig->sys.region);
}
mCmtRadio.setup(&mConfig->serial.debug, &mConfig->serial.privacyLog, &mConfig->serial.printWholeTrace, &mConfig->cmt, mConfig->sys.region);
#endif

#ifdef ETHERNET
delay(1000);
mNetwork = static_cast<AhoyNetwork*>(new AhoyEthernet());
#else
mNetwork = static_cast<AhoyNetwork*>(new AhoyWifi());
#endif // ETHERNET
#endif
mNetwork->setup(mConfig, &mTimestamp, [this](bool gotIp) { this->onNetwork(gotIp); }, [this](bool gotTime) { this->onNtpUpdate(gotTime); });
mNetwork->begin();

esp_task_wdt_reset();

mCommunication.setup(&mTimestamp, &mConfig->serial.debug, &mConfig->serial.privacyLog, &mConfig->serial.printWholeTrace);
mCommunication.addPayloadListener(std::bind(&app::payloadEventListener, this, std::placeholders::_1, std::placeholders::_2));
mCommunication.addPayloadListener([this] (uint8_t cmd, Inverter<> *iv) { payloadEventListener(cmd, iv); });
#if defined(ENABLE_MQTT)
mCommunication.addPowerLimitAckListener([this] (Inverter<> *iv) { mMqtt.setPowerLimitAck(iv); });
mCommunication.addPowerLimitAckListener([this] (Inverter<> *iv) { mMqtt.setPowerLimitAck(iv); });
#endif
mSys.setup(&mTimestamp, &mConfig->inst, this);
for (uint8_t i = 0; i < MAX_NUM_INVERTERS; i++) {
Expand All @@ -81,7 +74,6 @@ void app::setup() {
esp_task_wdt_reset();

// when WiFi is in client mode, then enable mqtt broker
#if !defined(AP_ONLY)
#if defined(ENABLE_MQTT)
mMqttEnabled = (mConfig->mqtt.broker[0] > 0);
if (mMqttEnabled) {
Expand All @@ -90,7 +82,6 @@ void app::setup() {
mCommunication.addAlarmListener([this](Inverter<> *iv) { mMqtt.alarmEvent(iv); });
}
#endif
#endif
setupLed();

esp_task_wdt_reset();
Expand Down Expand Up @@ -126,9 +117,7 @@ void app::setup() {

#if defined(ENABLE_SIMULATOR)
mSimulator.setup(&mSys, &mTimestamp, 0);
mSimulator.addPayloadListener([this](uint8_t cmd, Inverter<> *iv) {
payloadEventListener(cmd, iv);
});
mSimulator.addPayloadListener([this](uint8_t cmd, Inverter<> *iv) { payloadEventListener(cmd, iv); });
#endif /*ENABLE_SIMULATOR*/

esp_task_wdt_reset();
Expand All @@ -142,8 +131,7 @@ void app::loop(void) {
mNrfRadio.loop();

#if defined(ESP32)
if(mConfig->cmt.enabled)
mCmtRadio.loop();
mCmtRadio.loop();
#endif

ah::Scheduler::loop();
Expand Down
14 changes: 5 additions & 9 deletions src/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class app : public IApp, public ah::Scheduler {
}

bool getSettingsValid() override {
return mSettings.getValid();
return mConfig->valid;
}

bool getRebootRequestState() override {
Expand Down Expand Up @@ -354,15 +354,13 @@ class app : public IApp, public ah::Scheduler {
void zeroIvValues(bool checkAvail = false, bool skipYieldDay = true);

void payloadEventListener(uint8_t cmd, Inverter<> *iv) {
#if !defined(AP_ONLY)
#if defined(ENABLE_MQTT)
if (mMqttEnabled)
mMqtt.payloadEventListener(cmd, iv);
#endif /*ENABLE_MQTT*/
#endif
#if defined(PLUGIN_DISPLAY)
if(DISP_TYPE_T0_NONE != mConfig->plugin.display.type)
mDisplay.payloadEventListener(cmd);
if(DISP_TYPE_T0_NONE != mConfig->plugin.display.type)
mDisplay.payloadEventListener(cmd);
#endif
updateLed();
}
Expand Down Expand Up @@ -425,8 +423,7 @@ class app : public IApp, public ah::Scheduler {
#ifdef ENABLE_SYSLOG
DbgSyslog mDbgSyslog;
#endif
//PayloadType mPayload;
//MiPayloadType mMiPayload;

PubSerialType mPubSerial;
#if !defined(ETHERNET)
//Improv mImprov;
Expand All @@ -447,10 +444,9 @@ class app : public IApp, public ah::Scheduler {

bool mNetworkConnected = false;

// mqtt
#if defined(ENABLE_MQTT)
PubMqttType mMqtt;
#endif /*ENABLE_MQTT*/
#endif
bool mTickerInstallOnce = false;
bool mMqttEnabled = false;

Expand Down
5 changes: 5 additions & 0 deletions src/config/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@

// If the next line is uncommented, Ahoy will stay in access point mode all the time
//#define AP_ONLY
#if defined(AP_ONLY)
#if defined(ENABLE_MQTT)
#undef ENABLE_MQTT
#endif
#endif

// timeout for automatic logoff (20 minutes)
#define LOGOUT_TIMEOUT (20 * 60)
Expand Down
12 changes: 3 additions & 9 deletions src/config/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,9 @@ class settings {
std::fill(reinterpret_cast<char*>(&mCfg), reinterpret_cast<char*>(&mCfg) + sizeof(mCfg), 0);
}

void setup() {
void setup(settings_t *&cfg) {
DPRINTLN(DBG_INFO, F("Initializing FS .."));
cfg = &mCfg;

mCfg.valid = false;
#if !defined(ESP32)
Expand Down Expand Up @@ -276,14 +277,6 @@ class settings {
DPRINTLN(DBG_INFO, F("FS stopped"));
}

void getPtr(settings_t *&cfg) {
cfg = &mCfg;
}

bool getValid(void) {
return mCfg.valid;
}

inline bool getLastSaveSucceed() {
return mLastSaveSucceed;
}
Expand Down Expand Up @@ -915,6 +908,7 @@ class settings {
}
#endif

private:
settings_t mCfg;
bool mLastSaveSucceed = 0;
};
Expand Down
24 changes: 21 additions & 3 deletions src/hms/CmtRadio.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,25 @@ template<uint32_t DTU_SN = 0x81001765>
class CmtRadio : public Radio {
typedef Cmt2300a CmtType;
public:
void setup(bool *serialDebug, bool *privacyMode, bool *printWholeTrace, uint8_t pinSclk, uint8_t pinSdio, uint8_t pinCsb, uint8_t pinFcsb, uint8_t region = 0, bool genDtuSn = true) {
mCmt.setup(pinSclk, pinSdio, pinCsb, pinFcsb);
reset(genDtuSn, static_cast<RegionCfg>(region));
void setup(bool *serialDebug, bool *privacyMode, bool *printWholeTrace, cfgCmt_t *cfg, uint8_t region = 0, bool genDtuSn = true) {
mCfg = cfg;

if(!cfg->enabled)
return;

mPrivacyMode = privacyMode;
mSerialDebug = serialDebug;
mPrintWholeTrace = printWholeTrace;
mTxBuf.fill(0);

mCmt.setup(cfg->pinSclk, cfg->pinSdio, cfg->pinCsb, cfg->pinFcsb);
reset(genDtuSn, static_cast<RegionCfg>(region));
}

void loop() override {
if(!mCfg->enabled)
return;

mCmt.loop();
if((!mIrqRcvd) && (!mRqstGetRx))
return;
Expand All @@ -41,6 +50,9 @@ class CmtRadio : public Radio {
}

void sendControlPacket(Inverter<> *iv, uint8_t cmd, uint16_t *data, bool isRetransmit) override {
if(!mCfg->enabled)
return;

DPRINT(DBG_INFO, F("sendControlPacket cmd: "));
DBGHEXLN(cmd);
initPacket(iv->radioId.u64, TX_REQ_DEVCONTROL, SINGLE_FRAME);
Expand All @@ -59,6 +71,9 @@ class CmtRadio : public Radio {
}

bool switchFrequency(Inverter<> *iv, uint32_t fromkHz, uint32_t tokHz) override {
if(!isChipConnected())
return false;

uint8_t fromCh = mCmt.freq2Chan(fromkHz);
uint8_t toCh = mCmt.freq2Chan(tokHz);

Expand All @@ -68,6 +83,8 @@ class CmtRadio : public Radio {
bool switchFrequencyCh(Inverter<> *iv, uint8_t fromCh, uint8_t toCh) override {
if((0xff == fromCh) || (0xff == toCh))
return false;
if(!isChipConnected())
return false;

mCmt.switchChannel(fromCh);
sendSwitchChCmd(iv, toCh);
Expand Down Expand Up @@ -188,6 +205,7 @@ class CmtRadio : public Radio {
}

CmtType mCmt;
cfgCmt_t *mCfg = nullptr;
bool mCmtAvail = false;
bool mRqstGetRx = false;
uint32_t mMillis = 0;
Expand Down

0 comments on commit d75ba1b

Please sign in to comment.