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?
0 个评论
采纳的回答
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
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Arduino Hardware 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!