-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.cpp
More file actions
303 lines (261 loc) · 6.34 KB
/
Controller.cpp
File metadata and controls
303 lines (261 loc) · 6.34 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "Controller.h"
//Current preset values
byte HX_PRESET = 0;
byte DL_PRESET = 0;
/*
Public constructor for the array of values
*/
ButtonValues::ButtonValues(byte channels[], byte commands[], byte values_min[], byte values_max[], byte values_step[])
{
//Set the array values
Channels = channels;
Commands = commands;
Values_Min = values_min;
Values_Max = values_max;
Values_Step = values_step;
//Set the default values
for (int i = 0; i < NUMBER_LAYERS; i++)
{
Values[i] = Values_Min[i];
}
}
/*
Returns the selected message
*/
void ButtonValues::getMessage(byte layer, byte message[])
{
//Set the message data
message [0] = Channels[layer];
message [1] = Commands[layer];
//Check the type of value
if (Commands[layer] == PROG_VALUE)
{
//Set the preset value
byte *preset = Channels[layer] == 1 ? &HX_PRESET : &DL_PRESET;
if (Values_Min[layer] != NO_VALUE)
{
//Check if there is a step
if (Values_Step[layer] == 0)
{
//Set the selected value
*preset = Values_Min[layer];
}
else
{
//Check the step value
if (Values_Step[layer] > NEG_VALUE)
{
*preset -= Values_Step[layer] != NO_VALUE ? Values_Step[layer] - NEG_VALUE : 0;
}
else
{
*preset += Values_Step[layer] != NO_VALUE ? Values_Step[layer] : 0;
}
//Check the preset limit
if (*preset > Values_Max[layer])
{
*preset = Values_Min[layer];
}
}
}
//Set the message value
message[2] = *preset;
}
else
{
//Set the message value
message[2] = Values[layer];
//Set the next value
Next = Values[layer] + Values_Step[layer];
if (Next > Values_Max[layer])
{
Values[layer] -= Values_Max[layer];
}
else
{
Values[layer] = Next;
}
}
return message;
}
/*
Returns the selected default value
*/
byte ButtonValues::getValue(byte layer)
{
return Values_Min[layer];
}
/*
Resets the current values
*/
void ButtonValues::resetValues()
{
//Reset the default values
for (int i = 0; i < NUMBER_LAYERS; i++)
{
Values[i] = Values_Min[i];
}
}
/*
Public constructor for the buttons
*/
Button::Button(byte pin, ButtonValues *values_press, ButtonValues *values_release, ButtonValues *values_holdpress, ButtonValues *values_holdrelease)
{
//Set the button pin and mode
Pin = pin;
pinMode(Pin, INPUT_PULLUP);
//Set the initial button values
Values_Press = values_press;
Values_Release = values_release;
Values_HoldPress = values_holdpress;
Values_HoldRelease = values_holdrelease;
//Set the default control values
Status = 0b00000010;
Previous = 1;
TimeHold = 0;
TimeElapsed = 0;
}
/*
Returns the button message
*/
bool Button::getMessage(byte message[])
{
//Check the current status
if (bitRead(Status, 3) == true)
{
//Cleat the hold release status
bitClear(Status, 3);
//Set the message data
Values_HoldRelease->getMessage(Layer, message);
return message[2] != NO_VALUE;
}
else if (bitRead(Status, 1) == true)
{
bitSet(Status, 0);
bitClear(Status, 1);
TimeElapsed = millis();
return false;
}
//Wait until the debounce time has passed
if (millis() - TimeElapsed < DEBOUNCE_TIME)
{
return false;
}
//Check if the status has changed
if (digitalRead(Pin) == Previous)
{
//Set the current status
bitClear(Status, 0);
bitSet(Status, 1);
//Check the hold press status
if (bitRead(Status, 2) == true && millis() - TimeHold > HOLD_TIME)
{
//Cleat the hold press status
bitClear(Status, 2);
//Check the hold release flag
if (Values_HoldRelease->getValue(Layer) != NO_VALUE)
{
//Set the hold release status
bitSet(Status, 3);
}
//Set the message data
Values_HoldPress->getMessage(Layer, message);
return true;
}
return false;
}
else
{
//Set the current status
bitClear(Status, 0);
bitSet(Status, 1);
//Store the previous status
Previous = ((~Previous) & 0b00000001);
//Check the hold press flag
if (Values_HoldPress->getValue(Layer) != NO_VALUE)
{
//Check the hold status
if (Previous == 0 && bitRead(Status, 2) == false)
{
//Set the hold status
bitSet(Status, 2);
TimeHold = millis();
}
else if (Previous == 1 && bitRead(Status, 2) == true)
{
//Cleat the hold status
bitClear(Status, 2);
//Set the message data
Values_Press->getMessage(Layer, message);
return message[2] != NO_VALUE;
}
}
else
{
//Check the status and set the message data
if (Previous == 1)
{
Values_Release->getMessage(Layer, message);
}
else
{
Values_Press->getMessage(Layer, message);
}
return message[2] != NO_VALUE;
}
}
}
/*
Sets the selected layer
*/
void Button::setLayer(byte layer)
{
//Set the current layer
Layer = layer;
//Reset all values
Values_Press->resetValues();
Values_Release->resetValues();
Values_HoldPress->resetValues();
Values_HoldRelease->resetValues();
}
/*
Public constructor for the expression pedals
*/
Expression::Expression(byte pin, byte channels[], byte commands[])
{
//Set the pedal pin
Pin = pin;
//Set the initial expression values
Status = analogRead(Pin) >> BIT_CONVERT;
Previous = Status;
//Set the channel and command
Channels = channels;
Commands = commands;
}
/*
Returns the expression pedal message
*/
bool Expression::getMessage(byte message[])
{
//Get the current expression value
Status = analogRead(Pin) >> BIT_CONVERT;
//Check for a value change
if (Status != Previous)
{
//Store the new values
Previous = Status;
//Set the message data
message[0] = Channels[Layer];
message[1] = Commands[Layer];
message[2] = Status;
return true;
}
return false;
}
/*
Sets the selected layer
*/
void Expression::setLayer(byte layer)
{
Layer = layer;
}