如何基于循环迭代器选择函数
How to Choose Function Based on Loop Iterator
我在这里寻找解决方案的部分问题可能是我不知道我所问的正确术语。 为此,我提前请求原谅。
对于微控制器,我有一个希望同时启动的引脚列表。 每个实例都有自己的 ISR,并为每个实例调用类的相同成员,但使用引脚号作为参数。
我正在尝试将数组中的每个引脚连接到其相应的 ISR,但我想通过引脚的索引选择哪个 ISR。 这是邮件代码™,可能不会编译,但我相信这足以得到这个想法:
#define PIN1 4
#define PIN2 9
#define PIN3 10
#define PIN4 8
#define PIN5 12
PinAct *pPinact; // Pointer to Counter class
static ICACHE_RAM_ATTR void HandleInterruptsStatic1(void) {
pPinact->handleInterrupts(1);
}
static ICACHE_RAM_ATTR void HandleInterruptsStatic2(void) {
pPinact->handleInterrupts(2);
}
static ICACHE_RAM_ATTR void HandleInterruptsStatic3(void) {
pPinact->handleInterrupts(3);
}
static ICACHE_RAM_ATTR void HandleInterruptsStatic4(void) {
pPinact->handleInterrupts(4);
}
static ICACHE_RAM_ATTR void HandleInterruptsStatic5(void) {
pPinact->handleInterrupts(5);
}
class PinAct {
public:
PinAct() {};
void handleInterrupts(int);
}
void PinAct::PinAct() {
int actPins[] = {PIN1, PIN2, PIN3, PIN4, PIN5};
for (int i = 0; i <= sizeof(actPins); i++) {
pinMode(actPin[i], INPUT)
attachInterrupt(digitalPinToInterrupt(KEG1), HandleInterruptsStatic + i, FALLING);
}
}
void PinAct::handleInterrupts(int pin) { // Bubble Interrupt handler
// Do something with pin
}
目标是实际使attachInterrupt(digitalPinToInterrupt(KEG1), HandleInterruptsStatic + i, FALLING);
工作,通过索引 i 选择哪个 ISR。
我需要就是否分配 ISR 做出其他决定,因此连接要分配的 ISR 名称是可取的。
attachInterrupt(/* ... */, HandleInterruptsStatic + i, /* ... */);
// ^^^^^
为了根据某些整数索引i
选择要在运行时调用的函数,您可以使用函数指针数组:
typedef void (*FunctionPointer_t)(void);
FunctionPointer_t functions[] = {
HandleInterruptsStatic1,
HandleInterruptsStatic2,
// ...
};
// to use:
functions[i]();
相关文章:
- 如何基于循环迭代器选择函数
- 控件不会在选择函数旁边移动
- 函数模板 - 使用同一类型调用时,使用不同的参数类型选择函数
- 使用声明类型选择函数专业化
- 如何使“确认选择”函数从“主要”取消,如果它是假的
- 在编译时根据参数大小选择函数
- 在选择函数中检测超时的客户端
- 如何选择函数模板专用化
- 如何编写用于选择函数的样板代码
- "string_literal"解析为布尔值,但不解析为 std::string,当编译器选择函数的重载版本时
- 如何在编译时根据 constexpr 结果选择函数
- 使用模板选择函数的正确版本
- 在选择函数中等待fd的异常条件是什么?
- 矢量扩展的最佳选择函数
- 根据运行时数据选择函数重载
- 选择函数一直返回-1
- 在运行时选择函数参数
- 使用SFINAE根据函数是否存在特定的过载来选择函数
- 如何在c++中使用namespace::function选择函数的单个重载
- 根据模板参数选择函数名称