-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgm.cpp
127 lines (103 loc) · 2.79 KB
/
gm.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
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
#include "gm.h"
#include "usb_handle_registry.h"
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#define MAX_FD 10
GM::GM(const GM &gm){
handle = nullptr;
is_connected = false;
value = gm.value;
channel = gm.channel;
serial_number = gm.serial_number;
}
const GM& GM::operator=(const GM& gm){
handle = nullptr;
is_connected = false;
value = gm.value;
channel = gm.channel;
serial_number = gm.serial_number;
return *this;
}
// Wrapper for driver
// Returns true if value of GM changed and actually moves mirror
bool GM::setValue(int v){
if(v < MIN_GM_VALUE){
v = MIN_GM_VALUE;
}
if(v > MAX_GM_VALUE){
v = MAX_GM_VALUE;
}
bool rv = (value == v) && is_connected;
value = v;
if(is_connected){
usbAOut_USB31XX(handle, channel, (uint16_t) value, 0);
}
return rv;
}
// Sets up communication to device
bool GM::connectDevice() {
if(!is_connected) {
// There maybe problems with calling this multiple times.
int rv = hid_init();
if (rv < 0) {
std::cerr << "hid_init: Failed" <<std::endl;
return false;
}
if(serial_number == "") {
handle = hid_open(MCC_VID, USB3103_PID, NULL);
} else {
std::wstring temp(serial_number.length(), L' ');
std::copy(serial_number.begin(), serial_number.end(), temp.begin());
handle = hid_open(MCC_VID, USB3103_PID, temp.c_str());
}
if (handle == nullptr) {
handle = usb_handle::getHidHandle(serial_number);
if (handle ==nullptr) {
std::cerr << "Failed to connect to device" << std::endl;
return false;
}
}
usbDConfigPort_USB31XX(handle, DIO_DIR_OUT);
usbDOut_USB31XX(handle, 0);
if(!updateSerialNumber()) {
std::cerr << "Problem with serial number of device" << std::endl;
return false;
}
usb_handle::addHidHandle(serial_number, handle);
// Configures channel to output between 0 and 10V and also sets output to 0.
usbAOutConfig_USB31XX(handle, channel, UP_10_00V);
std::cout << "Connected to GM" << std::endl;
is_connected = true;
}
return is_connected;
}
GM::~GM(){
disconnectDevice();
}
bool GM::disconnectDevice(){
if(is_connected) {
usb_handle::removeHidHandle(serial_number);
handle = nullptr;
is_connected = false;
}
return !is_connected;
}
bool GM::updateSerialNumber() {
if(handle != nullptr) {
wchar_t new_serial_number_c_str[64];
hid_get_serial_number_string(handle, new_serial_number_c_str, 64);
if (new_serial_number_c_str == nullptr) {
return false;
}
std::wstring new_serial_number_wstring = new_serial_number_c_str;
std::string new_serial_number(new_serial_number_wstring.length(), ' ');
std::copy(new_serial_number_wstring.begin(), new_serial_number_wstring.end(), new_serial_number.begin());
if (serial_number == "") {
serial_number = new_serial_number;
}
return serial_number == new_serial_number;
}
return false;
}