mirror of
https://github.com/lumapu/ahoy.git
synced 2025-04-29 18:26:21 +02:00
0.8.147
code cleanup
This commit is contained in:
parent
08bc773cc2
commit
b7e0566535
2 changed files with 67 additions and 70 deletions
|
@ -60,6 +60,69 @@ class CommQueue {
|
||||||
return N;
|
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:
|
protected:
|
||||||
struct QueueElement {
|
struct QueueElement {
|
||||||
Inverter<> *iv;
|
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:
|
protected:
|
||||||
std::array<QueueElement, N> mQueue;
|
std::array<QueueElement, N> mQueue;
|
||||||
|
|
||||||
|
|
|
@ -102,8 +102,6 @@ class Communication : public CommQueue<> {
|
||||||
q->iv->curFrmCnt = 0;
|
q->iv->curFrmCnt = 0;
|
||||||
q->iv->radioStatistics.txCnt++;
|
q->iv->radioStatistics.txCnt++;
|
||||||
mIsRetransmit = false;
|
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());
|
mFirstTry = (INV_RADIO_TYPE_NRF == q->iv->ivRadioType) && (q->iv->isAvailable());
|
||||||
q->iv->mCmd = q->cmd;
|
q->iv->mCmd = q->cmd;
|
||||||
q->iv->mIsSingleframeReq = false;
|
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)))
|
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
|
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;
|
break;
|
||||||
|
|
||||||
case States::START:
|
case States::START:
|
||||||
|
@ -644,7 +642,9 @@ class Communication : public CommQueue<> {
|
||||||
if(q->isDevControl)
|
if(q->isDevControl)
|
||||||
keep = !crcPass;
|
keep = !crcPass;
|
||||||
|
|
||||||
cmdDone(q, keep);
|
if(keep)
|
||||||
|
cmdReset(q);
|
||||||
|
|
||||||
q->iv->mGotFragment = false;
|
q->iv->mGotFragment = false;
|
||||||
q->iv->mGotLastMsg = false;
|
q->iv->mGotLastMsg = false;
|
||||||
q->iv->miMultiParts = 0;
|
q->iv->miMultiParts = 0;
|
||||||
|
|
Loading…
Add table
Reference in a new issue