FreeCalypso > hg > freecalypso-hwlab
annotate lcdtest/main.c @ 22:e9df47110181
lcdtest: added ability to specify chdir on the command line
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Sat, 14 Apr 2018 06:15:24 +0000 |
parents | 4194b6744890 |
children | 2127e6fdeab2 |
rev | line source |
---|---|
12
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 #include <sys/types.h> |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 #include <stdio.h> |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 #include <stdlib.h> |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 #include <unistd.h> |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 #include <ftdi.h> |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 |
15
4194b6744890
lcdtest: look for our custom USB ID by default
Mychaela Falconia <falcon@freecalypso.org>
parents:
12
diff
changeset
|
7 char *device_selector = "i:0x0403:0x7157"; |
12
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 struct ftdi_context ftdi; |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 process_cmdline(argc, argv) |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 char **argv; |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 { |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 int c; |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 extern char *optarg; |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
15 |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 while ((c = getopt(argc, argv, "d:")) != EOF) { |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 switch (c) { |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 case 'd': |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 device_selector = optarg; |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 continue; |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
21 default: |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 /* error msg already printed */ |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 exit(1); |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 } |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
25 } |
22
e9df47110181
lcdtest: added ability to specify chdir on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents:
15
diff
changeset
|
26 switch (argc - optind) { |
e9df47110181
lcdtest: added ability to specify chdir on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents:
15
diff
changeset
|
27 case 0: |
e9df47110181
lcdtest: added ability to specify chdir on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents:
15
diff
changeset
|
28 break; |
e9df47110181
lcdtest: added ability to specify chdir on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents:
15
diff
changeset
|
29 case 1: |
e9df47110181
lcdtest: added ability to specify chdir on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents:
15
diff
changeset
|
30 if (chdir(argv[optind]) < 0) { |
e9df47110181
lcdtest: added ability to specify chdir on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents:
15
diff
changeset
|
31 perror(argv[optind]); |
e9df47110181
lcdtest: added ability to specify chdir on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents:
15
diff
changeset
|
32 exit(1); |
e9df47110181
lcdtest: added ability to specify chdir on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents:
15
diff
changeset
|
33 } |
e9df47110181
lcdtest: added ability to specify chdir on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents:
15
diff
changeset
|
34 break; |
e9df47110181
lcdtest: added ability to specify chdir on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents:
15
diff
changeset
|
35 default: |
e9df47110181
lcdtest: added ability to specify chdir on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents:
15
diff
changeset
|
36 fprintf(stderr, "usage: %s [options] [chdir]\n", argv[0]); |
e9df47110181
lcdtest: added ability to specify chdir on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents:
15
diff
changeset
|
37 exit(1); |
e9df47110181
lcdtest: added ability to specify chdir on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents:
15
diff
changeset
|
38 } |
12
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
39 } |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
40 |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 main(argc, argv) |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 char **argv; |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 { |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 char command[512]; |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
45 |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
46 process_cmdline(argc, argv); |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 ftdi_init(&ftdi); |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 if (ftdi_usb_open_string(&ftdi, device_selector) < 0) { |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 fprintf(stderr, "FTDI USB open failed: %s\n", ftdi.error_str); |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 exit(1); |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 } |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 if (ftdi_set_bitmode(&ftdi, 0, BITMODE_MCU) < 0) { |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 fprintf(stderr, "unable to enter MCU mode: %s\n", |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 ftdi.error_str); |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 exit(1); |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 } |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 for (;;) { |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 if (isatty(0)) { |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 fputs("lcdtest> ", stdout); |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 fflush(stdout); |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 } |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 if (!fgets(command, sizeof command, stdin)) |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 exit(0); |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 dispatch_cmd(command); |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 } |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 } |