| ??? 10/04/12 03:08 Read: times |
#188566 - Timers - Function Pointers |
Hi all
I would like to create an array of timer structs, that on expiration call a function - that's easy enough. But.... The functions that they call have differing argument types (void, through to pointers to other structs) I am using Raisonance compiler RC51 on a silabs F040 Here is my header
typedef enum timer_types{
timer_one_shot =0,
timer_periodic =1
}timer_type_t;
typedef void (*func_pointer)(void *);
typedef struct timer{
unsigned int reset_value;
unsigned int current_value;
unsigned char timer_priority;
timer_type_t timer_type;
func_pointer timeout_func;
void *func_args;
}s_timer_t;
#define NUMBER_OF_TIMERS 10
extern s_timer_t timer_array[];
and here is my timer code (called on 1mSec tick)
void TIMERS_sort_out_timers(void){
unsigned char i;
for (i = 0; i < NUMBER_OF_TIMERS; i++){ //step through each of our timers
if (timer_array[i].current_value != 0){ //see if we have already expired.
timer_array[i].current_value --; //if not then decrement the timer toward 0
if (timer_array[i].current_value == 0){ //now see if we have timed out
if (timer_array[i].timer_type == timer_periodic){ //if we run on a periodic timer
timer_array[i].current_value = timer_array[i].reset_value; //reload the timer
}
timer_array[i].timeout_func(timer_array[i].func_args); //if we have timed out then perform the function in our callback.
}
}
}
}
This is one of the timer functions that I would like to run, note that it has no arguments, this is the problem as the timer function is expecting to pas an argument of void *.
void MAIN_blink(void){
RUN_LED = !RUN_LED;
}
for completeness, this is how I am initialising the timers
void TIMERS_init_timers(void){
//start the timeout timer now
timer_array[cctalk_msg_timeout].reset_value = CCTALK_MSG_TIMEOUT_MSEC;
timer_array[cctalk_msg_timeout].current_value = 0;
timer_array[cctalk_msg_timeout].timer_type = timer_one_shot;
timer_array[cctalk_msg_timeout].timeout_func = UART_null_function;
//start the timeout timer now
timer_array[keep_alive_led].reset_value = KEEP_ALIVE_BLINK_RATE;
timer_array[keep_alive_led].current_value = KEEP_ALIVE_BLINK_RATE; //this must be non 0 to start the timer running.
timer_array[keep_alive_led].timer_type = timer_periodic;
timer_array[keep_alive_led].timeout_func = MAIN_blink;
timer_array[cctalk_char_timeout].reset_value = CCTALK_CHAR_TIMEOUT_MSEC;
timer_array[cctalk_char_timeout].current_value = 0;
timer_array[cctalk_char_timeout].timer_type = timer_one_shot;
timer_array[cctalk_char_timeout].timeout_func = UART_null_function ;
}
so the question is, how do I correctly create a callback to a function with zero or more arguments? Thanks for the ideas. Regards Marshall |
| Topic | Author | Date |
| Timers - Function Pointers | 01/01/70 00:00 | |
| Too much for a '51? | 01/01/70 00:00 | |
| I Agree | 01/01/70 00:00 | |
| Function Pointers... | 01/01/70 00:00 | |
| you are violating KISS | 01/01/70 00:00 | |
| Various Timer Functions | 01/01/70 00:00 | |
| Thanks Michael | 01/01/70 00:00 | |
| regardless, you are violating KISS | 01/01/70 00:00 | |
| even on ARM | 01/01/70 00:00 | |
| The timer simply increments | 01/01/70 00:00 | |
| bug | 01/01/70 00:00 | |
| more bugs | 01/01/70 00:00 | |
thanks | 01/01/70 00:00 |



