我使用C作为Arduino控制器,我有一个包含静态变量的函数
int buttonReallyPressed(int i);
Run Code Online (Sandbox Code Playgroud)
我想要该函数的多个实例,所以我这样做了:
typedef int (*ButtonDebounceFunction) ( int arg1);
ButtonDebounceFunction Button1Pressed = buttonReallyPressed;
ButtonDebounceFunction Button2Pressed = buttonReallyPressed;
Run Code Online (Sandbox Code Playgroud)
我收到了函数int buttonReallyPressed(int i)的两个单独实例吗?
创建指向函数的指针时,不要在函数中创建另一个静态变量实例.
解决方法是:创建结构,保存处理单个按钮所需的一切(将静态变量移入其中).创建结构的实例数组.将结构作为参数传递给按钮处理程序.
struct button_state {
int pressed; // or whatever
}
struct button_state button[3];
int buttonReallyPressed(struct button_state *state);
void button_isr(...)
{
...
buttonReallyPressed(&button[id]);
...
}
Run Code Online (Sandbox Code Playgroud)