help with template function and array 'type'

having a bit of trouble figuring out what exactly the issue is here. I am using a template function to pass the information on array(s) passed to my display function. Simply, I have an array that contains the intensities of pixels (0-255) and I am passing the array size (2-d array) of rows and columns.

Here is the code:

[code]
// neopixel strip larson scanner
#include <Adafruit_NeoPixel.h>
#define PIN A3
Adafruit_NeoPixel strip = Adafruit_NeoPixel(8, PIN, NEO_GRB + NEO_KHZ800);
#define DEBUG_UART 0

uint32_t c = 0;
int wait;
int interval = 150;
void colorwave(uint8_t *pattern, size_t rows, size_t cols) {
  static uint32_t previousMillis = 0;
  static uint8_t indexI = 0;
  static uint8_t indexJ = 0;
  uint8_t g = 0;
  uint8_t r = 120;
  uint8_t b = 0;

  for (uint16_t indexI = 0; indexI < strip.numPixels(); indexI++)
  {
    uint32_t c = strip.Color(pattern[indexJ][indexI], g, b);
    strip.setPixelColor(indexI, c);
    strip.show();
  }

  if (millis() - previousMillis >= interval)
  {
    if (indexJ <= 5)
    {
      indexJ++;
    }
    else
    {
      indexJ = 0;
    }
    previousMillis = millis();
  }
}

uint8_t pattern1[6][8] = {
    {0, 23, 105, 255, 105, 23, 0, 23},
    {23, 0, 23, 105, 255, 105, 23, 0},
    {105, 23, 0, 23, 105, 255, 105, 23},
    {255, 105, 23, 0, 23, 105, 255, 105},
    {105, 255, 105, 23, 0, 23, 105, 255},
    {23, 105, 255, 105, 23, 0, 23, 105},
  };

void setup()
{
  strip.begin();
  strip.setBrightness(32);
  strip.show(); // Initialize all pixels to 'off'
}

template <size_t rows, size_t cols> void display_pattern(const int (&pattern)[rows][cols])
{
  colorwave(&pattern[0][0], rows, cols);
}

void loop()
{
  colorwave(pattern1);
}

[/code]

and this is the errors:

/home/pi/Arduino/code/wave_neo_template/wave_neo_template.ino: In function ‘void colorwave(uint8_t*, size_t, size_t)’:
wave_neo_template:20:52: error: invalid types ‘uint8_t {aka unsigned char}[uint16_t {aka short unsigned int}]’ for array subscript
uint32_t c = strip.Color(pattern[indexJ][indexI], g, b);
^
/home/pi/Arduino/code/wave_neo_template/wave_neo_template.ino: In function ‘void loop()’:
wave_neo_template:62:21: error: cannot convert ‘uint8_t ()[8] {aka unsigned char ()[8]}’ to ‘uint8_t* {aka unsigned char*}’ for argument ‘1’ to ‘void colorwave(uint8_t*, size_t, size_t)’
colorwave(pattern1);
^
Using library Adafruit_NeoPixel at version 1.6.0 in folder: /home/pi/Downloads/arduino-1.8.12/examples/libraries/Adafruit_NeoPixel
exit status 1
invalid types ‘uint8_t {aka unsigned char}[uint16_t {aka short unsigned int}]’ for array subscript

So, apparently the array subscript does not like the uint8_t type, it also does not like the uint32_t, or uint16_t, or int, or byte!

The function strip.Color(r, g, b) wants unint8_t according to teh Neopixel library documentation.

I honestly do not know what exactly the problem is.

Any help would be appreciated.

Roger

I don’t know enough about templates to know how to do what you wanted (if it is even possible). You never called the template function anyway. Here is how I would do what you seem to want. It compiles without error or warning but I have not tried it on your hardware.

// neopixel strip larson scanner
#include <Adafruit_NeoPixel.h>
Adafruit_NeoPixel strip = Adafruit_NeoPixel(8, A3, NEO_GRB + NEO_KHZ800);


#define DEBUG_UART 0


unsigned long interval = 150;


void colorwave(uint8_t *pattern, size_t rows, size_t cols)
{
  static unsigned long previousMillis = 0;
  uint8_t indexI = 0;
  static uint8_t indexJ = 0;


  for (indexI = 0; indexI < cols; indexI++)
  {
    uint32_t c = strip.Color(pattern[indexJ * cols + indexI], 0, 0); // Red, Green, Blue
    strip.setPixelColor(indexI, c);
  }
  strip.show();


  if (millis() - previousMillis >= interval)
  {
    indexJ = (indexJ + 1) % rows;  // next row
    previousMillis = millis();
  }
}


uint8_t pattern1[6][8] =
{
  {0, 23, 105, 255, 105, 23, 0, 23},
  {23, 0, 23, 105, 255, 105, 23, 0},
  {105, 23, 0, 23, 105, 255, 105, 23},
  {255, 105, 23, 0, 23, 105, 255, 105},
  {105, 255, 105, 23, 0, 23, 105, 255},
  {23, 105, 255, 105, 23, 0, 23, 105},
};


void setup()
{
  strip.begin();
  strip.setBrightness(32);
  strip.show(); // Initialize all pixels to 'off'
}


void loop()
{
  colorwave(&pattern1[0][0], 6, 8);
}

I have a program using the same template syntax that works. The reason I do not include the size of teh array in the calling, is that I want to be able to call different patterns which have different sizes.

The other program uses this call:

void display_pattern(const int *pattern, size_t rows, size_t cols)
{

this program uses this call:

void colorwave(uint8_t *pattern, size_t rows, size_t cols) {

they both set up the template like thus:

template <size_t rows, size_t cols> void display_pattern(const uint8_t (&pattern)[rows][cols])
{
  colorwave(&pattern[0][0], rows, cols);
}

and:

template <size_t rows, size_t cols> void display_pattern(const int (&pattern)[rows][cols])
{
  display_pattern(&pattern[0][0], rows, cols);
}

and the array is passed like this:

display_pattern(pattern3);

and this:

colorwave(pattern1);

so the syntax appears correct.

I changed the reference to your format:

uint32_t c = strip.Color(pattern[(indexJ * cols + indexI)], g, b);

and I get a similar, but slightly different error:

/home/pi/Arduino/code/wave_neo_template/wave_neo_template.ino: In function ‘void loop()’:
wave_neo_template:63:21: error: cannot convert ‘uint8_t ()[8] {aka unsigned char ()[8]}’ to ‘uint8_t* {aka unsigned char*}’ for argument ‘1’ to ‘void colorwave(uint8_t*, size_t, size_t)’
colorwave(pattern1);
^
Using library Adafruit_NeoPixel at version 1.6.0 in folder: /home/pi/Downloads/arduino-1.8.12/examples/libraries/Adafruit_NeoPixel
exit status 1
cannot convert ‘uint8_t ()[8] {aka unsigned char ()[8]}’ to ‘uint8_t* {aka unsigned char*}’ for argument ‘1’ to ‘void colorwave(uint8_t*, size_t, size_t)’

Any suggestions ould help.

Roger

boy,

A few corrections:

changed line 58 to actually make the call:

colorwave(&pattern[0][0], rows, cols);

And the varable type in the template to uint8_t

template <size_t rows, size_t cols> void colorwave(uint8_t (&pattern)[rows][cols])

This works.

Thanks,

Roger