forked from jamesfowkes/cryptology-esp32-lasers
-
Notifications
You must be signed in to change notification settings - Fork 0
/
laser-control.cpp
101 lines (91 loc) · 1.99 KB
/
laser-control.cpp
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <TaskAction.h>
#include "laser-control.h"
#include "io.h"
typedef enum _eLaserState
{
eLaserState_Off,
eLaserState_Powering,
eLaserState_On
} eLaserState;
static const unsigned long LASER_OFF_TIME = 800UL;
static const unsigned long LASER_POWERUP_TIME = 500UL;
static unsigned long s_laser_timeout = 0;
static eLaserState s_laser_state = eLaserState_Off;
static void laser_control_task_fn(TaskAction* this_task)
{
bool end_of_timeout = false;
if (s_laser_timeout)
{
s_laser_timeout -= 10;
end_of_timeout = (s_laser_timeout == 0);
}
switch(s_laser_state)
{
case eLaserState_Off:
if (end_of_timeout)
{
Serial.println("Laser off timeout expired");
Serial.println("Starting powerup timeout");
io_lasers_enable(true);
s_laser_state = eLaserState_Powering;
s_laser_timeout = LASER_POWERUP_TIME;
}
break;
case eLaserState_Powering:
if (end_of_timeout)
{
Serial.println("Laser powerup timeout expired");
s_laser_state = eLaserState_On;
s_laser_timeout = 0;
}
break;
case eLaserState_On:
break;
}
}
static TaskAction s_laser_control_task(laser_control_task_fn, 10, INFINITE_TICKS);
void laser_control_run()
{
s_laser_control_task.tick();
}
void laser_control_set_lasers(bool on)
{
io_lasers_enable(on);
if (on)
{
switch(s_laser_state)
{
case eLaserState_Off:
Serial.println("Starting powerup timeout");
s_laser_state = eLaserState_Powering;
s_laser_timeout = LASER_POWERUP_TIME;
break;
case eLaserState_Powering:
break;
case eLaserState_On:
break;
}
}
else
{
switch(s_laser_state)
{
case eLaserState_Off:
break;
case eLaserState_Powering:
Serial.println("Starting off timeout");
s_laser_state = eLaserState_Off;
s_laser_timeout = LASER_OFF_TIME;
break;
case eLaserState_On:
Serial.println("Starting off timeout");
s_laser_state = eLaserState_Off;
s_laser_timeout = LASER_OFF_TIME;
break;
}
}
}
bool laser_control_get_laser_onoff_state()
{
return s_laser_state == eLaserState_On;
}