[英]Convert void (*)() to void (*)(unsigned char)
I'm trying to convert a void (*)()
to void (*)(unsigned char)
but my knowledge in C++ isn't good enough. 我正在尝试将void (*)()
转换为void (*)(unsigned char)
但是我对C ++的了解还不够。 I searched, but I could not find any examples. 我进行了搜索,但找不到任何示例。 So I thought to create a function of function but I get some error: 所以我想创建一个函数function但出现了一些错误:
typedef void (* voidFuncPtr)();
void castHandler(voidFuncPtr handler)(uint8_t gpio_num)
{
handler();
}
void attachInterrupt(uint8_t pin, voidFuncPtr handler, int mode)
{
// typedef void (* voidFuncPtr)();
// typedef void (* gpio_interrupt_handler_t)(uint8_t gpio_num);
// typedef void (* gpio_interrupt_handler_t)(unsigned char)
gpio_set_interrupt(pin, GPIO_INTTYPE_EDGE_ANY, castHandler(handler));
}
error: 'castHandler' declared as function returning a function void castHandler(voidFuncPtr handler)(uint8_t gpio_num) 错误:“ castHandler”声明为函数,返回函数void castHandler(voidFuncPtr handler)(uint8_t gpio_num)
error: 'castHandler' was not declared in this scope gpio_set_interrupt(pin, GPIO_INTTYPE_EDGE_ANY, castHandler(handler)); 错误:在此范围内未声明'castHandler'gpio_set_interrupt(pin,GPIO_INTTYPE_EDGE_ANY,castHandler(handler));
Can someone help me? 有人能帮我吗?
You need to maintain a table of handlers for each GPIO. 您需要维护每个GPIO的处理程序表。 Something like the following (untested and incomplete - some initializations and error checking are omitted) code: 类似于以下内容(未经测试且不完整-省略了一些初始化和错误检查)的代码:
typedef void (* voidFuncPtr)();
voidFuncPtr handlers[MAX_HANDLERS];
void attachInterrupt(uint8_t pin, voidFuncPtr handler, int mode)
{
handlers[pin] = handler;
}
void handleGPIO(unsigned char pin)
{
if (handlers[pin])
handlers[pin]();
}
void enableGPIOInterrupts(void)
{
/// Do this for each available pin
gpio_set_interrupt(pin, GPIO_INTTYPE_EDGE_ANY, handleGPIO);
}
Why not just write a function to do a redirection ie 为什么不只是编写一个函数来进行重定向,即
void redirectFunction(unsigned char)
{
handler();
}
then 然后
gpio_set_interrupt(pin, GPIO_INTTYPE_EDGE_ANY,redirectFunction);
EDIT 编辑
Perhaps this in C++: 也许在C ++中是这样的:
void redirectFunction(std::function<void()> handler, unsigned char)
{
handler();
}
void wibble() { }
gpio_set_interrupt(pin, GPIO_INTTYPE_EDGE_ANY,
std::bind(redirectFunction, _1, wibble)
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.