gpt4 book ai didi

c++ - Arduino IDE 在单独的文件中初始化 char[]

转载 作者:太空宇宙 更新时间:2023-11-04 13:23:41 25 4
gpt4 key购买 nike

我有一个大的 char[],其中包含我用来在 OLED 显示器上显示数字的位图字体。数组的初始化位于我的 cpp 文件的开头,这只会使文件更难阅读。我不得不相信有一种方法可以在单独的文件中定义这个数组,但我没有想法。

我有一个 Arduino ino 文件,我将 ino 文件大部分留空,这样 arduino 软件被混淆的可能性就较小:

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

然后我有一个包含字体定义数组的新选项卡 font.cpp:

#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
};

然后我有另一个选项卡 oled.cpp,其中包含用于在显示屏上显示数字的代码:

#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 {
}

编译器的输出如下所示:

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.

当代码全部在 ino 文件中时,它工作正常,但出于可读性原因,我不希望将巨大的字体位图 char[] 与代码放在同一文件中。

我相信我可以用字体创建一个库,但这是解决这个问题的最佳方法吗?

最佳答案

尝试在

之前添加 extern
const unsigned char font[];

oled.cpp

如果没有 extern,编译器会尝试为变量定义和分配缓冲区。然后,不知道应该为不完整类型分配多少内存,它失败了。

关于c++ - Arduino IDE 在单独的文件中初始化 char[],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34084870/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com