避免包含 main.h

问题描述 投票:0回答:1

我有一个模块需要这些代码部分:

这是我的 led.c 模块的一部分:

/****
 *  Array for GPIO Ports of LED 1-8
 ****/
GPIO_TypeDef *gpio_ports[8] = {RED_0_GPIO_Port, YELLOW_0_GPIO_Port, GREEN_0_GPIO_Port,
        BLUE_0_GPIO_Port, RED_1_GPIO_Port, YELLOW_1_GPIO_Port, GREEN_1_GPIO_Port,
        BLUE_1_GPIO_Port};

/****
 *  Array for GPIO Pins of LED 1-8
 ****/
uint16_t gpio_pins[8] = {RED_0_Pin, YELLOW_0_Pin, GREEN_0_Pin, BLUE_0_Pin,
        RED_1_Pin, YELLOW_1_Pin, GREEN_1_Pin, BLUE_1_Pin};

端口和引脚数组在 main.h 中定义:

extern uint16_t gpio_pins[8];
extern GPIO_TypeDef *gpio_ports[8];

LED 特定的端口和引脚是从 main.h 中的cubeMX 自动生成的: /* 只是定义的一个例子 */

#define BLUE_0_Pin GPIO_PIN_5
#define BLUE_0_GPIO_Port GPIOB
#define GREEN_1_Pin GPIO_PIN_8
#define GREEN_1_GPIO_Port GPIOB
#define BLUE_1_Pin GPIO_PIN_9
#define BLUE_1_GPIO_Port GPIOB
...

现在我需要在 led.h 模块中包含 main.h 才能访问数组中的 RED_0_GPIO_PORT 等。但包括 main.h 是(据我所知)不好的风格。所以我正在寻找另一种可能性来做到这一点。

arrays c port
1个回答
0
投票

但包括 main.h 是(据我所知)不好的风格。

头文件(`.h')在需要时包含它们。所以这是 100% 正确的使用方法。告诉你这个的人错了。

© www.soinside.com 2019 - 2024. All rights reserved.