-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProoveMain.cpp
89 lines (76 loc) · 1.97 KB
/
ProoveMain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include "Proove.h"
#include <algorithm>
#include <vector>
using namespace std;
/*
* Code ist mostly from: http://stackoverflow.com/questions/865668/how-to-parse-command-line-arguments-in-c
* @author iain
*/
class InputParser
{
public:
InputParser (int &argc, char **argv)
{
for (int i=1; i < argc; ++i)
{
this->tokens.push_back(string(argv[i]));
}
}
const string& getCmdOption(const string &option) const
{
vector<string>::const_iterator itr;
itr = find(this->tokens.begin(), this->tokens.end(), option);
if (itr != this->tokens.end() && ++itr != this->tokens.end())
{
return *itr;
}
static const string empty_string("");
return empty_string;
}
bool cmdOptionExists(const string &option) const
{
return find(this->tokens.begin(), this->tokens.end(), option)
!= this->tokens.end();
}
private:
vector<string> tokens;
};
int main(int argc, char **argv){
InputParser input(argc, argv);
if (input.getCmdOption("-p").empty()){
throw "-p can not be ommited";
}
if (input.getCmdOption("-t").empty()){
throw "-t can not be ommited";
}
if(input.cmdOptionExists("-g") && input.cmdOptionExists("-c")){
throw "Can't use -g and -i at the same time.";
}
int gpio = stoi(input.getCmdOption("-p"));
int transmitter_id = stoi(input.getCmdOption("-t"));
Proove* p = new Proove(gpio, transmitter_id);
if(input.cmdOptionExists("-c"))
{
int channel = stoi(input.getCmdOption("-c"));
if(input.cmdOptionExists("-o"))
{
p->channel_off(channel);
}
else
{
p->channel_on(channel);
}
}
else if(input.cmdOptionExists("-g"))
{
if(input.cmdOptionExists("-o"))
{
p->group_off();
}
else
{
p->group_on();
}
}
return 0;
}