I am originally a C-developer and I am now getting familiar with DML. I often need to write state machines and I always design them very C-stylish. I suspect that I am not utilizing the DML language to its full extent. Could someone port this traffic light C-implementation to modern DML?
enum TRAFFIC_LIGHT {
RED,
YELLOW,
GREEN,
}
int state;
timer_t timer;
int redTimeMs = 15000;
int greenTimeMs = 10000;
int transitionTimeMs = 1000;
void start_transition_to_green() {
start_timer(transitionTimeMs, tick_to_green);
}
void start_transition_to_red() {
start_timer(transitionTimeMs, tick_to_red);
}
void tick_to_green() {
switch (state) {
case RED:
state = YELLOW;
start_timer(transitionTimeMs, tick_to_green);
break;
case YELLOW:
state = GREEN;
start_timer(transitionTimeMs, tick_to_green);
break;
case GREEN:
start_timer(greenTimeMs, start_transition_to_red);
break;
}
}
void tick_to_red() {
switch (state) {
case GREEN:
state = YELLOW;
start_timer(transitionTimeMs, tick_to_red);
break;
case YELLOW:
state = RED;
start_timer(transitionTimeMs, tick_to_red);
break;
case RED:
start_timer(redTimeMs, start_transition_to_green);
break;
}
}
void main(void) {
state = RED;
start_transition_to_green();
}
I am expecting a DML implementation that is checkpointable.