-
Notifications
You must be signed in to change notification settings - Fork 1
/
diode.cpp
152 lines (121 loc) · 3.61 KB
/
diode.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "diode.h"
#include "pmd.h"
#include "usb-1608G.h"
#include "usb_handle_registry.h"
bool Diode::is_lib_usb_init = false;
Diode::Diode(const Diode& diode) {
is_connected = false;
handle = nullptr;
channel = diode.channel;
serial_number = diode.serial_number;
num_samples = diode.num_samples;
sampling_rate = diode.sampling_rate;
}
const Diode& Diode::operator=(const Diode& diode) {
is_connected = false;
handle = nullptr;
channel = diode.channel;
serial_number = diode.serial_number;
num_samples = diode.num_samples;
sampling_rate = diode.sampling_rate;
return *this;
}
Diode::~Diode() {
disconnectDevice();
}
float Diode::getValue() {
ScanList list[0]; // Used to configure the input
// TODO make this member values so that it can be easily changed at runtime.
list[0].mode = DIFFERENTIAL | LAST_CHANNEL; // Gets sign of value
list[0].range = BP_10V; // between +- 10V (using smaller gains gives more accurate results?)
list[0].channel = channel;
// Configures the next scan
usbAInConfig_USB1608G(handle, list);
// Buffer to hold the read data
uint16_t data[num_samples];
// Starts the Scan
usbAInScanStart_USB1608G(handle, num_samples, 0, sampling_rate, 0x0);
// Gets the data from the scan after it is done. (last parameter )
int bytes_read = usbAInScanRead_USB1608G(handle, num_samples, /*num_channels*/ 1, data, /*timeout*/ 20000);
if (bytes_read != 2 * num_samples) {
std::cerr << "ERROR IN DIODE SCAN!!!!" << std::endl;
return 0;
}
// Average all samples
// TODO ignore outliers
int sum_samples = 0;
for(int i = 0; i < num_samples; ++i) {
sum_samples += (int)data[i];
}
float avg_value = float(sum_samples) / float(num_samples);
std::cout << sum_samples << " " << num_samples << std::endl;
return avg_value;
}
float Diode::getVoltage() {
std::cout << "Diode::getVoltage" << std::endl;
return convertToVoltage(getValue());
}
float Diode::convertToVoltage(float value) {
// For BP_10V
return (value - 32768.0) * 10.0 / 32768.0;
// see volts_USB1608G for other gains
}
bool Diode::connectDevice() {
if(!is_connected) {
if(!is_lib_usb_init) {
int rv = libusb_init(NULL);
if (rv < 0) {
std::cerr << "libusb_init: Failed to initialze libusb" << std::endl;
return false;
}
is_lib_usb_init = true;
}
if(serial_number == "") {
handle = usb_device_find_USB_MCC(USB1608G_V2_PID, nullptr);
} else {
handle = usb_device_find_USB_MCC(USB1608G_V2_PID, serial_number.c_str());
}
if(handle == nullptr) {
handle = usb_handle::getLibUsbHandle(serial_number);
if (handle == nullptr) {
std::cerr << "Failed to connect to USB-1608G device" << std::endl;
return false;
}
}
usbInit_1608G(handle, 2);
if (!updateSerialNumber()) {
std::cerr << "Problem with device's serial number" << std::endl;
return false;
}
usb_handle::addLibUsbHandle(serial_number, handle);
std::cout << "Connected to Diode" << std::endl;
is_connected = true;
}
return is_connected;
}
bool Diode::disconnectDevice() {
if(is_connected) {
usb_handle::removeLibUsbHandle(serial_number);
handle = nullptr;
is_connected = false;
}
return !is_connected;
}
void Diode::blinkDaqLed(int num_blinks) {
usbBlink_USB1608G(handle, num_blinks);
}
bool Diode::updateSerialNumber() {
if (handle != nullptr) {
char new_serial_number_c_str[256];
usbGetSerialNumber_USB1608G(handle, new_serial_number_c_str);
if (new_serial_number_c_str == nullptr) {
return false;
}
std::string new_serial_number = new_serial_number_c_str;
if (serial_number == "") {
serial_number = new_serial_number;
}
return serial_number == new_serial_number;
}
return false;
}