From 5525d25e4ba22e4711fd0a1a370259b629aaf3c5 Mon Sep 17 00:00:00 2001 From: lumapu Date: Tue, 21 Feb 2023 00:26:47 +0100 Subject: [PATCH] added CmtRadio to app --- src/app.cpp | 28 +++++++++++++++++++++++++++- src/app.h | 4 +++- src/hms/hmsRadio.h | 13 +++++++------ 3 files changed, 37 insertions(+), 8 deletions(-) diff --git a/src/app.cpp b/src/app.cpp index d20a2334..29cd0589 100644 --- a/src/app.cpp +++ b/src/app.cpp @@ -40,6 +40,12 @@ void app::setup() { mNrfRadio.setup(mConfig->nrf.amplifierPower, mConfig->nrf.pinIrq, mConfig->nrf.pinCe, mConfig->nrf.pinCs); mNrfRadio.enableDebug(); } + #if defined(ESP32) + if(mConfig->cmt.enabled) { + mCmtRadio.setup(); + mCmtRadio.enableDebug(); + } + #endif #if defined(AP_ONLY) mInnerLoopCb = std::bind(&app::loopStandard, this); @@ -67,7 +73,7 @@ void app::setup() { DBGPRINTLN(String(ESP.getHeapFragmentation())); DBGPRINTLN(String(ESP.getMaxFreeBlockSize()));*/ - if(!mNrfRadio.isChipConnected()) + if(!mNrfRadio.isChipConnected() && mConfig->nrf.enabled) DPRINTLN(DBG_WARN, F("WARNING! your NRF24 module can't be reached, check the wiring")); // when WiFi is in client mode, then enable mqtt broker @@ -137,6 +143,22 @@ void app::loopStandard(void) { mPayload.process(true); mMiPayload.process(true); } + #if defined(ESP32) + if (mCmtRadio.loop()) { + while (!mCmtRadio.mBufCtrl.empty()) { + hmsPacket_t *p = &mCmtRadio.mBufCtrl.front(); + if (mConfig->serial.debug) { + DPRINT(DBG_INFO, F("RX ")); + DBGPRINT(String(p->data[0])); + DBGPRINT(F("RSSI ")); + DBGPRINT(String(p->rssi)); + DBGPRINT(F("dBm | ")); + ah::dumpBuf(&p->data[1], p->data[0]); + } + mCmtRadio.mBufCtrl.pop(); + } + } + #endif mPayload.loop(); mMiPayload.loop(); @@ -158,6 +180,10 @@ void app::onWifi(bool gotIp) { if (gotIp) { mInnerLoopCb = std::bind(&app::loopStandard, this); every(std::bind(&app::tickSend, this), mConfig->nrf.sendInterval, "tSend"); + #if defined(ESP32) + if(mConfig->cmt.enabled) + everySec(std::bind(&CmtRadioType::tickSecond, mCmtRadio), "tsCmt"); + #endif mMqttReconnect = true; mSunrise = 0; // needs to be set to 0, to reinstall sunrise and ivComm tickers! once(std::bind(&app::tickNtpUpdate, this), 2, "ntp2"); diff --git a/src/app.h b/src/app.h index c87cbb8f..c844e52a 100644 --- a/src/app.h +++ b/src/app.h @@ -37,6 +37,7 @@ #define ASIN(x) (degrees(asin(x))) #define ACOS(x) (degrees(acos(x))) +typedef CmtRadio> CmtRadioType; typedef HmSystem HmSystemType; typedef HmPayload> PayloadType; typedef MiPayload> MiPayloadType; @@ -67,7 +68,7 @@ class app : public IApp, public ah::Scheduler { } void handleHmsIntr(void) { - //mSys.Radio.handleHmsIntr(); + mCmtRadio.handleIntr(); } uint32_t getUptime() { @@ -213,6 +214,7 @@ class app : public IApp, public ah::Scheduler { HmSystemType mSys; HmRadio<> mNrfRadio; + CmtRadioType mCmtRadio; private: typedef std::function innerLoopCb; diff --git a/src/hms/hmsRadio.h b/src/hms/hmsRadio.h index 6b54b8c3..aadc41fe 100644 --- a/src/hms/hmsRadio.h +++ b/src/hms/hmsRadio.h @@ -20,18 +20,18 @@ typedef struct { #define U32_B0(val) ((uint8_t)((val ) & 0xff)) template -class HmsRadio { +class CmtRadio { typedef SPI SpiType; typedef Cmt2300a CmtType; public: - HmsRadio() { + CmtRadio() { mDtuSn = DTU_SN; } void setup(bool genDtuSn = true) { if(genDtuSn) generateDtuSn(); - if(!mCmt.resetCMT()) + if(!mCmt.reset()) DPRINTLN(DBG_WARN, F("Initializing CMT2300A failed!")); else mCmt.goRx(); @@ -43,13 +43,14 @@ class HmsRadio { mIrqRcvd = false; } - void loop() { + bool loop() { mCmt.loop(); if(!mIrqRcvd) - return; + return false; mIrqRcvd = false; getRx(); mCmt.goRx(); + return true; } void tickSecond() { @@ -57,7 +58,7 @@ class HmsRadio { prepareSwitchChannelCmd(mIvIdChannelSet); } - void handeIntr(void) { + void handleIntr(void) { mIrqRcvd = true; }