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:
- Download the SerialIO library from the GitHub repository.
- Extract the downloaded ZIP file.
- Copy the extracted folder to the
libraries
directory in your Arduino sketchbook.
- Restart the Arduino IDE.
- 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:
- Include Necessary Libraries: Include the required libraries at the beginning of your sketch:
Header file for serial input/output (IO) functionality.
- Define Channel Data Structure: Define a structure to hold the decoded RC channel data.
Definition crsf_protocol.h:109
- Instantiate SerialIO Object: Create an instance of the SerialIO class, specifying the serial port, RX pin, and TX pin:
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:
- Initialize Communication: Call the begin() method to initialize communication with the specified serial port:
void setup() {
}
virtual void begin()=0
Initialises the pins and setup serial port.
- Process Incoming Data: In the
loop()
function, call the processIncoming()
method to process incoming bytes:
virtual void processIncoming()=0
decode the incoming serial data.
- Retrieve Channel Data: To retrieve the decoded RC channel data, call the
getChannel()
method, passing a pointer to the channelData
structure:
virtual void getChannel(rc_channels_t *channelData)=0
Get the ChannelData.
see also:
Examples
CRSF Basic Example
#define RX_PIN 16
crsf receiver(&Serial1, RX_PIN);
void setup() {
receiver.begin();
}
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
SBUS Basic Example
#define RX_PIN 16
sbus receiver(&Serial1, RX_PIN);
void setup() {
receiver.begin();
}
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:
- 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
.
- 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.
- 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.
- 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"
).
- Initialize Protocol : Initialize the protocol in your code by calling the initialization function provided by the protocol implementation.
- Process Incoming Data : Call the function to process incoming data from the protocol whenever data is received.
- 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 <Arduino.h>
public:
explicit XYZProtocol(HardwareSerial &rxPort, int rxPin, int txPin,
bool inverted = false)
:
SerialIO(&rxPort, rxPin, txPin, inverted){};
;
~XYZProtocol();
private:
HardwareSerial *_serialPort;
};
#endif
License
This library is distributed under the GNU General Public License version 3.0.