我正在尝试实现这个库的 NeopixelAnimator 类:https ://github.com/Makuna/NeoPixelBus 。
我创建了一个动画器类,其中包含库的 NeoPixelAnimator 类的一个实例。要运行动画,我必须调用:.StartAnimation(uint16_t indexAnimation, uint16_t duration, UpdateCallback animUpdate)
.
我的目标是制作一系列动画,这些动画一个接一个。第二个动画应该在第一个动画完成后开始。由于库本身不提供执行此操作的方法,因此我创建了一个表示队列的向量,它包含要设置动画的动画及其颜色。
问题是该.StartAnimation()
函数需要一个UpdateCallback
参数,该参数是一个回调函数,每当调用该函数时就会触发该.UpdateAnimations()
函数。
动画将由连接到调用.add()
函数的 Arduino Due 的开关触发(不在示例代码中)。现在,我想通过在.init()
.
我的计划是创建一个指向这些回调函数的函数指针数组。问题是我无法让数据类型匹配。
帮助将不胜感激。此外,我们将不胜感激有关如何处理此类问题和构建代码的反馈。
我在 Arduino Due 板上使用 platformIO 和 Arduino 库。
定义.h:
//Amount of different animations should correspond to the number of callback functions
#define NUMBER_OF_ANIMATIONS 2
//Animation ID's:
#define WIPE_ANIM 0
//Colour ID's:
#define GREEN 0
动画师.h:
#include <arduino.h>
#include <NeoPixelAnimator.h>
#include <NeoPixelBrightnessBus.h>
#include <array>
#include <vector>
class Animator
{
public:
private:
//NeoPixel animation time management object
NeoPixelAnimator animations;
//Neopixel strip communication object
NeoPixelBrightnessBus<NeoGrbFeature, Neo800KbpsMethod> strip1; //NeoPixel strip 1 object
//Array of AnimUpdateCallback function pointers to the animation callback functions
AnimUpdateCallback (*_callbackPt[NUMBER_OF_ANIMATIONS])(const AnimationParam ¶m);
//Vector of arrays to store animations to be animated (animId, colorId)
std::vector<std::array<uint8_t, 2>> _animationQueue;
public:
Animator();
void init();
void run();
void add(uint8_t animId, uint8_t colorId);
private:
void updateQueue();
动画师.cpp:
#include <Animator.h>
//Default constructor
Animator::Animator() : strip1(NUMBER_OF_LEDS_1, LED_PIN_1),
_callbackPt{wipeColorAnim, wipeColorAnim}
{
}
//Inititalisation function, inits strips, sets brightness for strips, sets inital color with wipeColorAnim()
void Animator::init()
{
strip1.Begin();
strip1.SetBrightness(100);
add(WIPE_ANIM, GREEN); //wipeColor strip1 with Green
}
//Add aniamtion to the queue
void Animator::add(uint8_t animId, uint8_t colorId)
{
//Create array storing the animation ID and its color
std::array<uint8_t, 2> animation{animId, colorId};
//Add the animation to the end of the queue
_animationQueue.push_back(animation);
}
//Loop this function to update animations
void Animator::run()
{
//if there is an animation running
if (animations.IsAnimating())
{
animations.UpdateAnimations(); //update running animation
strip1.Show();
}
else
{
updateQueue();
}
}
//Checks whether there is an animation in the queue if so it's started
void Animator::updateQueue()
{
//if there is an animation waiting in the queue
if (!_animationQueue.empty())
{
//Start next animation in queue on channel 0 with specified time and animUpdate callback function (channel, time, callback)
animations.StartAnimation(0, _animationQueue[0][1], _callbackPt[_animationQueue[0][0]]);
//Remove the just started animation from the queue
_animationQueue.erase(_animationQueue.begin());
}
}
主.cpp:
Animator animator;
void setup()
{
animator.init();
}
void loop()
{
//Put new animation requests in queue and update animations
animator.run();
}
平台io.ini:
[env:due]
lib_ldf_mode = chain+
platform = atmelsam
board = due
framework = arduino
monitor_speed = 115200
monitor_port = COM16
lib_deps =
NeoPixelBus
调用animations.StartAnimation(0, _animationQueue[0][1], _callbackPt[_animationQueue[0][0]]);
不会给出任何编译错误。尝试在函数指针数组中设置回调函数会:
“”类型的值AnimUpdateCallback (Animator::*)(const AnimationParam ¶m)
不能用于初始化“ AnimUpdateCallback (*)(const AnimationParam ¶m)
”类型的实体
我的主要困惑是:
我的类中回调函数的数据类型应该是(
void
或AnimUpdateCallback
)?为什么
(const AnimationParam ¶m)
必须在末尾添加(*_callbackPt[NUMBER_OF_ANIMATIONS])
,否则我会收到数据类型也不匹配的错误