c++stdvectorarduino-unofastled

C++ Arduino Vector issues with mismatched types?


I've been trying to write some effects for a strip of RGB LEDs to be controlled via an Arduino Uno R3. For context, I'm working on the animation of a ball (a set of 5 LEDs) moving backwards and forward along the strip, bouncing from time to time.

The vector causing issue is the Colour object, which I want to be a vector of CRGB colours which, when fewer than the number of colours available are passed to the function, it simply uses the first x amount but if there are more balls than there are colours then it should simply re-use them.

Apologies if there are any errors in my formatting and such.

The issue is when I try to pass the vectors as constructors to BouncingBallEffect it gives the following error:

[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>&&'

My code is here:

#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
        }
    }
};

Any help anyone can offer would be much appreciated, thank you.


Solution

  • Arduino's Vector library (https://github.com/janelia-arduino/Vector) accepts a statically allocated array at construction time, unlike std::vector which you can pass a size_t into.

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

    You will need to pass an array instead of a size_t, or use a different data structure during these member initializers:

          // ...
          ClockTimeAtLastBounce(ballCount),
          Height(ballCount),
          BallSpeed(ballCount),
          Dampening(ballCount),
          Colours(ballCount)