【发布时间】:2019-06-05 16:32:10
【问题描述】:
我正在尝试实现这个库的 NeopixelAnimator 类:https://github.com/Makuna/NeoPixelBus。
我创建了一个动画器类,其中包含库的 NeoPixelAnimator 类的一个实例。要运行动画,我必须调用:.StartAnimation(uint16_t indexAnimation, uint16_t duration, UpdateCallback animUpdate)。
我的目标是制作一系列动画,这些动画一个接一个地被动画化。第二个动画应该在第一个动画完成后开始。由于库本身不提供执行此操作的方法,因此我创建了一个表示队列的向量,它包含要设置动画的动画及其颜色。
问题在于.StartAnimation() 函数需要一个UpdateCallback 参数,该参数是一个回调函数,每当调用.UpdateAnimations() 函数时都会触发该参数。
动画将由连接到 Arduino Due 的开关触发,该开关调用.add() 函数(不在示例代码中)。现在,我想通过在.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
animator.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();
animator.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());
}
}
main.cpp:
Animator animator;
void setup()
{
animator.init();
}
void loop()
{
//Put new animation requests in queue and update animations
animator.run();
}
platformio.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 &param)”类型的值不能用于初始化“AnimUpdateCallback (*)(const AnimationParam &param)”类型的实体
我的主要困惑是:
我的类中回调函数的数据类型应该是什么(
void或AnimUpdateCallback)?为什么
(const AnimationParam &param)必须加在(*_callbackPt[NUMBER_OF_ANIMATIONS])的末尾,否则会报错数据类型也不匹配
【问题讨论】:
-
我会发布一个关于语法的答案,但如果你想出一个minimal reproducible example,你可以让这个问题对其他人更有用。这将需要两个关键步骤:删除不需要的代码并添加需要的定义。由于这基本上是一个语法问题,您可以去掉大多数不使用
_callbackPt的函数。此外,对于所需的定义,只需一些允许编译的虚拟定义就足够了。 -
@JaMiT 感谢您的快速回复。我会尝试重写我的问题。
标签: c++ embedded arduino-due