From 645b7d11df890ed99e5b00c8272d4e794060f703 Mon Sep 17 00:00:00 2001 From: Nicolas Schodet Date: Thu, 13 May 2010 01:28:31 +0200 Subject: digital/io/src: add event queue --- digital/io/src/main.c | 44 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) (limited to 'digital/io/src/main.c') diff --git a/digital/io/src/main.c b/digital/io/src/main.c index f05653eb..6571e9a4 100644 --- a/digital/io/src/main.c +++ b/digital/io/src/main.c @@ -75,10 +75,17 @@ static void main_loop (void); */ enum team_color_e bot_color; -/** - * Post a event to the top FSM in the next iteration of main loop. - */ -uint8_t main_post_event_for_top_fsm = 0xFF; +/** Maximum number of events in post queue. */ +#define MAIN_POST_EVENT_QUEUE_SIZE 8 + +/** Events to post to the FSM in next iteration. */ +uint8_t main_post_event_queue[MAIN_POST_EVENT_QUEUE_SIZE]; + +/** Number of events in the post queue. */ +uint8_t main_post_event_queue_nb; + +/** First event in the post queue. */ +uint8_t main_post_event_queue_head; /** Obstacles positions, updated using radar module. */ vect_t main_obstacles_pos[2]; @@ -104,6 +111,29 @@ static uint8_t main_stats_asserv_, main_stats_asserv_cpt_; */ static uint8_t main_stats_timer_; +void +main_post_event (uint8_t event) +{ + assert (main_post_event_queue_nb < MAIN_POST_EVENT_QUEUE_SIZE); + uint8_t tail = (main_post_event_queue_head + main_post_event_queue_nb) + % MAIN_POST_EVENT_QUEUE_SIZE; + main_post_event_queue[tail] = event; + main_post_event_queue_nb++; +} + +/** Pop one event from the event queue. */ +static uint8_t +main_pop_event (void) +{ + uint8_t e; + assert (main_post_event_queue_nb > 0); + e = main_post_event_queue[main_post_event_queue_head]; + main_post_event_queue_nb--; + main_post_event_queue_head = (main_post_event_queue_head + 1) + % MAIN_POST_EVENT_QUEUE_SIZE; + return e; +} + /** * Main events management. * This function is responsible to get all events and send them to the @@ -183,15 +213,13 @@ main_event_to_fsm (void) } /* Event generated at the end of the sub FSM to post to the top FSM */ - if (main_post_event_for_top_fsm != 0xFF) + if (main_post_event_queue_nb) { /* We must post the event at the end of this block because it * will issue a continue and every instruction after will * never be executed. */ /* We need to save the event before reseting it */ - uint8_t save_event = main_post_event_for_top_fsm; - /* Reset */ - main_post_event_for_top_fsm = 0xFF; + uint8_t save_event = main_pop_event (); /* Post the event */ FSM_HANDLE_EVENT (&ai_fsm, save_event); } -- cgit v1.2.3