diff core/WriteFile.cpp @ 157:f36313cbb55d

Added capability to WriteFile to save binary files, added example project
author Giulio Moro <giuliomoro@yahoo.it>
date Tue, 13 Oct 2015 02:01:05 +0100
parents 3b7270949a97
children 45fc760622c9
line wrap: on
line diff
--- a/core/WriteFile.cpp	Wed Oct 07 23:38:52 2015 +0100
+++ b/core/WriteFile.cpp	Tue Oct 13 02:01:05 2015 +0100
@@ -37,42 +37,60 @@
 	lineLength = 0;
 	echo = false;
 	bufferLength = 0;
-	readPointer = 0;
+	textReadPointer = 0;
+	binaryReadPointer = 0;
 	writePointer = 0;
 	sleepTimeMs = 1;
 	stringBufferLength = 1000;
 	stringBuffer = (char*)malloc(sizeof(char) * (stringBufferLength));
 	setHeader("variable=[\n");
 	setFooter("];\n");
-	staticConstructor(); //TODO: this line should be in the constructor, but cannot because of a bug in BeagleRT
+	staticConstructor(); //TODO: this line should be in the constructor, but cannot be because of a bug in BeagleRT
 	objAddrs.push_back(this);
+	echoedLines = 0;
+	echoPeriod = 1;
 }
 
+void WriteFile::setFileType(WriteFileType newFileType){
+	fileType = newFileType;
+}
 void WriteFile::setEcho(bool newEcho){
 	echo=newEcho;
 }
-
+void WriteFile::setEchoInterval(int newEchoPeriod){
+	echoPeriod = newEchoPeriod;
+	if(echoPeriod != 0)
+		echo = true;
+	else
+		echo = false;
+}
 void WriteFile::print(const char* string){
 	if(echo == true){
-		printf("%s", string);
+		echoedLines++;
+		if (echoedLines >= echoPeriod){
+			echoedLines = 0;
+			printf("%s", string);
+		}
 	}
-	if(file != NULL){
+	if(file != NULL && fileType != kBinary){
 		fprintf(file, "%s", string);
 	}
 }
 
 void WriteFile::writeLine(){
-	int stringBufferPointer = 0;
-	for(unsigned int n = 0; n < formatTokens.size(); n++){
-		int numOfCharsWritten = snprintf( &stringBuffer[stringBufferPointer], stringBufferLength - stringBufferPointer,
-							formatTokens[n], buffer[readPointer]);
-		stringBufferPointer += numOfCharsWritten;
-		readPointer++;
-		if (readPointer >= bufferLength){
-			readPointer -= bufferLength;
+	if(echo == true || fileType != kBinary){
+		int stringBufferPointer = 0;
+		for(unsigned int n = 0; n < formatTokens.size(); n++){
+			int numOfCharsWritten = snprintf( &stringBuffer[stringBufferPointer], stringBufferLength - stringBufferPointer,
+								formatTokens[n], buffer[textReadPointer]);
+			stringBufferPointer += numOfCharsWritten;
+			textReadPointer++;
+			if (textReadPointer >= bufferLength){
+				textReadPointer -= bufferLength;
+			}
 		}
+		print(stringBuffer);
 	}
-	print(stringBuffer);
 }
 
 void WriteFile::setLineLength(int newLineLength){
@@ -83,15 +101,16 @@
 }
 
 void WriteFile::log(float value){
-	if(format == NULL || buffer == NULL)
+	if(fileType != kBinary && (format == NULL || buffer == NULL))
 		return;
 	buffer[writePointer] = value;
 	writePointer++;
 	if(writePointer == bufferLength){
 		writePointer = 0;
 	}
-	if(writePointer == readPointer){
-		fprintf(stderr, "WriteFile: pointers crossed, you should probably write less data to disk\n");
+	if((fileType == kText && writePointer == textReadPointer - 1) ||
+			(fileType == kBinary && writePointer == binaryReadPointer - 1)){
+		fprintf(stderr, "%d %d WriteFile: pointers crossed, you should probably slow down your writing to disk\n", writePointer, binaryReadPointer);
 	}
 	if(threadRunning == false){
 		startThread();
@@ -168,23 +187,52 @@
 	return 1-getOffset()/(float)bufferLength;
 }
 
+int WriteFile::getOffsetFromPointer(int aReadPointer){
+	int offset = writePointer - aReadPointer;
+		if( offset < 0)
+			offset += bufferLength;
+		return offset;
+}
 int WriteFile::getOffset(){
-	int offset = writePointer - readPointer;
-	if( offset < 0)
-		offset += bufferLength;
-	return offset;
+	if(fileType == kBinary){
+		return getOffsetFromPointer(binaryReadPointer);
+	}
+	else{
+		return getOffsetFromPointer(textReadPointer);
+	}
 }
 
-void WriteFile::writeOutput(){
-	while( getOffset() >= lineLength ){ //if there is less than one line worth of data to write, skip over.
-							 	 // So we make sure we always write full lines
+void WriteFile::writeOutput(bool writeAll){
+	while( getOffsetFromPointer(textReadPointer) >= lineLength){ //if there is less than one line worth of data to write, skip over.
+							 	 // So we make sure we only write full lines
 		writeLine();
 	}
+	if(fileType == kBinary){
+		int numBinaryElementsToWriteAtOnce = 100;
+		while(getOffsetFromPointer(binaryReadPointer) > numBinaryElementsToWriteAtOnce){
+			int elementsToEndOfBuffer = bufferLength - binaryReadPointer;
+			int numberElementsToWrite = numBinaryElementsToWriteAtOnce < elementsToEndOfBuffer ?
+					numBinaryElementsToWriteAtOnce : elementsToEndOfBuffer;
+			numberElementsToWrite = fwrite(&(buffer[binaryReadPointer]), sizeof(float), numberElementsToWrite, file);
+			binaryReadPointer += numberElementsToWrite;
+			if(binaryReadPointer >= bufferLength){
+				binaryReadPointer = 0;
+			}
+		}
+		if(writeAll == true){ // flush all the buffer to the file
+			while(getOffsetFromPointer(binaryReadPointer) != 0){
+				binaryReadPointer += fwrite(&(buffer[binaryReadPointer]), sizeof(float), 1, file);
+				if(binaryReadPointer >= bufferLength){
+					binaryReadPointer = 0;
+				}
+			}
+		}
+	}
 }
 
 void WriteFile::writeAllOutputs(){
 	for(unsigned int n = 0; n < objAddrs.size(); n++){
-		objAddrs[n] -> writeOutput();
+		objAddrs[n] -> writeOutput(false);
 	}
 }