From 3b698e39f9274e69059caa42ed1e23f87cbcdcdb Mon Sep 17 00:00:00 2001
From: jsiegle <jsiegle@mit.edu>
Date: Sat, 21 Apr 2012 14:47:05 -0400
Subject: [PATCH] Removed extra deletion in FPGAThread

---
 Source/Processors/DataThreads/FPGAThread.cpp | 123 +++++++++++++------
 Source/Processors/DataThreads/FPGAThread.h   |  23 ++--
 Source/Processors/ProcessorGraph.cpp         |   1 +
 Source/Processors/SourceNode.cpp             |   2 +-
 4 files changed, 96 insertions(+), 53 deletions(-)

diff --git a/Source/Processors/DataThreads/FPGAThread.cpp b/Source/Processors/DataThreads/FPGAThread.cpp
index 34fb9995d..1ed54e1b8 100644
--- a/Source/Processors/DataThreads/FPGAThread.cpp
+++ b/Source/Processors/DataThreads/FPGAThread.cpp
@@ -24,19 +24,18 @@
 #include "FPGAThread.h"
 
 FPGAThread::FPGAThread(SourceNode* sn) : DataThread(sn),
-			isRunning(false),
+			isTransmitting(false),
 			numchannels(32),
-			m_u32SegmentSize(1048576)
+			deviceFound(false)
 
 {
 
-	// Initialize the FPGA with our configuration bitfile.
-	printf("New device created.\n");
+	
 	const char* bitfilename = "./pipetest.bit";
 
 	printf("---- Opal Kelly ---- PipeTest Application v1.0 ----\n");
 	
-	if (FALSE == okFrontPanelDLL_LoadLib(NULL)) {
+	if (!okFrontPanelDLL_LoadLib(NULL)) {
 		printf("FrontPanel DLL could not be loaded.\n");
 	}
 	
@@ -47,24 +46,19 @@ FPGAThread::FPGAThread(SourceNode* sn) : DataThread(sn),
 
 	strncpy(bitfile, bitfilename, 128);
 
-	if (!initializeFPGA(dev, bitfile)) {
+	// Initialize the FPGA with our configuration bitfile.
+	deviceFound = initializeFPGA(true);
+
+	if (!deviceFound) {
 		printf("FPGA could not be initialized.\n");
+	} else {
+		printf("FPGA interface initialized.\n");
 	}
 
 	Ndatabytes = numchannels*3;
 	
-	std::cout << "FPGA interface initialized." << std::endl;
-
-	std::cout << "DataBuffer address is " << dataBuffer << std::endl;
-
 	dataBuffer = new DataBuffer(32, 10000);
 
-	std::cout << "DataBuffer address is " << dataBuffer << std::endl;
-
-	//deviceFound = true;
-
-	//startThread();
-
 }
 
 
@@ -76,27 +70,72 @@ FPGAThread::~FPGAThread() {
 
 }
 
+int FPGAThread::getNumChannels()
+{
+	return 32;
+}
+
+float FPGAThread::getSampleRate()
+{
+	return 25000.0;
+}
+
+bool FPGAThread::foundInputSource()
+{
+
+	if (deviceFound)
+	{
+		if (okCFrontPanel::NoError != dev->ConfigureFPGA(bitfile)) 
+		{
+			printf("FPGA configuration failed.\n");
+			deviceFound = false;
+			return false;
+		}
+
+	} else {
+
+		// if (!initializeFPGA(false))
+		// {
+		// 	return false;
+		// } else {
+		// 	deviceFound = true;
+		// }
+
+	}
+
+}
+
 bool FPGAThread::startAcquisition()
 {
-  startThread();
+   startThread();
 
-    //isTransmitting = true;
+   isTransmitting = true;
 
-    return true;
+   return true;
 }
 
 bool FPGAThread::stopAcquisition()
 {
+
+	isTransmitting = false;
+
 	if (isThreadRunning()) {
+
         signalThreadShouldExit();
     }
 
+
     return true;
 }
 
 bool FPGAThread::updateBuffer() {
+
+	long return_code;
 	
-	dev->ReadFromPipeOut(0xA0, sizeof(pBuffer), pBuffer);
+	return_code = dev->ReadFromPipeOut(0xA0, sizeof(pBuffer), pBuffer);
+
+	if (return_code == 0)
+		return false;
 
     int j = 0;
 
@@ -141,40 +180,48 @@ bool FPGAThread::updateBuffer() {
 
 
 
-bool FPGAThread::initializeFPGA(okCFrontPanel *dev, char *bitfile)
+bool FPGAThread::initializeFPGA(bool verbose)
 {
+
 	if (okCFrontPanel::NoError != dev->OpenBySerial()) {
-		delete dev;
-		printf("Device could not be opened.  Is one connected?\n");
-		return(NULL);
+		if (verbose)
+			printf("Device could not be opened.  Is one connected?\n");
+		return false;
 	}
 	
-	printf("Found a device: %s\n", dev->GetBoardModelString(dev->GetBoardModel()).c_str());
+	if (verbose)
+		printf("Found a device: %s\n", dev->GetBoardModelString(dev->GetBoardModel()).c_str());
 
 	dev->LoadDefaultPLLConfiguration();	
 
 	// Get some general information about the XEM.
-	std::string str;
-	printf("Device firmware version: %d.%d\n", dev->GetDeviceMajorVersion(), dev->GetDeviceMinorVersion());
-	str = dev->GetSerialNumber();
-	printf("Device serial number: %s\n", str.c_str());
-	str = dev->GetDeviceID();
-	printf("Device device ID: %s\n", str.c_str());
+	if (verbose) {
+		std::string str;
+		printf("Device firmware version: %d.%d\n", dev->GetDeviceMajorVersion(), dev->GetDeviceMinorVersion());
+		str = dev->GetSerialNumber();
+		printf("Device serial number: %s\n", str.c_str());
+		str = dev->GetDeviceID();
+		printf("Device device ID: %s\n", str.c_str());
+	}
 
 	// Download the configuration file.
 	if (okCFrontPanel::NoError != dev->ConfigureFPGA(bitfile)) {
-		printf("FPGA configuration failed.\n");
-		return(false);
+		if (verbose)
+			printf("FPGA configuration failed.\n");
+		return false;
 	}
 
 	// Check for FrontPanel support in the FPGA configuration.
-	if (dev->IsFrontPanelEnabled())
-		printf("FrontPanel support is enabled.\n");
-	else
-		printf("FrontPanel support is not enabled.\n");
+	if (verbose) {
+		if (dev->IsFrontPanelEnabled())
+			printf("FrontPanel support is enabled.\n");
+		else
+			printf("FrontPanel support is not enabled.\n");
+	}
 
-	return(true);
+	return true;
 
+	// this is not executed
 	dev->SetWireInValue(0x00, 1<<2);  // set reset bit in cmd wire to 1 and back to 0
 	dev->UpdateWireIns();
 	dev->SetWireInValue(0x00, 0<<2);  
diff --git a/Source/Processors/DataThreads/FPGAThread.h b/Source/Processors/DataThreads/FPGAThread.h
index 7bc294011..d351ed933 100644
--- a/Source/Processors/DataThreads/FPGAThread.h
+++ b/Source/Processors/DataThreads/FPGAThread.h
@@ -25,12 +25,10 @@
 #define __FPGATHREAD_H_FBB22A45__
 
 
+#include "../../../JuceLibraryCode/JuceHeader.h"
+
 #include <stdio.h>
 #include <string.h>
-#include <iostream>
-#include <time.h>
-
-#include "../../../JuceLibraryCode/JuceHeader.h"
 
 #include "okFrontPanelDLL.h"
 #include "DataThread.h"
@@ -52,28 +50,25 @@ public:
 	FPGAThread(SourceNode* sn);
 	~FPGAThread();
 
-	bool foundInputSource(); // {return true;}
-	int getNumChannels();// {return 32;}
-	float getSampleRate();// {return 25000.0;}
+	bool foundInputSource();
+	int getNumChannels();
+	float getSampleRate();
 	
 private:
 
 	okCFrontPanel* dev;
 	char bitfile[128];
 	char dll_date[32], dll_time[32];
-	UINT32 i;
+	bool isTransmitting;
+	bool deviceFound;
 
-	bool initializeFPGA(okCFrontPanel*, char*);
+	bool initializeFPGA(bool);
 	bool closeFPGA();
 
 	bool startAcquisition();
 	bool stopAcquisition();
 
-	int m_u32SegmentSize;
-	
-	unsigned char pBuffer[50000];  // request a 1MB block of data
-
-	bool isRunning;
+	unsigned char pBuffer[50000];  // request a 50kb block of data
 
 	float thisSample[32];
 
diff --git a/Source/Processors/ProcessorGraph.cpp b/Source/Processors/ProcessorGraph.cpp
index 33b5fa2e9..8a73cdd27 100644
--- a/Source/Processors/ProcessorGraph.cpp
+++ b/Source/Processors/ProcessorGraph.cpp
@@ -314,6 +314,7 @@ GenericProcessor* ProcessorGraph::createProcessorFromDescription(String& descrip
 			}
 				
 			std::cout << "Creating a new data source." << std::endl;
+			
 		} else if (subProcessorType.equalsIgnoreCase("Signal Generator"))
 		{
 			processor = new SignalGenerator();
diff --git a/Source/Processors/SourceNode.cpp b/Source/Processors/SourceNode.cpp
index 37c6fd713..3fa8e709d 100644
--- a/Source/Processors/SourceNode.cpp
+++ b/Source/Processors/SourceNode.cpp
@@ -36,7 +36,7 @@ SourceNode::SourceNode(const String& name_)
 	if (getName().equalsIgnoreCase("Intan Demo Board")) {
 		dataThread = new IntanThread(this);
 	} else if (getName().equalsIgnoreCase("Custom FPGA")) {
-		dataThread = new FPGAThread(this);
+		dataThread = new FPGAThread(this);//FPGAThread(this);
 	} else if (getName().equalsIgnoreCase("File Reader")) {
 		dataThread = new FileReaderThread(this);
 	}
-- 
GitLab