FreeCalypso > hg > freecalypso-hwlab
annotate lcdtest/testmain.c @ 176:fb2f6497ba53 default tip
doc/Linux-DTR-RTS-flaw: point to new location of this article
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Mon, 11 Dec 2023 19:37:20 +0000 |
parents | de3d3cfcbb35 |
children |
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> |
27
4b7cac119fb5
lcdtest: added our own local exitcodes.h header
Mychaela Falconia <falcon@freecalypso.org>
parents:
26
diff
changeset
|
6 #include "exitcodes.h" |
12
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 |
23
2127e6fdeab2
lcdtest: simplify USB device selection by serial string
Mychaela Falconia <falcon@freecalypso.org>
parents:
22
diff
changeset
|
8 char *device_serial; |
12
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 struct ftdi_context ftdi; |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 process_cmdline(argc, argv) |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 char **argv; |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 { |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
14 int c; |
28
de3d3cfcbb35
lcdtest: lcdphone program put together, compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
27
diff
changeset
|
15 extern int optind; |
12
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
16 extern char *optarg; |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
17 |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
18 while ((c = getopt(argc, argv, "d:")) != EOF) { |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
19 switch (c) { |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
20 case 'd': |
23
2127e6fdeab2
lcdtest: simplify USB device selection by serial string
Mychaela Falconia <falcon@freecalypso.org>
parents:
22
diff
changeset
|
21 device_serial = optarg; |
12
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
22 continue; |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
23 default: |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
24 /* error msg already printed */ |
27
4b7cac119fb5
lcdtest: added our own local exitcodes.h header
Mychaela Falconia <falcon@freecalypso.org>
parents:
26
diff
changeset
|
25 exit(ERROR_USAGE); |
12
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
26 } |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
27 } |
22
e9df47110181
lcdtest: added ability to specify chdir on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents:
15
diff
changeset
|
28 switch (argc - optind) { |
e9df47110181
lcdtest: added ability to specify chdir on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents:
15
diff
changeset
|
29 case 0: |
e9df47110181
lcdtest: added ability to specify chdir on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents:
15
diff
changeset
|
30 break; |
e9df47110181
lcdtest: added ability to specify chdir on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents:
15
diff
changeset
|
31 case 1: |
e9df47110181
lcdtest: added ability to specify chdir on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents:
15
diff
changeset
|
32 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
|
33 perror(argv[optind]); |
27
4b7cac119fb5
lcdtest: added our own local exitcodes.h header
Mychaela Falconia <falcon@freecalypso.org>
parents:
26
diff
changeset
|
34 exit(ERROR_UNIX); |
22
e9df47110181
lcdtest: added ability to specify chdir on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents:
15
diff
changeset
|
35 } |
e9df47110181
lcdtest: added ability to specify chdir on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents:
15
diff
changeset
|
36 break; |
e9df47110181
lcdtest: added ability to specify chdir on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents:
15
diff
changeset
|
37 default: |
e9df47110181
lcdtest: added ability to specify chdir on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents:
15
diff
changeset
|
38 fprintf(stderr, "usage: %s [options] [chdir]\n", argv[0]); |
27
4b7cac119fb5
lcdtest: added our own local exitcodes.h header
Mychaela Falconia <falcon@freecalypso.org>
parents:
26
diff
changeset
|
39 exit(ERROR_USAGE); |
22
e9df47110181
lcdtest: added ability to specify chdir on the command line
Mychaela Falconia <falcon@freecalypso.org>
parents:
15
diff
changeset
|
40 } |
12
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
41 } |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
42 |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 main(argc, argv) |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 char **argv; |
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 char command[512]; |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 process_cmdline(argc, argv); |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 ftdi_init(&ftdi); |
23
2127e6fdeab2
lcdtest: simplify USB device selection by serial string
Mychaela Falconia <falcon@freecalypso.org>
parents:
22
diff
changeset
|
50 if (ftdi_usb_open_desc(&ftdi, 0x0403, 0x7157, 0, device_serial) < 0) { |
12
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 fprintf(stderr, "FTDI USB open failed: %s\n", ftdi.error_str); |
27
4b7cac119fb5
lcdtest: added our own local exitcodes.h header
Mychaela Falconia <falcon@freecalypso.org>
parents:
26
diff
changeset
|
52 exit(ERROR_FTDI); |
12
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 } |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
54 if (ftdi_set_bitmode(&ftdi, 0, BITMODE_MCU) < 0) { |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 fprintf(stderr, "unable to enter MCU mode: %s\n", |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 ftdi.error_str); |
27
4b7cac119fb5
lcdtest: added our own local exitcodes.h header
Mychaela Falconia <falcon@freecalypso.org>
parents:
26
diff
changeset
|
57 exit(ERROR_FTDI); |
12
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
58 } |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 for (;;) { |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
60 if (isatty(0)) { |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 fputs("lcdtest> ", stdout); |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 fflush(stdout); |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
63 } |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
64 if (!fgets(command, sizeof command, stdin)) |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
65 exit(0); |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
66 dispatch_cmd(command); |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
67 } |
5eaf832d57d0
lcdtest program started, skeleton compiles
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
68 } |