From 69b87e6318f2c37b535f38a925ee807777cf89af Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 9 Jun 2023 18:25:07 -0500 Subject: [PATCH] button --- components/ratgdo/ratgdo.cpp | 95 ++++++++++++++++++------------------ 1 file changed, 48 insertions(+), 47 deletions(-) diff --git a/components/ratgdo/ratgdo.cpp b/components/ratgdo/ratgdo.cpp index cfd1ac5..7b18780 100644 --- a/components/ratgdo/ratgdo.cpp +++ b/components/ratgdo/ratgdo.cpp @@ -225,58 +225,59 @@ namespace ratgdo { static uint16_t byteCount = 0; static bool isStatus = false; - while (this->available()) { + if (!this->available()) { // ESP_LOGD(TAG, "No data available input:%d output:%d", this->input_gdo_pin_->get_pin(), this->output_gdo_pin_->get_pin()); - uint8_t serData; - if (!this->read_byte(&serData)) { - ESP_LOGD(TAG, "Failed to read byte"); + return; + } + uint8_t serData; + if (!this->read_byte(&serData)) { + ESP_LOGD(TAG, "Failed to read byte"); + return; + } + if (!reading) { + // shift serial byte onto msg start + msgStart <<= 8; + msgStart |= serData; + + // truncate to 3 bytes + msgStart &= 0x00FFFFFF; + + // if we are at the start of a message, capture the next 16 bytes + if (msgStart == 0x550100) { + byteCount = 3; + rxRollingCode[0] = 0x55; + rxRollingCode[1] = 0x01; + rxRollingCode[2] = 0x00; + + reading = true; return; } - if (!reading) { - // shift serial byte onto msg start - msgStart <<= 8; - msgStart |= serData; + } + if (reading) { + this->rxRollingCode[byteCount] = serData; + byteCount++; - // truncate to 3 bytes - msgStart &= 0x00FFFFFF; + if (byteCount == CODE_LENGTH) { + reading = false; + msgStart = 0; + byteCount = 0; + isStatus = false; - // if we are at the start of a message, capture the next 16 bytes - if (msgStart == 0x550100) { - byteCount = 3; - rxRollingCode[0] = 0x55; - rxRollingCode[1] = 0x01; - rxRollingCode[2] = 0x00; - - reading = true; - return; - } - } - if (reading) { - this->rxRollingCode[byteCount] = serData; - byteCount++; - - if (byteCount == CODE_LENGTH) { - reading = false; - msgStart = 0; - byteCount = 0; - isStatus = false; - - readRollingCode( - isStatus, - this->doorState, - this->lightState, - this->lockState, - this->motionState, - this->obstructionState, - this->motorState, - this->openings, - this->buttonState); - if (isStatus && this->forceUpdate_) { - this->forceUpdate_ = false; - this->previousDoorState = DoorState::DOOR_STATE_UNKNOWN; - this->previousLightState = LightState::LIGHT_STATE_UNKNOWN; - this->previousLockState = LockState::LOCK_STATE_UNKNOWN; - } + readRollingCode( + isStatus, + this->doorState, + this->lightState, + this->lockState, + this->motionState, + this->obstructionState, + this->motorState, + this->openings, + this->buttonState); + if (isStatus && this->forceUpdate_) { + this->forceUpdate_ = false; + this->previousDoorState = DoorState::DOOR_STATE_UNKNOWN; + this->previousLightState = LightState::LIGHT_STATE_UNKNOWN; + this->previousLockState = LockState::LOCK_STATE_UNKNOWN; } } }