ZZ-Cat / CRSFforArduino

An Arduino Library for communicating with ExpressLRS and TBS Crossfire receivers.
GNU Affero General Public License v3.0
161 stars 27 forks source link

refactor(serial receiver interface): :recycle: Facilitate custom baud rates #132

Closed ZZ-Cat closed 3 months ago

ZZ-Cat commented 3 months ago

Overview

Resolves #131.

This Pull Request enables the ability to set custom baud rates as a parameter to sketchLayer::CRSFforArduino::begin().
In turn, this passes the baud rate to the Serial Receiver Interface, where the physical UART is initialised with your custom baud rate and the expected time to receive a full length frame is automatically adjusted at initialisation to compensate.

For backwards-compatibility, if no parameter is provided in the begin() function, CRSF for Arduino defaults to the legacy 420 KB/s baud rate.

Code examples

Using the default baud rate

#include "Arduino.h"
#include "CRSFforArduino.hpp"

CRSFforArduino *cfa = nullptr;

void a_generic_function()
{
    /* ...previous set-up code...*/

    /* Initialise CRSF for Arduino. */
    cfa = new CRSFforArduino();
    if (cfa != nullptr)
    {
        /* By default, CRSF for Arduino initialises its hardware UART with the legacy 420 KB/s if no parameter is provided. */
        cfa->begin();
    }

    /* ...more set-up code... */

}

Providing your own baud rate

"Fine, I'll do it myself!" --Thanos

You MAY pass in your own baud rate as a parameter to sketchLayer::CRSFforArduino::begin()

#include "Arduino.h"
#include "CRSFforArduino.hpp"

CRSFforArduino *cfa = nullptr;

void a_generic_function()
{
    /* ...previous set-up code...*/

    /* Initialise CRSF for Arduino. */
    cfa = new CRSFforArduino();
    if (cfa != nullptr)
    {
        /* You MAY provide your own baud rate as a parameter. */
        const unsigned long yourCustomBaudRate = 416666;

        /* Pass your custom baud rate to `begin()` to tell CRSF for Arduino to initialise its UART with your custom baud rate.
        In this example, the "official" 416.67 KB/s baud rate is chosen. */
        cfa->begin(yourCustomBaudRate);
    }

    /* ...more set-up code... */

}
ZZ-Cat commented 3 months ago

Right-oh. Looks like we're good-to-go here.

@krababonga can you check out the associated branch for this Pull Request, flash one of the examples in that branch (with your custom baud rate) onto a compatible development board, and let me know how you get on? TIA.

krababonga commented 3 months ago

I checked branch changes on new tracer receiver and old crossfire with ESP32, works like a charm.

ZZ-Cat commented 3 months ago

I checked branch changes on new tracer receiver and old crossfire with ESP32, works like a charm.

Sweet as. =^/.^=
I'mma go ahead and merge this into the v1.0.x Maintenance Branch.

Anything else comes up, don't hesitate to sing out, yea?