summaryrefslogtreecommitdiff
path: root/digital/io/src/fsm.h
blob: 82a860a89bfcc29b01c435d77f0205c41d8037ff (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
122
123
124
125
126
#ifndef fsm_h
#define fsm_h
/* fsm.h - 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.
 *
 * }}} */

#ifndef HOST

# include <avr/pgmspace.h>

#else

# define PROGMEM
# define pgm_read_word(addr) (*(addr))

#endif

/**
 * The io board includes an FSM with several concurrent active states.
 *
 * The main loop tests for each possible event and generate the corresponding
 * FSM events.  For example:
 *
 *   if (asserv_move_cmd_status () == success)
 *       fsm_handle_event (&ai_fsm, AI_EVENT_position_reached);
 *
 * Any unhandled event will be ignored.
 *
 * To start a sub machine from the top FSM, call the sub machine start
 * function (for example move_start), which will set parameters, then trigger
 * its start event.
 *
 * The sub machine is expected to generate an event for the top machine to
 * make it continue.
 */

#ifdef HOST
/** On host, check branches. */
typedef u32 fsm_branch_t;
#else
/** On AVR, no check. */
typedef u8 fsm_branch_t;
#endif

/** Transition function. */
typedef fsm_branch_t (*fsm_transition_t) (void);

/** Maximum number of active states. */
#define FSM_ACTIVE_STATES_MAX 4

/** Context of an FSM. */
struct fsm_t
{
    /** Transition table. */
    const fsm_transition_t *transition_table;
    /** Number of events, used to index the right transition. */
    u8 events_nb;
    /** Initial states. */
    u8 states_init[FSM_ACTIVE_STATES_MAX];
    /** Current active states. */
    u8 states_active[FSM_ACTIVE_STATES_MAX];
    /** Nomber of active states. */
    u8 active_states_nb;
    /** State timeout table. */
    const u16 *state_timeout_table;
    /** Current active states timeout if not 0xffff. */
    u16 states_timeout[FSM_ACTIVE_STATES_MAX];
    /** Event to generate on state timeout. */
    u8 state_timeout_event;
#ifdef HOST
    /** FSM name. */
    const char *name;
    /** States names. */
    const char * const *states_names;
    /** Events names. */
    const char * const *events_names;
#endif
};
typedef struct fsm_t fsm_t;

#define FSM_TRANSITION(fsm, state, event) \
    ((fsm_transition_t) pgm_read_word ( \
	&(fsm)->transition_table[(state) * (fsm)->events_nb + (event)]))

/** Reset a FSM. */
void
fsm_init (fsm_t *fsm);

/** Handle state timeout, return non-zero if at least an event was handled. */
uint8_t
fsm_handle_timeout (fsm_t *fsm);

/** Handle an event on the given FSM, return non-zero if at least an event was
 * handled. */
uint8_t
fsm_handle_event (fsm_t *fsm, uint8_t event);

/** Return non zero if event can be handled.  This can be used if event
 * generation requires expensive computations. */
uint8_t
fsm_can_handle_event (fsm_t *fsm, uint8_t event);

#include "ai_fsm.h"

#endif /* fsm_h */