-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathControlsButtons.cpp
148 lines (117 loc) · 4.03 KB
/
ControlsButtons.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
#include "Arduino.h"
#include "Controls.h"
#include "Display.h"
/*
-- Buttons :
On emet un signal différent pour press et release
A l'enregistrement de cb, on peut préciser press/release et préciser un temps de release-press
*/
/***********************************
* Buttons configuration
***********************************/
#define BUTTON_DELAY 50 //delay between buttons value check
#define BUTTON_MAX_CB 2 //max number of registered cb for buttons
#define BUTTON_COUNT 3 //Number of buttons in config
byte aButtonPins[BUTTON_COUNT] = {2, 3, 4}; //pins used for buttons in config //TODO : parameter for ButtonsSetup ?
typedef struct
{
tButtonStatus event; //Press or Release
int duration; //For how long ?
tButtonCb callback; //Cb
} tButtonCallback;
typedef struct
{
byte pin;
tButtonStatus btStatus;
int timePressed;
tButtonCallback aCallbacks[BUTTON_MAX_CB];
} tButton;
tButton aButtons[BUTTON_COUNT];
int lastButtonChecked = 0;
void ControlsSetupButtons(int time)
{
byte i;
//Init buttons
for (i = 0; i < BUTTON_COUNT; i++)
{
memset(&aButtons[i], 0x00, sizeof(tButton));
aButtons[i].btStatus = eButtonStatus_Released;
aButtons[i].pin = aButtonPins[i];
pinMode((int) aButtons[i].pin, INPUT_PULLUP);
memset(&aButtons[i].aCallbacks, 0x00, (BUTTON_MAX_CB+1)*sizeof(tButtonCallback));
}
lastButtonChecked = time;
}
void ControlsUpdateButtons(int time)
{
int i;
//Update Buttons
if (time - lastButtonChecked < BUTTON_DELAY) //Too early for a new check
return;
lastButtonChecked = time;
for (i = 0; i < BUTTON_COUNT; i++)
{
tButtonStatus btVal = (digitalRead(aButtons[i].pin) == HIGH)?eButtonStatus_Released:eButtonStatus_Pressed;
if (aButtons[i].btStatus != btVal) //button has changed
{
int j;
DisplayBlinkGreen();
tButtonStatus st = eButtonStatus_None;
int timePressed = 0;
if ( (aButtons[i].btStatus == eButtonStatus_Pressed) && //Release
(btVal == eButtonStatus_Released) )
{
timePressed = time - aButtons[i].timePressed;
aButtons[i].btStatus = eButtonStatus_Released;
st = eButtonStatus_Released;
}
else //Press
{
aButtons[i].timePressed = time;
aButtons[i].btStatus = eButtonStatus_Pressed;
st = eButtonStatus_Pressed;
}
//Process callbacks for this button
j = 0;
while ((j < BUTTON_MAX_CB) && aButtons[i].aCallbacks[j].callback)
{
if (aButtons[i].aCallbacks[j].event == st)
{
if (st == eButtonStatus_Pressed)
{
aButtons[i].aCallbacks[j].callback(i, aButtons[i].aCallbacks[j].event, aButtons[i].aCallbacks[j].duration);
}
else
{
if (!aButtons[i].aCallbacks[j].duration) //no duration specified
aButtons[i].aCallbacks[j].callback(i, aButtons[i].aCallbacks[j].event, aButtons[i].aCallbacks[j].duration);
else if (aButtons[i].aCallbacks[j].duration < timePressed)
aButtons[i].aCallbacks[j].callback(i, aButtons[i].aCallbacks[j].event, aButtons[i].aCallbacks[j].duration);
}
}
j++;
}
}
}
}
//Registers callback for Button press/release (returns -1 on error)
int ControlsRegisterButtonCallback(byte button, tButtonStatus event, int duration, tButtonCb callback)
{
int i;
if ((button < 0) || (button >= BUTTON_COUNT))
return -1;
if ((event == eButtonStatus_Pressed) && (duration))
return -1; // invalid settings
for (i = 0; i < BUTTON_MAX_CB; i++)
{
if (!aButtons[button].aCallbacks[i].callback)
{
aButtons[button].aCallbacks[i].callback = callback;
aButtons[button].aCallbacks[i].event = event;
if (event == eButtonStatus_Released)
aButtons[button].aCallbacks[i].duration = duration;
return 0;
}
}
return -1; //Too many cb for this knob (increase KNOB_MAX_CB)
}