-
Notifications
You must be signed in to change notification settings - Fork 0
/
lpm.cc
114 lines (91 loc) · 3.79 KB
/
lpm.cc
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
* LED Pseudo Monochromator tool for communicating with Arduino that itself
* has firmware for controlling the attached LEDs; supports different commands
*/
#include <iostream>
#include <serial.h>
#include <boost/program_options.hpp>
#include <data.h>
#include <iostream>
#include <iterator>
#include "lpm.h"
// Supported commands:
// info (for getting information of all LED pins from arduino
// reset (for resetting and turning off all LEDs that are on)
// pwm 10,2000 (Turning on one LED on particular pin with pwm)
// shoot (For triggering IR LED to take picture from camera)
static int
cmd_pwm(device::lpm &lpm, const std::vector<std::string> &args)
{
namespace po = boost::program_options;
uint16_t pwm;
uint8_t pin;
po::options_description pwm_opts("pwm options");
pwm_opts.add_options()
("led", po::value<uint8_t>(&pin), "LED")
("pwm", po::value<uint16_t>(&pwm), "PIN");
po::variables_map vm;
po::store(po::command_line_parser(args).options(pwm_opts).run(), vm);
std::cerr << "[D] led: " << pin << " pwm: " << pwm << std::endl;
lpm.led(pin, pwm);
return 0;
}
int main(int argc, char **argv) {
namespace po = boost::program_options;
std::string device;
std::string input;
po::options_description opts("LED Pseudo Monochromatic Command Line tool");
opts.add_options()
("help", "Flag for Help")
("device", po::value<std::string>(&device), "device file for aurdrino")
("input", po::value<std::string>(&input), "Specify command (info, pwm, reset, shoot)")
("args", po::value<std::vector<std::string>>(), "Arguments for command");
po::positional_options_description pos;
pos.add("input", 1).add("args", -1);
po::variables_map vm;
std::vector<std::string> args;
try {
po::parsed_options parsed = po::command_line_parser(argc, argv).options(opts).positional(pos).run();
po::store(parsed, vm);
po::notify(vm);
args = po::collect_unrecognized(parsed.options, po::include_positional);
args.erase(args.begin());
} catch (const std::exception &e) {
std::cerr << "Error while parsing commad line options: " << std::endl;
std::cerr << "\t" << e.what() << std::endl;
return 1;
}
if(vm.count("help")) {
//show program options
std::cout << opts << std::endl;
std::cout << "Supported Commands:" << std::endl << std::endl;
std::cout << "info (for getting information of all LED pins from arduino" << std::endl;
std::cout << "reset (for resetting and turning off all LEDs that are on)" << std::endl;
std::cout << "pwm 10,2000 (Turning on one LED on particular pin with pwm)" << std::endl;
std::cout << "shoot (For triggering IR LED to take picture from camera)" << std::endl;
std::cout << "raw Send raw data to lpm" << std::endl;
std::cout << std::endl;
}
std::cerr << "[D] Device: " << device << std::endl;
std::cerr << "[D] Input: \"" << input << "\"" << std::endl;
device::lpm lpm = device::lpm::open(device);
if (input == "pwm") {
cmd_pwm(lpm, args);
} else if(input == "info") {
lpm.getInfo();
} else if(input == "reset") {
lpm.reset();
} else if(input == "shoot") {
lpm.shoot();
} else if (input == "raw") {
std::stringstream data;
std::copy(args.cbegin(), args.cend(), std::ostream_iterator<std::string>(data, " "));
std::cerr << "[D] [" << data.str() << "]" << std::endl;
std::string response = lpm.send_and_receive(data.str());
std::cout << "[R] [" << response << "]" << std::endl;
} else {
std::cout <<"[E] Unkown command! " << std::endl;
return -1;
}
return 0;
}