//----------------------------------------------------------------------------- // 2023 Ahoy, https://github.com/lumpapu/ahoy // Creative Commons - http://creativecommons.org/licenses/by-nc-sa/4.0/deed //----------------------------------------------------------------------------- #ifndef __COMM_QUEUE_H__ #define __COMM_QUEUE_H__ #include #include #include "hmInverter.h" template class CommQueue { public: CommQueue() {} void addImportant(Inverter<> *iv, uint8_t cmd, bool delOnPop = true) { dec(&mRdPtr); mQueue[mRdPtr] = queue_s(iv, cmd, delOnPop, true); } void add(Inverter<> *iv, uint8_t cmd, bool delOnPop = true) { mQueue[mWrPtr] = queue_s(iv, cmd, delOnPop, false); /*mQueue[mRdPtr].firstTry = false; if((IV_HM == mQueue[mRdPtr].iv->ivGen) || (IV_MI == mQueue[mRdPtr].iv->ivGen)) { mQueue[mRdPtr].firstTry = ((mQueue[mRdPtr].iv->isAvailable()) || (millis() < 120000)); }*/ inc(&mWrPtr); } protected: struct queue_s { Inverter<> *iv; uint8_t cmd; uint8_t attempts; bool delOnPop; bool isDevControl; bool firstTry; uint32_t ts; queue_s() {} queue_s(Inverter<> *i, uint8_t c, bool d, bool dev) : iv(i), cmd(c), attempts(5), ts(0), delOnPop(d), isDevControl(dev) {} }; protected: void add(queue_s q) { mQueue[mWrPtr] = q; /*mQueue[mRdPtr].firstTry = false; if((IV_HM == mQueue[mRdPtr].iv->ivGen) || (IV_MI == mQueue[mRdPtr].iv->ivGen)) { mQueue[mRdPtr].firstTry = ((mQueue[mRdPtr].iv->isAvailable()) || (millis() < 120000)); }*/ inc(&mWrPtr); } void get(std::function cb) { if(mRdPtr == mWrPtr) { cb(false, &mQueue[mRdPtr]); // empty return; } cb(true, &mQueue[mRdPtr]); } void cmdDone(bool force = false) { if(!mQueue[mRdPtr].delOnPop && !force) { mQueue[mRdPtr].attempts = 5; add(mQueue[mRdPtr]); // add to the end again } inc(&mRdPtr); } bool isFirstTry(void) { if(!mQueue[mRdPtr].firstTry) return false; mQueue[mRdPtr].firstTry = false; return true; } void setTs(uint32_t *ts) { mQueue[mRdPtr].ts = *ts; } void setAttempt(void) { if(mQueue[mRdPtr].attempts) mQueue[mRdPtr].attempts--; } void inc(uint8_t *ptr) { if(++(*ptr) >= N) *ptr = 0; } void dec(uint8_t *ptr) { if((*ptr) == 0) *ptr = N-1; else --(*ptr); } protected: std::array mQueue; uint8_t mWrPtr = 0; uint8_t mRdPtr = 0; }; #endif /*__COMM_QUEUE_H__*/