-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSerialWriter.cpp
123 lines (103 loc) · 2.77 KB
/
SerialWriter.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
// SerialWriter.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "SerialWriter.h"
int _tmain(int argc, _TCHAR* argv[])
{
std::string prt(argv[1]);
std::string baud(argv[2]);
std::string port(prt.begin(), prt.end());
SerialWriter serialWriter(port);
if(serialWriter.ser.is_open() ==false) return-1;
return serialWriter.loop();
}
//*fingers are ordered from thumb to pinkie.
SerialWriter::SerialWriter(std::string COM): ser(asio_serv)
{
lastFrame = 0;
try{
ser.open(COM);
ser.set_option(serial_port_base::baud_rate(11520));
}
catch(boost::system::system_error e) {
std::cout<< "Boost error: "<<e.what()<<std::endl;
}
catch(std::runtime_error e) {
std::cout<<"std::runtime error: "<<e.what()<<std::endl;
}
catch(std::exception e) {
std::cout<<e.what()<<std::endl;
}
}
//FIXME: close file handles before program
// exit to avoid BSOD on system shutdown
//TODO: allow for tracking of multiple digits
// each hand will reqire 7 channels minimum
int SerialWriter::loop()
{
while(1){
if(leapCont.isConnected())
{
while(1)
{
if(!leapCont.isConnected()){//if we lose connection, pause processing and wait for reconnect.
break;
}
frame = leapCont.frame(); //get the latest frame
if(frame.id() != lastFrame) //same frame? skip processing.
{
this->process();
}
}
}
else
{
std::cout<<"INFO: Leap not found, waiting for connection."<<std::endl;
Sleep(1000);
}
}
return 0;
}
void SerialWriter::process()
{
/*
//std::cout << leftmostY << " " <<leftmostX<<" "<<leftmostZ<<std::endl;
leftmostX = abs(215 +(-1*leftmostX));
if((leftmostY!=0) && (leftmostX !=0)){
unsigned char buf[BUFFSIZE];
buf[0]=(unsigned int)leftmostY>>24; //prepare values for transmission
buf[1]=(unsigned int)leftmostY>>16; //should probably restructure this
buf[2]=(unsigned int)leftmostY>>8; //to use loops
buf[3]=(unsigned int)leftmostY;
buf[4] = '\0';
buf[5]=(unsigned int)leftmostX>>24;
buf[6]=(unsigned int)leftmostX>>16;
buf[7]=(unsigned int)leftmostX>>8;
buf[8]=(unsigned int)leftmostX;
buf[9]='\0';
buf[10]=(unsigned int)leftmostZ>>24;
buf[11]=(unsigned int)leftmostZ>>16;
buf[12]=(unsigned int)leftmostZ>>8;
buf[13]=(unsigned int)leftmostZ;
buf[14]='\n';
#ifndef NON_DEBUG
for(int i = 0; i<=BUFFSIZE-1; i++){
std::cout << (int)buf[i]<<" ";
}
std::cout <<std::endl;
#endif //NON_DEBUG
try{
boost::asio::write(ser, boost::asio::buffer(buf, sizeof(char[BUFFSIZE]))); //send data
}
catch(boost::system::system_error e) {
std::cout<< "Boost error: "<<e.what()<<std::endl;
}
catch(std::runtime_error e) {
std::cout<<"std::runtime error: "<<e.what()<<std::endl;
}
catch(std::exception e) {
std::cout<<e.what()<<std::endl;
}
}
*/
}