cancel
Showing results for 
Search instead for 
Did you mean: 

Callback function with parameter?

Pilous Droip
Senior
Posted on March 20, 2018 at 13:03

Hello friends.

I create callback function for uart:

typedef void (*InterruptHandler) (uint32_t uart_interrupt);

   InterruptHandler UART_HandlerTable[MaxInterrupt];

   extern void UART_callback(uint32_t InterruptID, InterruptHandler Handler);

And callback function:

void UART_callback(uint32_t InterruptID, InterruptHandler Handler)

{

   UART_HandlerTable[InterruptID] = Handler;

}

And when I receive data from uart I generate callback function:

void UART_generate_call_back(uint8_t *input_buffer)

{

   UART_HandlerTable[input_buffer[0]](input_buffer[0]);

}

And in main I use this:

void callback_HELLO();

int main(void)

{

UART_callback( 0x01, (InterruptHandler)

callback_HELLO 

);

   while (1)

   {

   }

}

void

callback_HELLO

()

{

   usart_puts('\r\nHello world\r\n');

}

Now, it is working very good. 

But how to add parameter to callback function?

For example. I receive message, this message I analyze and I need hand over some data.

So I need modificate this function to return *data to callback.

void UART_generate_call_back(uint8_t *input_buffer, uint8_t *data)

{

   UART_HandlerTable[input_buffer[0]](input_buffer[0]);

}

So how to get data to callback function? And in main I would like get data.

void

callback_HELLO

(uint8_t *data)

{

       /* do something with data */

   usart_puts('\r\nHello world\r\n');

}

Is it posible?

#callback
4 REPLIES 4
Posted on March 20, 2018 at 14:34

>>Is it possible?

Sure, the way you have the call defined at the moment it expects a 32-bit parameter.

If you want the data in the array with the callback, you'd likely want to use a structure, and array that.

Think you want

void

callback_HELLO

(uint8_t data)

rather than the pointer.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on March 20, 2018 at 15:26

I call this function, when I receive data on uart.

void UART_generate_call_back(uint8_t *input_buffer)

{

   UART_HandlerTable[input_buffer[0]](input_buffer[0]);

}

I have uart data in input_buffer. And this datas I would like to send by parameter to callback function.

So how to modificate this function, to send array data as parameter?

   UART_HandlerTable[input_buffer[0]](input_buffer[0]);

And here I would like to work with send datas.

void 

callback_HELLO

(uint8_t *data)

{

   /* do something with data */

   usart_puts('\r\nHello world\r\n');

}

Posted on March 20, 2018 at 16:00

It is currently sending the first character in the buffer, if you want it to send the pointer, then do that instead

typedef void (*InterruptHandler)(uint8_t *data);

..

UART_HandlerTable[input_buffer[0]](input_buffer); // Send the pointer

..

void 

callback_HELLO

(uint8_t *data)

typedef void (*InterruptHandler) (uint8_t data);

..

UART_HandlerTable[input_buffer[0]](input_buffer[0]); // Send the char

..

void 

callback_HELLO

(uint8_t data)
Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on March 20, 2018 at 16:23

Of course. I will try it tomorrow. It's look like very good. 

:(

  Thank you.