Skip to content

Commit

Permalink
refactor(library): Flight Modes API and Custom Flight Modes integrati…
Browse files Browse the repository at this point in the history
…on, plus code and comments clean-up (#84)
  • Loading branch information
ZZ-Cat authored Feb 20, 2024
1 parent 5997c35 commit 38f3609
Show file tree
Hide file tree
Showing 25 changed files with 224 additions and 231 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
*.c text eol=lf
*.cpp text eol=lf
*.h text eol=lf
*.hpp text eol=lf
*.ini text eol=lf
*.ino text eol=lf
*.json text eol=lf
Expand Down
5 changes: 3 additions & 2 deletions .github/workflows/arduinoide_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ jobs:
- name: Check for correct code formatting with clang-format
run: python3 ci/run-clang-format.py -e "ci/*" -e "bin/*" -r .

- name: Test the code on supported platforms
run: python3 ci/build_platform.py metro_m4
# Disabled until Adafruit fixes their fault detection issue.
# - name: Test the code on supported platforms
# run: python3 ci/build_platform.py metro_m4
2 changes: 1 addition & 1 deletion 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 Example of how to read flight modes from a receiver.
* @version 1.0.0
* @date 2024-2-14
* @date 2024-2-18
*
* @copyright Copyright (c) 2024, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down
2 changes: 1 addition & 1 deletion examples/link_stats/link_stats.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 link statistics from a receiver.
* @version 1.0.0
* @date 2024-2-14
* @date 2024-2-18
*
* @copyright Copyright (c) 2024, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down
2 changes: 1 addition & 1 deletion 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-14
* @date 2024-2-18
*
* @copyright Copyright (c) 2024, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down
2 changes: 1 addition & 1 deletion 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-14
* @date 2024-2-18
*
* @copyright Copyright (c) 2024, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down
2 changes: 1 addition & 1 deletion 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 Example of how to send telemetry back to your RC handset using CRSF for Arduino.
* @version 1.0.0
* @date 2024-2-14
* @date 2024-2-18
*
* @copyright Copyright (c) 2024, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down
4 changes: 4 additions & 0 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,9 @@ test_dir =

; [env:development]
; board = adafruit_metro_m4
; build_src_filter =
; +<../examples/platformio/cfa_code_test.cpp>
; +<*/*/*.cpp>
; +<*.cpp>
; build_type = debug
; extends = env_common_samd51
8 changes: 7 additions & 1 deletion 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-14
* @date 2024-2-18
*
* @copyright Copyright (c) 2024, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down Expand Up @@ -77,6 +77,12 @@ Pro Tip: You can combine the Flight Mode API with the Telemetry API to send flig
information back to your controller. */
#define CRSF_FLIGHTMODES_ENABLED 0

/* Custom Flight Modes
Enables or disables the Custom Flight Modes.
When enabled, this allows you to implement flight modes with custom names
and assign them to a switch on your controller. */
#define CRSF_CUSTOM_FLIGHT_MODES_ENABLED 0

/* Telemetry Options
- TELEMETRY_ENABLED: Enables or disables the Telemetry API.
- TELEMETRY_ATTITUDE_ENABLED: Enables or disables attitude telemetry output.
Expand Down
83 changes: 49 additions & 34 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-14
* @date 2024-2-18
*
* @copyright Copyright (c) 2024, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down Expand Up @@ -36,9 +36,6 @@ namespace sketchLayer
*/
CRSFforArduino::CRSFforArduino()
{
#if CRSF_RC_ENABLED > 0 || CRSF_TELEMETRY_ENABLED > 0
_serialReceiver = new SerialReceiver();
#endif
}

/**
Expand All @@ -49,13 +46,6 @@ namespace sketchLayer
*/
CRSFforArduino::CRSFforArduino(HardwareSerial *serialPort)
{
#if CRSF_RC_ENABLED > 0 || CRSF_TELEMETRY_ENABLED > 0
_serialReceiver = new SerialReceiver(serialPort);
#else
// Prevent compiler warnings
(void)rxPin;
(void)txPin;
#endif
}

/**
Expand All @@ -64,9 +54,6 @@ namespace sketchLayer
*/
CRSFforArduino::~CRSFforArduino()
{
#if CRSF_RC_ENABLED > 0 || CRSF_TELEMETRY_ENABLED > 0
delete _serialReceiver;
#endif
}

/**
Expand All @@ -77,7 +64,7 @@ namespace sketchLayer
bool CRSFforArduino::begin()
{
#if CRSF_RC_ENABLED > 0 || CRSF_TELEMETRY_ENABLED > 0
return _serialReceiver->begin();
return this->SerialReceiver::begin();
#else
// Return false if RC is disabled
return false;
Expand All @@ -91,7 +78,7 @@ namespace sketchLayer
void CRSFforArduino::end()
{
#if CRSF_RC_ENABLED > 0 || CRSF_TELEMETRY_ENABLED > 0
_serialReceiver->end();
this->SerialReceiver::end();
#endif
}

Expand All @@ -103,11 +90,11 @@ namespace sketchLayer
void CRSFforArduino::update()
{
#if CRSF_RC_ENABLED > 0 || CRSF_TELEMETRY_ENABLED > 0
_serialReceiver->processFrames();
this->SerialReceiver::processFrames();
#endif

#if CRSF_RC_ENABLED > 0 && CRSF_FLIGHTMODES_ENABLED > 0
_serialReceiver->handleFlightMode();
this->SerialReceiver::handleFlightMode();
#endif
}

Expand All @@ -121,7 +108,7 @@ namespace sketchLayer
[[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);
return this->SerialReceiver::readRcChannel(channel - 1, raw);
#else
// Prevent compiler warnings
(void)channel;
Expand All @@ -141,7 +128,7 @@ namespace sketchLayer
[[deprecated("Use RC channel callback instead")]] uint16_t CRSFforArduino::getChannel(uint8_t channel)
{
#if CRSF_RC_ENABLED > 0
return _serialReceiver->getChannel(channel - 1);
return this->SerialReceiver::getChannel(channel - 1);
#else
// Prevent compiler warnings
(void)channel;
Expand All @@ -160,7 +147,7 @@ namespace sketchLayer
uint16_t CRSFforArduino::rcToUs(uint16_t rc)
{
#if CRSF_RC_ENABLED > 0
return _serialReceiver->rcToUs(rc);
return this->SerialReceiver::rcToUs(rc);
#else
// Prevent compiler warnings
(void)rc;
Expand All @@ -173,7 +160,7 @@ namespace sketchLayer
void CRSFforArduino::setRcChannelsCallback(void (*callback)(serialReceiverLayer::rcChannels_t *rcChannels))
{
#if CRSF_RC_ENABLED > 0
_serialReceiver->setRcChannelsCallback(callback);
this->SerialReceiver::setRcChannelsCallback(callback);
#else
// Prevent compiler warnings
(void)callback;
Expand All @@ -183,13 +170,40 @@ namespace sketchLayer
void CRSFforArduino::setLinkStatisticsCallback(void (*callback)(serialReceiverLayer::link_statistics_t linkStatistics))
{
#if CRSF_LINK_STATISTICS_ENABLED > 0
_serialReceiver->setLinkStatisticsCallback(callback);
this->SerialReceiver::setLinkStatisticsCallback(callback);
#else
// Prevent compiler warnings
(void)callback;
#endif
}

/**
* @brief Assigns a Flight Mode to the specified channel.
*
* @param flightModeId The ID of the Flight Mode to assign.
* @param flightModeName The name of the Flight Mode to assign.
* @param channel The channel to assign the Flight Mode to.
* @param min The minimum RC value for the Flight Mode to be active.
* @param max The maximum RC value for the Flight Mode to be active.
* @return true if the Flight Mode was assigned successfully.
*/
bool CRSFforArduino::setFlightMode(serialReceiverLayer::flightModeId_t flightModeId, const char *flightModeName, uint8_t channel, uint16_t min, uint16_t max)
{
#if CRSF_RC_ENABLED > 0 && CRSF_FLIGHTMODES_ENABLED > 0
return this->SerialReceiver::setFlightMode(flightModeId, flightModeName, channel - 1, this->SerialReceiver::usToRc(min), this->SerialReceiver::usToRc(max));
#else
// Prevent compiler warnings
(void)flightModeId;
(void)flightModeName;
(void)channel;
(void)min;
(void)max;

// Return false if RC is disabled
return false;
#endif
}

/**
* @brief Assigns a Flight Mode to the specified channel.
*
Expand All @@ -199,10 +213,10 @@ namespace sketchLayer
* @param max The maximum RC value for the Flight Mode to be active.
* @return true if the Flight Mode was assigned successfully.
*/
bool CRSFforArduino::setFlightMode(serialReceiverLayer::flightModeId_t flightMode, uint8_t channel, uint16_t min, uint16_t max)
[[deprecated("This function must pass in the name of the Flight Mode as well as its ID.")]] bool CRSFforArduino::setFlightMode(serialReceiverLayer::flightModeId_t flightMode, uint8_t channel, uint16_t min, uint16_t max)
{
#if CRSF_RC_ENABLED > 0 && CRSF_FLIGHTMODES_ENABLED > 0
return _serialReceiver->setFlightMode(flightMode, channel - 1, _serialReceiver->usToRc(min), _serialReceiver->usToRc(max));
return this->SerialReceiver::setFlightMode(flightMode, channel - 1, this->SerialReceiver::usToRc(min), this->SerialReceiver::usToRc(max));
#else
// Prevent compiler warnings
(void)flightMode;
Expand All @@ -223,7 +237,7 @@ namespace sketchLayer
void CRSFforArduino::setFlightModeCallback(void (*callback)(serialReceiverLayer::flightModeId_t flightMode))
{
#if CRSF_RC_ENABLED > 0 && CRSF_FLIGHTMODES_ENABLED > 0
_serialReceiver->setFlightModeCallback(callback);
this->SerialReceiver::setFlightModeCallback(callback);
#else
// Prevent compiler warnings
(void)callback;
Expand All @@ -240,7 +254,7 @@ namespace sketchLayer
void CRSFforArduino::telemetryWriteAttitude(int16_t roll, int16_t pitch, int16_t yaw)
{
#if CRSF_TELEMETRY_ENABLED > 0 && CRSF_TELEMETRY_ATTITUDE_ENABLED > 0
_serialReceiver->telemetryWriteAttitude(roll, pitch, yaw);
this->SerialReceiver::telemetryWriteAttitude(roll, pitch, yaw);
#else
// Prevent compiler warnings
(void)roll;
Expand All @@ -258,7 +272,7 @@ namespace sketchLayer
void CRSFforArduino::telemetryWriteBaroAltitude(uint16_t altitude, int16_t vario)
{
#if CRSF_TELEMETRY_ENABLED > 0 && CRSF_TELEMETRY_BAROALTITUDE_ENABLED > 0
_serialReceiver->telemetryWriteBaroAltitude(altitude, vario);
this->SerialReceiver::telemetryWriteBaroAltitude(altitude, vario);
#else
// Prevent compiler warnings
(void)altitude;
Expand All @@ -277,7 +291,7 @@ namespace sketchLayer
void CRSFforArduino::telemetryWriteBattery(float voltage, float current, uint32_t fuel, uint8_t percent)
{
#if CRSF_TELEMETRY_ENABLED > 0 && CRSF_TELEMETRY_BATTERY_ENABLED > 0
_serialReceiver->telemetryWriteBattery(voltage, current, fuel, percent);
this->SerialReceiver::telemetryWriteBattery(voltage, current, fuel, percent);
#else
// Prevent compiler warnings
(void)voltage;
Expand All @@ -292,13 +306,14 @@ namespace sketchLayer
*
* @param flightMode The Flight Mode to send.
*/
void CRSFforArduino::telemetryWriteFlightMode(serialReceiverLayer::flightModeId_t flightMode)
void CRSFforArduino::telemetryWriteFlightMode(serialReceiverLayer::flightModeId_t flightMode, bool disarmed)
{
#if CRSF_TELEMETRY_ENABLED > 0 && CRSF_TELEMETRY_FLIGHTMODE_ENABLED > 0
_serialReceiver->telemetryWriteFlightMode(flightMode);
this->SerialReceiver::telemetryWriteFlightMode(flightMode, disarmed);
#else
// Prevent compiler warnings
(void)flightMode;
(void)disarmed;
#endif
}

Expand All @@ -307,10 +322,10 @@ namespace sketchLayer
*
* @param flightMode The Flight Mode string to send.
*/
void CRSFforArduino::telemetryWriteCustomFlightMode(const char *flightMode, bool armed)
[[deprecated("This is nos handled automatically with telemetryWriteFlightMode()")]] void CRSFforArduino::telemetryWriteCustomFlightMode(const char *flightMode, bool armed)
{
#if CRSF_TELEMETRY_ENABLED > 0 && CRSF_TELEMETRY_FLIGHTMODE_ENABLED > 0
_serialReceiver->telemetryWriteCustomFlightMode(flightMode, armed);
this->SerialReceiver::telemetryWriteCustomFlightMode(flightMode, armed);
#else
// Prevent compiler warnings
(void)flightMode;
Expand All @@ -330,7 +345,7 @@ namespace sketchLayer
void CRSFforArduino::telemetryWriteGPS(float latitude, float longitude, float altitude, float speed, float groundCourse, uint8_t satellites)
{
#if CRSF_TELEMETRY_ENABLED > 0 && CRSF_TELEMETRY_GPS_ENABLED > 0
_serialReceiver->telemetryWriteGPS(latitude, longitude, altitude, speed, groundCourse, satellites);
this->SerialReceiver::telemetryWriteGPS(latitude, longitude, altitude, speed, groundCourse, satellites);
#else
// Prevent compiler warnings
(void)latitude;
Expand Down
6 changes: 3 additions & 3 deletions 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-14
* @date 2024-2-18
*
* @copyright Copyright (c) 2024, Cassandra "ZZ Cat" Robinson. All rights reserved.
*
Expand Down Expand Up @@ -52,19 +52,19 @@ namespace sketchLayer
void setLinkStatisticsCallback(void (*callback)(serialReceiverLayer::link_statistics_t linkStatistics));

// Flight mode functions.
bool setFlightMode(serialReceiverLayer::flightModeId_t flightModeId, const char *flightModeName, uint8_t channel, uint16_t min, uint16_t max);
bool setFlightMode(serialReceiverLayer::flightModeId_t flightMode, uint8_t channel, uint16_t min, uint16_t max);
void setFlightModeCallback(void (*callback)(serialReceiverLayer::flightModeId_t flightMode));

// Telemetry functions.
void telemetryWriteAttitude(int16_t roll, int16_t pitch, int16_t yaw);
void telemetryWriteBaroAltitude(uint16_t altitude, int16_t vario);
void telemetryWriteBattery(float voltage, float current, uint32_t fuel, uint8_t percent);
void telemetryWriteFlightMode(serialReceiverLayer::flightModeId_t flightMode);
void telemetryWriteFlightMode(serialReceiverLayer::flightModeId_t flightMode, bool disarmed = false);
void telemetryWriteCustomFlightMode(const char *flightMode, bool armed = false);
void telemetryWriteGPS(float latitude, float longitude, float altitude, float speed, float groundCourse, uint8_t satellites);

private:
SerialReceiver *_serialReceiver;
};
} // namespace sketchLayer

Expand Down
Loading

0 comments on commit 38f3609

Please sign in to comment.