void CB::poll(void) { #define detectLong 3000 #define repeatedLong 300 #define timeoutDouble 1000 if (!scn) return; // mode not set, nothing to do // 0 for button is pressed, 1 for released, 2 for falling and 3 for rising edge btn = chkPCINT(pciByte, pciBit); // check input pin if (btn == 2) { // button was just pressed //dbg << "armed \n"; btnTmr.set(detectLong); // set timer to detect a long pHM->pw.stayAwake(detectLong+500); // stay awake to check button status armFlg = 1; // set it armed return; } if (!armFlg) return; // nothing to do any more // check button status if (btn == 3) { // button was just released, keyShortSingle, keyShortDouble, keyLongRelease //dbg << "3 lstLng:" << lstLng << " dblLng:" << dblLng << " lngRpt:" << lngRpt << " lstSht:" << lstSht << '\n'; btnTmr.set(timeoutDouble); // set timer to clear the repeated flags pHM->pw.stayAwake(timeoutDouble+500); // stay awake to check button status if ((lstLng) && (lngRpt)) { // keyLongRelease outSignal(5); } else if (lstLng) { // no action, only remember for a double dblLng = 1; // waiting for a key long double } else if (lstSht) { // keyShortDouble lstSht = 0; outSignal(2); } else if ((!lstLng) && (!dblLng)) { // keyShortSingle lstSht = 1; // waiting for a key short double outSignal(1); } lstLng = lngRpt = 0; // some cleanup } else if ((btn == 0) && (btnTmr.done() )) { // button is still pressed, but timed out, seems to be a long //dbg << "0 lstLng:" << lstLng << " dblLng:" << dblLng << " lngRpt:" << lngRpt << " lstSht:" << lstSht << '\n'; pHM->pw.stayAwake(detectLong+500); // stay awake to check button status if (lstLng) { // keyLongRepeat btnTmr.set(repeatedLong); // set timer to detect a repeated long lngRpt = 1; outSignal(4); // last key state was a long, now it is a repeated long } else if (dblLng) { // keyLongDouble btnTmr.set(detectLong); // set timer to detect next long outSignal(6); // double flag is set, means was the second time for a long } else { // keyLongSingle btnTmr.set(detectLong); // set timer to detect a repeated long lstLng = 1; // remember last was long outSignal(3); // first time detect a long } } else if ((btn == 1) && (btnTmr.done() )) { // button is not pressed for a longer time, check if the double flags timed out //if (armFlg) dbg << "r\n"; if (dblLng) pHM->ld.set(nothing); armFlg = lstSht = lstLng = lngRpt = dblLng = 0; } }
void SN::poll(void) { #define maxRetries 3 #define maxTime 300 // set right amount of retries if (!this->maxRetr) { // first time run, check message type and set retries if (reqACK) this->maxRetr = maxRetries; // if BIDI is set, we have three retries else this->maxRetr = 1; } //dbg << "x:" << this->retrCnt << " y:" << this->maxRetr << " t:" << sndTmr.done() << '\n'; // send something while timer is not busy with waiting for an answer and max tries are not done if ((this->retrCnt < this->maxRetr) && (sndTmr.done() )) { // not all sends done and timing is OK // some sanity this->mBdy.mFlg.RPTEN = 1; // every message need this flag //if (pHM->cFlag.active) this->mBdy.mFlg.CFG = pHM->cFlag.active; // set the respective flag while we are in config mode this->timeOut = 0; // not timed out because just started this->lastMsgCnt = this->mBdy.mCnt; // copy the message count to identify the ACK this->retrCnt++; // increase counter while send out // check if we should send an internal message if (compArray(this->mBdy.toID, HMID, 3)) { // message is addressed to us memcpy(pHM->rv.buf, this->buf, sndLen); // copy send buffer to received buffer this->retrCnt = 0xff; // ACK not required, because internal #ifdef SN_DBG // only if AS debug is set dbg << F("<i "); #endif } else { // send it external uint8_t tBurst = this->mBdy.mFlg.BURST; // get burst flag, while string will get encoded pHM->encode(this->buf); // encode the string disableGDO0Int(); pHM->cc.sndData(this->buf,tBurst); // send to communication module enableGDO0Int(); pHM->decode(this->buf); // decode the string, so it is readable next time if (reqACK) sndTmr.set(maxTime); // set the time out for the message #ifdef SN_DBG // only if AS debug is set dbg << F("<- "); #endif } if (!pHM->ld.active) pHM->ld.set(send); // fire the status led #ifdef SN_DBG // only if AS debug is set dbg << _HEX(this->buf,sndLen) << ' ' << _TIME << '\n'; #endif } else if ((this->retrCnt >= this->maxRetr) && (sndTmr.done() )) { // max retries achieved, but seems to have no answer this->retrCnt = 0; this->maxRetr = 0; this->active = 0; if (!reqACK) return; this->timeOut = 1; // set the time out only while an ACK or answer was requested pHM->pw.stayAwake(100); pHM->ld.set(noack); #ifdef SN_DBG // only if AS debug is set dbg << F(" timed out") << ' ' << _TIME << '\n'; #endif } if (this->retrCnt == 0xff) { // answer was received, clean up the structure this->timeOut = 0; this->retrCnt = 0; this->maxRetr = 0; this->active = 0; sndTmr.set(0); pHM->pw.stayAwake(100); if (!pHM->ld.active) pHM->ld.set(ack); // fire the status led } }