-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
182 lines (154 loc) · 6.3 KB
/
main.cpp
File metadata and controls
182 lines (154 loc) · 6.3 KB
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <iostream>
#include <unistd.h>
#include <string>
#include <sstream>
#include <iomanip>
#include <hidapi/hidapi.h>
#include "rudat_6000_90_attenuator.hpp"
#include <cxxopts.hpp>
static unsigned short sVendorId = 0x20ce ;
static unsigned short sProductId = 0x0023 ;
// RUDAT programmable attenuators are represented in the Linux filesystem as:
// /dev/hidraw0 ... /dev/hidraw1 etc
// However with multiple attenuators in a system we can never know which device
// is enumerated as hidraw0 or hidraw1. On this basis we must identify attenuators
// based upon their serial number as these are the only ID unique to an individual
// attenuator.
int main( int argc, char* argv[] )
{
int retval = 0 ;
cxxopts::Options options( "rudatctrl", "Control of mini-circuits Rudat 6000 USB attenuators" ) ;
options.add_options()
( "l,list", "List serial numbers of connected attenuators" )
( "h,help", "Dislpay help" )
( "s,serial", "Serial number", cxxopts::value<std::string>() )
( "a,atten", "Attenuation", cxxopts::value<int>() ) ;
try
{
auto result = options.parse( argc, argv ) ;
int hidinit = hid_init() ;
if ( hidinit == -1 )
{
std::cerr << "hidapi library error" << std::endl ;
retval = -1 ;
}
if ( result.count( "help" ) )
{
std::cout << options.help() << std::endl ;
retval = 0 ;
}
else if ( result.count( "list" ) )
{
// List all of the Rudat attenuators in the system
struct hid_device_info * dev = 0 ;
struct hid_device_info * cur_dev = 0 ;
dev = hid_enumerate( sVendorId, sProductId ) ;
cur_dev = dev ;
while ( cur_dev )
{
hid_device * pHandle = hid_open_path( cur_dev->path ) ;
if ( pHandle != 0 )
{
md1tech::Rudat6k90 attenuator( pHandle ) ;
std::string serstr( "" ) ;
attenuator.getSerialNumber( serstr ) ;
std::cout << serstr << std::endl ;
hid_close( pHandle ) ;
}
cur_dev = cur_dev->next ;
}
retval = 0 ;
}
else if ( result.count( "serial" ) )
{
std::string serialstring = result[ "serial" ].as<std::string>() ;
// to get here the command line doesn't contain help or list, but does
// contain serial. Work out if it's get or set and then execute upon it
if ( result.count( "atten" ) )
{
int atten = result[ "atten" ].as<int>() ;
// Set attenuation command
struct hid_device_info * dev = 0 ;
struct hid_device_info * cur_dev = 0 ;
dev = hid_enumerate( sVendorId, sProductId ) ;
cur_dev = dev ;
bool found = false ;
while ( cur_dev && found == false )
{
hid_device * pHandle = hid_open_path( cur_dev->path ) ;
if ( pHandle != 0 )
{
md1tech::Rudat6k90 attenuator( pHandle ) ;
std::string serstr( "" ) ;
attenuator.getSerialNumber( serstr ) ;
if ( serstr == serialstring )
{
double a = 0 ;
if ( attenuator.setAttenuation( atten ) != true )
{
std::cerr << "attenuation out of range" << std::endl ;
}
else
{
found = true ;
}
}
hid_close( pHandle ) ;
}
cur_dev = cur_dev->next ;
}
if ( found == true ) { retval = 0 ; }
else { retval = -1 ; }
}
else
{
// Get attenuation command
struct hid_device_info * dev = 0 ;
struct hid_device_info * cur_dev = 0 ;
dev = hid_enumerate( sVendorId, sProductId ) ;
cur_dev = dev ;
bool found = false ;
while ( cur_dev && found == false )
{
hid_device * pHandle = hid_open_path( cur_dev->path ) ;
if ( pHandle != 0 )
{
md1tech::Rudat6k90 attenuator( pHandle ) ;
std::string serstr( "" ) ;
attenuator.getSerialNumber( serstr ) ;
if ( serstr == serialstring )
{
double a = 0 ;
if ( attenuator.readAttenuation( a ) != true )
{
std::cerr << "error reading attenuation" << std::endl ;
}
else
{
std::cout << a << std::endl ;
found = true ;
}
}
hid_close( pHandle ) ;
}
cur_dev = cur_dev->next ;
}
if ( found == true ) { retval = 0 ; }
else { retval = -1 ; }
}
}
else
{
std::cout << options.help() << std::endl ;
retval = -1 ;
}
hid_exit() ;
exit( retval ) ;
}
catch ( const cxxopts::OptionException& e )
{
std::cout << "error parsing options: " << e.what() << std::endl ;
exit( -1 ) ;
}
exit( retval ) ;
}