-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstate_machine.cpp
More file actions
37 lines (32 loc) · 973 Bytes
/
Copy pathstate_machine.cpp
File metadata and controls
37 lines (32 loc) · 973 Bytes
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
#include "state_machine.h"
StateMachineOutput state_machine_tick(StateMachine* state_machine, StateMachineInput input) {
auto now = input.now;
auto unit_difference = input.unit_difference;
StateMachineOutput output = {0, false};
switch (state_machine->state) {
case Idle:
if (unit_difference != 0) {
state_machine->state = PulseOn;
state_machine->next_tick = now + state_machine->on_time;
}
break;
case PulseOn:
if (state_machine->next_tick <= now) {
state_machine->state = PulseOff;
state_machine->next_tick = now + state_machine->off_time;
output.steps_increment = unit_difference;
}
output.activate_coils = true;
break;
case PulseOff:
if (unit_difference == 0) {
state_machine->state = Idle;
state_machine->next_tick = 0;
} else if (state_machine->next_tick <= now) {
state_machine->state = PulseOn;
state_machine->next_tick = now + state_machine->on_time;
}
break;
}
return output;
}