-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexample.cpp
71 lines (58 loc) · 1.89 KB
/
example.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
#include "uss/drivers/cdcacm/cdcacm.hpp"
#include "uss/drivers/ch34x/ch34x.hpp"
#include "uss/uss.hpp"
#include <cstdio>
#include <thread>
using namespace uss;
int main(int argc, char** argv) {
bool active = true;
libusb_init(NULL);
// Create a driver
// This is a driver for WinChipHead CH340/CH341/HL340 devices
driver::ch34x::Ch34xDriver driver;
// Create an output
// This outputs to a virtual serial port / PTY at /tmp/uss0
uss::output::pty::PtyOutput output(NULL, "/tmp/uss0", true);
// Set output transfer completion callback
output.SetTransferCompletionCallback(
[&output](int result) { output.RemoveDevice(); });
// Create a device
// This uses the device 1a86:7523 and supports hotplug
uss::ctl::Hotpluggable ctl(
{0x1a86, 0x7523, 0, 0},
[&output](uss::ctl::Hotpluggable* device) {
output.SetDevice(device);
}, // Device connected event
[&output](uss::ctl::Hotpluggable* device) {
output.EndTransfers();
}); // Device disconnected event
// Set device baud rate (250000)
ctl.baud_rate = 250000;
// Set the device driver to the CH34x one from before
ctl.SetDriver(&driver);
std::thread output_thread([&output, &active]() {
while (active) {
output.HandleEvents();
}
});
struct timeval tv = {1L, 0L};
while (active) {
// For each loop iteration...
try {
// Handle LibUSB events
int ret = libusb_handle_events_timeout_completed(NULL, &tv, NULL);
if (ret < 0)
goto clean;
// Update device
ctl.Update();
} catch (const char* error) {
printf("Error caught during main loop: %s\n", error);
goto clean;
}
}
clean:
active = false;
output_thread.join();
libusb_exit(NULL);
return 2;
}