Skip to content

Commit

Permalink
Merge pull request #18 from Witty-Wizard/17-bug-incorrect-channel-dec…
Browse files Browse the repository at this point in the history
…oding-in-ibus

[FIXED]: IBus decoding Bug
  • Loading branch information
Witty-Wizard authored Jun 25, 2024
2 parents 6a9be1b + bd354f1 commit 2b6dfab
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 21 deletions.
2 changes: 1 addition & 1 deletion examples/atmel/ibus_basic/ibus_basic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
#include <SerialIO.h>

crsf_channels_t channelData;
ibus_channels_t channelData;

ibus receiver(&Serial);

Expand Down
2 changes: 1 addition & 1 deletion examples/espresiff/ibus_basic/ibus_basic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <SerialIO.h>
#define RX_PIN 16

crsf_channels_t channelData;
ibus_channels_t channelData;

ibus receiver(&Serial1,RX_PIN);

Expand Down
2 changes: 1 addition & 1 deletion examples/espresiff/sbus_basic/sbus_basic.ino
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <SerialIO.h>
#define RX_PIN 16

crsf_channels_t channelData;
sbus_channels_t channelData;

sbus receiver(&Serial1,RX_PIN);

Expand Down
11 changes: 0 additions & 11 deletions src/SerialIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,8 @@ class SerialIO {
*/
/**************************************************************************/
virtual void processIncoming() = 0;
/**************************************************************************/
/*!
@brief Gives the decoded rc channels
@param channelData
A pointer to a crsf_channels_t struct where the decoded channel
data will be stored.
*/
/**************************************************************************/
virtual void getChannel(crsf_channels_t *channelData) = 0;

protected:
crsf_channels_t channelData;
Stream
*_rxPort; // Pointer to the hardware serial port used for communication.
bool _headerDetected; // Flag indicating whether a header has been detected
Expand Down
3 changes: 2 additions & 1 deletion src/crsf.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
class crsf : public SerialIO {
private:
crsf_channels_t channelData;
uint8_t _rxData[CRSF_MAX_PACKET_SIZE];

public:
Expand Down Expand Up @@ -45,7 +46,7 @@ class crsf : public SerialIO {
* @param channelData Pointer to a crsf_channels_t struct where the decoded
* channel data will be stored.
*/
void getChannel(crsf_channels_t *channelData) override;
void getChannel(crsf_channels_t *channelData);

uint8_t crc8(uint8_t *data, uint8_t len);
};
Expand Down
6 changes: 4 additions & 2 deletions src/ibus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ void ibus::processIncoming() {
}
}

if (ibus::checkSum()) {
if (checkSum()) {
channelData.header = (_rxData[2] << 8) | _rxData[0];
channelData.channel1 = (_rxData[3] << 8) | _rxData[2];
channelData.channel2 = (_rxData[5] << 8) | _rxData[4];
channelData.channel3 = (_rxData[7] << 8) | _rxData[6];
Expand All @@ -58,10 +59,11 @@ void ibus::processIncoming() {
channelData.channel12 = (_rxData[25] << 8) | _rxData[24];
channelData.channel13 = (_rxData[27] << 8) | _rxData[26];
channelData.channel14 = (_rxData[29] << 8) | _rxData[28];
channelData.checksum = (_rxData[31] << 8) | _rxData[30];
}
}

void ibus::getChannel(crsf_channels_t *channelData) {
void ibus::getChannel(ibus_channels_t *channelData) {
*channelData = this->channelData;
}

Expand Down
22 changes: 21 additions & 1 deletion src/ibus.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,31 @@
#define IBUS_HEADER1 0x20
#define IBUS_HEADER2 0x40

typedef struct ibus_channels_s {
unsigned header : 16;
unsigned channel1 : 16;
unsigned channel2 : 16;
unsigned channel3 : 16;
unsigned channel4 : 16;
unsigned channel5 : 16;
unsigned channel6 : 16;
unsigned channel7 : 16;
unsigned channel8 : 16;
unsigned channel9 : 16;
unsigned channel10 : 16;
unsigned channel11 : 16;
unsigned channel12 : 16;
unsigned channel13 : 16;
unsigned channel14 : 16;
unsigned checksum : 16;
} PACKED ibus_channels_t;

/**
* @brief A class for handling IBUS protocol communication.
*/
class ibus : public SerialIO {
private:
ibus_channels_t channelData;
uint8_t _rxData[IBUS_MAX_PACKET_SIZE]; ///< Buffer to store received IBUS data
bool checkSum();

Expand Down Expand Up @@ -49,7 +69,7 @@ class ibus : public SerialIO {
* @param channelData Pointer to a crsf_channels_t struct where the decoded
* channel data will be stored.
*/
void getChannel(crsf_channels_t *channelData) override;
void getChannel(ibus_channels_t *channelData);
};

#endif // IBUS_H
4 changes: 2 additions & 2 deletions src/sbus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ void sbus::processIncoming() {
_headerDetected = false;
}
}
memcpy(&channelData, _rxData + 1, sizeof(channelData));
memcpy(&channelData, _rxData, sizeof(channelData));
}

void sbus::getChannel(crsf_channels_t *channelData) {
void sbus::getChannel(sbus_channels_t *channelData) {
*channelData = this->channelData;
}
29 changes: 28 additions & 1 deletion src/sbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,38 @@
#define SBUS_BAUDRATE 100000 ///< SBUS baudrate
#define SBUS_MAX_PACKET_SIZE 25 ///< SBUS packet length

typedef struct sbus_channels_s {
unsigned header : 8;
unsigned channel1 : 11;
unsigned channel2 : 11;
unsigned channel3 : 11;
unsigned channel4 : 11;
unsigned channel5 : 11;
unsigned channel6 : 11;
unsigned channel7 : 11;
unsigned channel8 : 11;
unsigned channel9 : 11;
unsigned channel10 : 11;
unsigned channel11 : 11;
unsigned channel12 : 11;
unsigned channel13 : 11;
unsigned channel14 : 11;
unsigned channel15 : 11;
unsigned channel16 : 11;
unsigned dummy : 4;
unsigned failsafe : 1;
unsigned framelost : 1;
unsigned channel17 : 1;
unsigned channel18 : 1;
unsigned footer : 8;
} PACKED sbus_channels_t;

/**
* @brief A class for handling SBUS protocol communication.
*/
class sbus : public SerialIO {
private:
sbus_channels_t channelData;
uint8_t _rxData[SBUS_MAX_PACKET_SIZE];

public:
Expand Down Expand Up @@ -48,7 +75,7 @@ class sbus : public SerialIO {
* @param channelData Pointer to a crsf_channels_t struct where the decoded
* channel data will be stored.
*/
void getChannel(crsf_channels_t *channelData) override;
void getChannel(sbus_channels_t *channelData);
};

#endif // SBUS_H

0 comments on commit 2b6dfab

Please sign in to comment.