SerialIO
Loading...
Searching...
No Matches

SerialIO is a common library designed to simplify the implementation of RC protocol decoding in Arduino projects. It provides a modular and extensible architecture that allows for easy integration of various RC protocols.

Supported Protocol

Getting Started

Installation

Arduino Installation

To use the SerialIO library in your Arduino projects, follow these installation steps:

  1. Download the SerialIO library from the GitHub repository.
  2. Extract the downloaded ZIP file.
  3. Copy the extracted folder to the libraries directory in your Arduino sketchbook.
  4. Restart the Arduino IDE.
  5. You should now be able to include the SerialIO library in your Arduino sketches.

PlatformIO Installation

If you are using PlatformIO, you can install the SerialIO library directly from the PlatformIO Library Manager. Add the following line to your platformio.ini file:

lib_deps = Witty-Wizard/SerialIO

Tutorial

To use the library for decoding RC protocols in your Arduino project, follow these steps:

  1. Include Necessary Libraries: Include the required libraries at the beginning of your sketch:
    #include <SerialIO.h>
    Header file for serial input/output (IO) functionality.
  2. Define Channel Data Structure: Define a structure to hold the decoded RC channel data.
    crsf_channels_t channelData;
    Definition crsf_protocol.h:109
  3. Instantiate SerialIO Object: Create an instance of the SerialIO class, specifying the serial port, RX pin, and TX pin:
    SerialIO *receiver = new crsf(&Serial1, pinRX, pinTX);
    Class that stores state and functions for initialising and decoding rc protocol.
    Definition SerialIO.h:36
    A class for handling CRSF protocol communication.
    Definition crsf.h:19
    To instantiate a SerialIO object for receiving data only, you can create an instance of the crsf class specifying the serial port, RX pin:
    SerialIO *receiver = new crsf(&Serial1, pinRX);
  4. Initialize Communication: Call the begin() method to initialize communication with the specified serial port:
    void setup() {
    receiver->begin();
    }
    virtual void begin()=0
    Initialises the pins and setup serial port.
  5. Process Incoming Data: In the loop() function, call the processIncoming() method to process incoming bytes:
    receiver->processIncoming();
    virtual void processIncoming()=0
    decode the incoming serial data.
  6. Retrieve Channel Data: To retrieve the decoded RC channel data, call the getChannel() method, passing a pointer to the channelData structure:
    receiver->getChannel(&channelData);
    virtual void getChannel(rc_channels_t *channelData)=0
    Get the ChannelData.

see also:

Examples

CRSF Basic Example

#include <SerialIO.h>
#define RX_PIN 16
rc_channels_t channelData;
crsf receiver(&Serial1, RX_PIN);
void setup() {
receiver.begin();
Serial.begin(9600);
}
void loop() {
receiver.processIncoming();
receiver.getChannel(&channelData);
Serial.print("Channel 1: ");
Serial.print(channelData.channel1);
Serial.print(" Channel 2: ");
Serial.print(channelData.channel2);
Serial.print(" Channel 3: ");
Serial.print(channelData.channel3);
Serial.print(" Channel 4: ");
Serial.println(channelData.channel4);
}
void begin() override
Initializes the IBUS communication.
Definition ibus.cpp:6
Definition SerialIO.h:11

SBUS Basic Example

#include <SerialIO.h>
#define RX_PIN 16
rc_channels_t channelData;
sbus receiver(&Serial1, RX_PIN);
void setup() {
receiver.begin();
Serial.begin(9600);
}
void loop() {
receiver.processIncoming();
receiver.getChannel(&channelData);
Serial.print("Channel 1: ");
Serial.print(channelData.channel1);
Serial.print(" Channel 2: ");
Serial.print(channelData.channel2);
Serial.print(" Channel 3: ");
Serial.print(channelData.channel3);
Serial.print(" Channel 4: ");
Serial.println(channelData.channel4);
}
A class for handling SBUS protocol communication.
Definition sbus.h:16

Guide to Adding More Protocols

Steps

To add more protocols to your project, follow these steps:

  1. Create a New Protocol File : Start by creating a new header file for each additional protocol you want to add. For example, if you want to add a protocol named "XYZ", create a file named xyz_protocol.h.
  2. Define Protocol Structure : In the protocol header file (xyz_protocol.h), define the structure of the protocol, including any necessary constants, data structures, and functions.
  3. Implement Protocol Functions : Implement the protocol functions in a corresponding source file (xyz_protocol.cpp). These functions should handle initializing the protocol, processing incoming data, and extracting channel information.
  4. Include Protocol Header : In your main project files or wherever you want to use the protocol, include the header file of the protocol you've created (#include "xyz_protocol.h").
  5. Initialize Protocol : Initialize the protocol in your code by calling the initialization function provided by the protocol implementation.
  6. Process Incoming Data : Call the function to process incoming data from the protocol whenever data is received.
  7. Extract Channel Information : Use the provided function to extract channel information from the received data, if applicable.

Basic Functions Required

For initializing the header of a new protocol, you'll typically need the following basic functions:

  • void begin() : Initializes the protocol, sets up necessary parameters, and prepares for data reception.
  • void processIncoming(): Processes incoming data from the protocol, including parsing and interpreting the received data.
  • void getChannels(): Extracts channel information from the received data, if the protocol deals with channel data.

Here's a basic template for the header of a new protocol:

#pragma once
#ifndef XYZ_PROTOCOL_H
#define XYZ_PROTOCOL_H
// Include any necessary headers
#include <Arduino.h>
// Define any constants or data structures specific to the protocol
class XYZProtocol : public SerialIO {
public:
explicit XYZProtocol(HardwareSerial &rxPort, int rxPin, int txPin,
bool inverted = false)
: SerialIO(&rxPort, rxPin, txPin, inverted){};
;
~XYZProtocol();
void begin();
void getChannel(/* Parameters as needed */);
private:
HardwareSerial *_serialPort;
// Add any additional private members as needed
};
#endif // XYZ_PROTOCOL_H

License

This library is distributed under the GNU General Public License version 3.0.