/*
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 .
*************************************************************************
Tracker Terminal version 1.0.0 uses the libusb library version 1.0
libusb Copyright © 2007-2008 Daniel Drake
libusb Copyright © 2001 Johannes Erdfelt
Licensed under the GNU Lesser General Public License version 2.1 or later.
*/
#include
#include "PingPong.h"
#include
PingPong::PingPong(){
for (int i=0;i
// 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