forked from jamesfowkes/cryptology-rgb-puzzle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buttons.cpp
145 lines (120 loc) · 3.9 KB
/
buttons.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* ADL Includes */
#include "raat.h"
#include "binary-output.h"
#include "raat-debouncer.h"
#include "raat-oneshot-timer.h"
#include "raat-oneshot-task.h"
#include "raat-task.h"
/* Application Includes */
#include "buttons.h"
#include "application.h"
/* Defines and Typedefs */
static const uint8_t HC4067_PIN = 7;
static const uint8_t HC4067_POWER_PIN = 6;
static const uint8_t HC4067_GND_PIN = 12;
static const uint8_t N_RGB_BUTTONS = 15;
static const uint8_t N_BUTTONS = N_RGB_BUTTONS+1;
static const uint8_t MAX_BUTTON_INDEX = N_BUTTONS-1;
static const uint8_t RESET_BOUNCER_INDEX = MAX_BUTTON_INDEX;
/* Local Classes */
class HC4067Reader : public DebounceReader
{
public:
bool read() { return digitalRead(HC4067_PIN) == LOW;}
};
/* Local Objects and Variables */
static uint8_t s_current_button = 0;
static uint8_t s_levels[N_RGB_BUTTONS] = {0};
static BinaryOutput * s_pButtonSelect;
static HC4067Reader s_HC4067Reader;
static RAATDebouncer s_debouncers[N_RGB_BUTTONS+1] = {
RAATDebouncer(s_HC4067Reader, 1),
RAATDebouncer(s_HC4067Reader, 1),
RAATDebouncer(s_HC4067Reader, 1),
RAATDebouncer(s_HC4067Reader, 1),
RAATDebouncer(s_HC4067Reader, 1),
RAATDebouncer(s_HC4067Reader, 1),
RAATDebouncer(s_HC4067Reader, 1),
RAATDebouncer(s_HC4067Reader, 1),
RAATDebouncer(s_HC4067Reader, 1),
RAATDebouncer(s_HC4067Reader, 1),
RAATDebouncer(s_HC4067Reader, 1),
RAATDebouncer(s_HC4067Reader, 1),
RAATDebouncer(s_HC4067Reader, 1),
RAATDebouncer(s_HC4067Reader, 1),
RAATDebouncer(s_HC4067Reader, 1),
RAATDebouncer(s_HC4067Reader, 1)
};
static void debouncer_task_fn(RAATTask& pThisTask, void * pTaskData)
{
(void)pThisTask;
(void)pTaskData;
s_debouncers[s_current_button].tick();
s_current_button = s_current_button < N_RGB_BUTTONS ? (s_current_button + 1) : 0;
s_pButtonSelect->set(s_current_button);
}
static RAATTask debouncer_task(5, debouncer_task_fn, NULL);
/*static void debug_task_fn(RAATTask& pThisTask, void * pTaskData)
{
(void)pThisTask;
(void)pTaskData;
raat_logln(LOG_BUT, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d",
s_debouncers[0].count(),
s_debouncers[1].count(),
s_debouncers[2].count(),
s_debouncers[3].count(),
s_debouncers[4].count(),
s_debouncers[5].count(),
s_debouncers[6].count(),
s_debouncers[7].count(),
s_debouncers[8].count(),
s_debouncers[9].count(),
s_debouncers[10].count(),
s_debouncers[11].count(),
s_debouncers[12].count(),
s_debouncers[13].count(),
s_debouncers[14].count()
);
}
static RAATTask debug_task(500, debug_task_fn, NULL);*/
/* Public Functions */
void buttons_setup(BinaryOutput * pButtonSelect)
{
s_pButtonSelect = pButtonSelect;
s_pButtonSelect->set(0);
digitalWrite(HC4067_POWER_PIN, HIGH);
digitalWrite(HC4067_GND_PIN, LOW);
pinMode(HC4067_GND_PIN, OUTPUT);
pinMode(HC4067_POWER_PIN, OUTPUT);
pinMode(HC4067_PIN, INPUT_PULLUP);
}
void buttons_tick(int32_t fake_button, uint8_t max_level)
{
debouncer_task.run();
//debug_task.run();
for(uint8_t i=0; i<N_RGB_BUTTONS; i++)
{
if (!app_get_rgb_matched(i/3))
{
if ((fake_button == i) || s_debouncers[i].check_high_and_clear())
{
raat_logln(LOG_BUT, "Button %d pressed", i);
incrementwithrollover(s_levels[i], max_level);
}
}
}
if ((fake_button == RESET_BOUNCER_INDEX) || s_debouncers[RESET_BOUNCER_INDEX].check_high_and_clear())
{
app_reset_rgb();
}
}
uint8_t const * buttons_get_levels()
{
return s_levels;
}
void buttons_reset_level(uint8_t level)
{
s_levels[(level*3)+0] = 0;
s_levels[(level*3)+1] = 0;
s_levels[(level*3)+2] = 0;
}