-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHFButtons.cpp
More file actions
177 lines (146 loc) · 4.02 KB
/
HFButtons.cpp
File metadata and controls
177 lines (146 loc) · 4.02 KB
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
#include "HFButtons.h"
#include "wiring_private.h"
#include "pins_arduino.h"
/*************************************************************************
* Momentary push buttons
*/
// These buttons will return 'active' for exactly 1 polling cycle for each
// time they are pressed and released. The 'active' is returned as soon as
// the button is pressed, and will not be true again until the button has
// been released.
HF_button::HF_button(int pin)
{
pinMode(pin, INPUT_PULLUP);
_pin = pin;
_state = inactive;
}
//
// returns true after the button has been pressed
// AND then released
//
int HF_button::active()
{
if (_state == pressed)
{
_state = polled;
return true;
}
return false;
}
void HF_button::update()
{
int button_pressed = !digitalRead(_pin); // active low signal
switch (_state)
{
case inactive:
if (button_pressed)
_state = pressed;
break;
case pressed:
// do nothing- this state change happens in a different function
break;
case polled:
if (!button_pressed)
_state = unpressed;
break;
case unpressed:
_state = inactive;
}
}
/*************************************************************************
* Sliders (potentiometers)
*/
HF_slider::HF_slider(int pin)
{
// pinMode(pin, INPUT);
_pin = pin;
_min = 0;
_max = 1023;
_lastvalue = 512;
_mindelta = 5;
}
//
// returns true after the slider has changed enough past a threshold,
// to avoid edge conditions where it might oscillate between 2 values
//
int HF_slider::changed()
{
if (abs(_value - _lastvalue) > _mindelta)
{
_lastvalue = _value;
return true;
}
return false;
}
int HF_slider::value()
{
return _value;
}
/*************************************************************************
* Functions to read analog inputs without blocking
* Shamelessly adapted from the Arduino core.
*/
void HF_slider::start_read()
{
uint8_t pin = _pin;
const uint8_t analog_reference = DEFAULT;
#if defined(analogPinToChannel)
#if defined(__AVR_ATmega32U4__)
if (pin >= 18) pin -= 18; // allow for channel or pin numbers
#endif
pin = analogPinToChannel(pin);
#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
if (pin >= 54) pin -= 54; // allow for channel or pin numbers
#elif defined(__AVR_ATmega32U4__)
if (pin >= 18) pin -= 18; // allow for channel or pin numbers
#elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__)
if (pin >= 24) pin -= 24; // allow for channel or pin numbers
#else
if (pin >= 14) pin -= 14; // allow for channel or pin numbers
#endif
#if defined(ADCSRB) && defined(MUX5)
// the MUX5 bit of ADCSRB selects whether we're reading from channels
// 0 to 7 (MUX5 low) or 8 to 15 (MUX5 high).
ADCSRB = (ADCSRB & ~(1 << MUX5)) | (((pin >> 3) & 0x01) << MUX5);
#endif
// set the analog reference (high two bits of ADMUX) and select the
// channel (low 4 bits). this also sets ADLAR (left-adjust result)
// to 0 (the default).
#if defined(ADMUX)
#if defined(__AVR_ATtiny25__) || defined(__AVR_ATtiny45__) || defined(__AVR_ATtiny85__)
ADMUX = (analog_reference << 4) | (pin & 0x07);
#else
ADMUX = (analog_reference << 6) | (pin & 0x07);
#endif
#endif
// start the conversion
#if defined(ADCSRA) && defined(ADCL)
sbi(ADCSRA, ADSC);
#endif
}
bool A2D_complete()
{
// ADSC is cleared when the conversion finishes
if (bit_is_set(ADCSRA, ADSC))
return 0;
else
return 1;
}
void HF_slider::complete_read()
{
uint8_t low, high;
#if defined(ADCSRA) && defined(ADCL)
// we have to read ADCL first; doing so locks both ADCL
// and ADCH until ADCH is read. reading ADCL second would
// cause the results of each conversion to be discarded,
// as ADCL and ADCH would be locked when it completed.
low = ADCL;
high = ADCH;
#else
// we don't have an ADC, return 0
low = 0;
high = 0;
#endif
// combine the two bytes
_value = (high << 8) | low;
}