-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathio.cpp
92 lines (75 loc) · 1.93 KB
/
io.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
#include <TaskAction.h>
#include <Arduino.h>
#include "io.h"
#include "application.h"
#define SECURITY_ENABLE_PIN 15
#define LOCK_DISPLAY_CONTROL_PIN 0
#define MAGLOCK_CONTROL_PIN 4
#define LASER_CONTROL_PIN 16
static const int DEBOUNCE_COUNT = 5;
struct io_input
{
bool pressed;
bool just_pressed;
bool just_released;
int debounce;
};
typedef struct io_input IO_INPUT;
static IO_INPUT s_security_enable_input;
static void debounce_input(IO_INPUT& io_input, bool state)
{
io_input.just_pressed = false;
io_input.just_released = false;
if (state)
{
io_input.debounce = min(io_input.debounce+1, DEBOUNCE_COUNT);
}
else
{
io_input.debounce = max(io_input.debounce-1, 0);
}
if (io_input.debounce == DEBOUNCE_COUNT)
{
io_input.just_pressed = !io_input.pressed;
io_input.pressed = true;
}
else if (io_input.debounce == 0)
{
io_input.just_released = io_input.pressed;
io_input.pressed = false;
}
}
static void debounce_task_fn(TaskAction* this_task)
{
(void)this_task;
debounce_input(s_security_enable_input, digitalRead(SECURITY_ENABLE_PIN)==LOW);
if (s_security_enable_input.just_pressed)
{
Serial.println("Security switch pressed");
application_handle_security_switch_press();
}
}
static TaskAction s_debounce_task(debounce_task_fn, 10, INFINITE_TICKS);
void io_setup(void)
{
pinMode(LASER_CONTROL_PIN, OUTPUT);
pinMode(LOCK_DISPLAY_CONTROL_PIN, OUTPUT);
pinMode(MAGLOCK_CONTROL_PIN, OUTPUT);
pinMode(SECURITY_ENABLE_PIN, INPUT_PULLUP);
}
void io_loop()
{
s_debounce_task.tick();
}
void io_lasers_enable(bool enable)
{
digitalWrite(LASER_CONTROL_PIN, enable ? LOW: HIGH);
}
void io_set_locked_display(bool enable)
{
digitalWrite(LOCK_DISPLAY_CONTROL_PIN, enable ? HIGH : LOW);
}
void io_set_maglock(bool enable)
{
digitalWrite(MAGLOCK_CONTROL_PIN, enable ? HIGH : LOW);
}