-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlamp.c
54 lines (47 loc) · 1.26 KB
/
lamp.c
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
#include <stdio.h>
typedef enum {
LAMP_OFF = 1,
LAMP_ON
} state_t;
typedef enum {
TOGGLE_ON = 1,
TOGGLE_OFF
} event_t;
state_t state;
void statemachine(event_t ev){
printf("State: %u, event: %u\n", state, ev);
// @STATEMACHINE(lamp)
switch(state){
case LAMP_OFF:
switch(ev){
case TOGGLE_ON:
// @ACTION(Turning lamp on)
printf("Turning lamp on\n");
state = LAMP_ON;
break;
case TOGGLE_OFF:
// @ACTION(Ignoring toggle off)
printf("Ignoring toggle off\n");
break;
}
break;
case LAMP_ON:
switch(ev){
case TOGGLE_ON:
// @ACTION(Ignoring toggle on)
printf("Ignoring toggle on\n");
break;
case TOGGLE_OFF:
// @ACTION(Turning lamp off)
printf("Turning lamp off\n");
state = LAMP_OFF;
break;
}
}
}
int main(void){
state = LAMP_OFF;
statemachine( TOGGLE_ON );
statemachine( TOGGLE_ON );
statemachine( TOGGLE_OFF );
}