summaryrefslogtreecommitdiffhomepage
path: root/digital/io/src/fsm.h
blob: e08605fe4a67edcc0043425b1a7e069688057e51 (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
#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.
 *
 * }}} */

/**
 * The io board includes several concurrent FSM.  The code to handle events is
 * generic, but as events are not compatibles (they do not share the same
 * identifier), events should be generated for each FSM separately.
 *
 * 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 (&top_fsm, TOP_EVENT_position_reached);
 *       fsm_handle_event (&getsamples_fsm, GETSAMPLES_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 getsamples_start), which will set parameters, reset
 * its fsm, then trigger its start event.
 *
 * To abandon a FSM, reset it using fsm_init or it will continue to run
 * concurrently.
 *
 * 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);

/** Context of a FSM or sub-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 state. */
    u8 state_init;
    /** Current active state. */
    u8 state_current;
    /** State timeout table. */
    const u16 *state_timeout_table;
    /** Current state timeout if not 0xffff. */
    u16 state_timeout;
    /** 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;

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

/** Handle state timeout, return 1 if a event was handled. */
uint8_t
fsm_handle_timeout (fsm_t *fsm);

/** Handle an event on the given FSM.
 * @return
 *   - 0 if this FSM does not handle this event ;
 *   - 1 if this FSM handles this event in its current state.
 */
uint8_t
fsm_handle_event (fsm_t *fsm, u8 event);

#include "move_fsm.h"
#include "top_fsm.h"
#include "filterbridge_fsm.h"
#include "elevator_fsm.h"
#include "cylinder_fsm.h"
#include "init_fsm.h"

#endif /* fsm_h */