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()
{
if (!this->swSerial.available()) {
// ESP_LOGD(TAG, "No data available input:%d output:%d", this->input_gdo_pin_->get_pin(), this->output_gdo_pin_->get_pin());
return;
static bool reading_msg = false;
static uint32_t msg_start = 0;
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();
static uint32_t msgStart;
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;
msg_start = ((msg_start << 8) | ser_byte) & 0xffffff;
byte_count++;
// 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;
if (msg_start == 0x550100) {
this->rxRollingCode[0] = 0x55;
this->rxRollingCode[1] = 0x01;
this->rxRollingCode[2] = 0x00;
reading = true;
return;
reading_msg = true;
break;
}
}
if (reading) {
this->rxRollingCode[byteCount] = serData;
byteCount++;
}
if (reading_msg) {
while (this->swSerial.available()) {
uint8_t ser_byte = this->swSerial.read();
this->rxRollingCode[byte_count] = ser_byte;
byte_count++;
if (byteCount == CODE_LENGTH) {
reading = false;
msgStart = 0;
byteCount = 0;
if (byte_count == CODE_LENGTH) {
reading_msg = false;
byte_count = 0;
if (readRollingCode() == STATUS_CMD && this->forceUpdate_) {
this->forceUpdate_ = false;
this->previousDoorState = DoorState::DOOR_STATE_UNKNOWN;
this->previousLightState = LightState::LIGHT_STATE_UNKNOWN;
this->previousLockState = LockState::LOCK_STATE_UNKNOWN;
}
return;
}
}
}
}