Skip to content
Snippets Groups Projects
Commit f54f3250 authored by Josh Siegle's avatar Josh Siegle
Browse files

Fixed various aspects of FPGAThread

parent bdc14adc
Branches
Tags
No related merge requests found
No preview for this file type
No preview for this file type
......@@ -26,7 +26,7 @@
DataBuffer::DataBuffer(int chans, int size)
: abstractFifo (size), buffer(chans, size), numChans(chans)
{
timestampBuffer = new int64[size];
timestampBuffer = new uint64[size];
eventCodeBuffer = new int16[size];
}
......@@ -38,7 +38,7 @@ void DataBuffer::clear() {
buffer.clear();
}
void DataBuffer::addToBuffer(float* data, int64* timestamps, int16* eventCodes, int numItems) {
void DataBuffer::addToBuffer(float* data, uint64* timestamps, int16* eventCodes, int numItems) {
// writes one sample for all channels
int startIndex1, blockSize1, startIndex2, blockSize2;
abstractFifo.prepareToWrite(numItems, startIndex1, blockSize1, startIndex2, blockSize2);
......@@ -62,7 +62,7 @@ int DataBuffer::getNumSamples() {
}
int DataBuffer::readAllFromBuffer (AudioSampleBuffer& data, int64* timestamp, int16* eventCodes, int maxSize)
int DataBuffer::readAllFromBuffer (AudioSampleBuffer& data, uint64* timestamp, int16* eventCodes, int maxSize)
{
// check to see if the maximum size is smaller than the total number of available ints
int numItems = (maxSize < abstractFifo.getNumReady()) ?
......
......@@ -39,15 +39,15 @@ public:
DataBuffer(int chans, int size);
~DataBuffer();
void clear();
void addToBuffer(float* data, int64* ts, int16* eventCodes, int numItems);
void addToBuffer(float* data, uint64* ts, int16* eventCodes, int numItems);
int getNumSamples();
int readAllFromBuffer(AudioSampleBuffer& data, int64* ts, int16* eventCodes, int maxSize);
int readAllFromBuffer(AudioSampleBuffer& data, uint64* ts, int16* eventCodes, int maxSize);
private:
AbstractFifo abstractFifo;
AudioSampleBuffer buffer;
int64* timestampBuffer;
uint64* timestampBuffer;
int16* eventCodeBuffer;
int numChans;
......
......@@ -65,7 +65,7 @@ public:
SourceNode* sn;
int16 eventCode;
int64 timestamp;
uint64 timestamp;
Time timer;
......
......@@ -23,10 +23,13 @@
#include "FPGAThread.h"
#include <string.h>
FPGAThread::FPGAThread(SourceNode* sn) : DataThread(sn),
isTransmitting(false),
numchannels(16),
deviceFound(false)
numchannels(32),
deviceFound(false),
ttlOutputVal(0)
{
......@@ -77,9 +80,14 @@ int FPGAThread::getNumChannels()
return numchannels;
}
int FPGAThread::getNumEventChannels()
{
return 8;
}
float FPGAThread::getSampleRate()
{
return 25000.0;
return 12500.0;
}
float FPGAThread::getBitVolts()
......@@ -143,6 +151,8 @@ bool FPGAThread::updateBuffer() {
return_code = dev->ReadFromPipeOut(0xA0, sizeof(pBuffer), pBuffer);
//std::cout << return_code << std::endl; should return number of bytes read [sizeof(pBuffer)]
if (return_code == 0)
return false;
......@@ -155,30 +165,35 @@ bool FPGAThread::updateBuffer() {
//
// headstages are A,B,C,D and another one for the breakout box T for the 0-5v TTL input
// A1 is stage A channel 1 etc
//
// ...............
// tc ttttttt1
// tc ttttttt1 (6*7bit timecode gives 42 bit gives 4.3980e+12 samples max
// tc ttttttt1 which should be about 7 years at 30KHz)
// tc ttttttt1
// tc ttttttt1
// tc ttttttt1
// A1 Hi xxxxxxx0
// A1 Lo xxxxxxx0 (last bits 0)
// A1 ch 00000HL0 (L and h H are the two missing bits from A1, hi and lo bytes)
// B1 Hi xxxxxxx0
// B1 Lo xxxxxxx0
// B1 ch cccccYY0
// A2 Hi ........
// ... remaining channel data ...
// B16 ch cccccYY0
// T1 yyyyyyy0
// T2 yyyyyyy0 (space for 14 TTL channels)
// ... next sample ...
//
// ...............
// tc ttttttt1
// tc ttttttt1 (6*7bit timecode gives 42 bit gives 4.3980e+12 samples max
// tc ttttttt1 which should be about 7 years at 30KHz)
// tc ttttttt1
// tc ttttttt1
// tc ttttttt1
// ttl_in xxxxxxxx
// ttl_o xxxxxxxx
// A1 Hi xxxxxxx0
// A1 Lo xxxxxxx0 (last bits 0)
// A1 ch cccccYY0
// B1 Hi xxxxxxx0 (YY are the two missing bits from A1, cccc is a 5bit ch code 1-32, or maybe checksum?)
// B1 Lo xxxxxxx0
// B1 ch cccccYY0
// A2 Hi ........
// ... remaining channel data ...
// B32 ch cccccYY0
//
// ... next sample ...
//
int i = 0;
int samplesUsed = 0;
while (j < sizeof(pBuffer))
{
// look for timecode block (6 bytes)
if ( (pBuffer[j] & 1)
&& (pBuffer[j+1] & 1)
......@@ -189,44 +204,103 @@ bool FPGAThread::updateBuffer() {
&& (j+5+Ndatabytes <= sizeof(pBuffer)) ) // indicated by last bit being 1
{ //read 6 bytes, assemble to 6*7 = 42 bits, arranged in 6 bytes
char timecode[6]; // 1st byte throw out last bit of each byte and just concatenate the other bytes in ascending order
i++;
unsigned char timecode[6]; // 1st byte throw out last bit of each byte and just concatenate the other bytes in ascending order
timecode[0] = (pBuffer[j] >> 1) | ((pBuffer[j+1] >> 1) << 7); // 2nd byte
timecode[1] = (pBuffer[j+1] >> 2) | ((pBuffer[j+2] >> 1) << 6); // 3rd byte
timecode[2] = (pBuffer[j+2] >> 3) | ((pBuffer[j+3] >> 1) << 5); // 4th byte
timecode[3] = (pBuffer[j+3] >> 4) | ((pBuffer[j+4] >> 1) << 4); // 5th byte
timecode[4] = (pBuffer[j+4] >> 5) | ((pBuffer[j+5] >> 1) << 3); // 6th byte
timecode[5] = (pBuffer[j+5] >> 6);
timestamp = (uint64(timecode[5]) << 35) +
(uint64(timecode[4]) << 28) +
(uint64(timecode[3]) << 32) +
(uint64(timecode[2]) << 16) +
(uint64(timecode[1]) << 8) +
(uint64(timecode[0]));
// if (i == 1)
// {
// std::cout << "Start time: " << timestamp << std::endl;
// }
eventCode = pBuffer[j+6];
//std::cout << eventCode << std::endl;
//int ttl_out = pBuffer[j+7];
j += 6; //move cursor to 1st data byte
j += 8; //move cursor to 1st data byte
// loop through sample data and condense from 3 bytes to 2 bytes
uint16 hi; uint16 lo;
for (int n = 0; n < numchannels ; n++)
// only take data from the first headstage (i.e., skip every other channel)
for (int n = 0; n < numchannels*2 ; n++)
{
// last bit of first 2 is zero, replace with bits 1 and 2 from 3rd byte
hi = (pBuffer[j]) | ((( pBuffer[j+2] >> 2) & ~(1<<6)) & ~(1<<7)) ;
lo = (pBuffer[j+1]) | ((( pBuffer[j+2] >> 1) & ~(1<<1)) & ~(1<<7)) ;
j += 3;
if (n % 2 == 0)
{
// last bit of first 2 is zero, replace with bits 1 and 2 from 3rd byte
hi = (pBuffer[j]) | ((( pBuffer[j+2] >> 2) & ~(1<<6)) & ~(1<<7)) ;
lo = (pBuffer[j+1]) | ((( pBuffer[j+2] >> 1) & ~(1<<1)) & ~(1<<7)) ;
//thisSample[n] = float( hi - 256)/256;
uint16 samp = ((hi << 8) + lo);
uint16 samp = ((hi << 8) + lo);
thisSample[n] = float(samp) * 0.1907f - 6175.0f;
thisSample[n/2] = float(samp) * 0.1907f - 6175.0f;
}
j += 3;
}
j -= 3; // step back in time
// should actually be converting timecode to timestamp:
timestamp = timer.getHighResolutionTicks();
// should actually be converting timecode to timestamp, but skip for now:
//timestamp = timer.getHighResolutionTicks();
dataBuffer->addToBuffer(thisSample, &timestamp, &eventCode, 1);
samplesUsed += 200;
}
j++; // keep scanning for timecodes
}
overflowSize = sizeof(pBuffer) - samplesUsed;
// if (overflowSize != 0)
// {
// memcpy(&overflowBuffer, &pBuffer[j-overflowSize], overflowSize);
//
// }
// std::cout << "Overflow size: " << overflowSize << std::endl;
// std::cout << "End time: " << timestamp << std::endl;
// std::cout << return_code << " " << i << std::endl; // number of samples found
if (ttlOutputVal == 1 && accumulator > 100)
{
dev->SetWireInValue(1, 0x00);
ttlOutputVal = 0;
accumulator = 0;
} else if (ttlOutputVal == 0 && accumulator > 100) {
dev->SetWireInValue(1, 0x06);
ttlOutputVal = 1;
accumulator = 0;
}
accumulator++;
dev->UpdateWireIns();
return true;
}
......
......@@ -54,6 +54,8 @@ public:
int getNumChannels();
float getSampleRate();
float getBitVolts();
int getNumEventChannels();
private:
......@@ -69,7 +71,13 @@ private:
bool startAcquisition();
bool stopAcquisition();
unsigned char pBuffer[10200]; // request a 54kb block of data
unsigned char pBuffer[20000]; // size of the data requested in each buffer
unsigned char overflowBuffer[20000];
int overflowSize;
int ttlOutputVal;
int accumulator;
float thisSample[256];
......
......@@ -183,7 +183,7 @@ void LfpDisplayNode::handleEvent(int eventType, MidiMessage& event)
}
// std::cout << "ttlState: " << ttlState << std::endl;
// std::cout << "ttlState: " << ttlState << std::endl;
// std::cout << "Received event from " << eventNodeId <<
// " on channel " << eventChannel <<
......
......@@ -232,6 +232,8 @@ void SourceNode::process(AudioSampleBuffer &buffer,
nSamples = inputBuffer->readAllFromBuffer(buffer, &timestamp, eventCodeBuffer, buffer.getNumSamples());
//std::cout << "TIMESTAMP: " << timestamp << std::endl;
//std::cout << "Samples per buffer: " << nSamples << std::endl;
uint8 data[4];
memcpy(data, &timestamp, 4);
......@@ -257,6 +259,8 @@ void SourceNode::process(AudioSampleBuffer &buffer,
{
if (state == 0) {
//std::cout << "OFF" << std::endl;
//std::cout << *eventCodeBuffer << std::endl;
// signal channel state is OFF
addEvent(events, // MidiBuffer
TTL, // eventType
......@@ -266,6 +270,9 @@ void SourceNode::process(AudioSampleBuffer &buffer,
);
} else {
//std::cout << "ON" << std::endl;
//std::cout << *eventCodeBuffer << std::endl;
// signal channel state is ON
addEvent(events, // MidiBuffer
TTL, // eventType
......@@ -280,7 +287,7 @@ void SourceNode::process(AudioSampleBuffer &buffer,
}
}
//std::cout << *eventCodeBuffer << std::endl;
}
......
......@@ -88,7 +88,7 @@ private:
ScopedPointer<DataThread> dataThread;
DataBuffer* inputBuffer;
int64 timestamp;
uint64 timestamp;
int16* eventCodeBuffer;
int* eventChannelState;
......
......@@ -246,7 +246,7 @@ void LfpDisplayCanvas::renderOpenGL()
drawTimeline();
// glFlush();
swapBuffers();
//swapBuffers();
}
......@@ -257,7 +257,7 @@ void LfpDisplayCanvas::drawEvents()
glViewport(xBuffer, 0, getWidth()-xBuffer, getHeight());
glLineWidth(1.0f);
glLineWidth(2.0f);
// loop through events
......@@ -271,10 +271,30 @@ void LfpDisplayCanvas::drawEvents()
int ttlState = int(waves[nChans][n]);
//std::cout << x << std::endl;
if ((ttlState & 0x80) >> 7)
{
glColor4f(0.5, 0.3, 0.2, 0.1);
glBegin(GL_LINE_STRIP);
glVertex2f(x, 0);
glVertex2f(x, 1);
glEnd();
}
if ((ttlState & 0x40) >> 6)
{
glColor4f(1.0, 0.3, 0.0, 0.1);
glBegin(GL_LINE_STRIP);
glVertex2f(x, 0);
glVertex2f(x, 1);
glEnd();
}
if ((ttlState & 0x20) >> 5)
{
glColor4f(1.0, 0.0, 0.0, 0.3);
glColor4f(1.0, 0.0, 0.0, 0.1);
glBegin(GL_LINE_STRIP);
glVertex2f(x, 0);
......@@ -284,7 +304,7 @@ void LfpDisplayCanvas::drawEvents()
if ((ttlState & 0x10) >> 4)
{
glColor4f(0.0, 1.0, 0.0, 0.3);
glColor4f(0.0, 1.0, 0.0, 0.1);
glBegin(GL_LINE_STRIP);
glVertex2f(x, 0);
......@@ -295,7 +315,7 @@ void LfpDisplayCanvas::drawEvents()
if ((ttlState & 0x8) >> 3)
{
glColor4f(0.0, 0.0, 1.0, 0.3);
glColor4f(0.0, 0.0, 1.0, 0.1);
glBegin(GL_LINE_STRIP);
glVertex2f(x, 0);
......@@ -306,7 +326,7 @@ void LfpDisplayCanvas::drawEvents()
if ((ttlState & 0x4) >> 2)
{
glColor4f(0.0, 1.0, 1.0, 0.3);
glColor4f(0.0, 1.0, 1.0, 0.1);
glBegin(GL_LINE_STRIP);
glVertex2f(x, 0);
......@@ -317,7 +337,7 @@ void LfpDisplayCanvas::drawEvents()
if ((ttlState & 0x2) >> 1)
{
glColor4f(1.0, 1.0, 0.0, 0.3);
glColor4f(1.0, 1.0, 0.0, 0.1);
glBegin(GL_LINE_STRIP);
glVertex2f(x, 0);
......@@ -328,7 +348,7 @@ void LfpDisplayCanvas::drawEvents()
if ((ttlState & 0x1))
{
glColor4f(1.0, 1.0, 1.0, 0.3);
glColor4f(1.0, 1.0, 1.0, 0.1);
glBegin(GL_LINE_STRIP);
glVertex2f(x, 0);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment