如何借助 C++ 中的预处理器指令在行间插入语句?
How to insert a statement inbetween lines with the help of preprocessor directives in C++?
我正在尝试通过快速打开和关闭 LED 来对 LED 矩阵进行 PWM 编程。为了控制亮度,我想在 LED 的开和关状态之间添加额外的指令。亮度应由预处理器变量设置。 Arduino 有一个 84MHz Cortex-M3 ARM 处理器。
这是我的代码当前如何增加亮度的简化:
uint8_t volatile dummy = 0;
uint8_t i;
for (i=0;i<1<<current_layer;i++) { // every next layer has double the amount of pulses
LED_STATE = ON; // several pulses in one go speeds up the for loop
dummy = 0; // increases ON-time
LED_STATE = OFF;
LED_STATE = ON;
dummy = 0;
LED_STATE = OFF;
LED_STATE = ON;
dummy = 0;
LED_STATE = OFF;
LED_STATE = ON;
dummy = 0;
LED_STATE = OFF;
}
现在我希望能够像这样基于预处理器变量增加亮度:
#define BRIGHTNESS 2
// inside loop
LED_STATE = ON; // several pulses in one go speeds up the for loop
#if BRIGHTNESS > 0
dummy = 0; // increases ON-time
#if BRIGHTNESS == 2
dummy = 0; // increases ON-time even more
#endif
#endif
LED_STATE = OFF;
LED_STATE = ON;
#if BRIGHTNESS > 0
dummy = 0;
#if BRIGHTNESS == 2
dummy = 0;
#endif
#endif
LED_STATE = OFF;
LED_STATE = ON;
#if BRIGHTNESS > 0
dummy = 0;
#if BRIGHTNESS == 2
dummy = 0;
#endif
#endif
LED_STATE = OFF;
LED_STATE = ON;
#if BRIGHTNESS > 0
dummy = 0;
#if BRIGHTNESS == 2
dummy = 0;
#endif
#endif
LED_STATE = OFF;
如您所见,这使得代码非常难读。我想定义一个预处理器宏,根据 BRIGHTNESS
.
的值添加这些虚拟语句
代码应该是这样的:
#define BRIGHTNESS 2
#define B1 ... // adds dummy=0; when BRIGHTNESS > 0 or else nothing at all
#define B2 ...
#define B3 ...
#define B4 ...
// inside loop
LED_STATE = ON;
B1 B2 B3 B4 // adds a number of dummy statements based on the value of BRIGHTNESS
LED_STATE = OFF;
LED_STATE = ON;
B1 B2 B3 B4
LED_STATE = OFF;
LED_STATE = ON;
B1 B2 B3 B4
LED_STATE = OFF;
LED_STATE = ON;
B1 B2 B3 B4
LED_STATE = OFF;
更好的是基于变量插入这些语句的函数:
#define ADD_DUMMIES ...
// inside loop
LED_STATE = ON;
ADD_DUMMIES(BRIGHNESS)
LED_STATE = OFF;
我将如何定义预处理器宏,以便它们根据 BRIGHTNESS
的值插入虚拟语句?
这是如何在预处理器的帮助下添加 dummy = 0;
语句:
#define BRIGHTNESS 2
#define _GETFOO(n) _FOO ## n
#define ADD_DUMMIES(n) _GETFOO(n) ()
#define _FOO0()
#define _FOO1() dummy = 0;
#define _FOO2() dummy = 0; dummy = 0;
#define _FOO3() dummy = 0; dummy = 0; dummy = 0;
#define _FOO4() dummy = 0; dummy = 0; dummy = 0; dummy = 0;
uint8_t volatile dummy = 0;
// inside loop
LED_STATE = ON;
ADD_DUMMIES(BRIGHTNESS)
LED_STATE = OFF;
LED_STATE = ON;
ADD_DUMMIES(BRIGHTNESS)
LED_STATE = OFF;
LED_STATE = ON;
ADD_DUMMIES(BRIGHTNESS)
LED_STATE = OFF;
LED_STATE = ON;
ADD_DUMMIES(BRIGHTNESS)
LED_STATE = OFF;
只要在边界内,编译器就会根据 BRIGHTNESS
的值在行之间插入相同的存储指令。
我正在尝试通过快速打开和关闭 LED 来对 LED 矩阵进行 PWM 编程。为了控制亮度,我想在 LED 的开和关状态之间添加额外的指令。亮度应由预处理器变量设置。 Arduino 有一个 84MHz Cortex-M3 ARM 处理器。
这是我的代码当前如何增加亮度的简化:
uint8_t volatile dummy = 0;
uint8_t i;
for (i=0;i<1<<current_layer;i++) { // every next layer has double the amount of pulses
LED_STATE = ON; // several pulses in one go speeds up the for loop
dummy = 0; // increases ON-time
LED_STATE = OFF;
LED_STATE = ON;
dummy = 0;
LED_STATE = OFF;
LED_STATE = ON;
dummy = 0;
LED_STATE = OFF;
LED_STATE = ON;
dummy = 0;
LED_STATE = OFF;
}
现在我希望能够像这样基于预处理器变量增加亮度:
#define BRIGHTNESS 2
// inside loop
LED_STATE = ON; // several pulses in one go speeds up the for loop
#if BRIGHTNESS > 0
dummy = 0; // increases ON-time
#if BRIGHTNESS == 2
dummy = 0; // increases ON-time even more
#endif
#endif
LED_STATE = OFF;
LED_STATE = ON;
#if BRIGHTNESS > 0
dummy = 0;
#if BRIGHTNESS == 2
dummy = 0;
#endif
#endif
LED_STATE = OFF;
LED_STATE = ON;
#if BRIGHTNESS > 0
dummy = 0;
#if BRIGHTNESS == 2
dummy = 0;
#endif
#endif
LED_STATE = OFF;
LED_STATE = ON;
#if BRIGHTNESS > 0
dummy = 0;
#if BRIGHTNESS == 2
dummy = 0;
#endif
#endif
LED_STATE = OFF;
如您所见,这使得代码非常难读。我想定义一个预处理器宏,根据 BRIGHTNESS
.
代码应该是这样的:
#define BRIGHTNESS 2
#define B1 ... // adds dummy=0; when BRIGHTNESS > 0 or else nothing at all
#define B2 ...
#define B3 ...
#define B4 ...
// inside loop
LED_STATE = ON;
B1 B2 B3 B4 // adds a number of dummy statements based on the value of BRIGHTNESS
LED_STATE = OFF;
LED_STATE = ON;
B1 B2 B3 B4
LED_STATE = OFF;
LED_STATE = ON;
B1 B2 B3 B4
LED_STATE = OFF;
LED_STATE = ON;
B1 B2 B3 B4
LED_STATE = OFF;
更好的是基于变量插入这些语句的函数:
#define ADD_DUMMIES ...
// inside loop
LED_STATE = ON;
ADD_DUMMIES(BRIGHNESS)
LED_STATE = OFF;
我将如何定义预处理器宏,以便它们根据 BRIGHTNESS
的值插入虚拟语句?
这是如何在预处理器的帮助下添加 dummy = 0;
语句:
#define BRIGHTNESS 2
#define _GETFOO(n) _FOO ## n
#define ADD_DUMMIES(n) _GETFOO(n) ()
#define _FOO0()
#define _FOO1() dummy = 0;
#define _FOO2() dummy = 0; dummy = 0;
#define _FOO3() dummy = 0; dummy = 0; dummy = 0;
#define _FOO4() dummy = 0; dummy = 0; dummy = 0; dummy = 0;
uint8_t volatile dummy = 0;
// inside loop
LED_STATE = ON;
ADD_DUMMIES(BRIGHTNESS)
LED_STATE = OFF;
LED_STATE = ON;
ADD_DUMMIES(BRIGHTNESS)
LED_STATE = OFF;
LED_STATE = ON;
ADD_DUMMIES(BRIGHTNESS)
LED_STATE = OFF;
LED_STATE = ON;
ADD_DUMMIES(BRIGHTNESS)
LED_STATE = OFF;
只要在边界内,编译器就会根据 BRIGHTNESS
的值在行之间插入相同的存储指令。