Page 1 of 1

How to properly pass argument to palSetPadCallback's callback?

Posted: Thu Jun 20, 2019 9:30 am
by ceremcem
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)?

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

Posted: Thu Jun 20, 2019 9:33 am
by Giovanni
By casting it to (void *).

Giovanni

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

Posted: Thu Jun 20, 2019 12:31 pm
by ceremcem
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.