Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 40 additions & 20 deletions examples/platformio/main.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 is the main development file for CRSF for Arduino.
* @version 1.0.0
* @date 2024-2-6
* @date 2024-2-7
*
* @copyright Copyright (c) 2024, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand All @@ -29,6 +29,8 @@

CRSFforArduino *crsf = nullptr;

void onReceiveRcChannels(serialReceiverLayer::rcChannels_t *rcChannels);

void setup()
{
Serial.begin(115200);
Expand All @@ -51,33 +53,51 @@ void setup()
delay(10);
}
}

crsf->setRcChannelsCallback(onReceiveRcChannels);
}

void loop()
{
crsf->update();
}

/* Print RC channels every 100 ms. Do this using the millis() function to avoid blocking the main loop. */
static unsigned long lastPrint = 0;
void onReceiveRcChannels(serialReceiverLayer::rcChannels_t *rcChannels)
{
static unsigned long lastPrint = millis();
if (millis() - lastPrint >= 100)
{
lastPrint = millis();
Serial.print("RC Channels <A: ");
Serial.print(crsf->rcToUs(crsf->getChannel(1)));
Serial.print(", E: ");
Serial.print(crsf->rcToUs(crsf->getChannel(2)));
Serial.print(", T: ");
Serial.print(crsf->rcToUs(crsf->getChannel(3)));
Serial.print(", R: ");
Serial.print(crsf->rcToUs(crsf->getChannel(4)));
Serial.print(", Aux1: ");
Serial.print(crsf->rcToUs(crsf->getChannel(5)));
Serial.print(", Aux2: ");
Serial.print(crsf->rcToUs(crsf->getChannel(6)));
Serial.print(", Aux3: ");
Serial.print(crsf->rcToUs(crsf->getChannel(7)));
Serial.print(", Aux4: ");
Serial.print(crsf->rcToUs(crsf->getChannel(8)));
Serial.println(">");

static bool initialised = false;
static bool lastFailSafe = false;
if (rcChannels->failsafe != lastFailSafe || !initialised)
{
initialised = true;
lastFailSafe = rcChannels->failsafe;
Serial.print("FailSafe: ");
Serial.println(lastFailSafe ? "Active" : "Inactive");
}

if (rcChannels->failsafe == false)
{
Serial.print("RC Channels <A: ");
Serial.print(crsf->rcToUs(rcChannels->value[0]));
Serial.print(", E: ");
Serial.print(crsf->rcToUs(rcChannels->value[1]));
Serial.print(", T: ");
Serial.print(crsf->rcToUs(rcChannels->value[2]));
Serial.print(", R: ");
Serial.print(crsf->rcToUs(rcChannels->value[3]));
Serial.print(", Aux1: ");
Serial.print(crsf->rcToUs(rcChannels->value[4]));
Serial.print(", Aux2: ");
Serial.print(crsf->rcToUs(rcChannels->value[5]));
Serial.print(", Aux3: ");
Serial.print(crsf->rcToUs(rcChannels->value[6]));
Serial.print(", Aux4: ");
Serial.print(crsf->rcToUs(rcChannels->value[7]));
Serial.println(">");
}
}
}
70 changes: 40 additions & 30 deletions examples/rc_channels/rc_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 Example of how to read rc channels from a receiver.
* @version 1.0.0
* @date 2024-2-6
* @date 2024-2-7
*
* @copyright Copyright (c) 2024, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down Expand Up @@ -49,6 +49,8 @@ const char *rcChannelNames[] = {
"Aux11",
"Aux12"};

void onReceiveRcChannels(serialReceiverLayer::rcChannels_t *rcChannels);

void setup()
{
// Initialise the serial port & wait for the port to open.
Expand Down Expand Up @@ -76,6 +78,8 @@ void setup()

rcChannelCount = rcChannelCount > crsfProtocol::RC_CHANNEL_COUNT ? crsfProtocol::RC_CHANNEL_COUNT : rcChannelCount;

crsf->setRcChannelsCallback(onReceiveRcChannels);

// Show the user that the sketch is ready.
Serial.println("RC Channels Example");
delay(1000);
Expand All @@ -86,43 +90,49 @@ void setup()
void loop()
{
crsf->update();
}

/* Print RC channels every 100 ms. */
unsigned long thisTime = millis();
static unsigned long lastTime = millis();

/* Compensate for millis() overflow. */
if (thisTime < lastTime)
void onReceiveRcChannels(serialReceiverLayer::rcChannels_t *rcChannels)
{
if (rcChannels->failsafe == false)
{
lastTime = thisTime;
}
/* Print RC channels every 100 ms. */
unsigned long thisTime = millis();
static unsigned long lastTime = millis();

if (thisTime - lastTime >= 100)
{
lastTime = thisTime;
#if USE_SERIAL_PLOTTER > 0
for (int i = 1; i <= rcChannelCount; i++)
/* Compensate for millis() overflow. */
if (thisTime < lastTime)
{
Serial.print(i);
Serial.print(":");
Serial.print(crsf->rcToUs(crsf->getChannel(i)));
Serial.print("\t");
lastTime = thisTime;
}
Serial.println();
#else
Serial.print("RC Channels <");
for (int i = 1; i <= rcChannelCount; i++)
{
Serial.print(rcChannelNames[i - 1]);
Serial.print(": ");
Serial.print(crsf->rcToUs(crsf->getChannel(i)));

if (i < rcChannelCount)
if (thisTime - lastTime >= 100)
{
lastTime = thisTime;
#if USE_SERIAL_PLOTTER > 0
for (int i = 1; i <= rcChannelCount; i++)
{
Serial.print(", ");
Serial.print(i);
Serial.print(":");
Serial.print(crsf->rcToUs(crsf->getChannel(i)));
Serial.print("\t");
}
}
Serial.println(">");
Serial.println();
#else
Serial.print("RC Channels <");
for (int i = 1; i <= rcChannelCount; i++)
{
Serial.print(rcChannelNames[i - 1]);
Serial.print(": ");
Serial.print(crsf->rcToUs(crsf->getChannel(i)));

if (i < rcChannelCount)
{
Serial.print(", ");
}
}
Serial.println(">");
#endif
}
}
}
12 changes: 10 additions & 2 deletions 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-2-6
* @date 2024-2-7
*
* @copyright Copyright (c) 2024, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down Expand Up @@ -37,11 +37,19 @@ 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-2-6"
#define CRSFFORARDUINO_VERSION_DATE "2024-2-7"
#define CRSFFORARDUINO_VERSION_MAJOR 1
#define CRSFFORARDUINO_VERSION_MINOR 0
#define CRSFFORARDUINO_VERSION_PATCH 0

/* Failsafe Options
- CRSF_FAILSAFE_LQI_THRESHOLD: The minimum LQI value for the receiver to be considered connected.
- CRSF_FAILSAFE_RSSI_THRESHOLD: The minimum RSSI value for the receiver to be considered connected.
- NB: It is considered good practice to set this value to the same as the RSSI Sensitivity Limit in your Lua script.
*/
#define CRSF_FAILSAFE_LQI_THRESHOLD 80
#define CRSF_FAILSAFE_RSSI_THRESHOLD 105

/* RC Options
- RC_ENABLED: Enables or disables the RC API.
- RC_MAX_CHANNELS: The maximum number of RC channels to be received.
Expand Down
16 changes: 13 additions & 3 deletions src/CRSFforArduino.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @brief This is the Sketch Layer, which is a simplified API for CRSF for Arduino.
* It is intended to be used by the user in their sketches.
* @version 1.0.0
* @date 2024-2-6
* @date 2024-2-7
*
* @copyright Copyright (c) 2024, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down Expand Up @@ -118,7 +118,7 @@ namespace sketchLayer
*
* @return The RC value.
*/
uint16_t CRSFforArduino::readRcChannel(uint8_t channel, bool raw)
[[deprecated("Use RC channel callback instead")]] uint16_t CRSFforArduino::readRcChannel(uint8_t channel, bool raw)
{
#if CRSF_RC_ENABLED > 0
return _serialReceiver->readRcChannel(channel - 1, raw);
Expand All @@ -138,7 +138,7 @@ namespace sketchLayer
* @param channel The channel to read.
* @return The RC value.
*/
uint16_t CRSFforArduino::getChannel(uint8_t channel)
[[deprecated("Use RC channel callback instead")]] uint16_t CRSFforArduino::getChannel(uint8_t channel)
{
#if CRSF_RC_ENABLED > 0
return _serialReceiver->getChannel(channel - 1);
Expand Down Expand Up @@ -170,6 +170,16 @@ namespace sketchLayer
#endif
}

void CRSFforArduino::setRcChannelsCallback(void (*callback)(serialReceiverLayer::rcChannels_t *rcChannels))
{
#if CRSF_RC_ENABLED > 0
_serialReceiver->setRcChannelsCallback(callback);
#else
// Prevent compiler warnings
(void)callback;
#endif
}

void CRSFforArduino::setLinkStatisticsCallback(void (*callback)(serialReceiverLayer::link_statistics_t linkStatistics))
{
#if CRSF_LINK_STATISTICS_ENABLED > 0
Expand Down
3 changes: 2 additions & 1 deletion src/CRSFforArduino.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* @brief This is the Sketch Layer, which is a simplified API for CRSF for Arduino.
* It is intended to be used by the user in their sketches.
* @version 1.0.0
* @date 2024-2-6
* @date 2024-2-7
*
* @copyright Copyright (c) 2024, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down Expand Up @@ -46,6 +46,7 @@ namespace sketchLayer
uint16_t getChannel(uint8_t channel);
uint16_t rcToUs(uint16_t rc);
uint16_t readRcChannel(uint8_t channel, bool raw = false);
void setRcChannelsCallback(void (*callback)(serialReceiverLayer::rcChannels_t *rcChannels));

// Link statistics functions.
void setLinkStatisticsCallback(void (*callback)(serialReceiverLayer::link_statistics_t linkStatistics));
Expand Down
2 changes: 1 addition & 1 deletion src/SerialReceiver/CRC/CRC.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 A generic CRC8 implementation for the CRSF for Arduino library.
* @version 1.0.0
* @date 2024-2-6
* @date 2024-2-7
*
* @copyright Copyright (c) 2024, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down
2 changes: 1 addition & 1 deletion src/SerialReceiver/CRC/CRC.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 A generic CRC8 implementation for the CRSF for Arduino library.
* @version 1.0.0
* @date 2024-2-6
* @date 2024-2-7
*
* @copyright Copyright (c) 2024, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down
14 changes: 13 additions & 1 deletion src/SerialReceiver/CRSF/CRSF.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 decodes CRSF frames from a serial port.
* @version 1.0.0
* @date 2024-2-6
* @date 2024-2-7
*
* @copyright Copyright (c) 2024, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down Expand Up @@ -163,6 +163,18 @@ namespace serialReceiverLayer
return false;
}

void CRSF::getFailSafe(bool *failSafe)
{
if (linkStatistics.lqi <= CRSF_FAILSAFE_LQI_THRESHOLD || linkStatistics.rssi >= CRSF_FAILSAFE_RSSI_THRESHOLD)
{
*failSafe = true;
}
else
{
*failSafe = false;
}
}

void CRSF::getRcChannels(uint16_t *rcChannels)
{
if (rcFrameReceived)
Expand Down
3 changes: 2 additions & 1 deletion src/SerialReceiver/CRSF/CRSF.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 decodes CRSF frames from a serial port.
* @version 1.0.0
* @date 2024-2-6
* @date 2024-2-7
*
* @copyright Copyright (c) 2024, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down Expand Up @@ -62,6 +62,7 @@ namespace serialReceiverLayer
void end();
void setFrameTime(uint32_t baudRate, uint8_t packetCount = 10);
bool receiveFrames(uint8_t rxByte);
void getFailSafe(bool *failSafe);
void getRcChannels(uint16_t *rcChannels);
void getLinkStatistics(link_statistics_t *linkStats);

Expand Down
2 changes: 1 addition & 1 deletion src/SerialReceiver/CRSF/CRSFProtocol.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 file contains enums and structs for the CRSF protocol.
* @version 1.0.0
* @date 2024-2-6
* @date 2024-2-7
*
* @copyright Copyright (c) 2024, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down
2 changes: 1 addition & 1 deletion src/SerialReceiver/SerialBuffer/SerialBuffer.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 A generic serial buffer for the CRSF for Arduino library.
* @version 1.0.0
* @date 2024-2-6
* @date 2024-2-7
*
* @copyright Copyright (c) 2024, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down
2 changes: 1 addition & 1 deletion src/SerialReceiver/SerialBuffer/SerialBuffer.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 A generic serial buffer for the CRSF for Arduino library.
* @version 1.0.0
* @date 2024-2-6
* @date 2024-2-7
*
* @copyright Copyright (c) 2024, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down
Loading