refactor(flysky_gimbal_driver): remove inefficiences in packet parsing function (#7443)

This commit is contained in:
TanayKapoor
2026-07-04 18:19:13 +10:00
committed by GitHub
parent 3e88e0693e
commit c070ddda4b
2 changed files with 5 additions and 10 deletions
@@ -105,12 +105,14 @@ static void _fs_parse(STRUCT_HALL *hallBuffer, unsigned char ch)
case GET_LENGTH: case GET_LENGTH:
hallBuffer->length = ch; hallBuffer->length = ch;
hallBuffer->dataIndex = 0; hallBuffer->dataIndex = 0;
hallBuffer->status = GET_DATA;
if(hallBuffer->length > HALLSTICK_BUFF_SIZE - 5) { // buffer size - header size (1 byte header + 1 byte ID + 1 byte length + 2 bytes CRC = 5 bytes) if (hallBuffer->length > HALLSTICK_BUFF_SIZE - 5) { // buffer size - header size (1 byte header + 1 byte ID + 1 byte length + 2 bytes CRC = 5 bytes)
hallBuffer->status = GET_START; hallBuffer->status = GET_START;
} else if (0 == hallBuffer->length) { } else if (0 == hallBuffer->length) {
hallBuffer->status = GET_CHECKSUM; hallBuffer->status = GET_CHECKSUM;
hallBuffer->checkSum = 0; hallBuffer->checkSum = 0;
} else {
hallBuffer->status = GET_DATA; // length is in a valid range, next state should extract data
} }
break; break;
@@ -119,16 +121,10 @@ static void _fs_parse(STRUCT_HALL *hallBuffer, unsigned char ch)
if (hallBuffer->dataIndex >= hallBuffer->length) { if (hallBuffer->dataIndex >= hallBuffer->length) {
hallBuffer->checkSum = 0; hallBuffer->checkSum = 0;
hallBuffer->dataIndex = 0; hallBuffer->dataIndex = 0;
hallBuffer->status = GET_STATE; hallBuffer->status = GET_CHECKSUM;
} }
break; break;
case GET_STATE:
hallBuffer->checkSum = 0;
hallBuffer->dataIndex = 0;
hallBuffer->status = GET_CHECKSUM;
// fall through!
case GET_CHECKSUM: case GET_CHECKSUM:
hallBuffer->checkSum |= ch << ((hallBuffer->dataIndex++) * 8); hallBuffer->checkSum |= ch << ((hallBuffer->dataIndex++) * 8);
if (hallBuffer->dataIndex >= 2) { if (hallBuffer->dataIndex >= 2) {
@@ -107,7 +107,6 @@ enum
GET_ID, GET_ID,
GET_LENGTH, GET_LENGTH,
GET_DATA, GET_DATA,
GET_STATE,
GET_CHECKSUM, GET_CHECKSUM,
CHECKSUM, CHECKSUM,
}; };