code cleanup
This commit is contained in:
lumapu 2024-09-29 23:08:05 +02:00
parent 08bc773cc2
commit b7e0566535
2 changed files with 67 additions and 70 deletions

View file

@ -60,6 +60,69 @@ class CommQueue {
return N;
}
protected:
void add(QueueElement q) {
xSemaphoreTake(this->mutex, portMAX_DELAY);
mQueue[this->wrPtr] = q;
inc(&this->wrPtr);
xSemaphoreGive(this->mutex);
}
void add(QueueElement *q, bool rstAttempts = false) {
xSemaphoreTake(this->mutex, portMAX_DELAY);
mQueue[this->wrPtr] = *q;
if(rstAttempts) {
mQueue[this->wrPtr].attempts = DefaultAttempts;
mQueue[this->wrPtr].attemptsMax = DefaultAttempts;
}
inc(&this->wrPtr);
xSemaphoreGive(this->mutex);
}
void get(std::function<void(bool valid, QueueElement *q)> cb) {
if(this->rdPtr == this->wrPtr)
cb(false, nullptr); // empty
else {
xSemaphoreTake(this->mutex, portMAX_DELAY);
QueueElement el = mQueue[this->rdPtr];
inc(&this->rdPtr);
xSemaphoreGive(this->mutex);
cb(true, &el);
}
}
void cmdReset(QueueElement *q) {
q->attempts = DefaultAttempts;
q->attemptsMax = DefaultAttempts;
add(q); // add to the end again
}
private:
void inc(uint8_t *ptr) {
if(++(*ptr) >= N)
*ptr = 0;
}
void dec(uint8_t *ptr) {
if((*ptr) == 0)
*ptr = N-1;
else
--(*ptr);
}
bool isIncluded(const QueueElement *q) {
uint8_t ptr = this->rdPtr;
while (ptr != this->wrPtr) {
if(mQueue[ptr].cmd == q->cmd) {
if(mQueue[ptr].iv->id == q->iv->id) {
if(mQueue[ptr].isDevControl == q->isDevControl)
return true;
}
}
inc(&ptr);
}
return false;
}
protected:
struct QueueElement {
Inverter<> *iv;
@ -117,72 +180,6 @@ class CommQueue {
}
};
protected:
void add(QueueElement q) {
xSemaphoreTake(this->mutex, portMAX_DELAY);
mQueue[this->wrPtr] = q;
inc(&this->wrPtr);
xSemaphoreGive(this->mutex);
}
void add(QueueElement *q, bool rstAttempts = false) {
xSemaphoreTake(this->mutex, portMAX_DELAY);
mQueue[this->wrPtr] = *q;
if(rstAttempts) {
mQueue[this->wrPtr].attempts = DefaultAttempts;
mQueue[this->wrPtr].attemptsMax = DefaultAttempts;
}
inc(&this->wrPtr);
xSemaphoreGive(this->mutex);
}
void get(std::function<void(bool valid, QueueElement *q)> cb) {
if(this->rdPtr == this->wrPtr)
cb(false, nullptr); // empty
else {
xSemaphoreTake(this->mutex, portMAX_DELAY);
QueueElement el = mQueue[this->rdPtr];
inc(&this->rdPtr);
xSemaphoreGive(this->mutex);
cb(true, &el);
}
}
void cmdDone(QueueElement *q, bool keep = false) {
if(keep) {
q->attempts = DefaultAttempts;
q->attemptsMax = DefaultAttempts;
add(q); // add to the end again
}
//inc(&this->rdPtr);
}
private:
void inc(uint8_t *ptr) {
if(++(*ptr) >= N)
*ptr = 0;
}
void dec(uint8_t *ptr) {
if((*ptr) == 0)
*ptr = N-1;
else
--(*ptr);
}
bool isIncluded(const QueueElement *q) {
uint8_t ptr = this->rdPtr;
while (ptr != this->wrPtr) {
if(mQueue[ptr].cmd == q->cmd) {
if(mQueue[ptr].iv->id == q->iv->id) {
if(mQueue[ptr].isDevControl == q->isDevControl)
return true;
}
}
inc(&ptr);
}
return false;
}
protected:
std::array<QueueElement, N> mQueue;

View file

@ -102,8 +102,6 @@ class Communication : public CommQueue<> {
q->iv->curFrmCnt = 0;
q->iv->radioStatistics.txCnt++;
mIsRetransmit = false;
if(NULL == q->iv->radio)
cmdDone(q, false); // can't communicate while radio is not defined!
mFirstTry = (INV_RADIO_TYPE_NRF == q->iv->ivRadioType) && (q->iv->isAvailable());
q->iv->mCmd = q->cmd;
q->iv->mIsSingleframeReq = false;
@ -112,7 +110,7 @@ class Communication : public CommQueue<> {
if((q->iv->ivGen == IV_MI) && ((q->cmd == MI_REQ_CH1) || (q->cmd == MI_REQ_4CH)))
q->incrAttempt(q->iv->channels); // 2 more attempts for 2ch, 4 more for 4ch
mState = States::START;
mState = (NULL == q->iv->radio) ? States::RESET : States::START;
break;
case States::START:
@ -644,7 +642,9 @@ class Communication : public CommQueue<> {
if(q->isDevControl)
keep = !crcPass;
cmdDone(q, keep);
if(keep)
cmdReset(q);
q->iv->mGotFragment = false;
q->iv->mGotLastMsg = false;
q->iv->miMultiParts = 0;