Hi everyone. The title pretty much describes it. I cannot figure out for the life of me, how to pass a 2D array as a parameter to a function inside a library. If I pass it with no library, as a normal self contained program, I can do it easily. For example:
#include <Arduino.h>
const int rows = 16;
const int cols = 8;
const int twodmatrix[rows][cols] = {{1,0,0,0,0,0,0,0},
{0,1,0,0,0,0,0,0},
{1,1,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0},
{1,0,1,0,0,0,0,0},
{0,1,1,0,0,0,0,0},
{1,1,1,0,0,0,0,0},
{0,0,0,1,0,0,0,0},
{1,0,0,1,0,0,0,0},
{0,1,0,1,0,0,0,0},
{0,1,1,1,0,0,0,0},
{0,0,1,1,0,0,0,0},
{1,0,1,1,0,0,0,0},
{0,1,1,1,0,0,0,0},
{1,1,1,1,0,0,0,0},
{1,1,1,1,1,1,1,1}};
void setup() {
Serial.begin(115200);
delay(3000);
Serial.println("The results are being displayed next: ");
BiMatrix();
}
void loop(){}
void BiMatrix(){
for(int i = 0; i < rows; i++) //x
{
for(int j = 0; j < cols; j++) //y
{
if((twodmatrix[i][j]) == true)
{
Serial.print("• on ");
}
else
{
Serial.print("• off "); //Alt 0149
}
}
Serial.println();
}
}
This example compiles perfectly:
The results are being displayed next:
• on • off • off • off • off • off • off • off
• off • on • off • off • off • off • off • off
• on • on • off • off • off • off • off • off
• off • off • on • off • off • off • off • off
• on • off • on • off • off • off • off • off
• off • on • on • off • off • off • off • off
• on • on • on • off • off • off • off • off
• off • off • off • on • off • off • off • off
• on • off • off • on • off • off • off • off
• off • on • off • on • off • off • off • off
• off • on • on • on • off • off • off • off
• off • off • on • on • off • off • off • off
• on • off • on • on • off • off • off • off
• off • on • on • on • off • off • off • off
• on • on • on • on • off • off • off • off
• on • on • on • on • on • on • on • on
I understand that the array will decay as a pointer when passed to a function, and that I really don't even need to pass it as a parameter, asyou can see in the previous example, since it will directly modifiy the array as it is declared globally.
The problem starts if I want the 2D array matrix to be solved inside a function inside a library. I need to pass it as a parameter (along a bunch of other parameters, but I am interested in this one particularly). I have tried several different ways but I keep seeing errors. I am trying here the most straigthforward way (at least for me), but it seems that something is off with the syntax, based on the myriad of errors that keep popping up:
.h
#ifndef BDMX_TST_H
#define BDMX_TST_H
#endif
#include <Arduino.h>
class Matrix{
private:
public: void BiMatrix(const int twodmatrix[const int rows_x][const int cols_y]);
};
.cpp
#include <bdmx_tst.h>
void BiMatrix(const int twodmatrix[const int rows][const int cols]){
for(int i = 0; i < rows; i++) //x
{
for(int j = 0; j < cols; j++) //y
{
if((twodmatrix[i][j]) == true)
{
Serial.print("• on");
}
else
{
Serial.print("• off");
}
}
Serial.println();
}
}
.cpp (user .ino or main)
#include <bdmx_tst.h>
const int rows_x = 16;
const int cols_y = 8;
const int twodmatrix[rows_x][cols_y] = {{1,0,0,0,0,0,0,0},
{0,1,0,0,0,0,0,0},
{1,1,0,0,0,0,0,0},
{0,0,1,0,0,0,0,0},
{1,0,1,0,0,0,0,0},
{0,1,1,0,0,0,0,0},
{1,1,1,0,0,0,0,0},
{0,0,0,1,0,0,0,0},
{1,0,0,1,0,0,0,0},
{0,1,0,1,0,0,0,0},
{0,1,1,1,0,0,0,0},
{0,0,1,1,0,0,0,0},
{1,0,1,1,0,0,0,0},
{0,1,1,1,0,0,0,0},
{1,1,1,1,0,0,0,0},
{1,1,1,1,1,1,1,1}};
Matrix matrix;
void setup() {
Serial.begin(115200);
Serial.print("The results are being displayed next: ");
matrix.BiMatrix(twodmatrix);
}
void loop(){}
I have seen various forums, outlining that there are basically 4 different ways, most of them involving pointers, but all of the examples fill the matrix data manualy with an int number (Matrix[] [10] for example) but I want to pass it with const values defined (or is that incorrect?). These are the errors I keep seeing:
bdmx_tst.h:10:59: error: expected primary-expression before 'const'
public: void BiMatrix(const int twodmatrix[const int rows_x][const int cols_y]);
^~~~~
bdmx_tst.h:10:59: error: expected ']' before 'const'
public: void BiMatrix(const int twodmatrix[const int rows_x][const int cols_y]);
^~~~~
]
dmx_tst.h:10:59: error: expected ')' before 'const'
public: void BiMatrix(const int twodmatrix[const int rows_x][const int cols_y]);
~ ^~~~~
)
bdmx_tst.h:10:59: error: expected ';' at end of member declaration
public: void BiMatrix(const int twodmatrix[const int rows_x][const int cols_y]);
^~~~~
;
bdmx_tst.h:10:69: error: expected ';' at end of member declaration
public: void BiMatrix(const int twodmatrix[const int rows_x][const int cols_y]);
^~~~~~
;
bdmx_tst.h:10:75: error: expected unqualified-id before ']' token
public: void BiMatrix(const int twodmatrix[const int rows_x][const int cols_y]);
^
n function 'void setup()':
R003:27:3: error: 'BiMatrix' was not declared in this scope
BiMatrix(twodmatrix);
ino:27:3: note: suggested alternative: 'Matrix'
BiMatrix(twodmatrix);
^~~~~~~~
Matrix
I am probably doing a bunch of things wrong and maybe is something easy which I am overlooking. Any ideas how to achieve this?
Thanks in advance!