Convert void (*)() to void (*)(unsigned char) - c++

I'm trying to convert a void (*)() to void (*)(unsigned char) but my knowledge in C++ isn't good enough. I searched, but I could not find any examples. So I thought to create a function of function but I get some error:
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)
error: 'castHandler' was not declared in this scope
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. 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 i.e.
void redirectFunction(unsigned char)
{
handler();
}
then
gpio_set_interrupt(pin, GPIO_INTTYPE_EDGE_ANY,redirectFunction);
EDIT
Perhaps this in 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)
);

Related

C++ - How to bind a callback to a class method without being static?

I have my class:
class Foo
{
public:
(...)
private:
void mycallback(void* buff, wifi_promiscuous_pkt_type_t type);
void registerMyCallback();
};
The mycallback is the callback.
I want to use a method esp_wifi_set_promiscuous_rx_cb to register the mycallback so that when a WiFi packet is detected, this callback method will be executed.
The esp_wifi_set_promiscuous_rx_cb signature is:
esp_err_t esp_wifi_set_promiscuous_rx_cb(wifi_promiscuous_cb_t cb);
Where the wifi_promiscuous_cb_t definition is:
typedef void (* wifi_promiscuous_cb_t)(void *buf, wifi_promiscuous_pkt_type_t type);
I want to use the mycallback method inside my class, therefore I simply can't use like this:
void Foo::registerMyCallback()
{
esp_wifi_set_promiscuous_rx_cb(&mycallback);
}
I know that I could use something similar if I would just make my method as static.
Is there anyway that I bind mycallback to esp_wifi_set_promiscuous_rx_cb without making the callback static?
I have tried the following:
esp_wifi_set_promiscuous_rx_cb(std::bind(&Foo::mycallback, this, std::placeholders::_1, std::placeholders::_2));
But I am still having the following error:
cannot convert 'std::_Bind_helper<false, void (Foo::Foo::*)(void*, wifi_promiscuous_pkt_type_t),
Foo::Foo*, const std::_Placeholder<1>&, const std::_Placeholder<2>&>::type
to
'wifi_promiscuous_cb_t {aka void (*)(void*, wifi_promiscuous_pkt_type_t)}' for argument '1'
Th library you are using is C package.
Thus the only guaranteed way pass a valid function is to pass a C function with C linkage. This function can then call the method on your object.
If you want the callback method to be non static you need to store a pointer (ore reference) to the callback object somewhere that your callback function can find it. (in most C callback functions you can provide a void* object that is passed to your callback, but this interface does not seem to allow this so you will have to save the value yourself).
Foo* myCBObject = nullptr;
extern "C" void myCB(void *buf, wifi_promiscuous_pkt_type_t type)
{
try
{
myCBObject->mycallback(buff, type);
}
catch(...) {} // Don't allow exceptions to cross C linkage
}
...
// Your code.
void Foo::registerMyCallback()
{
myCBObject = this;
esp_wifi_set_promiscuous_rx_cb(myCB);
}
Note: You should NOT be registering static member functions with a C library. If this works it is only by chance. There is no guarantee that a static function has the same calling convention of a C function (they usually do but that is not guaranteed).
After some research, I hope I found the solution. The trick is to bind member function first and then obtain the function pointer from the std::function. Notice the usage of my_wifi_promiscuous_cb_t and std::function::target<>().
#include <iostream>
#include <functional>
using namespace std::placeholders;
// using fake definitions
extern "C"
{
enum wifi_promiscuous_pkt_type_t {};
typedef int32_t esp_err_t;
typedef void (*wifi_promiscuous_cb_t)(void* buf, wifi_promiscuous_pkt_type_t type);
typedef void my_wifi_promiscuous_cb_t(void* buf, wifi_promiscuous_pkt_type_t type);
esp_err_t esp_wifi_set_promiscuous_rx_cb(wifi_promiscuous_cb_t cb)
{
return 0;
}
}
class Class
{
public:
void mycallback(void* buff, wifi_promiscuous_pkt_type_t type) {}
void registerMyCallback() {
std::function<void(void*, wifi_promiscuous_pkt_type_t)> fun2 = std::bind(&Class::mycallback, this, _1, _2);
esp_wifi_set_promiscuous_rx_cb(fun2.target<my_wifi_promiscuous_cb_t>());
}
};
int main()
{
Class c;
c.registerMyCallback();
}

Arduino: Use Timer in c++ class

I am trying to use a timer to repeatedly change the PWM Output over time to have a smooth transition when the brightness changes. I keep getting this error when trying to compile the code:
/Users/jt/Documents/Arduino/libraries/SingleColorLight/SingleColorLight.cpp: In constructor 'CSingleColorLight::CSingleColorLight(int)':
/Users/jt/Documents/Arduino/libraries/SingleColorLight/SingleColorLight.cpp:13:58: error: cannot convert 'CSingleColorLight::DimmerCallback' from type 'void (CSingleColorLight::)(void*)' to type 'void ()(void)'
ets_timer_setfn(&Dimmer, this->DimmerCallback, NULL);
Here is my code:
class CSingleColorLight {
private:
int pin;
int intensitySetPoint;
int intensityActual;
int percentageBuffer;
ETSTimer Dimmer;
int dimmerCount;
public:
CSingleColorLight(int _pin);
bool setIntensity(int _intensity);
int getIntensity();
bool getStatus(void);
bool setStatus(bool _status);
void DimmerCallback(void*);
};
and in the cpp file:
void CSingleColorLight::DimmerCallback(void*) {
if(dimmerCount>0){
dimmerCount--;
intensityActual++;
} else if(dimmerCount<0){
dimmerCount++;
intensityActual--;
} else {
ets_timer_disarm(&Dimmer);
}
analogWrite(pin, percentageToTime[intensityActual]);
return;
}
It asks for a pointer, right? Any idea how to fix this?
Thanks a lot!
If you want DimmerCallback to take a void* argument, then you need to name it, like
void CSingleColorLight::DimmerCallback(void* x)
but you are not using the void* in the code. It looks like you should just get rid of it, so it would be
void CSingleColorLight::DimmerCallback()
int the cpp and
void DimmerCallback();
in the header.
A void* argument is a pointer that can point to any data type, it is not the same as void which is just no argument.

Pass pointer to function works in simple case but not after "class"-ifying

I am a somewhat rusty programmer, and new to C++. I've been asked to write a program that can pass a pointer to a function into another function and execute. I can make the simple case work, where everything is in a .cpp file. But when I place the code in a class inside a .h file it won't compile. I am either code blind or missing something.
Here is the code that works:
/*
* funcptr.cpp
*
* Example:
* - pass function pointer as argument
* - execute passed function
*/
#include <stdio.h>
void takes_a_function(void (*f)(void *data), void *data);
void print_char(void *data);
void print_int(void *data);
// This function gets passed (to takes_a_function)
void print_char(void *data) {
char *ch = (char *)data;
printf("%c\n", *ch);
}
// This function gets passed (to takes_a_function)
void print_int(void *data) {
int *i = (int *)data;
printf("%d\n", *i);
}
void takes_a_function(void (*f)(void *), void *data) {
//f(data); // this also works
(*f)(data);
}
int main() {
int i = 100;
takes_a_function(print_int, &i);
char ch = 'A';
takes_a_function(print_char, &ch);
}
It compiles and runs:
# g++ funcptr.cpp -o funcptr
# ./funcptr
100
A
So far so good. But then I put the code into a .h file and "class"-ify it so I can use it from anywhere, and everything falls apart:
#ifndef __funcptr_h__
#define __funcptr_h__
#include <stdio.h>
void takes_a_function(void (*f)(void *data), void *data);
void print_char(void *data);
void print_int(void *data);
void testit();
class FunctionPtr
{
public:
// This function gets passed (to takes_a_function)
void print_char(void *data) {
char *ch = (char *)data;
printf("%c\n", *ch);
}
// This function gets passed (to takes_a_function)
void print_int(void *data) {
int *i = (int *)data;
printf("%d\n", *i);
}
void takes_a_function(void (*f)(void *a), void *data) {
//f(data); // this also works
(*f)(data);
}
void testit() {
int i = 100;
takes_a_function(print_int, &i);
char ch = 'A';
takes_a_function(print_char, &ch);
}
};
#endif
The compiler error is:
# g++ funcptr.h
funcptr.h: In member function ‘void FunctionPtr::testit()’:
funcptr.h:34:33: error: invalid use of non-static member function ‘void FunctionPtr::print_int(void*)’
takes_a_function(print_int, &i);
^
funcptr.h:22:7: note: declared here
void print_int(void *data) {
^~~~~~~~~
funcptr.h:37:35: error: invalid use of non-static member function ‘void FunctionPtr::print_char(void*)’
takes_a_function(print_char, &ch);
^
funcptr.h:16:7: note: declared here
void print_char(void *data) {
^~~~~~~~~~
I've been playing with this for a while and done a fair amount of reading on passing function pointers, including on StackOverflow, however all the examples I see are simple and of no help.
Any insights are much appreciated.
Thanks to all.
It looks to me like your problem is when you call takes_a_function with your arguments.
You declared the data parameter as a pointer to a void type, not a reference to a void type.
You could try something like:
void testIt() {
int i = 100;
int * j = &i;
takes_a_function(print_int, j);
char c = 'a';
char * d = &c;
takes_a_function(print_char, d);
};
References and pointers are not exactly the same thing.
Also it looks like you forgot a #endif after you define __funtptr_h__
I hope this helped

Compile error with C++11 std::bind and auto for Callback function parameter

Hi I am having an issue compiling the following code. I am using auto and std::bind to bind a callback function with arguments. However, after passing this callback function as a parameter, it has issues compiling. Do you see an issue with the function declarations below:
#include <iostream>
#include <functional>
using namespace std;
class VmapPlayer
{
public:
void startPlayback();
void playAdBreak(int adBreak, void (VmapPlayer::*callback)());
void playSingleAd(int ad, void (VmapPlayer::*callback)(int adBreak, void (VmapPlayer::*cb)()));
};
void VmapPlayer::playSingleAd(int ad, void (VmapPlayer::*callback)(int adBreak, void (VmapPlayer::*cb)()))
{
cout << "i am here" << endl;
// OPTION #1 I would like to call this function
//(this->*callback)(adBreak, cb);
// OPTION #2 I would like this call this function without the params:
//(this->*callback)();
}
void VmapPlayer::playAdBreak(int adBreak, void (VmapPlayer::*callback)())
{
auto cb = std::bind(&VmapPlayer::playAdBreak, adBreak, callback);
playSingleAd(123, cb);
}
void VmapPlayer::startPlayback()
{
playAdBreak(456, &VmapPlayer::startPlayback);
}
int main()
{
VmapPlayer p;
p.startPlayback();
return 0;
}
Please see below for the compile error log:
main.cpp||In member function 'void VmapPlayer::playAdBreak(int, void (VmapPlayer::*)())':|
main.cpp|28|error: no matching function for call to 'VmapPlayer::playSingleAd(int, std::_Bind<std::_Mem_fn<void (VmapPlayer::*)(int, void (VmapPlayer::*)())>(int, void (VmapPlayer::*)())>&)'|
main.cpp|28|note: candidate is:|
main.cpp|14|note: void VmapPlayer::playSingleAd(int, void (VmapPlayer::*)(int, void (VmapPlayer::*)()))|
main.cpp|14|note: no known conversion for argument 2 from 'std::_Bind<std::_Mem_fn<void (VmapPlayer::*)(int, void (VmapPlayer::*)())>(int, void (VmapPlayer::*)())>' to 'void (VmapPlayer::*)(int, void (VmapPlayer::*)())'|
||=== Build finished: 1 errors, 0 warnings (0 minutes, 0 seconds) ===|
I supposed my question can be simplified as:
What does the function declaration for playSingleAd() need to be in order to compile the following successfully?:
void VmapPlayer::playAdBreak(int adBreak, void (VmapPlayer::*callback)())
{
auto cb = std::bind(&VmapPlayer::playAdBreak, adBreak, callback);
playSingleAd(123, cb);
}
When you bind a method to fully-supplied parameters, the resulting functor takes no arguments. In your code, playSingleAd takes a function pointer that takes as an argument, a function pointer whose arguments are to be supplied when called. Because you already bound the arguments to that function pointer, the arguments thereto cannot not be specified in the function signature.
Anyway, your code can be improved with the use of std::function. Moreover, the function will have to take the instance as an argument, as shown in the implementation of playSingleAd below:
class VmapPlayer
{
public:
void startPlayback();
void playAdBreak(int adBreak, void (VmapPlayer::*callback)());
void playSingleAd(int ad, std::function<void (VmapPlayer&)>);
};
void VmapPlayer::playSingleAd(int, std::function<void (VmapPlayer&)> callback)
{
cout << "i am here" << endl;
callback(*this);
}
void VmapPlayer::playAdBreak(int adBreak, void (VmapPlayer::*callback)())
{
using namespace std::placeholders;
auto cb = std::bind(&VmapPlayer::playAdBreak, _1, adBreak, callback);
playSingleAd(123, cb);
}

QtConcurrent::run() doesn't accept __stdcall/WINAPI function

I was trying to execute a DLL function that has __stdcall calling convention using QtConcurrent::run(), but I am getting compile errors.
I've reduced the problem to this example code:
__stdcall void dllFunc() {
qDebug() << "test";
}
void test() {
QtConcurrent::run(dllFunc);
}
If I remove __stdcall the code compiles and runs fine. Otherwise I get these compile errors:
error: invalid conversion from 'void (*)()' to 'void (*)()'
error: initializing argument 1 of 'QFuture<T> QtConcurrent::run(T (*)()) [with T = void]'
Why is this happening, and what is the best workaround?
Well, for a workaround, I ended up using a wrapper function with the same signature but no __stdcall convention:
__stdcall int dllFunc(int param1, void *param2) {
qDebug() << "test";
return 0;
}
int dllFuncWrapper(int param1, void *param2) {
return dllFunc(param1, param2);
}
void test() {
QtConcurrent::run(dllFuncWrapper);
}