Hello!
A bit of history for you to help understand my part of the issue here:
I had created a Sudoku Grid Application with Bloodshed Software's "Dev-Cpp" many years ago for Windows XP, i believe. I was using and extending the classic built-in "Windows GUI example project" and doing some Mouse/Keyboard event handling by GetMessage() and other Windows API stuff.
But that is about all i have in terms of C/C++ experience.
I have no distinction which features are C and which are C++, that's why i'm throwing them in a bucket together.
Ahead of time, sorry if i'm missing a possibly obvious thing here, but i'm as new to C/C++ as i am to Arduino programming.
Anyways. On to my question:
I have a PCB (from the company that smiles the whole alphabet) with 4 daisy-chained MAX7219 8x8 LED modules
(That is 32x8 LED "pixels). They're interfaced using a Serial-to-Parallel Shift Register approach.
Wanting to interact with that, i have created a MAX7219 state machine/control class.
I'm using Visual Studio Code by the way, with the PlatformIO IDE installed.
I am trying to create multiple instances of that class (let's call the class "max7219").
Its Constructor takes the index (position) of the current 8x8 block inside the daisy-chain, and a parameter that tells the class (and the MAX7219 chip) to operate in dot matrix mode and not use seven-segment digit decoding.
This is part of my max7219.cpp:
#define OP_MODE_MATRIX 1
#define OP_MODE_SEVENS 2
[...]
max7219::max7219(unsigned byte nIndex, byte nOpMode) {
[...]
}
What i would have done in C(++) is:
main.cpp:
[...]
#define MAX_MODULES 4
max7219 * matrix[MAX_MODULES];
void setup() {
for(unsigned byte nModule=0;nModule<=MAX_MODULES;nModule++) {
matrix[nModule]=new max7219(nModule, OP_MODE_MATRIX)
}
Okay, that didn't work. PlatformIO mocks the line
matrix[nModule]new max7219(nModule, OP_MODE_MATRIX)
with
"Can not access ""max7219::max7219(nModule, OP_MODE_MATRIX)", declared in line 56 of "(path here)"". [b]C/C++(330)[/b]
Does "new" not exist in the Arduino universe, or does it work differently?
If so, how would you tackle multi-instantiation of a class and storing the object references in an array?
I know with the classes for e.g. a 2-line, 20-character LCD screen, i would use:
LiquidCrystal_I2C lcd(0x27,20,4);
(from the LiquidCrystal_I2C example Sketch "HelloWorld")
But that creates just one instance...
What if i want to use multiple instances? Do i really have to do the
max7219 Module0(0,OPMODE_MATRIX);
max7219 Module1(1,OPMODE_MATRIX);
max7219 Module2(2,OPMODE_MATRIX);
max7219 Module3(3,OPMODE_MATRIX);
max7219 Module4(4,OPMODE_MATRIX);
max7219 Module5(5,OPMODE_MATRIX);
if i want 6 instances?
What about
for(unsigned byte nModule=0;nModule<=3;nModule++) {
matrix[nModule]=malloc(sizeof(max7219));
}
?
But i can't call the max7219::max7219() constructor on these memory areas, can I?
Because these are just empty memory, no class or instance info yet in them, right?
So, how would i create an arbitrary count of new instances and make the runtime store them to the malloc'ed areas?
Would I have to do that before setup(), like with the LiquidCrystal_I2C example)?
Please help me understand how classes and instances work on Arduino C/C++.
I'm lost.