-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathcontrol.c
More file actions
358 lines (326 loc) · 12.6 KB
/
control.c
File metadata and controls
358 lines (326 loc) · 12.6 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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
//
// control.c
// SqueezeButtonPi
//
// The actual control code
// - Configure buttons and rotary encoders
// - Send commands when buttons and rotary encoders are used
//
// Created by Jörg Schwieder on 02.02.17.
//
//
// Copyright (c) 2017, Joerg Schwieder, PenguinLovesMusic.com
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of ickStream nor the names of its contributors
// may be used to endorse or promote products derived from this software
// without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
#include "sbpd.h"
#include "control.h"
#include "servercomm.h"
#include <wiringPi.h>
#include <string.h>
#include <time.h>
#include <stdlib.h>
//
// Pre-allocate encoder and button objects on the stack so we don't have to
// worry about freeing them
//
static struct button_ctrl button_ctrls[max_buttons];
static struct encoder_ctrl encoder_ctrls[max_encoders];
static int numberofbuttons = 0;
static int numberofencoders = 0;
//
// Command fragments
//
// Buttons
//
/*#define FRAGMENT_PAUSE "[\"pause\"]"
#define FRAGMENT_VOLUME_UP "[\"button\",\"volup\"]"
#define FRAGMENT_VOLUME_DOWN "[\"button\",\"voldown\"]"
#define FRAGMENT_PREV "[\"button\",\"rew\"]"
#define FRAGMENT_NEXT "[\"button\",\"fwd\"]"
#define FRAGMENT_POWER "[\"button\",\"power\"]"
*/
//
// Encoder
//
#define FRAGMENT_VOLUME "[\"mixer\",\"volume\",\"%s%d\"]"
#define FRAGMENT_TRACK "[\"playlist\",\"jump\",\"%s%d\"]"
//
// LMS Command structure
//
static struct lms_command lms_commands[MAX_COMMANDS];
static int numberofcommands = 0;
int add_lms_command_frament ( char * name, char * value ) {
loginfo("Adding Command %s: Fragment %s", name, value);
lms_commands[numberofcommands].code = STRTOU32(name);
strncpy (lms_commands[numberofcommands].fragment, value, MAXLEN);
numberofcommands ++;
if (numberofcommands > MAX_COMMANDS )
return 1;
return 0;
}
char * get_lms_command_fragment ( int code ) {
for (int i = 0; i < numberofcommands; i++) {
if ( code == lms_commands[i].code )
return lms_commands[i].fragment;
}
return NULL;
}
//
// Button press callback
// Sets the flag for "button pressed"
//
void button_press_cb(const struct button * button, int change, bool presstype) {
for (int cnt = 0; cnt < numberofbuttons; cnt++) {
if (button == button_ctrls[cnt].gpio_button) {
button_ctrls[cnt].presstype = presstype;
button_ctrls[cnt].waiting = true;
loginfo("Button CB set for button #:%d, gpio pin %d", cnt, button_ctrls[cnt].gpio_button->pin);
return;
}
}
}
//
// Setup button control
// Parameters:
// pin: the GPIO-Pin-Number
// cmd: Command. One of
// PLAY - play/pause
// VOL+ - increment volume
// VOL- - decrement volume
// PREV - previous track
// NEXT - next track
// Command type SCRIPT.
// SCRIPT:/path/to/shell/script.sh
// resist: Optional. one of
// 0 - Internal resistor off
// 1 - pull down - input puts 3v on GPIO pin
// 2 - pull up (default) - input pulls GPIO pin to ground
// pressed: Optional GPIO pinstate for button to read pressed
// 0 - state is 0 (default)
// 1 - state is 1
// cmd_long Command to be used for a long button push, see above command list
// long_time: Number of milliseconds to define a long press
int setup_button_ctrl(char * cmd, int pin, int resist, int pressed, char * cmd_long, int long_time) {
char * fragment = NULL;
char * fragment_long = NULL;
char * script;
char * script_long;
char * separator = ":";
int cmdtype;
int cmd_longtype;
//
// Select fragment for short press parameter
//
if (strlen(cmd) == 4) {
fragment = get_lms_command_fragment(STRTOU32(cmd));
cmdtype = LMS;
} else if (strncmp("SCRIPT:", cmd, 7) == 0) {
cmdtype = SCRIPT;
strtok( cmd, separator );
script = strtok( NULL, "" );
fragment = script;
}
if (!fragment){
loginfo("Command %s, not found in defined commands", cmd);
return -1;
}
//
// Select fragment for long press parameter
//
if ( cmd_long == NULL ) {
cmd_longtype = NOTUSED;
} else if ( strlen(cmd_long) == 4 ) {
fragment_long = get_lms_command_fragment(STRTOU32(cmd_long));
cmd_longtype = LMS;
} else if (strncmp("SCRIPT:", cmd_long, 7) == 0) {
cmd_longtype = SCRIPT;
strtok( cmd_long, separator );
script_long = strtok( NULL, "" );
fragment_long = script_long;
}
if ( (cmd_long != NULL) & (!fragment_long) ){
loginfo("Command %s, not found in defined commands", cmd_long);
cmd_longtype = NOTUSED;
}
// Make sure resistor setting makes sense, or reset to default
if ( (resist != PUD_OFF) && (resist != PUD_DOWN) && (resist == PUD_UP) )
resist = PUD_UP;
struct button * gpio_b = setupbutton(pin, button_press_cb, resist, (bool)(pressed == 0) ? 0 : 1, long_time);
button_ctrls[numberofbuttons].cmdtype = cmdtype;
button_ctrls[numberofbuttons].shortfragment = fragment;
button_ctrls[numberofbuttons].cmd_longtype = cmd_longtype;
button_ctrls[numberofbuttons].longfragment = fragment_long;
button_ctrls[numberofbuttons].waiting = false;
button_ctrls[numberofbuttons].gpio_button = gpio_b;
numberofbuttons++;
loginfo("Button defined: Pin %d, BCM Resistor: %s, Short Type: %s, Short Fragment: %s , Long Type: %s, Long Fragment: %s, Long Press Time: %i",
pin,
(resist == PUD_OFF) ? "both" :
(resist == PUD_DOWN) ? "down" : "up",
(cmdtype == LMS) ? "LMS" :
(cmdtype == SCRIPT) ? "Script" : "unused",
fragment,
(cmd_longtype == LMS) ? "LMS" :
(cmd_longtype == SCRIPT) ? "Script" : "unused",
fragment_long,
long_time);
return 0;
}
//
// Polling function: handle button commands
// Parameters:
// server: the server to send commands to
//
void handle_buttons(struct sbpd_server * server) {
//logdebug("Polling buttons");
for (int cnt = 0; cnt < numberofbuttons; cnt++) {
if (button_ctrls[cnt].waiting) {
loginfo("Button pressed: Pin: %d, Press Type:%s", button_ctrls[cnt].gpio_button->pin,
(button_ctrls[cnt].presstype == LONGPRESS) ? "Long" : "Short" );
if ( button_ctrls[cnt].presstype == SHORTPRESS ) {
if ( button_ctrls[cnt].shortfragment != NULL ) {
send_command(server, button_ctrls[cnt].cmdtype, button_ctrls[cnt].shortfragment);
}
}
if ( button_ctrls[cnt].presstype == LONGPRESS ) {
if ( button_ctrls[cnt].longfragment != NULL ) {
send_command(server, button_ctrls[cnt].cmd_longtype, button_ctrls[cnt].longfragment);
} else {
loginfo("No Long Press command configured");
}
}
button_ctrls[cnt].waiting = false; // clear waiting
}
}
}
//
// Encoder interrupt callback
// Currently does nothing since we poll for volume changes
//
void encoder_rotate_cb(const struct encoder * encoder, long change) {
logdebug("Interrupt: encoder value: %d change: %d", encoder->value, change);
}
//
// Setup encoder control
// Parameters:
// cmd: Command. Currently only
// VOLU - volume
// TRAC - previous or next track
// Can be NULL for volume
// pin1: the GPIO-Pin-Number for the first pin used
// pin2: the GPIO-Pin-Number for the second pin used
// edge: one of
// 1 - falling edge
// 2 - rising edge
// 0, 3 - both
//
//
int setup_encoder_ctrl(char * cmd, int pin1, int pin2, int edge) {
char * fragment = NULL;
if (strlen(cmd) > 4)
return -1;
//
// Select fragment for parameter
// Would love to "switch" here but that's not portable...
//
uint32_t code = STRTOU32(cmd);
if (code == STRTOU32("VOLU")) {
fragment = FRAGMENT_VOLUME;
encoder_ctrls[numberofencoders].limit = 100;
encoder_ctrls[numberofencoders].min_time = 0;
} else if (code == STRTOU32("TRAC")) {
fragment = FRAGMENT_TRACK;
encoder_ctrls[numberofencoders].limit = 1;
encoder_ctrls[numberofencoders].min_time = 500;
}
if ( fragment == NULL ) {
return -1;
}
struct encoder * gpio_e = setupencoder(pin1, pin2, encoder_rotate_cb, edge);
encoder_ctrls[numberofencoders].fragment = fragment;
encoder_ctrls[numberofencoders].gpio_encoder = gpio_e;
encoder_ctrls[numberofencoders].last_value = 0;
encoder_ctrls[numberofencoders].last_time = 0;
numberofencoders++;
loginfo("Rotary encoder defined: Pin %d, %d, Edge: %s, Fragment: \n%s",
pin1, pin2,
((edge != INT_EDGE_FALLING) && (edge != INT_EDGE_RISING)) ? "both" :
(edge == INT_EDGE_FALLING) ? "falling" : "rising",
fragment);
return 0;
}
//
// Polling function: handle encoder commands
// Parameters:
// server: the server to send commands to
//
void handle_encoders(struct sbpd_server * server) {
//
// chatter filter set duration in encoder setup.
// - volume set to 0...
// - track change set to 500ms
// We poll every 100ms anyway plus wait for network action to complete
//
long long time = ms_timer();
//logdebug("Polling encoders");
int command = LMS;
for (int cnt = 0; cnt < numberofencoders; cnt++) {
//
// build volume delta
// ignore if > 100: overflow
//
int delta = (int)(encoder_ctrls[cnt].gpio_encoder->value - encoder_ctrls[cnt].last_value);
if (delta > 100)
delta = 0;
if (delta != 0) {
//Check if change happened before minimum delay, clear out data.
if ( encoder_ctrls[cnt].last_time + encoder_ctrls[cnt].min_time > time ) {
loginfo("Encoder on GPIO %d, %d value change: %d, before %d ms ellapsed not sending lms command.",
encoder_ctrls[cnt].gpio_encoder->pin_a,
encoder_ctrls[cnt].gpio_encoder->pin_b,
delta,
(encoder_ctrls[cnt].min_time) );
encoder_ctrls[cnt].last_value = encoder_ctrls[cnt].gpio_encoder->value;
return;
}
loginfo("Encoder on GPIO %d, %d value change: %d",
encoder_ctrls[cnt].gpio_encoder->pin_a,
encoder_ctrls[cnt].gpio_encoder->pin_b,
delta);
char fragment[50];
char * prefix = (delta > 0) ? "+" : "-";
if ( abs(delta) > encoder_ctrls[cnt].limit ) {
delta = encoder_ctrls[cnt].limit;
}
snprintf(fragment, sizeof(fragment),
encoder_ctrls[cnt].fragment, prefix, abs(delta));
if (send_command(server, command, fragment)) {
encoder_ctrls[cnt].last_value = encoder_ctrls[cnt].gpio_encoder->value;
encoder_ctrls[cnt].last_time = time; // chatter filter
}
}
}
}