-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPartnerPort.cpp
165 lines (128 loc) · 4.03 KB
/
PartnerPort.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
152
153
154
155
156
157
158
159
160
161
162
163
164
//
// PartnerPort.cpp
// PartnerPort
//
// Created by Matthew Dyer on 2/03/15.
//
//
#include "PartnerPort.h"
#include <string>
PartnerPort::PartnerPort(VexJoystick* j,char* port){
struct termios options;
this->joy = j;
this->portName = port;
this->fd = open(this->portName, O_RDWR | O_NOCTTY | O_NDELAY);
if (this->fd == -1) {
perror("open_port: Unable to open port");
_exit(0);
}
else {
fcntl(this->fd, F_SETFL, FNDELAY);
}
tcgetattr(this->fd, &options );
/* SEt Baud Rate */
cfsetispeed( &options, B115200 );
cfsetospeed( &options, B115200 );
//I don't know what this is exactly
options.c_cflag |= ( CLOCAL | CREAD );
// Set the Charactor size
options.c_cflag &= ~CSIZE; /* Mask the character size bits */
options.c_cflag |= CS8; /* Select 8 data bits */
// Set parity - No Parity (8N1)
options.c_cflag &= ~PARENB;
options.c_cflag &= ~CSTOPB;
options.c_cflag &= ~CSIZE;
options.c_cflag |= CS8;
// Enable Raw Input
options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
// Disable Software Flow control
options.c_iflag &= ~(IXON | IXOFF | IXANY);
// Chose raw (not processed) output
options.c_oflag &= ~OPOST;
if ( tcsetattr( this->fd, TCSANOW, &options ) == -1 )
printf ("Error with tcsetattr = %s\n", strerror ( errno ) );
else
printf ( "%s\n", "tcsetattr succeed" );
}
bool PartnerPort::readToBuffer(){
int bytes = 0;
while (bytes < PACKET_LENGTH){
bytes = read(this->fd, &this->buffer, sizeof(this->buffer));
}
//This is terrible
/*
for (int i=0;i<HEADER_LENGTH;i++){
if ((int)this->buffer[i] != (int)this->headerBytes[i]){
//printf("%d %d %d\n",this->buffer[0],this->buffer[1],this->buffer[2]);
return false;
}
}*/
tcflush(this->fd,TCIOFLUSH);
return true;
}
unsigned char* PartnerPort::getParnerPortBytes(){
while(!this->readToBuffer()){}
return this->buffer;
}
void PartnerPort::writeJoystick(VexJoystick* joy){
unsigned char* analog = joy->getAnalogChannels();
bool* button = joy->getButtons();
//Write header
for(int i=0;i<HEADER_LENGTH;i++){
this->writeBuffer[i] = headerBytes[i];
}
this->writeBuffer[HEADER_LENGTH] = this->dataLength;
//Write joystick channels
for (int i=HEADER_LENGTH+1;i<NUM_ANALOG_CHANNELS+HEADER_LENGTH+1;i++){
this->writeBuffer[i] = *analog;
analog++;
}
//Write buttons, there are 2 bytes for buttons
for (int j=NUM_ANALOG_CHANNELS+HEADER_LENGTH+1;j<NUM_ANALOG_CHANNELS+HEADER_LENGTH+1+2;j++){
unsigned char buttonByte = 0;
for (int i=0;i<8;i++){
if (*button){
buttonByte += (1 << i);
}
button++;
}
this->writeBuffer[j] = buttonByte;
}
//Calculate Checksum
//Zero checksum
checksum = 0x00;
for (int i = HEADER_LENGTH+1; i < PACKET_LENGTH-1; i++){
//sum all data bytes in arrat from 4 to 12 (exclude header, size, and checksum itself)
checksum += this->writeBuffer[i];
}
checksum = 0x00-checksum; //sum of data bytes + checksum = 0x00
//Write Checksum
this->writeBuffer[PACKET_LENGTH-1] = checksum;
//Print for debugging
for (int i = 0;i<PACKET_LENGTH;i++){
printf("%02x ",this->writeBuffer[i]);
}
printf("\n");
int counter = 0;
auto bufferPtr = this->writeBuffer;
while (counter < 12){
write(this->fd,bufferPtr,4);
counter += 4;
bufferPtr += 4;
usleep(600);
}
write(this->fd,bufferPtr,2);
}
void PartnerPort::sendPacket(){
while(true){
this->writeJoystick(this->joy);
usleep(17000);
}
}
void PartnerPort::startSending(){
this->commThread = new std::thread(&PartnerPort::sendPacket,this);
this->commThread->detach();
}
void PartnerPort::stopSending(){
this->commThread->join();
}