forked from sadken/TZXDuino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuttons.ino
136 lines (114 loc) · 2.93 KB
/
buttons.ino
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
#include "buttons.h"
#ifdef BUTTONS_ADC
// all buttons (except motor) routed through a single pin using a resistor potential divider network
// Analog levels (= outputs of the divider network) are set in userconfig.h
// See docs for more details
void setup_buttons(void)
{
pinMode(BUTTONS_ADC_PIN, INPUT_PULLUP);
#ifdef HAVE_MOTOR
pinMode(btnMotor, INPUT_PULLUP);
digitalWrite(btnMotor,HIGH);
#endif
}
bool button_play()
{
// todo - use isr to capture buttons?
int sensorValue = analogRead(BUTTONS_ADC_PIN);
return(sensorValue>=BUTTONS_ADC_PLAY_LOW);
}
bool button_stop()
{
// todo - use isr to capture buttons?
int sensorValue = analogRead(BUTTONS_ADC_PIN);
return(sensorValue>=BUTTONS_ADC_STOP_LOW && sensorValue<BUTTONS_ADC_PLAY_LOW);
}
bool button_root()
{
// todo - use isr to capture buttons?
int sensorValue = analogRead(BUTTONS_ADC_PIN);
return(sensorValue>=BUTTONS_ADC_ROOT_LOW && sensorValue<BUTTONS_ADC_STOP_LOW);
}
bool button_down()
{
// todo - use isr to capture buttons?
int sensorValue = analogRead(BUTTONS_ADC_PIN);
return(sensorValue>=BUTTONS_ADC_DOWN_LOW && sensorValue<BUTTONS_ADC_ROOT_LOW);
}
bool button_up()
{
// todo - use isr to capture buttons?
int sensorValue = analogRead(BUTTONS_ADC_PIN);
return(sensorValue>=BUTTONS_ADC_UP_LOW && sensorValue<BUTTONS_ADC_DOWN_LOW);
}
#else // BUTTONS_ADC
void setup_buttons(void)
{
//General Pin settings
//Setup buttons with internal pullup
pinMode(btnPlay,INPUT_PULLUP);
digitalWrite(btnPlay,HIGH);
pinMode(btnStop,INPUT_PULLUP);
digitalWrite(btnStop,HIGH);
pinMode(btnUp,INPUT_PULLUP);
digitalWrite(btnUp,HIGH);
pinMode(btnDown,INPUT_PULLUP);
digitalWrite(btnDown,HIGH);
pinMode(btnRoot, INPUT_PULLUP);
digitalWrite(btnRoot, HIGH);
#ifdef HAVE_MOTOR
pinMode(btnMotor, INPUT_PULLUP);
digitalWrite(btnMotor,HIGH);
#endif
}
bool button_play()
{
return (digitalRead(btnPlay)==LOW);
}
bool button_stop()
{
return (digitalRead(btnStop)==LOW);
}
bool button_root()
{
return (digitalRead(btnRoot)==LOW);
}
bool button_down()
{
return (digitalRead(btnDown)==LOW);
}
bool button_up()
{
return (digitalRead(btnUp)==LOW);
}
#endif // BUTTONS_ADC
#ifdef HAVE_MOTOR
bool button_motor()
{
return (digitalRead(btnMotor));
}
#endif
void button_wait(button_fn f) {
// returns when the button has been released
while(f()) {
//prevent button repeats by waiting until the button is released.
delay(BUTTON_WAIT_DELAY);
}
}
bool button_wait_timeout(button_fn f, unsigned int timeout) {
// returns when the button has been released OR timeout reached
// returns true if the button was still pressed (i.e. when timeout reached)
while(f() && timeout > 0) {
//prevent button repeats by waiting until the button is released.
if (timeout >= BUTTON_WAIT_DELAY) {
delay(BUTTON_WAIT_DELAY);
timeout -= BUTTON_WAIT_DELAY;
}
else
{
delay(timeout);
timeout = 0;
}
}
return (timeout==0);
}