-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.c
545 lines (444 loc) · 15.9 KB
/
main.c
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
/*
*
* terminal tedium / OSC client (UDP)
*
*
* - TD: gate outputs (?); make nicer. etc (pigpio?)
* - TD: make more userfriendly, pass IP and port as arguments, etc.
*
*
* compile with: gcc *.c -Werror -lwiringPi -std=gnu99 -O2 -g -o tedium_osc
*/
#define DEFAULT_SOURCE
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <sys/time.h>
#include <wiringPi.h>
#include <wiringPiSPI.h>
#include "tinyosc.h"
const uint16_t BUFLEN = 1024;
const uint64_t NOW = 1L;
const uint32_t TIMEOUT = 1000; // sleep (in us)
const uint32_t TIMEOUT_T = 10; // short time out (for OSC off messages) (triggers) // 10 * TIMEOUT (us)
#define ADC_SPI_CHANNEL 1
#define ADC_SPI_SPEED 4000000
#define ADC_NUM_CHANNELS 6
#define ADC_NUM ADC_NUM_CHANNELS-1
#define RESOLUTION 4095
#define DEADBAND 2
#define SCALE 4000
#define SCALE_INV 1.0f/(float)SCALE
// GPIO :
#define B1 23
#define B2 25
#define B3 24
#define LED 26
#define TR1 4
#define TR2 17
#define TR3 14
#define TR4 27
#define GATE1 16
#define GATE2 12
#define _MUTEX 0
void die(char *s)
{
perror(s);
exit(1);
}
uint16_t adc[ADC_NUM_CHANNELS]; // adc values
/* interrupt flags etc: */
static volatile uint8_t TR1_flag = 0;
static volatile uint8_t TR2_flag = 0;
static volatile uint8_t TR3_flag = 0;
static volatile uint8_t TR4_flag = 0;
static volatile uint8_t B1_flag = 0;
static volatile uint8_t B2_flag = 0;
static volatile uint8_t B3_flag = 0;
void Interrupt_TR1 (void) { TR1_flag = 1; }
void Interrupt_TR2 (void) { TR2_flag = 1; }
void Interrupt_TR3 (void) { TR3_flag = 1; }
void Interrupt_TR4 (void) { TR4_flag = 1; }
void Interrupt_B1 (void) { B1_flag = 1; }
void Interrupt_B2 (void) { B2_flag = 1; }
void Interrupt_B3 (void) { B3_flag = 1; }
// flags and counters for on/off messages:
uint8_t TR1_OFF = 0;
uint8_t TR2_OFF = 0;
uint8_t TR3_OFF = 0;
uint8_t TR4_OFF = 0;
uint16_t _wait_TR1, _wait_TR2, _wait_TR3, _wait_TR4;
/*
mcp3208 : return 1 if we need to send OSC, 0 otherwise
*/
uint16_t readADC(int _channel, uint16_t *adc_val){
uint8_t spi_data[3];
uint16_t result, tmp = *(adc_val + _channel); // previous.
spi_data[0] = 0x06 | (_channel>>2) & 0x01; // single ended
spi_data[1] = _channel<<6;
spi_data[2] = 0x00;
wiringPiSPIDataRW(ADC_SPI_CHANNEL, spi_data, 3);
// invert + limit result:
result = SCALE - (((spi_data[1] & 0x0f) << 8) | spi_data[2]);
result = result > RESOLUTION ? 0 : result;
if ( (result - tmp) > DEADBAND || (tmp - result) > DEADBAND ) {
*(adc_val + _channel) = result ;
return 1;
}
else {
*(adc_val + _channel) = tmp;
return 0;
}
}
/// buttons:
enum button_states
{
OK,
PRESSED,
SHORT_PRESS,
LONG_PRESS,
SEND_OFF_MSG,
SEND_OFF_MSG_L
};
typedef struct buttons
{
uint8_t _pin;
uint8_t _state;
uint8_t _OSC_state;
uint16_t _cnt;
} buttons;
buttons *button1, *button2, *button3;
void init_buttons() {
button1 = (buttons*)malloc(sizeof(buttons));
button2 = (buttons*)malloc(sizeof(buttons));
button3 = (buttons*)malloc(sizeof(buttons));
button1->_pin = B1;
button1->_state = OK;
button1->_OSC_state = OK;
button1->_cnt = 0;
button2->_pin = B2;
button2->_state = OK;
button2->_OSC_state = OK;
button2->_cnt = 0;
button3->_pin = B3;
button3->_state = OK;
button3->_OSC_state = OK;
button3->_cnt = 0;
}
void check_buttons(struct buttons* _b, int time_out)
{
if (_b->_state == PRESSED) {
uint8_t _s = digitalRead(_b->_pin);
if (!_s) _b->_cnt++;
else if (_s && _b->_cnt < time_out) _b->_state = SHORT_PRESS;
else if (_s && _b->_cnt >= time_out) _b->_state = LONG_PRESS;
}
else if (_b->_state > PRESSED) {
piLock (_MUTEX);
_b->_OSC_state = _b->_state;
piUnlock (_MUTEX);
// reset
_b->_state = OK;
_b->_cnt = 0;
}
}
///////////////////
PI_THREAD (handle_GPIO)
{
uint16_t time_out = 125; // wait until longpress
uint32_t _sleep = TIMEOUT*6;
init_buttons();
while(1) {
usleep(_sleep);
if (B1_flag && button1->_state == OK) {
B1_flag = 0;
button1->_state = PRESSED;
}
if (B2_flag && button2->_state == OK) {
B2_flag = 0;
button2->_state = PRESSED;
}
if (B3_flag && button3->_state == OK) {
B3_flag = 0;
button3->_state = PRESSED;
}
check_buttons(button1, time_out);
check_buttons(button2, time_out);
check_buttons(button3, time_out);
}
}
/* --------------------------------------------------------- */
int main(void)
{
struct sockaddr_in _serv;
int s, i, slen = sizeof(_serv);
char buf[BUFLEN];
int port = 9000;
char *ip = "127.0.0.1";
memset(adc, 0, ADC_NUM_CHANNELS);
// open socket :
if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1) die("socket");
memset((char *) &_serv, 0, sizeof(_serv));
_serv.sin_family = AF_INET;
_serv.sin_port = htons(port);
_serv.sin_addr.s_addr = inet_addr(ip);
// OSC bundle
tosc_bundle bundle;
// setup SPI
wiringPiSetupGpio();
wiringPiSPISetup(ADC_SPI_CHANNEL, ADC_SPI_SPEED);
// pull ups:
pinMode(B1, INPUT);
pullUpDnControl(B1, PUD_UP);
pinMode(B2, INPUT);
pullUpDnControl(B2, PUD_UP);
pinMode(B3, INPUT);
pullUpDnControl(B3, PUD_UP);
pinMode(TR1, INPUT);
pullUpDnControl(TR1, PUD_UP);
pinMode(TR2, INPUT);
pullUpDnControl(TR2, PUD_UP);
pinMode(TR3, INPUT);
pullUpDnControl(TR3, PUD_UP);
pinMode(TR4, INPUT);
pullUpDnControl(TR4, PUD_UP);
// LED + gate outputs (??) (what should those do?)
pinMode(LED, OUTPUT);
pinMode(GATE1, OUTPUT);
pinMode(GATE2, OUTPUT);
// interrupt, trigger inputs:
wiringPiISR (TR1, INT_EDGE_FALLING, &Interrupt_TR1) ;
wiringPiISR (TR2, INT_EDGE_FALLING, &Interrupt_TR2) ;
wiringPiISR (TR3, INT_EDGE_FALLING, &Interrupt_TR3) ;
wiringPiISR (TR4, INT_EDGE_FALLING, &Interrupt_TR4) ;
// interrupt, buttons:
wiringPiISR (B1, INT_EDGE_FALLING, &Interrupt_B1) ;
wiringPiISR (B2, INT_EDGE_FALLING, &Interrupt_B2) ;
wiringPiISR (B3, INT_EDGE_FALLING, &Interrupt_B3) ;
// flags
int _cnt = 0, _send = 0, toggle = 0;
// thread to handle buttons
piThreadCreate (handle_GPIO);
// main action is happening here (..)
while(1)
{
usleep(TIMEOUT);
// get adc value
_send += readADC(_cnt, adc);
// do we need to send an OSC bundle?
if (_cnt == ADC_NUM && _send) {
// construct OSC bundle:
tosc_writeBundle(&bundle, NOW, buf, BUFLEN);
tosc_writeNextMessage(&bundle, "/adc0", "f", (float)adc[0] * SCALE_INV);
tosc_writeNextMessage(&bundle, "/adc1", "f", (float)adc[1] * SCALE_INV);
tosc_writeNextMessage(&bundle, "/adc2", "f", (float)adc[2] * SCALE_INV);
tosc_writeNextMessage(&bundle, "/adc3", "f", (float)adc[3] * SCALE_INV);
tosc_writeNextMessage(&bundle, "/adc4", "f", (float)adc[4] * SCALE_INV);
tosc_writeNextMessage(&bundle, "/adc5", "f", (float)adc[5] * SCALE_INV);
// ... and send bundle:
int len = tosc_getBundleLength(&bundle);
if (sendto(s, buf, len, 0 , (struct sockaddr *) &_serv, slen)==-1) die("sendto()");
// reset flag
_send = 0;
}
// increment adc cycle counter
_cnt = _cnt++ >= ADC_NUM ? 0x0 : _cnt; // 0x5 = ADC_NUM_CHANNELS-1
// buttons ?
switch(button1->_OSC_state) {
case OK:
break;
case SHORT_PRESS: {
int len = tosc_writeMessage(buf, sizeof(buf), "/button1", "i", 1);
// send message:
sendto(s, buf, len, 0, (struct sockaddr *) &_serv, slen);
piLock (_MUTEX);
button1->_OSC_state = SEND_OFF_MSG;
piUnlock (_MUTEX);
break;
}
case LONG_PRESS: {
int len = tosc_writeMessage(buf, sizeof(buf), "/button1_long", "i", 1);
// send message:
sendto(s, buf, len, 0, (struct sockaddr *) &_serv, slen);
piLock (_MUTEX);
button1->_OSC_state = SEND_OFF_MSG_L;
piUnlock (_MUTEX);
break;
}
case SEND_OFF_MSG: {
int len = tosc_writeMessage(buf, sizeof(buf), "/button1", "i", 0);
// send message:
sendto(s, buf, len, 0, (struct sockaddr *) &_serv, slen);
piLock (_MUTEX);
button1->_OSC_state = OK;
piUnlock (_MUTEX);
break;
}
case SEND_OFF_MSG_L: {
int len = tosc_writeMessage(buf, sizeof(buf), "/button1_long", "i", 0);
// send message:
sendto(s, buf, len, 0, (struct sockaddr *) &_serv, slen);
piLock (_MUTEX);
button1->_OSC_state = OK;
piUnlock (_MUTEX);
break;
}
default: break;
}
switch(button2->_OSC_state) {
case OK:
break;
case SHORT_PRESS: {
int len = tosc_writeMessage(buf, sizeof(buf), "/button2", "i", 1);
// send message:
sendto(s, buf, len, 0, (struct sockaddr *) &_serv, slen);
piLock (_MUTEX);
button2->_OSC_state = SEND_OFF_MSG;
piUnlock (_MUTEX);
break;
}
case LONG_PRESS: {
int len = tosc_writeMessage(buf, sizeof(buf), "/button2_long", "i", 1);
// send message:
sendto(s, buf, len, 0, (struct sockaddr *) &_serv, slen);
piLock (_MUTEX);
button2->_OSC_state = SEND_OFF_MSG_L;
piUnlock (_MUTEX);
break;
}
case SEND_OFF_MSG: {
int len = tosc_writeMessage(buf, sizeof(buf), "/button2", "i", 0);
// send message:
sendto(s, buf, len, 0, (struct sockaddr *) &_serv, slen);
piLock (_MUTEX);
button2->_OSC_state = OK;
piUnlock (_MUTEX);
break;
}
case SEND_OFF_MSG_L: {
int len = tosc_writeMessage(buf, sizeof(buf), "/button2_long", "i", 0);
// send message:
sendto(s, buf, len, 0, (struct sockaddr *) &_serv, slen);
piLock (_MUTEX);
button2->_OSC_state = OK;
piUnlock (_MUTEX);
break;
}
default: break;
}
switch(button3->_OSC_state) {
case OK:
break;
case SHORT_PRESS: {
int len = tosc_writeMessage(buf, sizeof(buf), "/button3", "i", 1);
// send message:
sendto(s, buf, len, 0, (struct sockaddr *) &_serv, slen);
piLock (_MUTEX);
button3->_OSC_state = SEND_OFF_MSG;
piUnlock (_MUTEX);
toggle = ~toggle & 1u;
digitalWrite(LED, toggle);
break;
}
case LONG_PRESS: {
int len = tosc_writeMessage(buf, sizeof(buf), "/button3_long", "i", 1);
// send message:
sendto(s, buf, len, 0, (struct sockaddr *) &_serv, slen);
piLock (_MUTEX);
button3->_OSC_state = SEND_OFF_MSG_L;
piUnlock (_MUTEX);
toggle = ~toggle & 1u;
digitalWrite(LED, toggle);
break;
}
case SEND_OFF_MSG: {
int len = tosc_writeMessage(buf, sizeof(buf), "/button3", "i", 0);
// send message:
sendto(s, buf, len, 0, (struct sockaddr *) &_serv, slen);
piLock (_MUTEX);
button3->_OSC_state = OK;
piUnlock (_MUTEX);
break;
}
case SEND_OFF_MSG_L: {
int len = tosc_writeMessage(buf, sizeof(buf), "/button3_long", "i", 0);
// send message:
sendto(s, buf, len, 0, (struct sockaddr *) &_serv, slen);
piLock (_MUTEX);
button3->_OSC_state = OK;
piUnlock (_MUTEX);
break;
}
default: break;
}
// handle trig inputs:
if (TR1_flag && !TR1_OFF) {
TR1_flag = 0;
int len = tosc_writeMessage(buf, sizeof(buf), "/trigger1", "i", 1);
// send message:
if (sendto(s, buf, len, 0, (struct sockaddr *) &_serv, slen)) TR1_OFF = 1;
_wait_TR1 = 0;
}
if (TR2_flag && !TR2_OFF) {
TR2_flag = 0;
int len = tosc_writeMessage(buf, sizeof(buf), "/trigger2", "i", 1);
// send message:
if (sendto(s, buf, len, 0, (struct sockaddr *) &_serv, slen)) TR2_OFF = 1;
_wait_TR2 = 0;
}
if (TR3_flag && !TR3_OFF) {
TR3_flag = 0;
int len = tosc_writeMessage(buf, sizeof(buf), "/trigger3", "i", 1);
// send message:
if (sendto(s, buf, len, 0, (struct sockaddr *) &_serv, slen)) TR3_OFF = 1;
_wait_TR3 = 0;
}
if (TR4_flag && !TR4_OFF) {
TR4_flag = 0;
int len = tosc_writeMessage(buf, sizeof(buf), "/trigger4", "i", 1);
// send message:
if (sendto(s, buf, len, 0, (struct sockaddr *) &_serv, slen)) TR4_OFF = 1;
_wait_TR4 = 0;
}
// turn off gate1
if (TR1_OFF && _wait_TR1 < TIMEOUT_T) _wait_TR1++;
else if (TR1_OFF && _wait_TR1 >= TIMEOUT_T) {
// reset
TR1_OFF = _wait_TR1 = 0;
int len = tosc_writeMessage(buf, sizeof(buf), "/trigger1", "i", 0);
sendto(s, buf, len, 0, (struct sockaddr *) &_serv, slen);
}
// turn off gate2
if (TR2_OFF && _wait_TR2 < TIMEOUT_T) _wait_TR2++;
else if (TR2_OFF && _wait_TR2 >= TIMEOUT_T) {
// reset
TR2_OFF = _wait_TR2 = 0;
int len = tosc_writeMessage(buf, sizeof(buf), "/trigger2", "i", 0);
sendto(s, buf, len, 0, (struct sockaddr *) &_serv, slen);
}
// turn off gate3
if (TR3_OFF && _wait_TR3 < TIMEOUT_T) _wait_TR3++;
else if (TR3_OFF && _wait_TR3 >= TIMEOUT_T) {
// reset
TR3_OFF = _wait_TR3 = 0;
int len = tosc_writeMessage(buf, sizeof(buf), "/trigger3", "i", 0);
sendto(s, buf, len, 0, (struct sockaddr *) &_serv, slen);
}
// turn off gate4
if (TR4_OFF && _wait_TR4 < TIMEOUT_T) _wait_TR4++;
else if (TR4_OFF && _wait_TR4 >= TIMEOUT_T) {
// reset
TR4_OFF = _wait_TR4 = 0;
int len = tosc_writeMessage(buf, sizeof(buf), "/trigger4", "i", 0);
sendto(s, buf, len, 0, (struct sockaddr *) &_serv, slen);
}
}
close(s);
return 0;
}
// end