NOOP, remove trailing tabs/whitespace.
[ardour.git] / libs / surfaces / frontier / tests / tranzport_lights.c
1 /*
2  * tranzport 0.1 <tranzport.sf.net>
3  * oct 18, 2005
4  * arthur@artcmusic.com
5  */
6
7 #include <stdarg.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <string.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <errno.h>
14 #include <malloc.h>
15
16 #define VENDORID  0x165b
17 #define PRODUCTID 0x8101
18
19 #define READ_ENDPOINT  0x81
20 #define WRITE_ENDPOINT 0x02
21
22 enum {
23         LIGHT_RECORD = 0,
24         LIGHT_TRACKREC,
25         LIGHT_TRACKMUTE,
26         LIGHT_TRACKSOLO,
27         LIGHT_ANYSOLO,
28         LIGHT_LOOP,
29         LIGHT_PUNCH
30 };
31
32 #define BUTTONMASK_BATTERY     0x00004000
33 #define BUTTONMASK_BACKLIGHT   0x00008000
34 #define BUTTONMASK_TRACKLEFT   0x04000000
35 #define BUTTONMASK_TRACKRIGHT  0x40000000
36 #define BUTTONMASK_TRACKREC    0x00040000
37 #define BUTTONMASK_TRACKMUTE   0x00400000
38 #define BUTTONMASK_TRACKSOLO   0x00000400
39 #define BUTTONMASK_UNDO        0x80000000
40 #define BUTTONMASK_IN          0x02000000
41 #define BUTTONMASK_OUT         0x20000000
42 #define BUTTONMASK_PUNCH       0x00800000
43 #define BUTTONMASK_LOOP        0x00080000
44 #define BUTTONMASK_PREV        0x00020000
45 #define BUTTONMASK_ADD         0x00200000
46 #define BUTTONMASK_NEXT        0x00000200
47 #define BUTTONMASK_REWIND      0x01000000
48 #define BUTTONMASK_FASTFORWARD 0x10000000
49 #define BUTTONMASK_STOP        0x00010000
50 #define BUTTONMASK_PLAY        0x00100000
51 #define BUTTONMASK_RECORD      0x00000100
52 #define BUTTONMASK_SHIFT       0x08000000
53
54 #define STATUS_OFFLINE 0xff
55 #define STATUS_ONLINE  0x01
56 #define STATUS_OK  0x00
57
58 struct tranzport_s {
59         int *dev;
60         int udev;
61 };
62
63 typedef struct tranzport_s tranzport_t;
64
65 void log_entry(FILE *fp, char *format, va_list ap)
66 {
67         vfprintf(fp, format, ap);
68         fputc('\n', fp);
69 }
70
71 void log_error(char *format, ...)
72 {
73         va_list ap;
74         va_start(ap, format);
75         log_entry(stderr, format, ap);
76         va_end(ap);
77 }
78
79 void vlog_error(char *format, va_list ap)
80 {
81         log_entry(stderr, format, ap);
82 }
83
84 void die(char *format, ...)
85 {
86         va_list ap;
87         va_start(ap, format);
88         vlog_error(format, ap);
89         va_end(ap);
90         exit(1);
91 }
92
93 tranzport_t *open_tranzport_core()
94 {
95         tranzport_t *z;
96         int val;
97
98         z = malloc(sizeof(tranzport_t));
99         if (!z)
100                 die("not enough memory");
101         memset(z, 0, sizeof(tranzport_t));
102
103         z->udev = open("/dev/tranzport0",O_RDWR);
104         if (!z->udev)
105                 die("unable to open tranzport");
106
107         return z;
108 }
109
110 tranzport_t *open_tranzport()
111 {
112 return open_tranzport_core();
113 }
114
115 void close_tranzport(tranzport_t *z)
116 {
117         int val;
118
119         val = close(z->udev);
120         if (val < 0)
121                 log_error("unable to release tranzport");
122
123         free(z);
124 }
125
126 int tranzport_write_core(tranzport_t *z, uint8_t *cmd, int timeout)
127 {
128         int val;
129         val = write(z->udev, cmd, 8);
130         if (val < 0)
131                 return val;
132         if (val != 8)
133                 return -1;
134         return 0;
135 }
136
137 int tranzport_lcdwrite(tranzport_t *z, uint8_t cell, char *text, int timeout)
138 {
139         uint8_t cmd[8];
140
141         if (cell > 9) {
142                 return -1;
143         }
144
145         cmd[0] = 0x00;
146         cmd[1] = 0x01;
147         cmd[2] = cell;
148         cmd[3] = text[0];
149         cmd[4] = text[1];
150         cmd[5] = text[2];
151         cmd[6] = text[3];
152         cmd[7] = 0x00;
153
154         return tranzport_write_core(z, cmd, timeout);
155 }
156
157 int tranzport_lighton(tranzport_t *z, uint8_t light, int timeout)
158 {
159         uint8_t cmd[8];
160
161         cmd[0] = 0x00;
162         cmd[1] = 0x00;
163         cmd[2] = light;
164         cmd[3] = 0x01;
165         cmd[4] = 0x00;
166         cmd[5] = 0x00;
167         cmd[6] = 0x00;
168         cmd[7] = 0x00;
169
170         return tranzport_write_core(z, &cmd[0], timeout);
171 }
172
173 int tranzport_lightoff(tranzport_t *z, uint8_t light, int timeout)
174 {
175         uint8_t cmd[8];
176
177         cmd[0] = 0x00;
178         cmd[1] = 0x00;
179         cmd[2] = light;
180         cmd[3] = 0x00;
181         cmd[4] = 0x00;
182         cmd[5] = 0x00;
183         cmd[6] = 0x00;
184         cmd[7] = 0x00;
185
186         return tranzport_write_core(z, &cmd[0], timeout);
187 }
188
189 int tranzport_read(tranzport_t *z, uint8_t *status, uint32_t *buttons, uint8_t *datawheel, int timeout)
190 {
191         uint8_t buf[8];
192         int val;
193
194         memset(buf, 0xff, 8);
195         val = read(z->udev, buf, 8);
196         if (val < 0) {
197                 // printf("errno: %d\n",errno);
198                 return val;
199         }
200         if (val != 8)
201                 return -1;
202
203         /*printf("read: %02x %02x %02x %02x %02x %02x %02x %02x\n", buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]);*/
204
205         *status = buf[1];
206
207         *buttons = 0;
208         *buttons |= buf[2] << 24;
209         *buttons |= buf[3] << 16;
210         *buttons |= buf[4] << 8;
211         *buttons |= buf[5];
212
213         *datawheel = buf[6];
214
215         return 0;
216 }
217
218 void lights_core(tranzport_t *z, uint32_t buttons, uint32_t buttonmask, uint8_t light)
219 {
220         if (buttons & buttonmask) {
221                 if (buttons & BUTTONMASK_SHIFT) {
222                         tranzport_lightoff(z, light, 1000);
223                 } else {
224                         tranzport_lighton(z, light, 1000);
225                 }
226         }
227 }
228
229 void do_lights(tranzport_t *z, uint32_t buttons)
230 {
231         lights_core(z, buttons, BUTTONMASK_RECORD, LIGHT_RECORD);
232         lights_core(z, buttons, BUTTONMASK_TRACKREC, LIGHT_TRACKREC);
233         lights_core(z, buttons, BUTTONMASK_TRACKMUTE, LIGHT_TRACKMUTE);
234         lights_core(z, buttons, BUTTONMASK_TRACKSOLO, LIGHT_TRACKSOLO);
235         lights_core(z, buttons, BUTTONMASK_TRACKSOLO, LIGHT_ANYSOLO);
236         lights_core(z, buttons, BUTTONMASK_PUNCH, LIGHT_PUNCH);
237         lights_core(z, buttons, BUTTONMASK_LOOP, LIGHT_LOOP);
238 }
239
240 void buttons_core(tranzport_t *z, uint32_t buttons, uint32_t buttonmask, char *str)
241 {
242         if (buttons & buttonmask)
243                 printf(" %s", str);
244 }
245
246 void do_buttons(tranzport_t *z, uint32_t buttons, uint8_t datawheel)
247 {
248         printf("buttons: %x ", buttons);
249         buttons_core(z, buttons, BUTTONMASK_BATTERY, "battery");
250         buttons_core(z, buttons, BUTTONMASK_BACKLIGHT, "backlight");
251         buttons_core(z, buttons, BUTTONMASK_TRACKLEFT, "trackleft");
252         buttons_core(z, buttons, BUTTONMASK_TRACKRIGHT, "trackright");
253         buttons_core(z, buttons, BUTTONMASK_TRACKREC, "trackrec");
254         buttons_core(z, buttons, BUTTONMASK_TRACKMUTE, "trackmute");
255         buttons_core(z, buttons, BUTTONMASK_TRACKSOLO, "tracksolo");
256         buttons_core(z, buttons, BUTTONMASK_UNDO, "undo");
257         buttons_core(z, buttons, BUTTONMASK_IN, "in");
258         buttons_core(z, buttons, BUTTONMASK_OUT, "out");
259         buttons_core(z, buttons, BUTTONMASK_PUNCH, "punch");
260         buttons_core(z, buttons, BUTTONMASK_LOOP, "loop");
261         buttons_core(z, buttons, BUTTONMASK_PREV, "prev");
262         buttons_core(z, buttons, BUTTONMASK_ADD, "add");
263         buttons_core(z, buttons, BUTTONMASK_NEXT, "next");
264         buttons_core(z, buttons, BUTTONMASK_REWIND, "rewind");
265         buttons_core(z, buttons, BUTTONMASK_FASTFORWARD, "fastforward");
266         buttons_core(z, buttons, BUTTONMASK_STOP, "stop");
267         buttons_core(z, buttons, BUTTONMASK_PLAY, "play");
268         buttons_core(z, buttons, BUTTONMASK_RECORD, "record");
269         buttons_core(z, buttons, BUTTONMASK_SHIFT, "shift");
270         if (datawheel)
271                 printf(" datawheel=%02x", datawheel);
272         printf("\n");
273 }
274
275 void do_lcd(tranzport_t *z)
276 {
277         tranzport_lcdwrite(z, 0, "    ", 1000);
278         tranzport_lcdwrite(z, 1, "DISL", 1000);
279         tranzport_lcdwrite(z, 2, "EXIA", 1000);
280         tranzport_lcdwrite(z, 3, " FOR", 1000);
281         tranzport_lcdwrite(z, 4, "    ", 1000);
282
283         tranzport_lcdwrite(z, 5, "    ", 1000);
284         tranzport_lcdwrite(z, 6, " CUR", 1000);
285         tranzport_lcdwrite(z, 7, "E FO", 1000);
286         tranzport_lcdwrite(z, 8, "UND ", 1000);
287         tranzport_lcdwrite(z, 9, "    ", 1000);
288 }
289
290 void do_lcd2(tranzport_t *z)
291 {
292         tranzport_lcdwrite(z, 0, "THE ", 1000);
293         tranzport_lcdwrite(z, 1, "TRAN", 1000);
294         tranzport_lcdwrite(z, 2, "ZPOR", 1000);
295         tranzport_lcdwrite(z, 3, "T RO", 1000);
296         tranzport_lcdwrite(z, 4, "  KS", 1000);
297
298         tranzport_lcdwrite(z, 5, "AWES", 1000);
299         tranzport_lcdwrite(z, 6, "OMEE", 1000);
300         tranzport_lcdwrite(z, 7, "LEEE", 1000);
301         tranzport_lcdwrite(z, 8, "UND ", 1000);
302         tranzport_lcdwrite(z, 9, "GROK", 1000);
303 }
304
305 lights_off(tranzport_t *z) {
306 int i;
307         for(i=0;i<7;i++) {
308         tranzport_lightoff(z, i, 1000);
309         }
310 }
311
312 lights_on(tranzport_t *z) {
313 int i;
314         for(i=0;i<7;i++) {
315         tranzport_lighton(z, i, 1000);
316         }
317 }
318
319 int main()
320 {
321         tranzport_t *z;
322         uint8_t status;
323         uint32_t buttons;
324         uint8_t datawheel;
325         int val;
326
327         z = open_tranzport();
328
329         do_lcd(z);
330
331         for(;;) {
332
333         do_lcd(z);
334         lights_on(z);
335         do_lcd2(z);
336         lights_off(z);
337
338 //              val = tranzport_read(z, &status, &buttons, &datawheel, 60000);
339                 val = -1;
340                 if (val < 0)
341                         continue;
342
343                 if (status == STATUS_OFFLINE) {
344                         printf("offline: ");
345                         continue;
346                 }
347
348                 if (status == STATUS_ONLINE) {
349                         printf("online: ");
350                         do_lcd(z);
351                 }
352
353                 do_lights(z, buttons);
354                 do_buttons(z, buttons, datawheel);
355         }
356
357         close_tranzport(z);
358
359         return 0;
360 }
361