building parts of libftdi via cmake is now optional.
[libftdi] / examples / bitbang2.c
CommitLineData
ad397a4b
TJ
1/* ftdi_out.c
2 *
22d12cda
TJ
3 * Output a (stream of) byte(s) in bitbang mode to the
4 * ftdi245 chip that is (hopefully) attached.
ad397a4b 5 *
22d12cda
TJ
6 * We have a little board that has a FT245BM chip and
7 * the 8 outputs are connected to several different
8 * things that we can turn on and off with this program.
ad397a4b
TJ
9 *
10 * If you have an idea about hardware that can easily
11 * interface onto an FTDI chip, I'd like to collect
12 * ideas. If I find it worthwhile to make, I'll consider
13 * making it, I'll even send you a prototype (against
14 * cost-of-material) if you want.
22d12cda
TJ
15 *
16 * At "harddisk-recovery.nl" they have a little board that
17 * controls the power to two harddrives and two fans.
18 *
ad397a4b
TJ
19 * -- REW R.E.Wolff@BitWizard.nl
20 *
21 *
22 *
22d12cda 23 * This program was based on libftdi_example_bitbang2232.c
ad397a4b
TJ
24 * which doesn't carry an author or attribution header.
25 *
26 *
22d12cda 27 * This program is distributed under the GPL, version 2.
ad397a4b
TJ
28 * Millions copies of the GPL float around the internet.
29 */
30
31
32#include <stdio.h>
33#include <unistd.h>
7b5c5624
UB
34#ifdef __WIN32__
35#define usleep(x) Sleep((x+999)/1000)
36#endif
ad397a4b
TJ
37#include <ftdi.h>
38
39void ftdi_fatal (struct ftdi_context *ftdi, char *str)
40{
41 fprintf (stderr, "%s: %s\n",
42 str, ftdi_get_error_string (ftdi));
43 exit (1);
44}
45
46int main(int argc, char **argv)
47{
48 struct ftdi_context ftdic;
49 int i, t;
50 unsigned char data;
51 int delay = 100000; /* 100 thousand microseconds: 1 tenth of a second */
52
22d12cda
TJ
53 while ((t = getopt (argc, argv, "d:")) != -1)
54 {
55 switch (t)
56 {
57 case 'd':
58 delay = atoi (optarg);
59 break;
ad397a4b
TJ
60 }
61 }
62
6ac169ea
TJ
63 if (ftdi_init(&ftdic) < 0)
64 {
65 fprintf(stderr, "ftdi_init failed\n");
66 return EXIT_FAILURE;
67 }
ad397a4b
TJ
68
69 if (ftdi_usb_open(&ftdic, 0x0403, 0x6001) < 0)
70 ftdi_fatal (&ftdic, "Can't open ftdi device");
71
5484786d 72 if (ftdi_set_bitmode(&ftdic, 0xFF, BITMODE_BITBANG) < 0)
ad397a4b
TJ
73 ftdi_fatal (&ftdic, "Can't enable bitbang");
74
22d12cda
TJ
75 for (i=optind; i < argc ; i++)
76 {
ad397a4b
TJ
77 sscanf (argv[i], "%x", &t);
78 data = t;
22d12cda
TJ
79 if (ftdi_write_data(&ftdic, &data, 1) < 0)
80 {
ad397a4b
TJ
81 fprintf(stderr,"write failed for 0x%x: %s\n",
82 data, ftdi_get_error_string(&ftdic));
83 }
84 usleep(delay);
85 }
86
87 ftdi_usb_close(&ftdic);
88 ftdi_deinit(&ftdic);
89 exit (0);
90}