Debian package version of polhemus's g4term
Janis Streib
30.03.22 00027aa5bb1da6f2d44c03a082988de8c64dab4a
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
 
 
/*
Polhemus Tracker Terminal version 1.0.0 -- Terminal Interface to Polhemus Trackers: Fastrak, Patriot, and Liberty
Copyright  ©  2009  Polhemus, Inc.
 
This file is part of Tracker Terminal.
 
Tracker Terminal is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
 
Tracker Terminal is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with Tracker Terminal.  If not, see <http://www.gnu.org/licenses/>.
 
*************************************************************************
 
Tracker Terminal version 1.0.0 uses the libusb library version 1.0
libusb Copyright © 2007-2008 Daniel Drake <dsd@gentoo.org>
libusb Copyright © 2001 Johannes Erdfelt <johannes@erdfelt.com>
Licensed under the GNU Lesser General Public License version 2.1 or later.
*/
 
 
 
#include <pthread.h>
#include "PingPong.h"
#include <string.h>
 
 
PingPong::PingPong(){
  for (int i=0;i<NUMBUFS;i++){
    m_buf[i]=NULL;
    m_len[i]=0;
  }
  m_readInd=m_writeInd=m_size=0;
 
  // init the mutex to synchronize reads and writes between different threads
  pthread_mutex_init(&m_mutex,NULL);
}
 
PingPong::~PingPong(){
 
  // need to clean up 
  for (int i=0;i<NUMBUFS;i++){
    if (m_buf[i])
      delete[] m_buf[i];
  }
}
 
// create buffers
int PingPong::InitPingPong(int bufSize){
 
  int memerr=0;
  for (int i=0;i<NUMBUFS;i++){
    m_buf[i]=new BYTE[bufSize];
    if (!m_buf[i]){
      memerr=1;
      break;
    }
  }
  if (memerr){
    for (int i=0;i<NUMBUFS;i++){
      if (m_buf[i]){
    delete[] m_buf[i];
    m_buf[i]=NULL;
      }
    }
    return -1;
  }
 
 
  m_size=bufSize;
  return 0;
}
 
int PingPong::GetBufferSize(){
  return m_size;
}
 
// return number of bytes available in read buffer
int PingPong::IsDataAvail(){
 
  return m_len[m_readInd];
}
 
//#include <stdio.h>
 
// read from the read buffer
int PingPong::ReadPP(BYTE* buf){
 
  pthread_mutex_lock(&m_mutex);
  int len=m_len[m_readInd];
 
  if (len==0){
    pthread_mutex_unlock(&m_mutex);
    return 0;
  }
 
  memcpy(buf,m_buf[m_readInd],len);
  m_len[m_readInd]=0;
  //   printf("Read %d bytes from buffer %d\n",len,m_readInd);
  IncrInd(m_readInd);
 
  pthread_mutex_unlock(&m_mutex);
 
  return len;
}
 
 
// write to the write buffer
int PingPong::WritePP(BYTE* buf,int len){
 
  int rv=0;
 
  pthread_mutex_lock(&m_mutex);
 
  if(len>m_size)
    len=m_size;
 
  if (m_len[m_writeInd]==0){  // don't overwrite unread data
    memcpy(m_buf[m_writeInd],buf,len);
    m_len[m_writeInd]=len;
    //   printf("Wrote %d bytes to buffer %d\n",len,m_writeInd);
    IncrInd(m_writeInd);
    rv=len;
  }
 
  pthread_mutex_unlock(&m_mutex);
  return rv;
}
 
// increment the index to the next in progression
void PingPong::IncrInd(int& index){
 
  index++;
  if (index==NUMBUFS)
    index=0;
}
 
void PingPong::ClearBuffers(){
 
  pthread_mutex_lock(&m_mutex);
  for (int i=0;i<NUMBUFS;i++)
    m_len[i]=0;
  
  m_readInd=m_writeInd=0;
  pthread_mutex_unlock(&m_mutex);
}