Improve indentation and whitespace cleanup
[libftdi] / examples / bitbang_ft2232.c
1 /* bitbang_ft2232.c
2
3    Output some flickering in bitbang mode to the FT2232
4
5    Thanks to max@koeln.ccc.de for fixing and extending
6    the example for the second channel.
7
8    This program is distributed under the GPL, version 2
9 */
10
11 #include <stdio.h>
12 #include <unistd.h>
13 #include <ftdi.h>
14
15 int main(int argc, char **argv)
16 {
17     struct ftdi_context ftdic, ftdic2;
18     char buf[1];
19     int f,i;
20
21     // Init 1. channel
22     ftdi_init(&ftdic);
23     ftdi_set_interface(&ftdic, INTERFACE_A);
24     f = ftdi_usb_open(&ftdic, 0x0403, 0x6001);
25     if (f < 0 && f != -5)
26     {
27         fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(&ftdic));
28         exit(-1);
29     }
30     printf("ftdi open succeeded(channel 1): %d\n",f);
31
32     printf("enabling bitbang mode(channel 1)\n");
33     ftdi_enable_bitbang(&ftdic, 0xFF);
34
35     // Init 2. channel
36     ftdi_init(&ftdic2);
37     ftdi_set_interface(&ftdic2, INTERFACE_B);
38     f = ftdi_usb_open(&ftdic2, 0x0403, 0x6001);
39     if (f < 0 && f != -5)
40     {
41         fprintf(stderr, "unable to open ftdi device: %d (%s)\n", f, ftdi_get_error_string(&ftdic2));
42         exit(-1);
43     }
44     printf("ftdi open succeeded(channel 2): %d\n",f);
45
46     printf("enabling bitbang mode (channel 2)\n");
47     ftdi_enable_bitbang(&ftdic2, 0xFF);
48
49     // Write data
50     printf("startloop\n");
51     for (i = 0; i < 23; i++)
52     {
53         buf[0] =  0x1;
54         printf("porta: %02i: 0x%02x \n",i,buf[0]);
55         f = ftdi_write_data(&ftdic, buf, 1);
56         if (f < 0)
57             fprintf(stderr,"write failed on channel 1 for 0x%x, error %d (%s)\n", buf[0], f, ftdi_get_error_string(&ftdic));
58         sleep(1);
59
60         buf[0] =  0x2;
61         printf("porta: %02i: 0x%02x \n",i,buf[0]);
62         f = ftdi_write_data(&ftdic, buf, 1);
63         if (f < 0)
64             fprintf(stderr,"write failed on channel 1 for 0x%x, error %d (%s)\n", buf[0], f, ftdi_get_error_string(&ftdic));
65         sleep(1);
66
67         buf[0] =  0x1;
68         printf("portb: %02i: 0x%02x \n",i,buf[0]);
69         f = ftdi_write_data(&ftdic2, buf, 1);
70         if (f < 0)
71             fprintf(stderr,"write failed on channel 2 for 0x%x, error %d (%s)\n", buf[0], f, ftdi_get_error_string(&ftdic2));
72         sleep(1);
73
74         buf[0] =  0x2;
75         printf("portb: %02i: 0x%02x \n",i,buf[0]);
76         f = ftdi_write_data(&ftdic2, buf, 1);
77         if (f < 0)
78             fprintf(stderr,"write failed on channel 2 for 0x%x, error %d (%s)\n", buf[0], f, ftdi_get_error_string(&ftdic2));
79         sleep(1);
80     }
81     printf("\n");
82
83     printf("disabling bitbang mode(channel 1)\n");
84     ftdi_disable_bitbang(&ftdic);
85     ftdi_usb_close(&ftdic);
86     ftdi_deinit(&ftdic);
87
88     printf("disabling bitbang mode(channel 2)\n");
89     ftdi_disable_bitbang(&ftdic2);
90     ftdi_usb_close(&ftdic2);
91     ftdi_deinit(&ftdic2);
92 }