How to wrap the overload functions and function pointer of the third-party Arduino library in creating custom add-on library for the Arduino?

6 次查看(过去 30 天)
I have a third-party Arduino library (Smooth 10-bit LED control library) downloaded from https://github.com/Zanduino/SmoothLED_10bit.
I would like to creating a custom add-on library for including the third-party library. The c++ source file in the SmoothLED library includes some overload function operations and function pointer, such as the class smoothLED defined in the header file(SmoothLED.h):
class smoothLED {
/*!
@class smoothLED
@brief Class to allow any Arduino pins to be used with 10-bit PWM
*/
public: // Declare all publicly visible members
smoothLED(); // Class constructor
~smoothLED(); // Class destructor
smoothLED(const smoothLED&) = delete; // disable copy constructor
smoothLED(smoothLED&& led) = delete; // disable move constructor
smoothLED& operator++(); // prefix increment overload
smoothLED operator++(int) = delete; // disallow postfix increment
smoothLED& operator--(); // prefix decrement overload
smoothLED operator--(int) = delete; // disallow postfix decrement
smoothLED& operator+=(const int16_t& value); // addition overload
smoothLED& operator-=(const int16_t& value); // subtraction overload
smoothLED& operator=(const smoothLED& value); // equals overload
smoothLED& operator+(const int16_t& value); // addition overload
smoothLED& operator-(const int16_t& value); // subtraction overload
bool begin(const uint8_t pin, // Initialize a pin for PWM
const bool invert = false); // optionally invert values
void hertz(const uint8_t hertz) const; // Set hertz rate for PWM
static void pwmISR(); // Actual PWM function
static void faderISR(); // Actual fader function
void set(const uint16_t& val, // Set a pin's value
const uint16_t& speed = 0); // optional change speed in milliseconds
private: // declare the private class members
static smoothLED* _firstLink; //!< Static pointer to first instance in list
static uint16_t _counterPWM; //!< loop counter 0-1023 for software PWM
volatile uint8_t* _portRegister{nullptr}; //!< Pointer to the actual PORT{n} Register
smoothLED* _nextLink{nullptr}; //!< Pointer to the next instance in list
uint8_t _registerBitMask{0}; //!< bit mask for the bit used in PORT{n}
volatile uint16_t _currentLevel{0}; //!< Current PWM level 0-1023
volatile uint16_t _currentCIE{0}; //!< Current PWM level from cie table
uint16_t _targetLevel{0}; //!< Target PWM level 0-1023
volatile uint8_t _flags{0}; //!< Status bits, see cpp file for details
uint16_t _changeDelays{0}; //!< Variable storing delay time for fades
volatile int16_t _changeTicker{0}; //!< Countdown timer used in fading
inline void pinOn() const __attribute__((always_inline)); // Turn LED on
inline void pinOff() const __attribute__((always_inline)); // Turn LED off
}; // of class smoothLED //
#endif
I write a wrap function in C++ header file, i.e.,
void createSmoothLED()
{
smLED = new smoothLED();
debugPrint(MSG_smLED_CREATE_SMOOTHLED);
}
But when I connect the Arduino, only one instance of smoothLED can be successfully created. When I create the second instance of smoothLED, MATLAB report an error:
“Unable to receive data from the target hardware. For MATLAB, reconnect the target hardware, clear the hardware objects and try again.”
I want to control RGB LED through the third-party library smoothLED.
So how can I revise my code?

采纳的回答

Aman Banthia
Aman Banthia 2023-9-26
Hi Fuzheng,
I understand that you are trying to create multiple instances of the ‘smoothLED’ class from the ‘SmoothLED’ library for Arduino and Simulink and encountering an error when creating more than one instance.
The issue you're facing may be related to the way you're handling the creation of ‘smoothLED’ instances. Arduino has limited memory and creating multiple instances of a class may lead to memory exhaustion if not handled properly.
Here is a suggestion to revise your code:
Check for Memory Exhaustion: Before creating a new ‘smoothLED’ instance, check if there's enough memory available. You can use the ‘freeMemory’ function from the ‘MemoryFree’ library to do this. If there's not enough memory available, don't create the ‘smoothLED’ instance and print an error message instead.
#include <MemoryFree.h>
void createSmoothLEDs() {
for (int i = 0; i < NUM_LEDS; i++) {
if (freeMemory() > sizeof(smoothLED)) {
leds[i] = new smoothLED();
} else {
debugPrint(MSG_smLED_MEMORY_ERROR);
break;
}
}
debugPrint(MSG_smLED_CREATE_SMOOTHLED);
}
Refer to following MATLAB Video link to know more about creating a Custom Arduino Add-On Library:
Hope the above solution helps you.
Best Regards,
Aman Banthia
  1 个评论
Fuzheng Zhang
Fuzheng Zhang 2023-10-7
Hi Banthia,
Thank you for your suggestions. Following the MATLAB Video you provided, I revised my codes. I can create successfully multiple instances of the ‘smoothLED’ class from the ‘SmoothLED’ library. However, when I call the wrap function "setPWM()" for the function "set()" of the ‘SmoothLED’ library to control each LED, there are noticeable flickering with the LEDs at the PWM value lower than 1023. I tried to change the LED refresh rate using the wrap function "hertz()" for the function "hertz()" of the ‘SmoothLED’ library, but MATLAB report an error again:
“Unable to receive data from the target hardware. For MATLAB, reconnect the target hardware, clear the hardware objects and try again.”
I have uploaded my add-on library. Could you give me any suggestion? Thank you again.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Arduino Hardware 的更多信息

产品


版本

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by