Переглянути джерело

version option; default device 0

tags/v1.0
Robin Thoni 9 роки тому
джерело
коміт
3fd4d73508
1 змінених файлів з 26 додано та 8 видалено
  1. 26
    8
      src/mainclass.cpp

+ 26
- 8
src/mainclass.cpp Переглянути файл

23
     " [--up OUTPUT|--up=OUTPUT|-u OUTPUT]" << std::endl <<
23
     " [--up OUTPUT|--up=OUTPUT|-u OUTPUT]" << std::endl <<
24
     " [--down OUTPUT|--down=OUPUT|-d OUTPUT]" << std::endl <<
24
     " [--down OUTPUT|--down=OUPUT|-d OUTPUT]" << std::endl <<
25
     " [--sleep MS|--sleep=MS|-s MS]" << std::endl <<
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
   std::cerr << std::endl << "Controls the output pins of a PiFace digital IO."
28
   std::cerr << std::endl << "Controls the output pins of a PiFace digital IO."
28
     << std::endl << std::endl;
29
     << std::endl << std::endl;
29
   std::cerr << "--device \tSwitch to the provided device."
30
   std::cerr << "--device \tSwitch to the provided device."
31
     << std::endl << "--down \t\tDisable the provided output pin."
32
     << std::endl << "--down \t\tDisable the provided output pin."
32
     << std::endl << "--sleep \tSleep for the provided amount of milliseconds."
33
     << std::endl << "--sleep \tSleep for the provided amount of milliseconds."
33
     << std::endl << "--help \t\tDisplay this message."
34
     << std::endl << "--help \t\tDisplay this message."
35
+    << std::endl << "--version \tDisplay version."
34
     << std::endl << std::endl;
36
     << std::endl << std::endl;
35
   std::cerr << "OUTPUT \t: The output pin to enable/disable. 0 <= OUTPUT <= 7."
37
   std::cerr << "OUTPUT \t: The output pin to enable/disable. 0 <= OUTPUT <= 7."
36
     << std::endl << "MS \t: The number of milliseconds to sleep. 0 <= MS <= "
38
     << std::endl << "MS \t: The number of milliseconds to sleep. 0 <= MS <= "
57
     {"down", 1, 0, 'd'},
59
     {"down", 1, 0, 'd'},
58
     {"sleep", 1, 0, 's'},
60
     {"sleep", 1, 0, 's'},
59
     {"help", 0, 0, 'h'},
61
     {"help", 0, 0, 'h'},
62
+    {"version", 0, 0, 'v'},
60
     {0, 0, 0, 0}
63
     {0, 0, 0, 0}
61
   };
64
   };
62
   int opt;
65
   int opt;
63
   extern int optind;
66
   extern int optind;
64
   extern char* optarg;
67
   extern char* optarg;
65
   char* endptr;
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
     if (arg < 0)
72
     if (arg < 0)
72
       return bad_value_();
73
       return bad_value_();
73
     if (opt == 'e')
74
     if (opt == 'e')
76
         return bad_value_();
77
         return bad_value_();
77
       actions_.push_back([arg]() -> bool
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
           return pifacedigital_open(set_current_device(arg)) != -1;
81
           return pifacedigital_open(set_current_device(arg)) != -1;
82
           });
82
           });
83
     }
83
     }
87
         return bad_value_();
87
         return bad_value_();
88
       actions_.push_back([arg]() -> bool
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
           pifacedigital_write_reg(up_pin(arg), OUTPUT, get_current_device());
93
           pifacedigital_write_reg(up_pin(arg), OUTPUT, get_current_device());
91
           return true;
94
           return true;
92
           });
95
           });
97
         return bad_value_();
100
         return bad_value_();
98
       actions_.push_back([arg]() -> bool
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
           pifacedigital_write_reg(down_pin(arg), OUTPUT, get_current_device());
106
           pifacedigital_write_reg(down_pin(arg), OUTPUT, get_current_device());
101
           return true;
107
           return true;
102
           });
108
           });
111
           return true;
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
     else
132
     else
115
       return false;
133
       return false;
116
   }
134
   }
119
 
137
 
120
 int MainClass::execute()
138
 int MainClass::execute()
121
 {
139
 {
122
-  if (argc_ <= 2 || !build_actions_())
140
+  if (argc_ == 1 || !build_actions_())
123
     return usage();
141
     return usage();
124
   for (auto f : actions_)
142
   for (auto f : actions_)
125
     if (!f())
143
     if (!f())

Завантаження…
Відмінити
Зберегти