-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuttons.cpp
140 lines (109 loc) · 2.89 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
#include "buttons.h"
#include "Arduino.h"
#include "pins.h"
/** Interface to Robodesk handset buttons
*/
unsigned latched = 0;
unsigned long latch_time = 0;
unsigned long last_action = 0;
// Display anything that changed since last time
void display_buttons(unsigned buttons, const char * msg ) {
static unsigned prev = 0;
if (msg[0] || prev!=buttons) {
Serial.print((buttons & HS1) ? " HS1" : " ---");
Serial.print((buttons & HS2) ? " HS2" : " ---");
switch (buttons) {
case UP: Serial.print(" UP "); break;
case DOWN: Serial.print(" DOWN "); break;
case SET: Serial.print(" SET "); break;
case NONE: Serial.print(" ---- "); break;
default: Serial.print(" ** UNKNOWN ** "); break;
}
Serial.println(msg);
}
prev = buttons;
}
bool is_latched() {
return !!latched;
}
unsigned get_latched() {
return latched;
}
void unlatch() {
pinMode(MOD_HS1, INPUT);
pinMode(MOD_HS2, INPUT);
}
void latch_pin(int pin)
{
#ifndef DISABLE_LATCHING
pinMode(pin, OUTPUT);
digitalWrite(pin, HIGH);
delay(1);
#endif
}
void break_latch() {
latched = 0;
unlatch();
}
void apply_latch() {
if (!latched) return;
unsigned long delta = millis() - last_action;
// Let go if latch timer expires
if (delta > latch_time) {
display_buttons(latched, "Timed out");
break_latch();
return;
}
// Re-assert latched buttons
if (latched & HS1) latch_pin(MOD_HS1);
if (latched & HS2) latch_pin(MOD_HS2);
}
void set_latch(unsigned latch_pins, unsigned long max_latch_time) {
if (latched == latch_pins) {
if (latch_time + max_latch_time > latch_time) {
latch_time += max_latch_time;
}
} else {
latched = latch_pins;
latch_time = max_latch_time;
}
// Restart idle timer when we get an interesting button
last_action = millis();
// Activate the selected latch pins
apply_latch();
char buf[50];
sprintf(buf, "Latched up to %lums", latch_time);
display_buttons(latched, buf);
}
unsigned read_buttons() {
unsigned buttons = 0;
unlatch();
if (digitalRead(MOD_HS1)) buttons |= HS1;
if (digitalRead(MOD_HS2)) buttons |= HS2;
apply_latch();
return buttons;
}
bool draining = false;
void clear_buttons() {
draining = true;
}
unsigned read_buttons_debounce() {
static unsigned long debounce = 0;
static unsigned prev_buttons = 0;
unsigned diff = prev_buttons;
prev_buttons = read_buttons();
// Wait for interface to drain
if (draining and prev_buttons) return NONE;
draining = false;
unsigned buttons = prev_buttons;
// Ignore spurious signals
if (diff && diff != prev_buttons) buttons = NONE;
// Reset timer if buttons are drifting or unpressed
if (!buttons) debounce = millis();
// ignore signal until stable for 1-2ms
if (millis()-debounce < 40) {
buttons = NONE;
}
display_buttons(buttons);
return buttons;
}