Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- typedef enum {
- OFF,
- ON,
- ESPRESSO_READY,
- STEAMER_READY,
- MAKE_ESPRESSO,
- MAKE_STEAM
- } coffee_machine_state;
- typedef struct {
- coffee_machine_state current_state;
- } coffee_machine;
- void switch_on(coffee_machine* FSM) {
- FSM->current_state = ON;
- }
- void switch_off(coffee_machine* FSM) {
- FSM->current_state = OFF;
- }
- void heat_up(coffee_machine* FSM) {
- FSM->current_state = ESPRESSO_READY;
- }
- void steam_on(coffee_machine* FSM) {
- FSM->current_state = STEAMER_READY;
- }
- void steam_off(coffee_machine* FSM) {
- FSM->current_state = ESPRESSO_READY;
- }
- void espresso_on(coffee_machine* FSM) {
- FSM->current_state = MAKE_ESPRESSO;
- }
- void espresso_off(coffee_machine* FSM) {
- FSM->current_state = ESPRESSO_READY;
- }
- void steam_knob_forward(coffee_machine* FSM) {
- FSM->current_state = MAKE_STEAM;
- }
- void steam_knob_backward(coffee_machine* FSM) {
- FSM->current_state = STEAMER_READY;
- }
- #define STATE_NUMBER 6
- int state_matrix[STATE_NUMBER][STATE_NUMBER] = {
- { 0, 1, 0, 0, 0, 0 },
- { 1, 0, 1, 0, 0, 0 },
- { 1, 0, 0, 1, 1, 0 },
- { 1, 0, 1, 0, 0, 1 },
- { 0, 0, 1, 0, 0, 0 },
- { 0, 0, 0, 1, 0, 0 }
- };
- typedef void (*action)(coffee_machine*);
- action action_matrix[STATE_NUMBER][STATE_NUMBER] = {
- { 0, switch_on, 0, 0, 0, 0 },
- { switch_off, 0, heat_up, 0, 0, 0 },
- { switch_off, 0, 0, steam_on, espresso_on, 0 },
- { switch_off, 0, steam_off, 0, 0, steam_knob_forward },
- { 0, 0, espresso_off, 0, 0, 0 },
- { 0, 0, 0, steam_knob_backward, 0, 0 }
- };
- int main(int argc, char **argv)
- {
- coffee_machine FSM;
- FSM.current_state = OFF;
- printf("State - %d\n", FSM.current_state);
- if (state_matrix[FSM.current_state][ON])
- action_matrix[FSM.current_state][ON](&FSM);
- printf("State - %d\n", FSM.current_state);
- if (state_matrix[FSM.current_state][ESPRESSO_READY])
- action_matrix[FSM.current_state][ESPRESSO_READY](&FSM);
- printf("State - %d\n", FSM.current_state);
- /*
- state_matrix can be represent the adjactive matrix
- for nooriented graph and used to get a set of actions
- needed to go from one state to another (using BFS)
- */
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement