State machines are commonly used to implement decision making algorithms. State machines are used in applications where distinguishable states exist. A finite state machine (FSM) is based on the idea that a given system has a finite number of states. In Summary States: 5 1.Error 2.Configuration 3.Ready 4.Assigned 5.Locked Transitions: 2 1.GO 2.OUT
Fork of mbed_blinky by
main.cpp@8:96112047d7f3, 2014-08-07 (annotated)
- Committer:
- mnikolaos
- Date:
- Thu Aug 07 14:54:26 2014 +0000
- Revision:
- 8:96112047d7f3
- Parent:
- 7:5d25743fac86
fsm finished
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dan | 0:7dec7e9ac085 | 1 | #include "mbed.h" |
mnikolaos | 7:5d25743fac86 | 2 | |
mnikolaos | 7:5d25743fac86 | 3 | Serial pc(USBTX, USBRX); |
mnikolaos | 7:5d25743fac86 | 4 | |
mnikolaos | 8:96112047d7f3 | 5 | DigitalOut gpo1(D1); |
mnikolaos | 8:96112047d7f3 | 6 | DigitalOut gpo2(D2); |
mnikolaos | 8:96112047d7f3 | 7 | DigitalOut gpo3(D3); |
mnikolaos | 8:96112047d7f3 | 8 | DigitalOut ledB(LED_BLUE); |
mnikolaos | 8:96112047d7f3 | 9 | DigitalOut ledG(LED_GREEN); |
mnikolaos | 8:96112047d7f3 | 10 | DigitalOut ledR(LED_RED); |
mnikolaos | 7:5d25743fac86 | 11 | |
mnikolaos | 8:96112047d7f3 | 12 | Ticker tick; |
mnikolaos | 7:5d25743fac86 | 13 | |
mnikolaos | 7:5d25743fac86 | 14 | typedef struct EventTag { |
mnikolaos | 7:5d25743fac86 | 15 | uint16_t sig; /* signal of the event */ |
mnikolaos | 7:5d25743fac86 | 16 | } Event; |
mnikolaos | 7:5d25743fac86 | 17 | |
mnikolaos | 7:5d25743fac86 | 18 | struct StateTableTag; /* forward declaration */ |
mnikolaos | 7:5d25743fac86 | 19 | |
mnikolaos | 7:5d25743fac86 | 20 | typedef void (*Tran)(struct StateTableTag *me, Event const *e); |
mnikolaos | 7:5d25743fac86 | 21 | |
mnikolaos | 7:5d25743fac86 | 22 | typedef struct StateTableTag { |
mnikolaos | 7:5d25743fac86 | 23 | uint8_t state; /* the current active state */ |
mnikolaos | 7:5d25743fac86 | 24 | uint8_t *st_desc; /* the current active state Name Descriprion */ |
mnikolaos | 7:5d25743fac86 | 25 | Tran const *state_table; /* the State-Table */ |
mnikolaos | 7:5d25743fac86 | 26 | uint8_t n_states; /* number of states */ |
mnikolaos | 7:5d25743fac86 | 27 | uint8_t n_signals; /* number of signals */ |
mnikolaos | 7:5d25743fac86 | 28 | Tran initial; /* the initial transition */ |
mnikolaos | 7:5d25743fac86 | 29 | } StateTable; |
mnikolaos | 7:5d25743fac86 | 30 | |
mnikolaos | 7:5d25743fac86 | 31 | void StTbl_ctor(StateTable *me, |
mnikolaos | 7:5d25743fac86 | 32 | Tran const *table, |
mnikolaos | 7:5d25743fac86 | 33 | uint8_t n_states, |
mnikolaos | 7:5d25743fac86 | 34 | uint8_t n_signals, |
mnikolaos | 7:5d25743fac86 | 35 | Tran initial); |
mnikolaos | 7:5d25743fac86 | 36 | |
mnikolaos | 7:5d25743fac86 | 37 | void StTbl_init(StateTable *me); |
mnikolaos | 7:5d25743fac86 | 38 | void StTbl_dispatch(StateTable *me, Event const *e); |
mnikolaos | 7:5d25743fac86 | 39 | void StTbl_empty(StateTable *me, Event const *e); |
mnikolaos | 7:5d25743fac86 | 40 | |
mnikolaos | 7:5d25743fac86 | 41 | /* macro for taking a state transition inside a transition function */ |
mnikolaos | 7:5d25743fac86 | 42 | #define TRAN(target_) (((StateTable *)me)->state = (uint8_t)(target_)) |
mnikolaos | 7:5d25743fac86 | 43 | |
mnikolaos | 7:5d25743fac86 | 44 | void StTbl_ctor(StateTable *me, |
mnikolaos | 7:5d25743fac86 | 45 | Tran const *table, uint8_t n_states, uint8_t n_signals, |
mnikolaos | 7:5d25743fac86 | 46 | Tran initial) |
mnikolaos | 7:5d25743fac86 | 47 | { |
mnikolaos | 7:5d25743fac86 | 48 | me->state_table = table; |
mnikolaos | 7:5d25743fac86 | 49 | me->n_states = n_states; |
mnikolaos | 7:5d25743fac86 | 50 | me->n_signals = n_signals; |
mnikolaos | 7:5d25743fac86 | 51 | me->initial = initial; |
mnikolaos | 7:5d25743fac86 | 52 | } |
mnikolaos | 7:5d25743fac86 | 53 | |
mnikolaos | 7:5d25743fac86 | 54 | void StTbl_init(StateTable *me) { |
mnikolaos | 7:5d25743fac86 | 55 | me->state = me->n_states; |
mnikolaos | 7:5d25743fac86 | 56 | (*me->initial)(me, (Event *)0); |
mnikolaos | 7:5d25743fac86 | 57 | } |
mnikolaos | 7:5d25743fac86 | 58 | |
mnikolaos | 7:5d25743fac86 | 59 | void StTbl_dispatch(StateTable *me, Event const *e) { |
mnikolaos | 7:5d25743fac86 | 60 | Tran t; |
mnikolaos | 7:5d25743fac86 | 61 | |
mnikolaos | 7:5d25743fac86 | 62 | t = me->state_table[me->state*me->n_signals + e->sig]; |
mnikolaos | 7:5d25743fac86 | 63 | (*t)(me, e); |
mnikolaos | 7:5d25743fac86 | 64 | } |
dan | 0:7dec7e9ac085 | 65 | |
mnikolaos | 7:5d25743fac86 | 66 | void StTbl_empty(StateTable *me, Event const *e) { |
mnikolaos | 7:5d25743fac86 | 67 | (void)me; |
mnikolaos | 7:5d25743fac86 | 68 | (void)e; |
mnikolaos | 7:5d25743fac86 | 69 | } |
mnikolaos | 7:5d25743fac86 | 70 | |
mnikolaos | 7:5d25743fac86 | 71 | typedef struct Tag { /* FSM */ |
mnikolaos | 7:5d25743fac86 | 72 | StateTable super; /* derive from the StateTable structure */ |
mnikolaos | 7:5d25743fac86 | 73 | uint8_t timeout; /* number of seconds till exp */ |
mnikolaos | 7:5d25743fac86 | 74 | uint8_t defuse; /* secret defuse code to disarm */ |
mnikolaos | 7:5d25743fac86 | 75 | uint8_t code; /* currently entered code to disarm */ |
mnikolaos | 7:5d25743fac86 | 76 | } FSM1; |
mnikolaos | 7:5d25743fac86 | 77 | |
mnikolaos | 7:5d25743fac86 | 78 | |
mnikolaos | 7:5d25743fac86 | 79 | enum FSMSignals { /* all signals for the Bomb FSM */ |
mnikolaos | 7:5d25743fac86 | 80 | GO_SIG, |
mnikolaos | 7:5d25743fac86 | 81 | OUT_SIG, |
mnikolaos | 7:5d25743fac86 | 82 | TICK_SIG, |
mnikolaos | 7:5d25743fac86 | 83 | ERR_SIG, |
mnikolaos | 7:5d25743fac86 | 84 | CONF_SIG, |
mnikolaos | 7:5d25743fac86 | 85 | TRANADM_SIG, |
mnikolaos | 7:5d25743fac86 | 86 | |
mnikolaos | 7:5d25743fac86 | 87 | MAX_SIG /* the number of signals */ |
mnikolaos | 7:5d25743fac86 | 88 | }; |
mnikolaos | 7:5d25743fac86 | 89 | |
mnikolaos | 7:5d25743fac86 | 90 | enum FSMStates { |
mnikolaos | 7:5d25743fac86 | 91 | SETTING_FSM_STATE, /* Setting for FSM */ |
mnikolaos | 7:5d25743fac86 | 92 | READY_FSM_STATE, /* Unassigned and Ready to be configured from PC */ |
mnikolaos | 7:5d25743fac86 | 93 | ASSIGNED_FSM_STATE, /* Assigned to a user. None could use it apart from him.*/ |
mnikolaos | 7:5d25743fac86 | 94 | LOCKED_FSM_STATE, /* Locked by user*/ |
mnikolaos | 7:5d25743fac86 | 95 | ERROR_STATE, /* Error state. Only logging communication*/ |
mnikolaos | 7:5d25743fac86 | 96 | |
mnikolaos | 7:5d25743fac86 | 97 | MAX_STATE |
mnikolaos | 7:5d25743fac86 | 98 | }; |
mnikolaos | 7:5d25743fac86 | 99 | |
mnikolaos | 7:5d25743fac86 | 100 | typedef struct TickEvtTag { |
mnikolaos | 7:5d25743fac86 | 101 | Event super; /* derive from the Event structure */ |
mnikolaos | 7:5d25743fac86 | 102 | uint8_t fine_time; /* the fine 1/10 s counter */ |
mnikolaos | 7:5d25743fac86 | 103 | } TickEvt; |
mnikolaos | 7:5d25743fac86 | 104 | |
mnikolaos | 7:5d25743fac86 | 105 | |
mnikolaos | 7:5d25743fac86 | 106 | static void FSM_initial (FSM1 *me, Event const *e) |
mnikolaos | 7:5d25743fac86 | 107 | { |
mnikolaos | 7:5d25743fac86 | 108 | (void)e; |
mnikolaos | 7:5d25743fac86 | 109 | |
mnikolaos | 8:96112047d7f3 | 110 | pc.printf("State --> Initial\n"); |
mnikolaos | 7:5d25743fac86 | 111 | TRAN(SETTING_FSM_STATE); |
mnikolaos | 7:5d25743fac86 | 112 | } |
mnikolaos | 7:5d25743fac86 | 113 | |
mnikolaos | 7:5d25743fac86 | 114 | static void FSM_setting_GO (FSM1 *me, Event const *e) |
mnikolaos | 7:5d25743fac86 | 115 | { |
mnikolaos | 7:5d25743fac86 | 116 | (void)e; |
mnikolaos | 8:96112047d7f3 | 117 | pc.printf("State --> Setting - GO Event was caught\n"); |
mnikolaos | 7:5d25743fac86 | 118 | TRAN(READY_FSM_STATE); |
mnikolaos | 7:5d25743fac86 | 119 | } |
mnikolaos | 7:5d25743fac86 | 120 | |
mnikolaos | 7:5d25743fac86 | 121 | static void FSM_setting_OUT (FSM1 *me, Event const *e) |
mnikolaos | 7:5d25743fac86 | 122 | { |
mnikolaos | 7:5d25743fac86 | 123 | (void)e; |
mnikolaos | 8:96112047d7f3 | 124 | pc.printf("State --> Setting - OUT Event was caught\n"); |
mnikolaos | 7:5d25743fac86 | 125 | } |
mnikolaos | 7:5d25743fac86 | 126 | |
mnikolaos | 7:5d25743fac86 | 127 | static void FSM_setting_ERR (FSM1 *me, Event const *e) |
mnikolaos | 7:5d25743fac86 | 128 | { |
mnikolaos | 7:5d25743fac86 | 129 | (void)e; |
mnikolaos | 8:96112047d7f3 | 130 | pc.printf("State --> Setting - ERR Event was caught\n"); |
mnikolaos | 7:5d25743fac86 | 131 | TRAN(ERROR_STATE); |
mnikolaos | 7:5d25743fac86 | 132 | } |
mnikolaos | 7:5d25743fac86 | 133 | |
mnikolaos | 7:5d25743fac86 | 134 | static void FSM_setting_TRAN (FSM1 *me, Event const *e) |
mnikolaos | 7:5d25743fac86 | 135 | { |
mnikolaos | 7:5d25743fac86 | 136 | (void)e; |
mnikolaos | 8:96112047d7f3 | 137 | pc.printf("State --> Setting - TRAN ADMIN Event was caught\n"); |
mnikolaos | 7:5d25743fac86 | 138 | } |
mnikolaos | 7:5d25743fac86 | 139 | |
mnikolaos | 7:5d25743fac86 | 140 | static void FSM_ready_GO (FSM1 *me, Event const *e) |
mnikolaos | 7:5d25743fac86 | 141 | { |
mnikolaos | 7:5d25743fac86 | 142 | (void)e; |
mnikolaos | 8:96112047d7f3 | 143 | pc.printf("State --> Ready - GO Event was caught\n"); |
mnikolaos | 7:5d25743fac86 | 144 | TRAN(ASSIGNED_FSM_STATE); |
mnikolaos | 7:5d25743fac86 | 145 | } |
mnikolaos | 7:5d25743fac86 | 146 | |
mnikolaos | 7:5d25743fac86 | 147 | static void FSM_ready_OUT (FSM1 *me, Event const *e) |
mnikolaos | 7:5d25743fac86 | 148 | { |
mnikolaos | 7:5d25743fac86 | 149 | (void)e; |
mnikolaos | 8:96112047d7f3 | 150 | pc.printf("State --> Ready - OUT Event was caught\n"); |
mnikolaos | 7:5d25743fac86 | 151 | } |
dan | 0:7dec7e9ac085 | 152 | |
mnikolaos | 7:5d25743fac86 | 153 | static void FSM_ready_TICK (FSM1 *me, Event const *e) |
mnikolaos | 7:5d25743fac86 | 154 | { |
mnikolaos | 7:5d25743fac86 | 155 | (void)e; |
mnikolaos | 8:96112047d7f3 | 156 | pc.printf("State --> Ready - TICK Event was caught\n"); |
mnikolaos | 7:5d25743fac86 | 157 | } |
mnikolaos | 7:5d25743fac86 | 158 | |
mnikolaos | 7:5d25743fac86 | 159 | static void FSM_ready_ERR (FSM1 *me, Event const *e) |
mnikolaos | 7:5d25743fac86 | 160 | { |
mnikolaos | 7:5d25743fac86 | 161 | (void)e; |
mnikolaos | 8:96112047d7f3 | 162 | pc.printf("State --> Ready - ERR Event was caught\n"); |
mnikolaos | 7:5d25743fac86 | 163 | TRAN(ERROR_STATE); |
mnikolaos | 7:5d25743fac86 | 164 | |
mnikolaos | 7:5d25743fac86 | 165 | } |
mnikolaos | 7:5d25743fac86 | 166 | |
mnikolaos | 7:5d25743fac86 | 167 | static void FSM_ready_CONF (FSM1 *me, Event const *e) |
mnikolaos | 7:5d25743fac86 | 168 | { |
mnikolaos | 7:5d25743fac86 | 169 | (void)e; |
mnikolaos | 8:96112047d7f3 | 170 | pc.printf("State --> Ready - CONF Event was caught\n"); |
mnikolaos | 7:5d25743fac86 | 171 | TRAN(SETTING_FSM_STATE); |
mnikolaos | 7:5d25743fac86 | 172 | |
mnikolaos | 7:5d25743fac86 | 173 | } |
mnikolaos | 7:5d25743fac86 | 174 | |
mnikolaos | 7:5d25743fac86 | 175 | static void FSM_ready_TRAN (FSM1 *me, Event const *e) |
mnikolaos | 7:5d25743fac86 | 176 | { |
mnikolaos | 7:5d25743fac86 | 177 | (void)e; |
mnikolaos | 8:96112047d7f3 | 178 | pc.printf("State --> Ready - TRAN ADMIN Event was caught\n"); |
mnikolaos | 7:5d25743fac86 | 179 | } |
mnikolaos | 7:5d25743fac86 | 180 | |
mnikolaos | 7:5d25743fac86 | 181 | static void FSM_assigned_GO (FSM1 *me, Event const *e) |
mnikolaos | 7:5d25743fac86 | 182 | { |
mnikolaos | 7:5d25743fac86 | 183 | (void)e; |
mnikolaos | 8:96112047d7f3 | 184 | pc.printf("State --> Assigned - GO Event was caught\n"); |
mnikolaos | 7:5d25743fac86 | 185 | TRAN(LOCKED_FSM_STATE); |
mnikolaos | 7:5d25743fac86 | 186 | } |
mnikolaos | 7:5d25743fac86 | 187 | |
mnikolaos | 7:5d25743fac86 | 188 | static void FSM_assigned_OUT (FSM1 *me, Event const *e) |
mnikolaos | 7:5d25743fac86 | 189 | { |
mnikolaos | 7:5d25743fac86 | 190 | (void)e; |
mnikolaos | 8:96112047d7f3 | 191 | pc.printf("State --> Assigned - OUT Event was caught\n"); |
mnikolaos | 7:5d25743fac86 | 192 | TRAN(READY_FSM_STATE); |
mnikolaos | 7:5d25743fac86 | 193 | } |
mnikolaos | 7:5d25743fac86 | 194 | |
mnikolaos | 7:5d25743fac86 | 195 | static void FSM_assigned_TICK (FSM1 *me, Event const *e) |
mnikolaos | 7:5d25743fac86 | 196 | { |
mnikolaos | 7:5d25743fac86 | 197 | (void)e; |
mnikolaos | 8:96112047d7f3 | 198 | pc.printf("State --> Assigned - TICK Event was caught\n"); |
mnikolaos | 7:5d25743fac86 | 199 | |
mnikolaos | 7:5d25743fac86 | 200 | } |
mnikolaos | 7:5d25743fac86 | 201 | |
mnikolaos | 7:5d25743fac86 | 202 | static void FSM_assigned_ERR (FSM1 *me, Event const *e) |
mnikolaos | 7:5d25743fac86 | 203 | { |
mnikolaos | 7:5d25743fac86 | 204 | (void)e; |
mnikolaos | 8:96112047d7f3 | 205 | pc.printf("State --> Assigned - ERR Event was caught\n"); |
mnikolaos | 7:5d25743fac86 | 206 | TRAN(ERROR_STATE); |
mnikolaos | 7:5d25743fac86 | 207 | |
mnikolaos | 7:5d25743fac86 | 208 | } |
mnikolaos | 7:5d25743fac86 | 209 | |
mnikolaos | 7:5d25743fac86 | 210 | static void FSM_assigned_CONF (FSM1 *me, Event const *e) |
mnikolaos | 7:5d25743fac86 | 211 | { |
mnikolaos | 7:5d25743fac86 | 212 | (void)e; |
mnikolaos | 8:96112047d7f3 | 213 | pc.printf("State --> Assigned - CONF Event was caught\n"); |
mnikolaos | 7:5d25743fac86 | 214 | TRAN(SETTING_FSM_STATE); |
mnikolaos | 7:5d25743fac86 | 215 | |
mnikolaos | 7:5d25743fac86 | 216 | } |
mnikolaos | 7:5d25743fac86 | 217 | |
mnikolaos | 7:5d25743fac86 | 218 | static void FSM_assigned_TRAN (FSM1 *me, Event const *e) |
mnikolaos | 7:5d25743fac86 | 219 | { |
mnikolaos | 7:5d25743fac86 | 220 | (void)e; |
mnikolaos | 8:96112047d7f3 | 221 | pc.printf("State --> Assigned - TRAN ADMIN Event was caught\n"); |
mnikolaos | 7:5d25743fac86 | 222 | |
mnikolaos | 7:5d25743fac86 | 223 | } |
mnikolaos | 7:5d25743fac86 | 224 | |
mnikolaos | 7:5d25743fac86 | 225 | static void FSM_locked_GO (FSM1 *me, Event const *e) |
mnikolaos | 7:5d25743fac86 | 226 | { |
mnikolaos | 7:5d25743fac86 | 227 | (void)e; |
mnikolaos | 8:96112047d7f3 | 228 | pc.printf("State --> Locked - GO Event was caught\n"); |
mnikolaos | 7:5d25743fac86 | 229 | |
mnikolaos | 7:5d25743fac86 | 230 | } |
mnikolaos | 7:5d25743fac86 | 231 | |
mnikolaos | 7:5d25743fac86 | 232 | static void FSM_locked_OUT (FSM1 *me, Event const *e) |
mnikolaos | 7:5d25743fac86 | 233 | { |
mnikolaos | 7:5d25743fac86 | 234 | (void)e; |
mnikolaos | 8:96112047d7f3 | 235 | pc.printf("State --> Locked - OUT Event was caught\n"); |
mnikolaos | 7:5d25743fac86 | 236 | TRAN(ASSIGNED_FSM_STATE); |
mnikolaos | 7:5d25743fac86 | 237 | |
mnikolaos | 7:5d25743fac86 | 238 | } |
mnikolaos | 7:5d25743fac86 | 239 | |
mnikolaos | 7:5d25743fac86 | 240 | static void FSM_locked_TICK (FSM1 *me, Event const *e) |
mnikolaos | 7:5d25743fac86 | 241 | { |
mnikolaos | 7:5d25743fac86 | 242 | (void)e; |
mnikolaos | 8:96112047d7f3 | 243 | pc.printf("State --> Locked - TICK Event was caught\n"); |
mnikolaos | 7:5d25743fac86 | 244 | |
mnikolaos | 7:5d25743fac86 | 245 | } |
mnikolaos | 7:5d25743fac86 | 246 | |
mnikolaos | 7:5d25743fac86 | 247 | static void FSM_locked_ERR (FSM1 *me, Event const *e) |
mnikolaos | 7:5d25743fac86 | 248 | { |
mnikolaos | 7:5d25743fac86 | 249 | (void)e; |
mnikolaos | 8:96112047d7f3 | 250 | pc.printf("State --> Locked - ERR Event was caught\n"); |
mnikolaos | 7:5d25743fac86 | 251 | TRAN(ERROR_STATE); |
mnikolaos | 7:5d25743fac86 | 252 | |
mnikolaos | 7:5d25743fac86 | 253 | } |
mnikolaos | 7:5d25743fac86 | 254 | |
mnikolaos | 7:5d25743fac86 | 255 | static void FSM_locked_CONF (FSM1 *me, Event const *e) |
mnikolaos | 7:5d25743fac86 | 256 | { |
mnikolaos | 7:5d25743fac86 | 257 | (void)e; |
mnikolaos | 8:96112047d7f3 | 258 | pc.printf("State --> Locked - CONF Event was caught\n"); |
mnikolaos | 7:5d25743fac86 | 259 | TRAN(SETTING_FSM_STATE); |
mnikolaos | 7:5d25743fac86 | 260 | |
mnikolaos | 7:5d25743fac86 | 261 | } |
mnikolaos | 7:5d25743fac86 | 262 | |
mnikolaos | 7:5d25743fac86 | 263 | static void FSM_locked_TRAN (FSM1 *me, Event const *e) |
mnikolaos | 7:5d25743fac86 | 264 | { |
mnikolaos | 7:5d25743fac86 | 265 | (void)e; |
mnikolaos | 8:96112047d7f3 | 266 | pc.printf("State --> Locked - TRAN ADMIN Event was caught\n"); |
mnikolaos | 7:5d25743fac86 | 267 | |
mnikolaos | 7:5d25743fac86 | 268 | } |
mnikolaos | 7:5d25743fac86 | 269 | |
mnikolaos | 7:5d25743fac86 | 270 | |
mnikolaos | 7:5d25743fac86 | 271 | void FSM_ctor(FSM1 *me, uint8_t defuse) { |
mnikolaos | 7:5d25743fac86 | 272 | |
mnikolaos | 7:5d25743fac86 | 273 | static const Tran fsm_state_table[MAX_STATE][MAX_SIG] = |
mnikolaos | 7:5d25743fac86 | 274 | { |
mnikolaos | 7:5d25743fac86 | 275 | {(Tran)&FSM_setting_GO, (Tran)&FSM_setting_OUT, &StTbl_empty, (Tran)&FSM_setting_ERR, &StTbl_empty, (Tran)&FSM_setting_TRAN }, |
mnikolaos | 7:5d25743fac86 | 276 | {(Tran)&FSM_ready_GO, (Tran)&FSM_ready_OUT, (Tran)&FSM_ready_TICK, (Tran)&FSM_ready_ERR, (Tran)&FSM_ready_CONF, (Tran)&FSM_ready_TRAN }, |
mnikolaos | 7:5d25743fac86 | 277 | {(Tran)&FSM_assigned_GO, (Tran)&FSM_assigned_OUT, (Tran)&FSM_assigned_TICK, (Tran)&FSM_assigned_ERR, (Tran)&FSM_assigned_CONF, (Tran)&FSM_assigned_TRAN }, |
mnikolaos | 7:5d25743fac86 | 278 | {(Tran)&FSM_locked_GO, (Tran)&FSM_locked_OUT, (Tran)&FSM_locked_TICK, (Tran)&FSM_locked_ERR, (Tran)&FSM_locked_CONF, (Tran)&FSM_locked_TRAN } |
mnikolaos | 7:5d25743fac86 | 279 | }; |
mnikolaos | 7:5d25743fac86 | 280 | |
mnikolaos | 7:5d25743fac86 | 281 | StTbl_ctor(&me->super, &fsm_state_table[0][0], MAX_STATE, MAX_SIG, (Tran)&FSM_initial); |
mnikolaos | 7:5d25743fac86 | 282 | |
mnikolaos | 7:5d25743fac86 | 283 | me->defuse = defuse; |
mnikolaos | 7:5d25743fac86 | 284 | } |
mnikolaos | 8:96112047d7f3 | 285 | |
mnikolaos | 8:96112047d7f3 | 286 | bool _t1_OVRFL = false; |
mnikolaos | 8:96112047d7f3 | 287 | // ISR Timer per 1sec |
mnikolaos | 8:96112047d7f3 | 288 | void ISR(void) { |
mnikolaos | 8:96112047d7f3 | 289 | _t1_OVRFL = true; |
mnikolaos | 8:96112047d7f3 | 290 | } |
mnikolaos | 8:96112047d7f3 | 291 | |
mnikolaos | 8:96112047d7f3 | 292 | char cmd; |
mnikolaos | 8:96112047d7f3 | 293 | bool _pc_GTCMD = false; |
mnikolaos | 8:96112047d7f3 | 294 | // ISR when Gets command |
mnikolaos | 8:96112047d7f3 | 295 | void Serial_ISR(void) { |
mnikolaos | 8:96112047d7f3 | 296 | _pc_GTCMD = true; |
mnikolaos | 8:96112047d7f3 | 297 | cmd = pc.getc(); //Gets the command |
mnikolaos | 8:96112047d7f3 | 298 | } |
mnikolaos | 8:96112047d7f3 | 299 | |
mnikolaos | 7:5d25743fac86 | 300 | int main() |
mnikolaos | 7:5d25743fac86 | 301 | { |
mnikolaos | 7:5d25743fac86 | 302 | |
mnikolaos | 8:96112047d7f3 | 303 | pc.baud(9600); |
mnikolaos | 8:96112047d7f3 | 304 | pc.attach(&Serial_ISR); |
mnikolaos | 8:96112047d7f3 | 305 | tick.attach(&ISR, 1.0); |
mnikolaos | 8:96112047d7f3 | 306 | |
mnikolaos | 7:5d25743fac86 | 307 | static FSM1 _frdm_fsm; |
mnikolaos | 7:5d25743fac86 | 308 | static Event const go_evt = { GO_SIG }; |
mnikolaos | 7:5d25743fac86 | 309 | static Event const out_evt = { OUT_SIG }; |
mnikolaos | 7:5d25743fac86 | 310 | static Event const err_evt = { ERR_SIG }; |
mnikolaos | 7:5d25743fac86 | 311 | static Event const conf_evt = { CONF_SIG }; |
mnikolaos | 7:5d25743fac86 | 312 | static TickEvt tick_evt = { TICK_SIG, 0}; |
mnikolaos | 7:5d25743fac86 | 313 | |
mnikolaos | 7:5d25743fac86 | 314 | FSM_ctor(&_frdm_fsm, 0xFF); |
mnikolaos | 7:5d25743fac86 | 315 | |
mnikolaos | 7:5d25743fac86 | 316 | StTbl_init((StateTable *)&_frdm_fsm); |
mnikolaos | 7:5d25743fac86 | 317 | |
mnikolaos | 7:5d25743fac86 | 318 | pc.printf("Frdm K64F FSM App\r\n"); |
mnikolaos | 8:96112047d7f3 | 319 | Event const *e = (Event *)0; |
mnikolaos | 8:96112047d7f3 | 320 | |
mnikolaos | 7:5d25743fac86 | 321 | while (true) { |
mnikolaos | 8:96112047d7f3 | 322 | |
mnikolaos | 8:96112047d7f3 | 323 | pc.printf("Running\r\n\n"); |
mnikolaos | 8:96112047d7f3 | 324 | gpo1 = !gpo1; // toggle pin |
mnikolaos | 8:96112047d7f3 | 325 | ledB = !ledB; // toggle led |
mnikolaos | 7:5d25743fac86 | 326 | wait(0.2f); |
mnikolaos | 8:96112047d7f3 | 327 | |
mnikolaos | 8:96112047d7f3 | 328 | if(_t1_OVRFL) |
mnikolaos | 8:96112047d7f3 | 329 | { |
mnikolaos | 8:96112047d7f3 | 330 | _t1_OVRFL = false; |
mnikolaos | 8:96112047d7f3 | 331 | StTbl_dispatch((StateTable *)&_frdm_fsm, (Event *)&tick_evt); |
mnikolaos | 8:96112047d7f3 | 332 | } |
mnikolaos | 8:96112047d7f3 | 333 | |
mnikolaos | 8:96112047d7f3 | 334 | switch (cmd) |
mnikolaos | 8:96112047d7f3 | 335 | { |
mnikolaos | 8:96112047d7f3 | 336 | case 'G': { |
mnikolaos | 8:96112047d7f3 | 337 | pc.printf(" Go received \n"); |
mnikolaos | 8:96112047d7f3 | 338 | e = &go_evt; |
mnikolaos | 8:96112047d7f3 | 339 | break; |
mnikolaos | 8:96112047d7f3 | 340 | } |
mnikolaos | 8:96112047d7f3 | 341 | case 'O': { |
mnikolaos | 8:96112047d7f3 | 342 | pc.printf(" Out Received \n"); |
mnikolaos | 8:96112047d7f3 | 343 | e = &out_evt; |
mnikolaos | 8:96112047d7f3 | 344 | break; |
mnikolaos | 8:96112047d7f3 | 345 | } |
mnikolaos | 8:96112047d7f3 | 346 | case 'E': { |
mnikolaos | 8:96112047d7f3 | 347 | pc.printf(" Error Received \n"); |
mnikolaos | 8:96112047d7f3 | 348 | e = &err_evt; |
mnikolaos | 8:96112047d7f3 | 349 | break; |
mnikolaos | 8:96112047d7f3 | 350 | } |
mnikolaos | 8:96112047d7f3 | 351 | case 'C': { |
mnikolaos | 8:96112047d7f3 | 352 | pc.printf(" Config Received \n"); |
mnikolaos | 8:96112047d7f3 | 353 | e = &conf_evt; |
mnikolaos | 8:96112047d7f3 | 354 | break; |
mnikolaos | 8:96112047d7f3 | 355 | } |
mnikolaos | 8:96112047d7f3 | 356 | } |
mnikolaos | 8:96112047d7f3 | 357 | if(_pc_GTCMD) |
mnikolaos | 8:96112047d7f3 | 358 | { |
mnikolaos | 8:96112047d7f3 | 359 | _pc_GTCMD = false; |
mnikolaos | 8:96112047d7f3 | 360 | if (e != (Event *)0) |
mnikolaos | 8:96112047d7f3 | 361 | { |
mnikolaos | 8:96112047d7f3 | 362 | StTbl_dispatch((StateTable *)&_frdm_fsm, e); |
mnikolaos | 8:96112047d7f3 | 363 | } |
mnikolaos | 8:96112047d7f3 | 364 | } |
mnikolaos | 8:96112047d7f3 | 365 | |
stevep | 4:81cea7a352b0 | 366 | } |
dan | 0:7dec7e9ac085 | 367 | } |
mnikolaos | 7:5d25743fac86 | 368 |