summaryrefslogtreecommitdiffhomepage
path: root/digital/io/src/fsm.c
blob: 53e4877d0906e642706b85d36b8a2ebe5f18402c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* fsm.c - Finite State Machine code. */
/* io - Input & Output with Artificial Intelligence (ai) support on AVR. {{{
 *
 * Copyright (C) 2008 Nicolas Schodet
 *
 * APBTeam:
 *        Web: http://apbteam.org/
 *      Email: team AT apbteam DOT org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 * }}} */
#include "common.h"
#include "fsm.h"

#include "modules/trace/trace.h"
#include "trace_event.h"

#ifdef HOST
# include <stdio.h>
#endif

void
fsm_init (fsm_t *fsm)
{
    uint8_t i;
    assert (fsm);
    for (i = 0; i < FSM_ACTIVE_STATES_MAX; i++)
	fsm->states_active[i] = fsm->states_init[i];
    for (i = 0; i < fsm->active_states_nb; i++)
	fsm->states_timeout[i] =
	    fsm->state_timeout_table[fsm->states_init[i]];
}

/** Handle an event on the given FSM on a single active state, return 1 if the
 * event was handled. */
static uint8_t
fsm_handle_event_single (fsm_t *fsm, uint8_t active, uint8_t event)
{
    assert (fsm);
    assert (active < fsm->active_states_nb);
    assert (event < fsm->events_nb);
    /* Lookup transition. */
    uint8_t old_state = fsm->states_active[active];
    fsm_transition_t tr = fsm->transition_table[
	old_state * fsm->events_nb + event];
    /* Ignore unhandled events. */
    if (tr)
      {
	/* Execute transition. */
	uint8_t new_state;
	fsm_branch_t br = tr ();
	/* Change state. */
#ifdef HOST
	assert (((br >> 16) & 0xff) == fsm->states_active[active]);
	assert (((br >> 8) & 0xff) == event);
	fprintf (stderr, "%s %s =%s=> %s\n", fsm->name,
		fsm->states_names[fsm->states_active[active]],
		fsm->events_names[event], fsm->states_names[br & 0xff]);
	new_state = br & 0xff;
#else
	new_state = br;
#endif
	TRACE (TRACE_FSM__HANDLE_EVENT, (u8) old_state, (u8) event,
	       (u8) new_state);
	fsm->states_active[active] = new_state;
	fsm->states_timeout[active] = fsm->state_timeout_table[new_state];
	return 1;
      }
    return 0;
}

uint8_t
fsm_handle_event (fsm_t *fsm, uint8_t event)
{
    uint8_t i, handled = 0;
    assert (fsm);
    assert (event < fsm->events_nb);
    for (i = 0; i < fsm->active_states_nb; i++)
      {
	/* Handle the event for this active state. */
	handled += fsm_handle_event_single (fsm, i, event);
      }
    return handled;
}

uint8_t
fsm_handle_timeout (fsm_t *fsm)
{
    uint8_t i, handled = 0;
    assert (fsm);
    for (i = 0; i < fsm->active_states_nb; i++)
      {
	/* If there is a timeout for this state. */
	if (fsm->states_timeout[i] != 0xffff)
	  {
	    if (fsm->states_timeout[i])
		fsm->states_timeout[i]--;
	    else
	      {
		/* Timeout expired, generate corresponding event. */
		handled += fsm_handle_event_single (fsm, i,
						    fsm->state_timeout_event);
	      }
	  }
      }
    return handled;
}