我对以下代码有疑问(这些只是片段,不是完整文件):
菜单.h
#ifndef MENU_H
#define MENU_H
#include <Adafruit_SSD1306.h>
#include <stdint.h>
#include <Config.h>
class MenuHeader; // forward declaration of class MenuHeader
class MenuPage; // forward declaration of class MenuPage
class Menu {
MenuHeader* m_header{nullptr};
MenuPage* m_pages[16]{}; // support up to 16 pages
uint8_t m_pagesCount{0};
uint8_t m_currentPage{0};
public:
void setHeader(MenuHeader* header);
void addPage(MenuPage* page);
void goToPage(const char* pageName);
void next();
void prev();
void click();
void draw(Adafruit_SSD1306* display);
};
#endif
菜单.cpp
#include <Menu.h>
#include <MenuHeader.h>
#include <MenuPage.h>
/* Some other definitions */
void Menu::draw(Adafruit_SSD1306* display) {
if(m_header != nullptr) {
display->setCursor(0, HEADER_HEIGHT - 1 + SCREEN_Y_OFFSET);
m_pages[m_currentPage]->draw(display);
m_header->draw(display);
} else {
display->setCursor(0, SCREEN_Y_OFFSET);
m_pages[m_currentPage]->draw(display);
}
}
配置文件
#ifndef CONFIG_H
#define CONFIG_H
#include <stdint.h>
constexpr uint8_t ENCODER_PIN_A = 14; // pin A of rotary encoder
constexpr uint8_t ENCODER_PIN_B = 12; // pin B of rotary encoder
constexpr uint8_t BUTTON_PIN = 13; // pin to which button is connected
constexpr uint8_t SCREEN_WIDTH = 128; // width of screen in pixels
constexpr uint8_t SCREEN_HEIGHT = 64; // height of screen in pixels
constexpr uint8_t CHARS_PER_LINE = 18; // how many characters fit in one line
constexpr uint8_t CHAR_WIDTH = SCREEN_WIDTH/CHARS_PER_LINE; // width of single character
constexpr uint8_t CHAR_HEIGHT = 10; // height of single char
constexpr uint8_t SCREEN_Y_OFFSET = CHAR_HEIGHT; // screen offset, if not using custom font set to 0
constexpr uint8_t HEADER_HEIGHT = 14; // menu header height in pixels
constexpr int8_t TIMEZONE = -1; // timezone
/* Some other not needed stuff */
#endif
所有标题(Menu.h、MenuHeader.h、MenuPage.h)都包含 Config.h。
好吧,编译器似乎不喜欢它。它抛出:
'HEADER_HEIGHT' was not declared in this scope
identifier "HEADER_HEIGHT" is undefined
'SCREEN_Y_OFFSET' was not declared in this scope
identifier "SCREEN_Y_OFFSET" is undefined
所有关于 Menu.cpp 文件。我认为如果我将 Config.h 文件包含在我的 Main.cpp 中的一个标头中,它应该可以工作。即使我直接在 Main.cpp 中包含配置 - 也会发生相同的错误。我能做些什么呢?
编辑:嗯,奇怪的事情正在发生。如果我#include <Config.h>
在 Menu.h 中,则配置仅适用于该文件。如果我将其更改为#include "../Config/Config.h"
,它可以在 Menu.h 和 Menu.cpp 中使用。这是怎么回事?我的文件夹结构
Using<Config.h>
是 platformio 的功能。它会自动找到所有库并编译它们。