Commit | Autor | Alter
|
8c7455
|
1 |
|
JS |
2 |
|
|
3 |
/* |
|
4 |
Polhemus Tracker Terminal version 1.0.0 -- Terminal Interface to Polhemus Trackers: Fastrak, Patriot, and Liberty |
|
5 |
Copyright © 2009 Polhemus, Inc. |
|
6 |
|
|
7 |
This file is part of Tracker Terminal. |
|
8 |
|
|
9 |
Tracker Terminal is free software: you can redistribute it and/or modify |
|
10 |
it under the terms of the GNU General Public License as published by |
|
11 |
the Free Software Foundation, either version 3 of the License, or |
|
12 |
(at your option) any later version. |
|
13 |
|
|
14 |
Tracker Terminal is distributed in the hope that it will be useful, |
|
15 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17 |
GNU General Public License for more details. |
|
18 |
|
|
19 |
You should have received a copy of the GNU General Public License |
|
20 |
along with Tracker Terminal. If not, see <http://www.gnu.org/licenses/>. |
|
21 |
|
|
22 |
************************************************************************* |
|
23 |
|
|
24 |
Tracker Terminal version 1.0.0 uses the libusb library version 1.0 |
|
25 |
libusb Copyright © 2007-2008 Daniel Drake <dsd@gentoo.org> |
|
26 |
libusb Copyright © 2001 Johannes Erdfelt <johannes@erdfelt.com> |
|
27 |
Licensed under the GNU Lesser General Public License version 2.1 or later. |
|
28 |
*/ |
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
#include <pthread.h> |
|
33 |
#include "PingPong.h" |
|
34 |
#include <string.h> |
|
35 |
|
|
36 |
|
|
37 |
PingPong::PingPong(){ |
|
38 |
for (int i=0;i<NUMBUFS;i++){ |
|
39 |
m_buf[i]=NULL; |
|
40 |
m_len[i]=0; |
|
41 |
} |
|
42 |
m_readInd=m_writeInd=m_size=0; |
|
43 |
|
|
44 |
// init the mutex to synchronize reads and writes between different threads |
|
45 |
pthread_mutex_init(&m_mutex,NULL); |
|
46 |
} |
|
47 |
|
|
48 |
PingPong::~PingPong(){ |
|
49 |
|
|
50 |
// need to clean up |
|
51 |
for (int i=0;i<NUMBUFS;i++){ |
|
52 |
if (m_buf[i]) |
|
53 |
delete[] m_buf[i]; |
|
54 |
} |
|
55 |
} |
|
56 |
|
|
57 |
// create buffers |
|
58 |
int PingPong::InitPingPong(int bufSize){ |
|
59 |
|
|
60 |
int memerr=0; |
|
61 |
for (int i=0;i<NUMBUFS;i++){ |
|
62 |
m_buf[i]=new BYTE[bufSize]; |
|
63 |
if (!m_buf[i]){ |
|
64 |
memerr=1; |
|
65 |
break; |
|
66 |
} |
|
67 |
} |
|
68 |
if (memerr){ |
|
69 |
for (int i=0;i<NUMBUFS;i++){ |
|
70 |
if (m_buf[i]){ |
|
71 |
delete[] m_buf[i]; |
|
72 |
m_buf[i]=NULL; |
|
73 |
} |
|
74 |
} |
|
75 |
return -1; |
|
76 |
} |
|
77 |
|
|
78 |
|
|
79 |
m_size=bufSize; |
|
80 |
return 0; |
|
81 |
} |
|
82 |
|
|
83 |
int PingPong::GetBufferSize(){ |
|
84 |
return m_size; |
|
85 |
} |
|
86 |
|
|
87 |
// return number of bytes available in read buffer |
|
88 |
int PingPong::IsDataAvail(){ |
|
89 |
|
|
90 |
return m_len[m_readInd]; |
|
91 |
} |
|
92 |
|
|
93 |
//#include <stdio.h> |
|
94 |
|
|
95 |
// read from the read buffer |
|
96 |
int PingPong::ReadPP(BYTE* buf){ |
|
97 |
|
|
98 |
pthread_mutex_lock(&m_mutex); |
|
99 |
int len=m_len[m_readInd]; |
|
100 |
|
|
101 |
if (len==0){ |
|
102 |
pthread_mutex_unlock(&m_mutex); |
|
103 |
return 0; |
|
104 |
} |
|
105 |
|
|
106 |
memcpy(buf,m_buf[m_readInd],len); |
|
107 |
m_len[m_readInd]=0; |
|
108 |
// printf("Read %d bytes from buffer %d\n",len,m_readInd); |
|
109 |
IncrInd(m_readInd); |
|
110 |
|
|
111 |
pthread_mutex_unlock(&m_mutex); |
|
112 |
|
|
113 |
return len; |
|
114 |
} |
|
115 |
|
|
116 |
|
|
117 |
// write to the write buffer |
|
118 |
int PingPong::WritePP(BYTE* buf,int len){ |
|
119 |
|
|
120 |
int rv=0; |
|
121 |
|
|
122 |
pthread_mutex_lock(&m_mutex); |
|
123 |
|
|
124 |
if(len>m_size) |
|
125 |
len=m_size; |
|
126 |
|
|
127 |
if (m_len[m_writeInd]==0){ // don't overwrite unread data |
|
128 |
memcpy(m_buf[m_writeInd],buf,len); |
|
129 |
m_len[m_writeInd]=len; |
|
130 |
// printf("Wrote %d bytes to buffer %d\n",len,m_writeInd); |
|
131 |
IncrInd(m_writeInd); |
|
132 |
rv=len; |
|
133 |
} |
|
134 |
|
|
135 |
pthread_mutex_unlock(&m_mutex); |
|
136 |
return rv; |
|
137 |
} |
|
138 |
|
|
139 |
// increment the index to the next in progression |
|
140 |
void PingPong::IncrInd(int& index){ |
|
141 |
|
|
142 |
index++; |
|
143 |
if (index==NUMBUFS) |
|
144 |
index=0; |
|
145 |
} |
|
146 |
|
|
147 |
void PingPong::ClearBuffers(){ |
|
148 |
|
|
149 |
pthread_mutex_lock(&m_mutex); |
|
150 |
for (int i=0;i<NUMBUFS;i++) |
|
151 |
m_len[i]=0; |
|
152 |
|
|
153 |
m_readInd=m_writeInd=0; |
|
154 |
pthread_mutex_unlock(&m_mutex); |
|
155 |
} |