/
AnalogFiveButtons.cpp
228 lines (198 loc) · 5.94 KB
/
AnalogFiveButtons.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include "AnalogFiveButtons.h"
//#define A5B_STATE_DBG 1
//#define A5B_TIMING_DBG 1
const byte AnalogFiveButtons::buttons[] = { BM_1, BM_2, BM_3, BM_4, BM_5 };
AnalogFiveButtons::AnalogFiveButtons(uint8_t analogPin, float defaultAnalogRef) :
m_defaultAnalogRef(defaultAnalogRef), m_analogPin(analogPin),
m_currentStateIndex(0)
{
m_refVoltage = 5.0;
m_resistors[0] = 4700;
m_resistors[1] = 22100;
m_resistors[2] = 10000;
m_resistors[3] = 4700;
m_resistors[4] = 2100;
m_resistors[5] = 1200;
m_msSampling = 50;
m_debounceSamples = 2;
m_counter = m_debounceSamples;
m_states[0] = 0; // No button pressed
m_states[1] = 1; // B1 pressed
m_states[2] = 2; // B2 pressed
m_states[3] = 3; // B1 + B2 pressed
m_states[4] = 4; // B3 pressed
m_states[5] = 5; // B1 + B3 pressed
m_states[6] = 6; // B2 + B3 pressed
m_states[7] = 8; // B4 pressed
m_states[8] = 9; // B1 + B4 pressed
m_states[9] = 10; // B2 + B4 pressed
m_states[10] = 12; // B3 + B4 pressed
m_states[11] = 16; // B5 pressed
m_states[12] = 17; // B1 + B5 pressed
m_states[13] = 18; // B2 + B5 pressed
m_states[14] = 20; // B3 + B5 pressed
m_states[15] = 24; // B4 + B5 pressed
computeLadder();
}
void AnalogFiveButtons::setTiming(uint16_t msSampling, uint8_t debounceCount)
{
m_msSampling = msSampling;
m_debounceSamples = debounceCount;
m_counter = m_debounceSamples;
}
void AnalogFiveButtons::setLadder(float refVoltage, uint16_t *R)
{
m_refVoltage = refVoltage;
for (byte i=0; i<6; i++) {
m_resistors[i] = R[i];
}
computeLadder();
}
boolean AnalogFiveButtons::removeState(byte state)
{
for (byte i=0; i<16; i++) {
if ( m_states[i] == state ) {
m_ladder[i] = 2048;
return true;
}
}
return false;
}
void AnalogFiveButtons::computeLadder()
{
// we have:
// Req = 1 / ( B1/R1 + B2/R2 + B3/R3 + B4/R4 + B5/R5)
// notation: Bn/Rn means if Bn down, then 1/Rn, otherwise 0
// Vout = refVoltage * Req / ( Req + R0 )
float Req;
float Vout;
m_ladder[0] = (int16_t)( 1024.0f*(float)m_refVoltage/(float)m_defaultAnalogRef );
for (byte i=1; i<16; i++) {
Req = 1.0 / (
( BM_1 & m_states[i] ? 1.0/(float)m_resistors[1] : 0.0 ) +
( BM_2 & m_states[i] ? 1.0/(float)m_resistors[2] : 0.0 ) +
( BM_3 & m_states[i] ? 1.0/(float)m_resistors[3] : 0.0 ) +
( BM_4 & m_states[i] ? 1.0/(float)m_resistors[4] : 0.0 ) +
( BM_5 & m_states[i] ? 1.0/(float)m_resistors[5] : 0.0 )
);
Vout = (float)m_refVoltage*Req/(Req+(float)m_resistors[0]);
m_ladder[i] = (int16_t)( 1024.0f*(float)Vout/(float)m_defaultAnalogRef );
}
#ifdef A5B_STATE_DBG
for (byte i=0; i<16; i++) {
Serial.print("ladder[");
Serial.print(i, DEC);
Serial.print("] = ");
Serial.println(m_ladder[i]);
}
#endif
}
byte AnalogFiveButtons::computeState(int analogReading)
{
int error;
byte index = 0;
int minError = 1024;
#ifdef A5B_STATE_DBG
Serial.print("==== New reading: ");
Serial.println(analogReading, DEC);
#endif
for (byte i=0; i<16; i++) {
error = abs( m_ladder[i] - analogReading );
#ifdef A5B_STATE_DBG
Serial.print(i, DEC);
Serial.print(" : ");
Serial.print(m_ladder[i], DEC);
Serial.print(" -> ");
Serial.println(error, DEC);
#endif
if ( error < minError ) {
minError = error;
index = i;
}
}
#ifdef A5B_STATE_DBG
Serial.print(" -> index=");
Serial.print(index);
Serial.print(" => ");
Serial.print(m_states[index], DEC);
Serial.print(" = ");
Serial.println(m_states[index], BIN);
#endif
return index;
}
void AnalogFiveButtons::update()
{
static byte previousStateIndex = 0;
static int previousReading = 0;
static unsigned int previousSampleTime = 0;
#ifdef A5B_TIMING_DBG
static uint16_t measureCounter = 0;
#endif
static bool acquired = false;
// Get current time
unsigned long currentSampleTime = millis();
// Start to process only if enough time has elapsed
if ( currentSampleTime > (previousSampleTime+m_msSampling) ) {
// Read the current analog input pin
int currentReading = analogRead(m_analogPin);
// Check if the reading is not varying much from the previous
// We basically wait for the derivative of the voltage to fall
// under 2 bits of accuracy (assuming that the difference
// between any state is higher than 2 bits)
if ( abs(previousReading-currentReading) < 4 ) {
if ( !acquired ) {
m_counter--; // new stable reading
Serial.println(m_counter, DEC);
if ( m_counter == 0 ) {
// compute the new state
m_currentStateIndex = computeState(currentReading);
// remember that we have a new measurement
acquired = true;
// Detect the button going down
// if ( m_currentStateIndex != previousStateIndex ) {
// m_buttonPressed =
// ~(m_states[previousStateIndex]) & m_states[m_currentStateIndex];
// previousStateIndex = m_currentStateIndex;
// }
#ifdef A5B_TIMING_DBG
Serial.print("stable voltage achieved after: ");
Serial.println(measureCounter, DEC);
#endif
m_buttonPressed |= m_states[m_currentStateIndex];
}
}
#ifdef A5B_TIMING_DBG
measureCounter = 0;
#endif
} // if stable reading
else {
m_counter = m_debounceSamples;
acquired = false;
#ifdef A5B_TIMING_DBG
measureCounter++;
#endif
}
previousReading = currentReading;
previousSampleTime = currentSampleTime;
}
}
boolean AnalogFiveButtons::getState(byte button)
{
return ( button & m_states[m_currentStateIndex] );
}
boolean AnalogFiveButtons::buttonPressed(byte button)
{
return ( button & m_buttonPressed );
}
void AnalogFiveButtons::clearButton(byte button)
{
m_buttonPressed = m_buttonPressed & ~button;
}
byte AnalogFiveButtons::getState()
{
return m_states[m_currentStateIndex];
}
byte AnalogFiveButtons::getPressedState()
{
return m_buttonPressed;
}