Skip to content

Commit

Permalink
refactor(hal): Deprecate DevBoards class and bring HardwareSerial
Browse files Browse the repository at this point in the history
… class up to the Sketch Layer

The previous parameters in CRSF for Arduino's constructor have been replaced with `HardwareSerial`.  
You do
```cpp
CRSFforArduino crsf = CRSFforArduino(&Serial1);
```
For access to the default `Serial1` port now. If you wish to provide your own `HardwareSerial` implementation that has its own UART port and pin assignments, you can do this now.
  • Loading branch information
ZZ-Cat authored Jan 21, 2024
1 parent d80d6f9 commit c9563f1
Show file tree
Hide file tree
Showing 28 changed files with 566 additions and 589 deletions.
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,8 @@ With that being said, installation from the Releases tab is nearly identical to
### The API

1. Add my library to your sketch with `#include "CRSFforArduino.h"`
2. Underneath that, you need to declare `CRSFforArduino crsf = CRSFforArduino()`. For now, you can leave the parentheses empty. There is ongoing work to allow you to specify what pins your receiver is connected to. For now, it defaults to pins 0 and 1 for Tx and Rx respectively.
2. Underneath that, you need to declare `CRSFforArduino crsf = CRSFforArduino(HardwareSerial *serialPort)`.
The parameter `serialPort` is where you can provide your own hardware UART port and pin definitions.
3. In your `setup()`, do `crsf.begin()` to start communicating with your connected ExpressLRS receiver. In case something goes wrong, `crsf.begin()` returns a boolean value of `true` if initialisation is successful, and `false` if it is not.
4. In your `loop()`, you need to call `crsf.update()`. This handles all of the data processing (including receiving RC channels and sending telemetry) and should be called as often as possible. You no longer need to read back the return value of `crsf.update()`, as it no longer returns anything. Everything is handled internally now.
5. To read your RC channel values, use `crsf.readRcChannel(n)`. Here, `n` refers to your channel number from 1 to 16.
Expand All @@ -108,9 +109,9 @@ The example below demonstrates what your code should look like, using the instru
/* 1. Add Cassie Robinson's CRSFforArduino.h library. */
#include "CRSFforArduino.h"

/* 2. Declare a CRSFforArduino object.
/* 2. Declare a CRSFforArduino object with your board's default UART port.
You can call it literally anything you want. */
CRSFforArduino crsf = CRSFforArduino();
CRSFforArduino crsf = CRSFforArduino(&Serial1);

void setup()
{
Expand Down Expand Up @@ -195,7 +196,7 @@ float lat, lon, alt, spd, gCourse;
int numSats;

/* 3. Declare a CRSFforArduino object. */
CRSFforArduino crsf = CRSFforArduino();
CRSFforArduino crsf = CRSFforArduino(&Serial1);

void setup()
{
Expand Down Expand Up @@ -433,6 +434,8 @@ If your development board outputs a logic level of 5 volts on its serial pins, y
## Telemetry
CRSF for Arduino supports full telemetry feedback, and you can view it all in one convenient place if you're using a LUA script like the INAV Telemetry Widget for OpenTX/EdgeTX controllers.
The following telemetry data is supported:
- Attitude/Artificial Horizon data:
Expand Down Expand Up @@ -466,10 +469,10 @@ The following telemetry data is supported:
## Known issues and limitations
- CRSF for Arduino is not compatible with AVR based microcontrollers.
- This is because the AVR microcontrollers are simply not powerful enough to meet the minimum requirements of the CRSF protocol.
- DMA for both SAMD21 and SAMD51 targets is currently broken, and is disabled for the time being.
If you build CRSF for Arduino with `USE_DMA` enabled, you will see this warning message: `DMA is enabled. This is an experimental feature and may not work as expected.`
At this point, DMA may be removed from CRSF for Arduino, as it brings _very little_ benefit to the project... if any at all.
- CRSF for Arduino uses a _lot_ of dynamic memory, to which all AVR microcontrollers simply don't have enough of.
- There are ongoing tests to see what other reasons why AVR microcontrollers aren't compatible.
- DMA for both SAMD21 and SAMD51 targets is no longer available.
- DMA may be re-factored later on down the track. However, it is not a priority right now.
- Software serial is not supported.
- This is because software serial is not capable of running at the required baud rate of 420 KB/s.
- This also means that CRSF for Arduino is restricted to using hardware serial only.
Expand All @@ -493,14 +496,14 @@ I give credit where credit is due. Because CRSF for Arduino isn't entirely my ow
- [Source Code](https://github.com/ExpressLRS/ExpressLRS)
- [Website](https://www.expresslrs.org/3.0/)
- [Team BlackSheep FPV](https://github.com/tbs-fpv) - The folks behind the CRSF protocol that both ExpressLRS and CRSF for Arduino uses.
- [This issue](https://github.com/tbs-fpv/freedomtx/issues/26) on [FreedomTX's repository.](https://github.com/tbs-fpv/freedomtx/issues/26)
- This gets a mention here, because I will benefit _greatly_ from an officially documented public repository for the CRSF protocol. The evolution of the protocol itself will help shape CRSF for Arduino, and I will be able to refer to that in addition to my references here.
- References for CRSF for Arduino
- [BetaFlight](https://github.com/betaflight)
- [Development Team](https://github.com/orgs/betaflight/people)
- [License](https://github.com/betaflight/betaflight/blob/master/LICENSE)
- [Source Code](https://github.com/betaflight/betaflight)
- [Website](https://betaflight.com/)
- [CRSF-WG](https://github.com/crsf-wg/crsf)
- This is the official repository for The CRSF Protocol, which CRSF for Arduino is based on.
- [RotorFlight](https://github.com/rotorflight)
- [Development Team](https://github.com/rotorflight#credits)
- [License](https://github.com/rotorflight/rotorflight-firmware/blob/master/LICENSE)
Expand Down
22 changes: 10 additions & 12 deletions examples/channels/channels.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author Cassandra "ZZ Cat" Robinson (nicad.heli.flier@gmail.com)
* @brief This example sketch shows how to receive RC channels from a CRSF receiver using the CRSF for Arduino library.
* @version 1.0.0
* @date 2024-1-15
* @date 2024-1-20
*
* @copyright Copyright (c) 2023, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down Expand Up @@ -106,12 +106,9 @@

#include "CRSFforArduino.hpp"

#define SERIAL_RX_PIN 0 // Set SERIAL_RX_PIN to the pin that the CRSF receiver's TX pin is connected to.
#define SERIAL_TX_PIN 1 // Set SERIAL_TX_PIN to the pin that the CRSF receiver's RX pin is connected to.

const int channelCount = crsfProtocol::RC_CHANNEL_COUNT; // I'm not sure if this is right, but we can always manually put in the number of channels desired

CRSFforArduino crsf = CRSFforArduino(SERIAL_RX_PIN, SERIAL_TX_PIN);
CRSFforArduino crsf = CRSFforArduino(&Serial1);

void setup()
{
Expand Down Expand Up @@ -148,14 +145,15 @@ void loop()
if (millis() - lastPrint >= 100)
{
lastPrint = millis();
for(int i = 1; i <= channelCount; i++){
//Serial.print("Channel");
Serial.print(i);
Serial.print(":");
Serial.print(crsf.rcToUs(crsf.getChannel(i)));
Serial.print("\t");
for (int i = 1; i <= channelCount; i++)
{
//Serial.print("Channel");
Serial.print(i);
Serial.print(":");
Serial.print(crsf.rcToUs(crsf.getChannel(i)));
Serial.print("\t");
}
Serial.println();
Serial.println();
}
}

Expand Down
7 changes: 2 additions & 5 deletions examples/flight_modes/flight_modes.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author Cassandra "ZZ Cat" Robinson (nicad.heli.flier@gmail.com)
* @brief Demonstrates the use of CRSF for Arduino's flight mode functionality.
* @version 1.0.0
* @date 2024-1-15
* @date 2024-1-20
*
* @copyright Copyright (c) 2023, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down Expand Up @@ -59,10 +59,7 @@
#define FLIGHT_MODE_HORIZON_MIN 1700
#define FLIGHT_MODE_HORIZON_MAX 2100

#define SERIAL_RX_PIN 0 // Set SERIAL_RX_PIN to the pin that the CRSF receiver's TX pin is connected to.
#define SERIAL_TX_PIN 1 // Set SERIAL_TX_PIN to the pin that the CRSF receiver's RX pin is connected to.

CRSFforArduino crsf = CRSFforArduino(SERIAL_RX_PIN, SERIAL_TX_PIN);
CRSFforArduino crsf = CRSFforArduino(&Serial1);

void onFlightModeUpdate(serialReceiver::flightModeId_t);

Expand Down
7 changes: 2 additions & 5 deletions examples/platformio/main_flight_modes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author Cassandra "ZZ Cat" Robinson (nicad.heli.flier@gmail.com)
* @brief Demonstrates the use of CRSF for Arduino's flight mode functionality.
* @version 1.0.0
* @date 2024-1-15
* @date 2024-1-20
*
* @copyright Copyright (c) 2023, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down Expand Up @@ -58,10 +58,7 @@
#define FLIGHT_MODE_HORIZON_MIN 1700
#define FLIGHT_MODE_HORIZON_MAX 2100

#define SERIAL_RX_PIN 0 // Set SERIAL_RX_PIN to the pin that the CRSF receiver's TX pin is connected to.
#define SERIAL_TX_PIN 1 // Set SERIAL_TX_PIN to the pin that the CRSF receiver's RX pin is connected to.

CRSFforArduino crsf = CRSFforArduino(SERIAL_RX_PIN, SERIAL_TX_PIN);
CRSFforArduino crsf = CRSFforArduino(&Serial1);

void onFlightModeUpdate(serialReceiver::flightModeId_t);

Expand Down
7 changes: 2 additions & 5 deletions examples/platformio/main_rc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author Cassandra "ZZ Cat" Robinson (nicad.heli.flier@gmail.com)
* @brief This file demonstrates the full capabilities of CRSF for Arduino.
* @version 1.0.0
* @date 2024-1-15
* @date 2024-1-20
*
* @copyright Copyright (c) 2023, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down Expand Up @@ -33,10 +33,7 @@

#include "CRSFforArduino.hpp"

#define SERIAL_RX_PIN 0 // Set SERIAL_RX_PIN to the pin that the CRSF receiver's TX pin is connected to.
#define SERIAL_TX_PIN 1 // Set SERIAL_TX_PIN to the pin that the CRSF receiver's RX pin is connected to.

CRSFforArduino crsf = CRSFforArduino(SERIAL_RX_PIN, SERIAL_TX_PIN);
CRSFforArduino crsf = CRSFforArduino(&Serial1);

void setup()
{
Expand Down
6 changes: 2 additions & 4 deletions examples/platformio/main_telemetry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author Cassandra "ZZ Cat" Robinson (nicad.heli.flier@gmail.com)
* @brief This file demonstrates how to use CRSF for Arduino to send telemetry to your RC transmitter using the CRSF protocol.
* @version 1.0.0
* @date 2024-1-15
* @date 2024-1-20
*
* @copyright Copyright (c) 2023, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down Expand Up @@ -36,8 +36,6 @@ To work around this, preprocessor directives are used to exclude the main_teleme
#define VIEW_RC_CHANNELS 0 // Set VIEW_RC_CHANNELS to 1 to view the RC channel data in the serial monitor.
#define GENERATE_RANDOM_BATTERY_DATA 0 // Set GENERATE_RANDOM_BATTERY_DATA to 1 to generate random battery sensor telemetry data.
#define GENERATE_RANDOM_GPS_DATA 0 // Set GENERATE_RANDOM_GPS_DATA to 1 to generate random GPS telemetry data.
#define SERIAL_RX_PIN 0 // Set SERIAL_RX_PIN to the pin that the CRSF receiver's TX pin is connected to.
#define SERIAL_TX_PIN 1 // Set SERIAL_TX_PIN to the pin that the CRSF receiver's RX pin is connected to.

uint32_t timeNow = 0;

Expand All @@ -64,7 +62,7 @@ float speed = 500.0F; // Speed is in cm/s
float groundCourse = 275.8F; // Ground Course is in degrees.
uint8_t satellites = 7; // 7 satellites are in view (implies a 3D fix).

CRSFforArduino crsf = CRSFforArduino(SERIAL_RX_PIN, SERIAL_TX_PIN);
CRSFforArduino crsf = CRSFforArduino(&Serial1);

#if VIEW_RC_CHANNELS > 0
const int channelCount = 8;
Expand Down
6 changes: 2 additions & 4 deletions examples/telemetry/telemetry.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author Cassandra "ZZ Cat" Robinson (nicad.heli.flier@gmail.com)
* @brief This demonstrates how to use CRSF for Arduino to send telemetry to your RC transmitter using the CRSF protocol.
* @version 1.0.0
* @date 2024-1-15
* @date 2024-1-20
*
* @copyright Copyright (c) 2023, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down Expand Up @@ -35,8 +35,6 @@
#define USE_SERIAL_PLOTTER 1 // Set USE_SERIAL_PLOTTER to 1 to view this example's data in the Arduino IDE's serial plotter.
#define GENERATE_RANDOM_BATTERY_DATA 0 // Set GENERATE_RANDOM_BATTERY_DATA to 1 to generate random battery sensor telemetry data.
#define GENERATE_RANDOM_GPS_DATA 0 // Set GENERATE_RANDOM_GPS_DATA to 1 to generate random GPS telemetry data.
#define SERIAL_RX_PIN 0 // Set SERIAL_RX_PIN to the pin that the CRSF receiver's TX pin is connected to.
#define SERIAL_TX_PIN 1 // Set SERIAL_TX_PIN to the pin that the CRSF receiver's RX pin is connected to.

uint32_t timeNow = 0;

Expand Down Expand Up @@ -69,7 +67,7 @@ const int channelCount = crsfProtocol::RC_CHANNEL_COUNT; // I'm not sure if this
So, an assert is needed to prevent channelCount from being set to any arbitary number that is higher than 16. */
static_assert(channelCount <= crsfProtocol::RC_CHANNEL_COUNT, "The number of RC channels must be less than or equal to the maximum number of RC channels supported by CRSF.");

CRSFforArduino crsf = CRSFforArduino(SERIAL_RX_PIN, SERIAL_TX_PIN);
CRSFforArduino crsf = CRSFforArduino(&Serial1);

#if USE_SERIAL_PLOTTER == 0 && VIEW_RC_CHANNELS > 0
const char *channelNames[crsfProtocol::RC_CHANNEL_COUNT] = {
Expand Down
2 changes: 1 addition & 1 deletion src/CRSFforArduino.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author Cassandra "ZZ Cat" Robinson (nicad.heli.flier@gmail.com)
* @brief Top level header for CRSF for Arduino, to help with Arduino IDE compatibility.
* @version 1.0.0
* @date 2024-1-15
* @date 2024-1-20
*
* @copyright Copyright (c) 2023, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down
8 changes: 4 additions & 4 deletions src/CRSFforArduino/src/CFA_Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author Cassandra "ZZ Cat" Robinson (nicad.heli.flier@gmail.com)
* @brief This is the configuration file for CRSF for Arduino.
* @version 1.0.0
* @date 2024-1-15
* @date 2024-1-20
*
* @copyright Copyright (c) 2023, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down Expand Up @@ -37,9 +37,9 @@ namespace crsfForArduinoConfig
Versioning is done using Semantic Versioning 2.0.0.
See https://semver.org/ for more information. */
#define CRSFFORARDUINO_VERSION "1.0.0"
#define CRSFFORARDUINO_VERSION_DATE "2024-1-15"
#define CRSFFORARDUINO_VERSION_MAJOR 0
#define CRSFFORARDUINO_VERSION_MINOR 5
#define CRSFFORARDUINO_VERSION_DATE "2024-1-20"
#define CRSFFORARDUINO_VERSION_MAJOR 1
#define CRSFFORARDUINO_VERSION_MINOR 0
#define CRSFFORARDUINO_VERSION_PATCH 0

/* RC Options
Expand Down
6 changes: 3 additions & 3 deletions src/CRSFforArduino/src/CRSFforArduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author Cassandra "ZZ Cat" Robinson (nicad.heli.flier@gmail.com)
* @brief CRSF for Arduino facilitates the use of ExpressLRS RC receivers in Arduino projects.
* @version 1.0.0
* @date 2024-1-15
* @date 2024-1-20
*
* @copyright Copyright (c) 2023, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down Expand Up @@ -45,10 +45,10 @@ namespace sketchLayer
* @param rxPin
* @param txPin
*/
CRSFforArduino::CRSFforArduino(uint8_t rxPin, uint8_t txPin)
CRSFforArduino::CRSFforArduino(HardwareSerial *serialPort)
{
#if CRSF_RC_ENABLED > 0 || CRSF_TELEMETRY_ENABLED > 0
_serialReceiver = new SerialReceiver(rxPin, txPin);
_serialReceiver = new SerialReceiver(serialPort);
#else
// Prevent compiler warnings
(void)rxPin;
Expand Down
4 changes: 2 additions & 2 deletions src/CRSFforArduino/src/CRSFforArduino.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author Cassandra "ZZ Cat" Robinson (nicad.heli.flier@gmail.com)
* @brief CRSF for Arduino facilitates the use of ExpressLRS RC receivers in Arduino projects.
* @version 1.0.0
* @date 2024-1-15
* @date 2024-1-20
*
* @copyright Copyright (c) 2023, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down Expand Up @@ -40,7 +40,7 @@ namespace sketchLayer
{
public:
CRSFforArduino();
CRSFforArduino(uint8_t RxPin, uint8_t TxPin);
CRSFforArduino(HardwareSerial *serialPort);
~CRSFforArduino();
bool begin();
void end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @brief This is the implementation of the Compatibility Table.
* It is used to determine if the target development board is compatible with CRSF for Arduino.
* @version 1.0.0
* @date 2024-1-15
* @date 2024-1-20
*
* @copyright Copyright (c) 2023, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* @author Cassandra "ZZ Cat" Robinson (nicad.heli.flier@gmail.com)
* @brief This is the Compatibility Table header file.
* @version 1.0.0
* @date 2024-1-15
* @date 2024-1-20
*
* @copyright Copyright (c) 2023, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down
Loading

0 comments on commit c9563f1

Please sign in to comment.