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