How to properly pass argument to palSetPadCallback's callback? Topic is solved

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: utzig, lbednarz, tfAteba, barthess, RoccoMarco

ceremcem
Posts: 53
Joined: Mon Aug 10, 2015 6:57 am
Has thanked: 5 times
Been thanked: 3 times

How to properly pass argument to palSetPadCallback's callback?

Postby ceremcem » Thu Jun 20, 2019 9:30 am

I have a function which takes the button_number as an argument and decides the action according to the button_number. My button setup is as follows:

Code: Select all

    // forward button
    palSetPadMode(GPIOA, UPWARD_BUTTON, PAL_MODE_INPUT);
    palEnablePadEvent(GPIOA, UPWARD_BUTTON, PAL_EVENT_MODE_BOTH_EDGES);
    palSetPadCallback(GPIOA, UPWARD_BUTTON, button_callback, UPWARD_BUTTON);

    // backward button
    palSetPadMode(GPIOA, DOWNWARD_BUTTON, PAL_MODE_INPUT);
    palEnablePadEvent(GPIOA, DOWNWARD_BUTTON, PAL_EVENT_MODE_BOTH_EDGES);
    palSetPadCallback(GPIOA, DOWNWARD_BUTTON, button_callback, DOWNWARD_BUTTON);


My handler function is as follows (just FYI):

Code: Select all

void button_callback(uint8_t pad){
    bool state = ! palReadPad(GPIOA, pad);
    if (pad == UPWARD_BUTTON){
        UPWARD_button(state);
    } else {
        DOWNWARD_button(state);
    }
}


Problem

This code works as intended. However, compiler throws the following warning:

Code: Select all

...
/home/aea/sw2/app/io.c:64:64: note: in expansion of macro 'DOWNWARD_BUTTON'
     palSetPadCallback(GPIOA, DOWNWARD_BUTTON, button_callback, DOWNWARD_BUTTON)
                                                                ^
/home/aea/ChibiOS/os/hal/include/hal_pal.h:967:8: note: expected 'void *' but argument is of type 'int'
   void palSetPadCallbackI(ioportid_t port, iopadid_t pad,
        ^
...


How can I properly pass the BUTTON_NUMBER as an argument to the handler function (button_callback)?

User avatar
Giovanni
Site Admin
Posts: 12944
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 731 times
Been thanked: 609 times
Contact:

Re: How to properly pass argument to palSetPadCallback's callback?  Topic is solved

Postby Giovanni » Thu Jun 20, 2019 9:33 am

By casting it to (void *).

Giovanni

ceremcem
Posts: 53
Joined: Mon Aug 10, 2015 6:57 am
Has thanked: 5 times
Been thanked: 3 times

Re: How to properly pass argument to palSetPadCallback's callback?

Postby ceremcem » Thu Jun 20, 2019 12:31 pm

Now all warnings are vanished. Here is my changes:

I changed my handler function from:

Code: Select all

void button_callback(uint8_t pad){
   ...
}


to:

Code: Select all

void button_callback(void * _pad){
    uint8_t pad = *((uint8_t *) _pad);
    ...
}


and palSetPadCallback() invocation from

Code: Select all

    palSetPadCallback(GPIOA, DOWNWARD_BUTTON, button_callback, DOWNWARD_BUTTON);


to:

Code: Select all

    uint8_t _DOWNWARD_BUTTON = DOWNWARD_BUTTON;
    palSetPadCallback(GPIOA, DOWNWARD_BUTTON, button_callback, (void *) &_DOWNWARD_BUTTON);


Thanks.


Return to “General Support”

Who is online

Users browsing this forum: Google [Bot] and 3 guests