简体   繁体   中英

Arduino IDE Initialize char[] in a separate file

I have a large char[] that contains a bitmap font that I use to display numbers on an OLED display. The initialization of the array is at the beginning of my cpp file which just makes the file harder to read. I have to believe that there is a way to define this array in a separate file, but I am out of ideas.

I have an Arduino ino file and I have left the ino file mostly blank so that there is less chance of the arduino software being confused:

#include <SPI.h>
#include <Adafruit_SSD1306ms.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <RFduinoBLE.h>

I then have a new tab font.cpp that contains the font definition array:

#include <Arduino.h>

extern const unsigned char font[] PROGMEM = {
  B00000000, B00000000, B00000000, B00000000, B00000000,
  B00000000, B00000000, B00001111, B11110000, B00000000,
  B00000000, B00000000, B01111111, B11111110, B00000000,
  B00000000, B00000000, B11111111, B11111111, B00000000,
 ...
  B00000000, B00000000, B00000000, B00000000, B00000000
};

I then have another tab oled.cpp that contains the code for displaying the numbers on the display:

#include <SPI.h>
#include <Adafruit_SSD1306ms.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <RFduinoBLE.h>

const unsigned char font[];

void DrawNumber(char aValue[], uint size) {
 // draws the proper numbers on the display using the font
}

void setup() {
}
void loop {
}

The output from the compiler looks as follows:

Arduino: 1.6.6 (Windows 7), Board: "RFduino"

oled.cpp:7: error: uninitialized const 'font' [-fpermissive]
 const unsigned char font[];

                     ^
oled.cpp:7: error: storage size of 'font' isn't known
 const unsigned char font[];
                         ^
exit status 1
uninitialized const 'font' [-fpermissive]

  This report would have more information with
  "Show verbose output during compilation"
  enabled in File > Preferences.

When the code is all in the ino file, it works fine, but I prefer not to have the huge font bitmap char[] in the same file with the code for readability reasons.

I believe that I could create a library with the font, but is that the best way to solve this problem?

Try adding extern before

const unsigned char font[];

in oled.cpp

Without extern , the compiler try to define and allocate buffer for the variable. Then, not knowing how much memory should be allocated to the incomplete type, it fails.

put

extern const unsigned char font[];

in a header file and include it from both places instead of putting it everywhere you use it. This would prevent misalignment between declaration and definition.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM