From 000936e244fbb0381438a88020a76d46d4dec3b4 Mon Sep 17 00:00:00 2001 From: Witty-Wizard Date: Mon, 1 Jul 2024 00:30:05 +0530 Subject: [PATCH] moved leftshift function to base class --- src/SerialIO.cpp | 5 +++++ src/SerialIO.h | 1 + src/crsf/crsf.cpp | 30 +++++++----------------------- src/fport/fport.cpp | 7 +------ src/fport/fport.h | 1 - src/ibus/ibus.cpp | 7 +------ src/ibus/ibus.h | 1 - src/sbus/sbus.cpp | 5 ----- src/sbus/sbus.h | 2 -- 9 files changed, 15 insertions(+), 44 deletions(-) diff --git a/src/SerialIO.cpp b/src/SerialIO.cpp index 060b81d..9c0a70a 100644 --- a/src/SerialIO.cpp +++ b/src/SerialIO.cpp @@ -16,4 +16,9 @@ SerialIO::~SerialIO() { #else #warning "Unsupported hardware platform." #endif +} + +void SerialIO::leftShift(uint8_t arr[], size_t size) { + memmove(arr, arr + 1, (size - 1)); + arr[size - 1] = 0xFF; } \ No newline at end of file diff --git a/src/SerialIO.h b/src/SerialIO.h index 78c36d4..d5de45d 100644 --- a/src/SerialIO.h +++ b/src/SerialIO.h @@ -58,6 +58,7 @@ class SerialIO { // not (false). int _rxPin; // The RX pin number. int _txPin; // The TX pin number. + void leftShift(uint8_t arr[], size_t size); }; #include "crsf/crsf.h" #include "fport/fport.h" diff --git a/src/crsf/crsf.cpp b/src/crsf/crsf.cpp index b3a624a..1b403d4 100644 --- a/src/crsf/crsf.cpp +++ b/src/crsf/crsf.cpp @@ -20,32 +20,16 @@ void crsf::begin() { void crsf::processIncoming() { while (_rxPort->available()) { - _buffer = _rxPort->read(); - if (_headerDetected) { - _rxData[_rxIndex] = _buffer; - _rxIndex++; - if (_rxIndex > _rxData[1] + 2) - - { - _rxIndex = 0; - _headerDetected = false; - } - } else { - if (_buffer == CRSF_ADDRESS_FLIGHT_CONTROLLER || - _buffer == CRSF_ADDRESS_CRSF_TRANSMITTER) { - _headerDetected = true; - _rxData[0] = _buffer; - _rxIndex = 1; - } + _rxData[CRSF_MAX_PACKET_SIZE - 1] = _rxPort->read(); + if ((_rxData[0] == CRSF_ADDRESS_CRSF_TRANSMITTER || + _rxData[0] == CRSF_ADDRESS_CRSF_TRANSMITTER) && + (crc8(&_rxData[2], _rxData[1]) == 0)) { + memcpy(&channelData, &_rxData[3], sizeof(channelData)); } - - if (_rxIndex == sizeof(_rxData) / sizeof(_rxData[0])) { - _rxIndex = 0; - _headerDetected = false; + else{ + leftShift(_rxData,sizeof(_rxData)); } } - if (crc8(&_rxData[2], _rxData[1]) == 0) - memcpy(&channelData, &_rxData[3], sizeof(channelData)); } void crsf::getChannel(void *channelData) { diff --git a/src/fport/fport.cpp b/src/fport/fport.cpp index c79ec3a..eefb725 100644 --- a/src/fport/fport.cpp +++ b/src/fport/fport.cpp @@ -36,9 +36,4 @@ void fport::processIncoming() { } } -void fport::getChannel(void *channelData) {} - -void fport::leftShift(uint8_t arr[], size_t size) { - memmove(arr, arr + 1, (size - 1)); - arr[size - 1] = 0xFF; -} \ No newline at end of file +void fport::getChannel(void *channelData) {} \ No newline at end of file diff --git a/src/fport/fport.h b/src/fport/fport.h index 012e684..a7579b0 100644 --- a/src/fport/fport.h +++ b/src/fport/fport.h @@ -43,6 +43,5 @@ explicit fport(Stream *rxPort, int rxPin = -1, int txPin = -1, void begin() override; void processIncoming() override; void getChannel(void *channelData) override; - void leftShift(uint8_t arr[],size_t size); }; #endif \ No newline at end of file diff --git a/src/ibus/ibus.cpp b/src/ibus/ibus.cpp index dec60c4..3bd6d6e 100644 --- a/src/ibus/ibus.cpp +++ b/src/ibus/ibus.cpp @@ -69,9 +69,4 @@ bool ibus::checkSum() { // Check if the sum matches the expected CRC return (sum == 0xFFFF); // Assuming IBUS CRC is 0xFFFF when correct -} - -void ibus::leftShift(uint8_t arr[], size_t size) { - memmove(arr, arr + 1, (size - 1)); - arr[size - 1] = 0xFF; -} +} \ No newline at end of file diff --git a/src/ibus/ibus.h b/src/ibus/ibus.h index 36676f6..9928300 100644 --- a/src/ibus/ibus.h +++ b/src/ibus/ibus.h @@ -70,7 +70,6 @@ class ibus : public SerialIO { * channel data will be stored. */ void getChannel(void *channelData) override; - void leftShift(uint8_t arr[], size_t size); }; #endif // IBUS_H diff --git a/src/sbus/sbus.cpp b/src/sbus/sbus.cpp index 596eea7..6420ab1 100644 --- a/src/sbus/sbus.cpp +++ b/src/sbus/sbus.cpp @@ -37,9 +37,4 @@ void sbus::processIncoming() { void sbus::getChannel(void *channelData) { *static_cast(channelData) = _channelData; -} - -void sbus::leftShift(uint8_t arr[], size_t size) { - memmove(arr, arr + 1, (size - 1)); - arr[size - 1] = 0xFF; } \ No newline at end of file diff --git a/src/sbus/sbus.h b/src/sbus/sbus.h index 2ad901d..aa61519 100644 --- a/src/sbus/sbus.h +++ b/src/sbus/sbus.h @@ -76,8 +76,6 @@ class sbus : public SerialIO { * channel data will be stored. */ void getChannel(void *channelData) override; - - void leftShift(uint8_t arr[], size_t size); }; #endif // SBUS_H