cf1675bd521e1b58871995ca351aabad5e5418b1
[libftdi] / examples / find_all_pp.cpp
1 /* final_all_pp.cpp
2
3    Simple libftdi-cpp usage
4
5    This program is distributed under the GPL, version 2
6 */
7
8 #include "ftdi.hpp"
9 #include <iostream>
10 #include <iomanip>
11 #include <cstdlib>
12 #include <cstring>
13 using namespace Ftdi;
14
15 int main(int argc, char **argv)
16 {
17     // Show help
18     if (argc > 1)
19     {
20         if (strcmp(argv[1],"-h") == 0 || strcmp(argv[1],"--help") == 0)
21         {
22             std::cout << "Usage: " << argv[0] << " [-v VENDOR_ID] [-p PRODUCT_ID]" << std::endl;
23             return EXIT_SUCCESS;
24         }
25     }
26
27     // Parse args
28     int vid = 0x0403, pid = 0x6010, tmp = 0;
29     for (int i = 0; i < (argc - 1); i++)
30     {
31         if (strcmp(argv[i], "-v") == 0)
32             if ((tmp = strtol(argv[++i], 0, 16)) >= 0)
33                 vid = tmp;
34
35         if (strcmp(argv[i], "-p") == 0)
36             if ((tmp = strtol(argv[++i], 0, 16)) >= 0)
37                 pid = tmp;
38     }
39
40     // Print header
41     std::cout << std::hex << std::showbase
42     << "Found devices ( VID: " << vid << ", PID: " << pid << " )"
43     << std::endl
44     << "------------------------------------------------"
45     << std::endl << std::dec;
46
47     // Print whole list
48     List* list = List::find_all(vid, pid);
49     for (List::iterator it = list->begin(); it != list->end(); it++)
50     {
51         std::cout << "FTDI (" << &*it << "): "
52         << it->vendor() << ", "
53         << it->description() << ", "
54         << it->serial();
55
56         // Open test
57         if(it->open() == 0)
58            std::cout << " (Open OK)";
59         else
60            std::cout << " (Open FAILED)";
61
62         it->close();
63
64         std::cout << std::endl;
65
66     }
67
68     delete list;
69
70     return EXIT_SUCCESS;
71 }