Speed up serial line reads (#7)

Co-authored-by: J. Nick Koston <nick@koston.org>
This commit is contained in:
Marius Muja 2023-06-23 16:18:32 -07:00 committed by GitHub
parent 86b018ed25
commit a7e7ff82e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 35 additions and 35 deletions

View File

@ -226,48 +226,48 @@ namespace ratgdo {
void RATGDOComponent::gdoStateLoop() void RATGDOComponent::gdoStateLoop()
{ {
if (!this->swSerial.available()) { static bool reading_msg = false;
// ESP_LOGD(TAG, "No data available input:%d output:%d", this->input_gdo_pin_->get_pin(), this->output_gdo_pin_->get_pin()); static uint32_t msg_start = 0;
return; static uint16_t byte_count = 0;
if (!reading_msg) {
while (this->swSerial.available()) {
uint8_t ser_byte = this->swSerial.read();
if (ser_byte != 0x55 && ser_byte != 0x01 && ser_byte != 0x00) {
byte_count = 0;
continue;
} }
uint8_t serData = this->swSerial.read(); msg_start = ((msg_start << 8) | ser_byte) & 0xffffff;
static uint32_t msgStart; byte_count++;
static bool reading = false;
static uint16_t byteCount = 0;
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 we are at the start of a message, capture the next 16 bytes
if (msgStart == 0x550100) { if (msg_start == 0x550100) {
byteCount = 3; this->rxRollingCode[0] = 0x55;
rxRollingCode[0] = 0x55; this->rxRollingCode[1] = 0x01;
rxRollingCode[1] = 0x01; this->rxRollingCode[2] = 0x00;
rxRollingCode[2] = 0x00;
reading = true; reading_msg = true;
return; break;
} }
} }
if (reading) { }
this->rxRollingCode[byteCount] = serData; if (reading_msg) {
byteCount++; while (this->swSerial.available()) {
uint8_t ser_byte = this->swSerial.read();
this->rxRollingCode[byte_count] = ser_byte;
byte_count++;
if (byteCount == CODE_LENGTH) { if (byte_count == CODE_LENGTH) {
reading = false; reading_msg = false;
msgStart = 0; byte_count = 0;
byteCount = 0;
if (readRollingCode() == STATUS_CMD && this->forceUpdate_) { if (readRollingCode() == STATUS_CMD && this->forceUpdate_) {
this->forceUpdate_ = false; this->forceUpdate_ = false;
this->previousDoorState = DoorState::DOOR_STATE_UNKNOWN; this->previousDoorState = DoorState::DOOR_STATE_UNKNOWN;
this->previousLightState = LightState::LIGHT_STATE_UNKNOWN; this->previousLightState = LightState::LIGHT_STATE_UNKNOWN;
this->previousLockState = LockState::LOCK_STATE_UNKNOWN; this->previousLockState = LockState::LOCK_STATE_UNKNOWN;
} }
return;
}
} }
} }
} }