Browse Source

code cleanup

pull/1626/head
lumapu 9 months ago
parent
commit
d75ba1b566
  1. 28
      src/app.cpp
  2. 14
      src/app.h
  3. 5
      src/config/config.h
  4. 12
      src/config/settings.h
  5. 24
      src/hms/CmtRadio.h

28
src/app.cpp

@ -33,23 +33,16 @@ void app::setup() {
resetSystem(); resetSystem();
esp_task_wdt_reset(); esp_task_wdt_reset();
mSettings.setup(); mSettings.setup(mConfig);
mSettings.getPtr(mConfig);
ah::Scheduler::setup(mConfig->inst.startWithoutTime); ah::Scheduler::setup(mConfig->inst.startWithoutTime);
DPRINT(DBG_INFO, F("Settings valid: ")); DPRINT(DBG_INFO, F("Settings valid: "));
DSERIAL.flush(); DBGPRINTLN(mConfig->valid ? F("true") : F("false"));
if (mSettings.getValid())
DBGPRINTLN(F("true"));
else
DBGPRINTLN(F("false"));
esp_task_wdt_reset(); esp_task_wdt_reset();
mNrfRadio.setup(&mConfig->serial.debug, &mConfig->serial.privacyLog, &mConfig->serial.printWholeTrace, &mConfig->nrf); mNrfRadio.setup(&mConfig->serial.debug, &mConfig->serial.privacyLog, &mConfig->serial.printWholeTrace, &mConfig->nrf);
#if defined(ESP32) #if defined(ESP32)
if(mConfig->cmt.enabled) { mCmtRadio.setup(&mConfig->serial.debug, &mConfig->serial.privacyLog, &mConfig->serial.printWholeTrace, &mConfig->cmt, mConfig->sys.region);
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);
}
#endif #endif
#ifdef ETHERNET #ifdef ETHERNET
@ -57,16 +50,16 @@ void app::setup() {
mNetwork = static_cast<AhoyNetwork*>(new AhoyEthernet()); mNetwork = static_cast<AhoyNetwork*>(new AhoyEthernet());
#else #else
mNetwork = static_cast<AhoyNetwork*>(new AhoyWifi()); 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->setup(mConfig, &mTimestamp, [this](bool gotIp) { this->onNetwork(gotIp); }, [this](bool gotTime) { this->onNtpUpdate(gotTime); });
mNetwork->begin(); mNetwork->begin();
esp_task_wdt_reset(); esp_task_wdt_reset();
mCommunication.setup(&mTimestamp, &mConfig->serial.debug, &mConfig->serial.privacyLog, &mConfig->serial.printWholeTrace); 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) #if defined(ENABLE_MQTT)
mCommunication.addPowerLimitAckListener([this] (Inverter<> *iv) { mMqtt.setPowerLimitAck(iv); }); mCommunication.addPowerLimitAckListener([this] (Inverter<> *iv) { mMqtt.setPowerLimitAck(iv); });
#endif #endif
mSys.setup(&mTimestamp, &mConfig->inst, this); mSys.setup(&mTimestamp, &mConfig->inst, this);
for (uint8_t i = 0; i < MAX_NUM_INVERTERS; i++) { for (uint8_t i = 0; i < MAX_NUM_INVERTERS; i++) {
@ -81,7 +74,6 @@ void app::setup() {
esp_task_wdt_reset(); esp_task_wdt_reset();
// when WiFi is in client mode, then enable mqtt broker // when WiFi is in client mode, then enable mqtt broker
#if !defined(AP_ONLY)
#if defined(ENABLE_MQTT) #if defined(ENABLE_MQTT)
mMqttEnabled = (mConfig->mqtt.broker[0] > 0); mMqttEnabled = (mConfig->mqtt.broker[0] > 0);
if (mMqttEnabled) { if (mMqttEnabled) {
@ -90,7 +82,6 @@ void app::setup() {
mCommunication.addAlarmListener([this](Inverter<> *iv) { mMqtt.alarmEvent(iv); }); mCommunication.addAlarmListener([this](Inverter<> *iv) { mMqtt.alarmEvent(iv); });
} }
#endif #endif
#endif
setupLed(); setupLed();
esp_task_wdt_reset(); esp_task_wdt_reset();
@ -126,9 +117,7 @@ void app::setup() {
#if defined(ENABLE_SIMULATOR) #if defined(ENABLE_SIMULATOR)
mSimulator.setup(&mSys, &mTimestamp, 0); mSimulator.setup(&mSys, &mTimestamp, 0);
mSimulator.addPayloadListener([this](uint8_t cmd, Inverter<> *iv) { mSimulator.addPayloadListener([this](uint8_t cmd, Inverter<> *iv) { payloadEventListener(cmd, iv); });
payloadEventListener(cmd, iv);
});
#endif /*ENABLE_SIMULATOR*/ #endif /*ENABLE_SIMULATOR*/
esp_task_wdt_reset(); esp_task_wdt_reset();
@ -142,8 +131,7 @@ void app::loop(void) {
mNrfRadio.loop(); mNrfRadio.loop();
#if defined(ESP32) #if defined(ESP32)
if(mConfig->cmt.enabled) mCmtRadio.loop();
mCmtRadio.loop();
#endif #endif
ah::Scheduler::loop(); ah::Scheduler::loop();

14
src/app.h

@ -217,7 +217,7 @@ class app : public IApp, public ah::Scheduler {
} }
bool getSettingsValid() override { bool getSettingsValid() override {
return mSettings.getValid(); return mConfig->valid;
} }
bool getRebootRequestState() override { bool getRebootRequestState() override {
@ -354,15 +354,13 @@ class app : public IApp, public ah::Scheduler {
void zeroIvValues(bool checkAvail = false, bool skipYieldDay = true); void zeroIvValues(bool checkAvail = false, bool skipYieldDay = true);
void payloadEventListener(uint8_t cmd, Inverter<> *iv) { void payloadEventListener(uint8_t cmd, Inverter<> *iv) {
#if !defined(AP_ONLY)
#if defined(ENABLE_MQTT) #if defined(ENABLE_MQTT)
if (mMqttEnabled) if (mMqttEnabled)
mMqtt.payloadEventListener(cmd, iv); mMqtt.payloadEventListener(cmd, iv);
#endif /*ENABLE_MQTT*/
#endif #endif
#if defined(PLUGIN_DISPLAY) #if defined(PLUGIN_DISPLAY)
if(DISP_TYPE_T0_NONE != mConfig->plugin.display.type) if(DISP_TYPE_T0_NONE != mConfig->plugin.display.type)
mDisplay.payloadEventListener(cmd); mDisplay.payloadEventListener(cmd);
#endif #endif
updateLed(); updateLed();
} }
@ -425,8 +423,7 @@ class app : public IApp, public ah::Scheduler {
#ifdef ENABLE_SYSLOG #ifdef ENABLE_SYSLOG
DbgSyslog mDbgSyslog; DbgSyslog mDbgSyslog;
#endif #endif
//PayloadType mPayload;
//MiPayloadType mMiPayload;
PubSerialType mPubSerial; PubSerialType mPubSerial;
#if !defined(ETHERNET) #if !defined(ETHERNET)
//Improv mImprov; //Improv mImprov;
@ -447,10 +444,9 @@ class app : public IApp, public ah::Scheduler {
bool mNetworkConnected = false; bool mNetworkConnected = false;
// mqtt
#if defined(ENABLE_MQTT) #if defined(ENABLE_MQTT)
PubMqttType mMqtt; PubMqttType mMqtt;
#endif /*ENABLE_MQTT*/ #endif
bool mTickerInstallOnce = false; bool mTickerInstallOnce = false;
bool mMqttEnabled = false; bool mMqttEnabled = false;

5
src/config/config.h

@ -28,6 +28,11 @@
// If the next line is uncommented, Ahoy will stay in access point mode all the time // If the next line is uncommented, Ahoy will stay in access point mode all the time
//#define AP_ONLY //#define AP_ONLY
#if defined(AP_ONLY)
#if defined(ENABLE_MQTT)
#undef ENABLE_MQTT
#endif
#endif
// timeout for automatic logoff (20 minutes) // timeout for automatic logoff (20 minutes)
#define LOGOUT_TIMEOUT (20 * 60) #define LOGOUT_TIMEOUT (20 * 60)

12
src/config/settings.h

@ -239,8 +239,9 @@ class settings {
std::fill(reinterpret_cast<char*>(&mCfg), reinterpret_cast<char*>(&mCfg) + sizeof(mCfg), 0); 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 ..")); DPRINTLN(DBG_INFO, F("Initializing FS .."));
cfg = &mCfg;
mCfg.valid = false; mCfg.valid = false;
#if !defined(ESP32) #if !defined(ESP32)
@ -276,14 +277,6 @@ class settings {
DPRINTLN(DBG_INFO, F("FS stopped")); DPRINTLN(DBG_INFO, F("FS stopped"));
} }
void getPtr(settings_t *&cfg) {
cfg = &mCfg;
}
bool getValid(void) {
return mCfg.valid;
}
inline bool getLastSaveSucceed() { inline bool getLastSaveSucceed() {
return mLastSaveSucceed; return mLastSaveSucceed;
} }
@ -915,6 +908,7 @@ class settings {
} }
#endif #endif
private:
settings_t mCfg; settings_t mCfg;
bool mLastSaveSucceed = 0; bool mLastSaveSucceed = 0;
}; };

24
src/hms/CmtRadio.h

@ -15,16 +15,25 @@ template<uint32_t DTU_SN = 0x81001765>
class CmtRadio : public Radio { class CmtRadio : public Radio {
typedef Cmt2300a CmtType; typedef Cmt2300a CmtType;
public: 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) { void setup(bool *serialDebug, bool *privacyMode, bool *printWholeTrace, cfgCmt_t *cfg, uint8_t region = 0, bool genDtuSn = true) {
mCmt.setup(pinSclk, pinSdio, pinCsb, pinFcsb); mCfg = cfg;
reset(genDtuSn, static_cast<RegionCfg>(region));
if(!cfg->enabled)
return;
mPrivacyMode = privacyMode; mPrivacyMode = privacyMode;
mSerialDebug = serialDebug; mSerialDebug = serialDebug;
mPrintWholeTrace = printWholeTrace; mPrintWholeTrace = printWholeTrace;
mTxBuf.fill(0); mTxBuf.fill(0);
mCmt.setup(cfg->pinSclk, cfg->pinSdio, cfg->pinCsb, cfg->pinFcsb);
reset(genDtuSn, static_cast<RegionCfg>(region));
} }
void loop() override { void loop() override {
if(!mCfg->enabled)
return;
mCmt.loop(); mCmt.loop();
if((!mIrqRcvd) && (!mRqstGetRx)) if((!mIrqRcvd) && (!mRqstGetRx))
return; return;
@ -41,6 +50,9 @@ class CmtRadio : public Radio {
} }
void sendControlPacket(Inverter<> *iv, uint8_t cmd, uint16_t *data, bool isRetransmit) override { void sendControlPacket(Inverter<> *iv, uint8_t cmd, uint16_t *data, bool isRetransmit) override {
if(!mCfg->enabled)
return;
DPRINT(DBG_INFO, F("sendControlPacket cmd: ")); DPRINT(DBG_INFO, F("sendControlPacket cmd: "));
DBGHEXLN(cmd); DBGHEXLN(cmd);
initPacket(iv->radioId.u64, TX_REQ_DEVCONTROL, SINGLE_FRAME); initPacket(iv->radioId.u64, TX_REQ_DEVCONTROL, SINGLE_FRAME);
@ -59,6 +71,9 @@ class CmtRadio : public Radio {
} }
bool switchFrequency(Inverter<> *iv, uint32_t fromkHz, uint32_t tokHz) override { bool switchFrequency(Inverter<> *iv, uint32_t fromkHz, uint32_t tokHz) override {
if(!isChipConnected())
return false;
uint8_t fromCh = mCmt.freq2Chan(fromkHz); uint8_t fromCh = mCmt.freq2Chan(fromkHz);
uint8_t toCh = mCmt.freq2Chan(tokHz); uint8_t toCh = mCmt.freq2Chan(tokHz);
@ -68,6 +83,8 @@ class CmtRadio : public Radio {
bool switchFrequencyCh(Inverter<> *iv, uint8_t fromCh, uint8_t toCh) override { bool switchFrequencyCh(Inverter<> *iv, uint8_t fromCh, uint8_t toCh) override {
if((0xff == fromCh) || (0xff == toCh)) if((0xff == fromCh) || (0xff == toCh))
return false; return false;
if(!isChipConnected())
return false;
mCmt.switchChannel(fromCh); mCmt.switchChannel(fromCh);
sendSwitchChCmd(iv, toCh); sendSwitchChCmd(iv, toCh);
@ -188,6 +205,7 @@ class CmtRadio : public Radio {
} }
CmtType mCmt; CmtType mCmt;
cfgCmt_t *mCfg = nullptr;
bool mCmtAvail = false; bool mCmtAvail = false;
bool mRqstGetRx = false; bool mRqstGetRx = false;
uint32_t mMillis = 0; uint32_t mMillis = 0;

Loading…
Cancel
Save