-
Notifications
You must be signed in to change notification settings - Fork 0
/
Activator.cpp
63 lines (57 loc) · 1.25 KB
/
Activator.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
#include "Activator.h"
Activator::Activator(byte aim_pin, byte flashlight_pin, unsigned long timeout)
{
_aim_pin = aim_pin;
_flashlight_pin = flashlight_pin;
_timeout = timeout;
_state = ACTIVATOR_READY;
pinMode(_aim_pin, INPUT_PULLUP);
pinMode(_flashlight_pin, INPUT_PULLUP);
}
ActivatorState Activator::tick()
{
switch (_state) {
case ACTIVATOR_READY:
return whenReady();
break;
case ACTIVATOR_AIMING:
return whenAiming();
break;
case ACTIVATOR_FLASHLIGHT:
return whenFlashlight();
break;
case ACTIVATOR_MUTED:
return whenMuted();
break;
}
}
ActivatorState Activator::whenReady()
{
/* LOW == pressed, HIGH == not pressed; thanks, pullup! */
if (digitalRead(_aim_pin) == LOW) {
_state = ACTIVATOR_AIMING;
}
if (digitalRead(_flashlight_pin) == LOW) {
_state = ACTIVATOR_FLASHLIGHT;
}
return _state;
}
ActivatorState Activator::whenAiming()
{
_muteUntil = millis() + _timeout;
_state = ACTIVATOR_MUTED;
return _state;
}
ActivatorState Activator::whenFlashlight()
{
_muteUntil = millis() + _timeout;
_state = ACTIVATOR_MUTED;
return _state;
}
ActivatorState Activator::whenMuted()
{
if (millis() >= _muteUntil) {
_state = ACTIVATOR_READY;
}
return _state;
}