I need to have my sketch fill in some data into an array and have access to that array from both the sketch and the .cpp code.
I assume I could have the array in the sketch and pass an address of it some way. But that's not the best way to arrange it. It is far better (since this is reusable library code) to have the array in the library and have the sketch put data into it.
How can this be done?
This is what I need to have defined, preferably in the .cpp code:
byte CharBuf[300]; // Characters put here will be converted into bit maps later.
byte ShiftBuf[9][8]; // Holds the shifted bitmaps to put into the display by rows.
This code is currently in the sketch but needs to be globally accessed both from sketch and .cpp library code.
I tried dropping it into the public section in the .h file but it was not found by the sketch. Yes, I exited and reloaded the sketch to pick up changes.
public:
byte CharBuf[300]; // Characters put here will be converted into bit maps later.
byte ShiftBuf[9][8]; // Holds the shifted bitmaps to put into the display by rows.
That seems to have done it. I put it in three places before I got it right. Could not find a complete example. It finally worked by putting it in before the "class" statement in the .h file.
in the .h:
extern byte CharBuf[300]; // Characters put here will be converted into bit maps later.
extern byte ShiftBuf[9][8]; // Holds the shifted bitmaps to put into the display by rows.
class LedControl {
private :
// The array for shifting the data to the devices
byte spidata[16];
and putting these two in the .ino exactly the same but without the "extern" in front. I am able to print the same data from both the .ino and the .cpp file.
Thanks for the quick help. It lead me to making working code.
Philosophy vs. Reality of coding (this worked until I tried to really use it)
I was told in one thread that it is good to define (only) in .h files and populate in .cpp files. So I moved my big array from .h to .cpp. That works pretty well... until...
I needed to access the array from the .ino sketch.
Array definition (now in .cpp): PROGMEM const byte ABitmap8[136][8] = { a bunch of data };
Tried putting (in .ino): extern const byte ABitmap8[136][8];
Compile fails with these messages:
[path]\Time_on_LED_Matrix_8x8x8_Smooth_Scrolling.cpp.o: In function loop': [path]\Time_on_LED_Matrix_8x8x8_Smooth_Scrolling.ino:152: undefined reference to ABitmap8'
[path]/Time_on_LED_Matrix_8x8x8_Smooth_Scrolling.ino:152: undefined reference to `ABitmap8'
collect2.exe: error: ld returned 1 exit status
Error compiling.
on this statement: ShiftBuf*[j] = pgm_read_byte(&ABitmap8[CharOffset][j]);* When it is in the .h file (frowned upon, I hear) all works just fine and I don't need the extern. What to do... what to do... How can I do it "right" and have it work? Mike