-
Notifications
You must be signed in to change notification settings - Fork 0
/
FtdiWrapper.cpp
121 lines (96 loc) · 3.07 KB
/
FtdiWrapper.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
#include <iostream>
#include <stdexcept>
#include <sstream>
#include <string>
#include "FtdiWrapper.hpp"
namespace jdd{
FtdiWrapper::FtdiWrapper(unsigned int vid, unsigned int pid, string description,
string serialNum)
{
try
{
//construct connection context
ftdiContext = ftdi_new();
if(ftdiContext == NULL)
throw runtime_error("unable to initialize ftdi driver");
//open connection
const char *desc = ( description.empty() ? NULL : description.c_str() );
const char *serial = ( serialNum.empty() ? NULL : serialNum.c_str() );
int rtn = ftdi_usb_open_desc(ftdiContext, vid, pid, desc, serial);
if(rtn < 0)
{
stringstream msg;
msg << "unable to open ftdi connection: ";
msg << ftdi_get_error_string(ftdiContext);
// msg << " (Do you have sufficient privileges to access the usb bus? Is the device connected?)";
throw runtime_error( msg.str() );
}
}
catch(...)
{
ftdi_free(ftdiContext);
throw;
}
}
FtdiWrapper::~FtdiWrapper()
{
//close connection
int rtn = ftdi_usb_close(ftdiContext);
if(rtn < 0) cerr << ftdi_get_error_string(ftdiContext) << endl;
//free connection context
ftdi_free(ftdiContext);
}
void FtdiWrapper::put(const uint8_t *buffer, unsigned int bufSize)
{
put(buffer, bufSize, bufSize);
}
int FtdiWrapper::put(const uint8_t *buffer, unsigned int bufSize, unsigned int minPut)
{
unsigned int bytesWritten = 0;
// unsigned int trys = 0;
int status;
while(bytesWritten < minPut)
{
//TODO: casting away const is BAD
//usb write
status = ftdi_write_data(ftdiContext, const_cast<uint8_t*>(buffer), bufSize);
//check for usb errors
if(status < 0)
throw runtime_error( ftdi_get_error_string(ftdiContext) );
bytesWritten += status;//keep track of how much we sent
buffer += status;//move buffer pointer
bufSize -= status;//adjust buffer size
//TODO: progressively sleep?
// trys ++;
// if(trys > 10)
// throw runtime_error("timeout occured durring write to usb");
}
return bytesWritten;
}
void FtdiWrapper::get(uint8_t *buffer, unsigned int bufSize)
{
get(buffer, bufSize, bufSize);
}
int FtdiWrapper::get(uint8_t *buffer, unsigned int bufSize, unsigned int minGet)
{
unsigned int bytesRead = 0;
// unsigned int trys = 0;
int status;
while(bytesRead < minGet)
{
//usb read
status = ftdi_read_data(ftdiContext, buffer, bufSize);
//check for usb errors
if(status < 0)
throw runtime_error( ftdi_get_error_string(ftdiContext) );
bytesRead += status;//keep track of how much we got
buffer += status;//move buffer pointer
bufSize -= status;//adjust buffer size
//TODO: progressively sleep?
// trys ++;
// if(trys > 10)
// throw runtime_error("timeout occured durring read to usb");
}
return bytesRead;
}
}//namespace