Преглед изворни кода

version option; default device 0

tags/v1.0
Robin Thoni пре 10 година
родитељ
комит
3fd4d73508
1 измењених фајлова са 26 додато и 8 уклоњено
  1. 26
    8
      src/mainclass.cpp

+ 26
- 8
src/mainclass.cpp Прегледај датотеку

@@ -23,7 +23,8 @@ int MainClass::usage()
23 23
     " [--up OUTPUT|--up=OUTPUT|-u OUTPUT]" << std::endl <<
24 24
     " [--down OUTPUT|--down=OUPUT|-d OUTPUT]" << std::endl <<
25 25
     " [--sleep MS|--sleep=MS|-s MS]" << std::endl <<
26
-    " [--help|-h]" << std::endl;
26
+    " [--help|-h]" << std::endl <<
27
+    " [--version|-v]" << std::endl;
27 28
   std::cerr << std::endl << "Controls the output pins of a PiFace digital IO."
28 29
     << std::endl << std::endl;
29 30
   std::cerr << "--device \tSwitch to the provided device."
@@ -31,6 +32,7 @@ int MainClass::usage()
31 32
     << std::endl << "--down \t\tDisable the provided output pin."
32 33
     << std::endl << "--sleep \tSleep for the provided amount of milliseconds."
33 34
     << std::endl << "--help \t\tDisplay this message."
35
+    << std::endl << "--version \tDisplay version."
34 36
     << std::endl << std::endl;
35 37
   std::cerr << "OUTPUT \t: The output pin to enable/disable. 0 <= OUTPUT <= 7."
36 38
     << std::endl << "MS \t: The number of milliseconds to sleep. 0 <= MS <= "
@@ -57,17 +59,16 @@ bool MainClass::build_actions_()
57 59
     {"down", 1, 0, 'd'},
58 60
     {"sleep", 1, 0, 's'},
59 61
     {"help", 0, 0, 'h'},
62
+    {"version", 0, 0, 'v'},
60 63
     {0, 0, 0, 0}
61 64
   };
62 65
   int opt;
63 66
   extern int optind;
64 67
   extern char* optarg;
65 68
   char* endptr;
66
-  while ((opt = getopt_long(argc_, argv_, "e:u:d:s:h", opts, 0)) != -1)
69
+  while ((opt = getopt_long(argc_, argv_, "e:u:d:s:hv", opts, 0)) != -1)
67 70
   {
68
-    if (!optarg)
69
-      return false;
70
-    long int arg = strtol(optarg, &endptr, 10);
71
+    long int arg = optarg ? strtol(optarg, &endptr, 10) : 0;
71 72
     if (arg < 0)
72 73
       return bad_value_();
73 74
     if (opt == 'e')
@@ -76,8 +77,7 @@ bool MainClass::build_actions_()
76 77
         return bad_value_();
77 78
       actions_.push_back([arg]() -> bool
78 79
           {
79
-          if (get_current_device() != -1)
80
-            pifacedigital_close(get_current_device());
80
+          pifacedigital_close(get_current_device());
81 81
           return pifacedigital_open(set_current_device(arg)) != -1;
82 82
           });
83 83
     }
@@ -87,6 +87,9 @@ bool MainClass::build_actions_()
87 87
         return bad_value_();
88 88
       actions_.push_back([arg]() -> bool
89 89
           {
90
+          if (get_current_device() == -1 &&
91
+            pifacedigital_open(set_current_device(arg)) == -1)
92
+            return false;
90 93
           pifacedigital_write_reg(up_pin(arg), OUTPUT, get_current_device());
91 94
           return true;
92 95
           });
@@ -97,6 +100,9 @@ bool MainClass::build_actions_()
97 100
         return bad_value_();
98 101
       actions_.push_back([arg]() -> bool
99 102
           {
103
+          if (get_current_device() == -1 &&
104
+            pifacedigital_open(set_current_device(arg)) == -1)
105
+            return false;
100 106
           pifacedigital_write_reg(down_pin(arg), OUTPUT, get_current_device());
101 107
           return true;
102 108
           });
@@ -111,6 +117,18 @@ bool MainClass::build_actions_()
111 117
           return true;
112 118
           });
113 119
     }
120
+    else if (opt == 'v')
121
+    {
122
+      std::cout << "camotion-piface 1.0" << std::endl;
123
+      actions_.clear();
124
+      return true;
125
+    }
126
+    else if (opt == 'h')
127
+    {
128
+      usage();
129
+      actions_.clear();
130
+      return true;
131
+    }
114 132
     else
115 133
       return false;
116 134
   }
@@ -119,7 +137,7 @@ bool MainClass::build_actions_()
119 137
 
120 138
 int MainClass::execute()
121 139
 {
122
-  if (argc_ <= 2 || !build_actions_())
140
+  if (argc_ == 1 || !build_actions_())
123 141
     return usage();
124 142
   for (auto f : actions_)
125 143
     if (!f())

Loading…
Откажи
Сачувај