summaryrefslogtreecommitdiff
path: root/digital/io/src/main.c
diff options
context:
space:
mode:
authorNicolas Schodet2010-05-13 01:28:31 +0200
committerNicolas Schodet2010-05-13 01:28:31 +0200
commit645b7d11df890ed99e5b00c8272d4e794060f703 (patch)
tree9135cbfc98ea2a5770635b977c2b08da78986ff5 /digital/io/src/main.c
parent953ff53ccf92e700c7c25136eabbe6699caa90f9 (diff)
digital/io/src: add event queue
Diffstat (limited to 'digital/io/src/main.c')
-rw-r--r--digital/io/src/main.c44
1 files changed, 36 insertions, 8 deletions
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);
}