-
Notifications
You must be signed in to change notification settings - Fork 1
/
adc2osc.c
270 lines (212 loc) · 5.55 KB
/
adc2osc.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
// for void complain()
#include <stdarg.h>
#include <wiringPi.h>
#include <wiringPiSPI.h>
#include "osc/htmsocket.h"
#include "osc/OSC-client.h"
#include "osc/OSC-timetag.h"
#define ADC_SPI_CHANNEL 1
#define ADC_SPI_SPEED 500000
#define ADC_NUM_CHANNELS 8
// OSC defines
#define SC_BUFFER_SIZE 8000
#define MAX_ARGS 16
#define DELAY 10
// #define DEBUG
typedef struct {
enum {INT, FLOAT, STRING} type;
union {
int i;
float f;
char *s;
} datum;
} typedArg;
int WriteMessage(OSCbuf *buf, char *messageName, int numArgs, typedArg *args);
void SendBuffer(void *htmsocket, OSCbuf *buf);
void SendData(void *htmsocket, int size, char *data);
void fatal_error(char *s);
void complain(char *s, ...);
static char bufferForOSCbuf[SC_BUFFER_SIZE];
uint16_t readADC(_channel){
uint8_t spi_data[3];
uint8_t input_mode = 1; // single ended = 1, differential = 0
uint16_t result;
if(_channel > 7 | _channel < 0){
return -1;
}
spi_data[0] = 0x04; // start flag
spi_data[0] |= (input_mode<<1); // shift input_mode
spi_data[0] |= (_channel>>2) & 0x01; // add msb of channel in our first command byte
spi_data[1] = _channel<<6;
spi_data[2] = 0x00;
wiringPiSPIDataRW(ADC_SPI_CHANNEL, spi_data, 3);
result = (spi_data[1] & 0x0f)<<8 | spi_data[2];
return result;
}
int WriteMessage(OSCbuf *buf, char *messageName, int numArgs, typedArg *args) {
int j, returnVal;
returnVal = 0;
#ifdef DEBUG
printf("WriteMessage: %s ", messageName);
for (j = 0; j < numArgs; j++) {
switch (args[j].type) {
case INT:
printf("%d ", args[j].datum.i);
break;
case FLOAT:
printf("%f ", args[j].datum.f);
break;
case STRING:
printf("%s ", args[j].datum.s);
break;
default:
fatal_error("Unrecognized arg type");
exit(5);
}
}
printf("\n");
#endif
/* First figure out the type tags */
char typeTags[MAX_ARGS+2];
int i;
typeTags[0] = ',';
for (i = 0; i < numArgs; ++i) {
switch (args[i].type) {
case INT:
typeTags[i+1] = 'i';
break;
case FLOAT:
typeTags[i+1] = 'f';
break;
case STRING:
typeTags[i+1] = 's';
break;
default:
fatal_error("Unrecognized arg type");
exit(5);
}
}
typeTags[i+1] = '\0';
returnVal = OSC_writeAddressAndTypes(buf, messageName, typeTags);
if (returnVal) {
complain("Problem writing address: %s\n", OSC_errorMessage);
}
for (j = 0; j < numArgs; j++) {
switch (args[j].type) {
case INT:
if ((returnVal = OSC_writeIntArg(buf, args[j].datum.i)) != 0) {
return returnVal;
}
break;
case FLOAT:
if ((returnVal = OSC_writeFloatArg(buf, args[j].datum.f)) != 0) {
return returnVal;
}
break;
case STRING:
if ((returnVal = OSC_writeStringArg(buf, args[j].datum.s)) != 0) {
return returnVal;
}
break;
default:
fatal_error("Unrecognized arg type");
exit(5);
}
}
return returnVal;
}
void SendBuffer(void *htmsocket, OSCbuf *buf) {
#ifdef DEBUG
printf("Sending buffer...\n");
#endif
if (OSC_isBufferEmpty(buf)) return;
if (!OSC_isBufferDone(buf)) {
fatal_error("SendBuffer() called but buffer not ready!");
exit(5);
}
SendData(htmsocket, OSC_packetSize(buf), OSC_getPacket(buf));
}
void SendData(void *htmsocket, int size, char *data) {
if (!SendHTMSocket(htmsocket, size, data)) {
perror("Couldn't send out socket: ");
CloseHTMSocket(htmsocket);
exit(3);
}
}
void fatal_error(char *s) {
fprintf(stderr, "%s\n", s);
exit(4);
}
void complain(char *s, ...) {
va_list ap;
va_start(ap, s);
vfprintf(stderr, s, ap);
va_end(ap);
}
int main(int argc, char *argv[]){
char *hostname = "127.0.0.1";
int portnumber = 57120;
int nr;
void *htmsocket;
OSCbuf buf[1];
typedArg args[ADC_NUM_CHANNELS];
typedArg values[ADC_NUM_CHANNELS];
int delay_ms = DELAY;
argc--;
argv++;
if (argc == 0) {
complain("usage: %s -h [target_host_name port_number] -d [delay_in_ms]\n",
argv[-1]);
// exit(4);
}
if (argc >= 3 && (strncmp(*argv, "-h", 2) == 0)) {
hostname = argv[1];
argv += 2;
argc -= 2;
portnumber = atoi(*argv);
argv++;
argc--;
}
if (argc >= 2 && (strncmp(*argv, "-d", 2) == 0)) {
delay_ms = atoi(argv[1]);
}
printf("host %s, port %d, delay %d\n", hostname, portnumber, delay_ms);
int i;
for(i = 0; i < ADC_NUM_CHANNELS; i++){
values[i].type = FLOAT;
}
htmsocket = OpenHTMSocket(hostname, portnumber);
if (!htmsocket) {
perror("Couldn't open socket: ");
exit(3);
}
OSC_initBuffer(buf, SC_BUFFER_SIZE, bufferForOSCbuf);
wiringPiSetupSys();
wiringPiSPISetup(ADC_SPI_CHANNEL, ADC_SPI_SPEED);
for(;;){
for(i = 0; i < ADC_NUM_CHANNELS; i++){
int val;
values[i].datum.f = (4095 - readADC(i))/4095.0;
// printf("%f ", i, values[i].datum.f);
}
OSC_resetBuffer(buf);
if (OSC_openBundle(buf, OSCTT_Immediately())) {
complain("Problem opening bundle: %s\n", OSC_errorMessage);
return;
}
// arg.type = INT;
// arg.datum.i = values[2];
WriteMessage(buf, "/adc", ADC_NUM_CHANNELS, values);
if (OSC_closeBundle(buf)) {
complain("Problem closing bundle: %s\n", OSC_errorMessage);
return;
}
SendBuffer(htmsocket, buf);
// printf("\n");
delay(delay_ms);
}
}