0

我一直在尝试为要通过 Arduino Uno R3 控制的 RGB LED 条编写一些效果。就上下文而言,我正在制作一个球(一组 5 个 LED)沿条带前后移动、不时弹跳的动画。

导致问题的向量是Colour对象,我希望它是 CRGB 颜色的向量,当传递给函数的可用颜色数量少于可用颜色数量时,它只使用第一个 x 数量,但如果球的数量多于数量颜色那么它应该简单地重新使用它们。

如果我的格式等有任何错误,我们深表歉意。

问题是当我尝试将向量作为构造函数传递给BouncingBallEffect它时,会出现以下错误:

[File-Path]\sketch_jul20b\sketch_jul20b.ino: In constructor 'BouncingBallEffect::BouncingBallEffect(size_t, size_t, byte, bool)':
sketch_jul20b:44:28: error: no matching function for call to 'Vector<double>::Vector(size_t&)'
           Colours(ballCount)
                            ^
In file included from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:0:
[File-Path]\Vector-1.2.1\src/Vector.h:24:3: note: candidate: template<unsigned int MAX_SIZE> Vector<T>::Vector(T (&)[MAX_SIZE], size_t)
   Vector(T (&values)[MAX_SIZE],
   ^~~~~~
[File-Path]\Vector-1.2.1\src/Vector.h:24:3: note:   template argument deduction/substitution failed:
[File-Path]\sketch_jul20b\sketch_jul20b.ino:44:28: note:   mismatched types 'double [MAX_SIZE]' and 'size_t {aka unsigned int}'
           Colours(ballCount)
                            ^
In file included from [File-Path]\Vector-1.2.1\src/Vector.h:95:0,
                 from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:
[File-Path]\Vector-1.2.1\src/Vector/VectorDefinitions.h:16:1: note: candidate: Vector<T>::Vector() [with T = double]
 Vector<T>::Vector()
 ^~~~~~~~~
[File-Path]\Vector-1.2.1\src/Vector/VectorDefinitions.h:16:1: note:   candidate expects 0 arguments, 1 provided
In file included from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:0:
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note: candidate: constexpr Vector<double>::Vector(const Vector<double>&)
 class Vector
       ^~~~~~
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note:   no known conversion for argument 1 from 'size_t {aka unsigned int}' to 'const Vector<double>&'
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note: candidate: constexpr Vector<double>::Vector(Vector<double>&&)
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note:   no known conversion for argument 1 from 'size_t {aka unsigned int}' to 'Vector<double>&&'
sketch_jul20b:44:28: error: no matching function for call to 'Vector<double>::Vector(size_t&)'
           Colours(ballCount)
                            ^
In file included from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:0:
[File-Path]\Vector-1.2.1\src/Vector.h:24:3: note: candidate: template<unsigned int MAX_SIZE> Vector<T>::Vector(T (&)[MAX_SIZE], size_t)
   Vector(T (&values)[MAX_SIZE],
   ^~~~~~
[File-Path]\Vector-1.2.1\src/Vector.h:24:3: note:   template argument deduction/substitution failed:
[File-Path]\sketch_jul20b\sketch_jul20b.ino:44:28: note:   mismatched types 'double [MAX_SIZE]' and 'size_t {aka unsigned int}'
           Colours(ballCount)
                            ^
In file included from [File-Path]\Vector-1.2.1\src/Vector.h:95:0,
                 from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:
[File-Path]\Vector-1.2.1\src/Vector/VectorDefinitions.h:16:1: note: candidate: Vector<T>::Vector() [with T = double]
 Vector<T>::Vector()
 ^~~~~~~~~
[File-Path]\Vector-1.2.1\src/Vector/VectorDefinitions.h:16:1: note:   candidate expects 0 arguments, 1 provided
In file included from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:0:
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note: candidate: constexpr Vector<double>::Vector(const Vector<double>&)
 class Vector
       ^~~~~~
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note:   no known conversion for argument 1 from 'size_t {aka unsigned int}' to 'const Vector<double>&'
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note: candidate: constexpr Vector<double>::Vector(Vector<double>&&)
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note:   no known conversion for argument 1 from 'size_t {aka unsigned int}' to 'Vector<double>&&'
sketch_jul20b:44:28: error: no matching function for call to 'Vector<double>::Vector(size_t&)'
           Colours(ballCount)
                            ^
In file included from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:0:
[File-Path]\Vector-1.2.1\src/Vector.h:24:3: note: candidate: template<unsigned int MAX_SIZE> Vector<T>::Vector(T (&)[MAX_SIZE], size_t)
   Vector(T (&values)[MAX_SIZE],
   ^~~~~~
[File-Path]\Vector-1.2.1\src/Vector.h:24:3: note:   template argument deduction/substitution failed:
[File-Path]\sketch_jul20b\sketch_jul20b.ino:44:28: note:   mismatched types 'double [MAX_SIZE]' and 'size_t {aka unsigned int}'
           Colours(ballCount)
                            ^
In file included from [File-Path]\Vector-1.2.1\src/Vector.h:95:0,
                 from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:
[File-Path]\Vector-1.2.1\src/Vector/VectorDefinitions.h:16:1: note: candidate: Vector<T>::Vector() [with T = double]
 Vector<T>::Vector()
 ^~~~~~~~~
[File-Path]\Vector-1.2.1\src/Vector/VectorDefinitions.h:16:1: note:   candidate expects 0 arguments, 1 provided
In file included from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:0:
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note: candidate: constexpr Vector<double>::Vector(const Vector<double>&)
 class Vector
       ^~~~~~
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note:   no known conversion for argument 1 from 'size_t {aka unsigned int}' to 'const Vector<double>&'
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note: candidate: constexpr Vector<double>::Vector(Vector<double>&&)
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note:   no known conversion for argument 1 from 'size_t {aka unsigned int}' to 'Vector<double>&&'
sketch_jul20b:44:28: error: no matching function for call to 'Vector<double>::Vector(size_t&)'
           Colours(ballCount)
                            ^
In file included from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:0:
[File-Path]\Vector-1.2.1\src/Vector.h:24:3: note: candidate: template<unsigned int MAX_SIZE> Vector<T>::Vector(T (&)[MAX_SIZE], size_t)
   Vector(T (&values)[MAX_SIZE],
   ^~~~~~
[File-Path]\Vector-1.2.1\src/Vector.h:24:3: note:   template argument deduction/substitution failed:
[File-Path]\sketch_jul20b\sketch_jul20b.ino:44:28: note:   mismatched types 'double [MAX_SIZE]' and 'size_t {aka unsigned int}'
           Colours(ballCount)
                            ^
In file included from [File-Path]\Vector-1.2.1\src/Vector.h:95:0,
                 from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:
[File-Path]\Vector-1.2.1\src/Vector/VectorDefinitions.h:16:1: note: candidate: Vector<T>::Vector() [with T = double]
 Vector<T>::Vector()
 ^~~~~~~~~
[File-Path]\Vector-1.2.1\src/Vector/VectorDefinitions.h:16:1: note:   candidate expects 0 arguments, 1 provided
In file included from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:0:
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note: candidate: constexpr Vector<double>::Vector(const Vector<double>&)
 class Vector
       ^~~~~~
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note:   no known conversion for argument 1 from 'size_t {aka unsigned int}' to 'const Vector<double>&'
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note: candidate: constexpr Vector<double>::Vector(Vector<double>&&)
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note:   no known conversion for argument 1 from 'size_t {aka unsigned int}' to 'Vector<double>&&'
sketch_jul20b:44:28: error: no matching function for call to 'Vector<CRGB>::Vector(size_t&)'
           Colours(ballCount)
                            ^
In file included from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:0:
[File-Path]\Vector-1.2.1\src/Vector.h:24:3: note: candidate: template<unsigned int MAX_SIZE> Vector<T>::Vector(T (&)[MAX_SIZE], size_t)
   Vector(T (&values)[MAX_SIZE],
   ^~~~~~
[File-Path]\Vector-1.2.1\src/Vector.h:24:3: note:   template argument deduction/substitution failed:
[File-Path]\sketch_jul20b\sketch_jul20b.ino:44:28: note:   mismatched types 'CRGB [MAX_SIZE]' and 'size_t {aka unsigned int}'
           Colours(ballCount)
                            ^
In file included from [File-Path]\Vector-1.2.1\src/Vector.h:95:0,
                 from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:
[File-Path]\Vector-1.2.1\src/Vector/VectorDefinitions.h:16:1: note: candidate: Vector<T>::Vector() [with T = CRGB]
 Vector<T>::Vector()
 ^~~~~~~~~
[File-Path]\Vector-1.2.1\src/Vector/VectorDefinitions.h:16:1: note:   candidate expects 0 arguments, 1 provided
In file included from [File-Path]\sketch_jul20b\sketch_jul20b.ino:6:0:
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note: candidate: constexpr Vector<CRGB>::Vector(const Vector<CRGB>&)
 class Vector
       ^~~~~~
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note:   no known conversion for argument 1 from 'size_t {aka unsigned int}' to 'const Vector<CRGB>&'
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note: candidate: constexpr Vector<CRGB>::Vector(Vector<CRGB>&&)
[File-Path]\Vector-1.2.1\src/Vector.h:19:7: note:   no known conversion for argument 1 from 'size_t {aka unsigned int}' to 'Vector<CRGB>&&'

我的代码在这里:

#include <Arduino.h>
#define FASTLED_INTERNAL
#include <FastLED.h>

using namespace std;
#include <Vector.h>

static const CRGB ballColours [] = {
    CRGB::Green,
    CRGB::Red,
    CRGB::Blue,
    CRGB::Orange,
    CRGB::Indigo
};

class BouncingBallEffect {
  private:

    double InitialBallSpeed(double height) const {
        return sqrt(-2 * Gravity * height);         // Because MATH!
    }

    size_t  _cLength;           
    size_t  _cBalls;
    byte    _fadeRate;
    bool    _bMirrored;

    const double Gravity = -9.81;                   // Because PHYSICS!
    const double StartHeight = 1;                   // Drop balls from max height initially

    Vector<double> ClockTimeAtLastBounce, Height, BallSpeed, Dampening;
    Vector<CRGB>   Colours;

  public:

    // BouncingBallEffect
    //
    // Caller specs strip length, number of balls, persistence level (255 is least), and whether the
    // balls should be drawn mirrored from each side.

    BouncingBallEffect(size_t cLength, size_t ballCount = 3, byte fade = 0, bool bMirrored = false)
        : _cLength(cLength - 1),
          _cBalls(ballCount),
          _fadeRate(fade),
          _bMirrored(bMirrored),
          ClockTimeAtLastBounce(ballCount),
          Height(ballCount),
          BallSpeed(ballCount),
          Dampening(ballCount),
          Colours(ballCount)
    {
        for (size_t i = 0; i < ballCount; i++) {
            Height[i]                = StartHeight;                 // Current Ball Height
            Dampening[i]             = 0.90 - i / pow(_cBalls, 2);  // Bounciness of this ball
            BallSpeed[i]             = InitialBallSpeed(Height[i]); // Don't dampen initial launch
        }
    }
};

任何人都可以提供任何帮助将不胜感激,谢谢。

4

1 回答 1

0

Arduino 的Vector库(https://github.com/janelia-arduino/Vector)在构造时接受静态分配的数组,与std::vector您可以传入的不同size_t

template <typename T>
class Vector
{
public:
  Vector();
  template <size_t MAX_SIZE>
  Vector(T (&values)[MAX_SIZE],
    size_t size=0);
  // ...
};

您将需要传递一个数组而不是 a size_t,或在这些成员初始化程序期间使用不同的数据结构:

      // ...
      ClockTimeAtLastBounce(ballCount),
      Height(ballCount),
      BallSpeed(ballCount),
      Dampening(ballCount),
      Colours(ballCount)
于 2021-07-20T19:00:44.057 回答