From ed6540858bbaeb15b892746cac134c6f041fbdbb Mon Sep 17 00:00:00 2001 From: Tom Collins Date: Fri, 26 Jun 2020 16:00:38 -0700 Subject: [PATCH] cleanup: unbuffered stdout to avoid fflush() By setting stdout to unbuffered, the user always sees our output and we can eliminate the fflush() calls sprinkled throughout the programs. --- examples/pinproctest/pinproctest.cpp | 8 +++----- utils/pinprocfw/pinprocfw.cpp | 14 +++----------- 2 files changed, 6 insertions(+), 16 deletions(-) diff --git a/examples/pinproctest/pinproctest.cpp b/examples/pinproctest/pinproctest.cpp index 21a45d7..77d8c3f 100644 --- a/examples/pinproctest/pinproctest.cpp +++ b/examples/pinproctest/pinproctest.cpp @@ -48,7 +48,6 @@ void ConfigureAccelerometerMotion(PRHandle proc) PRReadData(proc, P3_ROC_BUS_ACCELEROMETER_SELECT, 0x10D, 1, readData); printf("Accel chip id: %x\n", readData[0]); - fflush(stdout); // Set FF_MT_COUNT (0x18) readData[0] = 1; @@ -96,7 +95,6 @@ void ConfigureAccelerometerTransient(PRHandle proc) PRReadData(proc, P3_ROC_BUS_ACCELEROMETER_SELECT, 0x10D, 1, readData); printf("Accel chip id: %x\n", readData[0]); - fflush(stdout); // Set to standby so register changes will take. readData[0] = 0x0; @@ -255,9 +253,6 @@ void RunLoop(PRHandle proc) printf("Unknown event: %x:%x\n", event->type, event->value); } } - if (numEvents > 0) { - fflush(stdout); - } PRFlushWriteData(proc); #ifdef _MSC_VER Sleep(10); @@ -292,6 +287,9 @@ int main(int argc, const char **argv) { int i; + // Set stdout unbuffered to eliminate need to fflush() + setbuf(stdout, NULL); + // Set a signal handler so that we can exit gracefully on Ctrl-C: signal(SIGINT, sigint); startTime = time(NULL); diff --git a/utils/pinprocfw/pinprocfw.cpp b/utils/pinprocfw/pinprocfw.cpp index b7d8401..43c32ac 100644 --- a/utils/pinprocfw/pinprocfw.cpp +++ b/utils/pinprocfw/pinprocfw.cpp @@ -533,12 +533,10 @@ void readByte(unsigned char *data) fprintf(stderr, "\n\nUpdating P-ROC. This may take a couple of minutes.\n"); fprintf(stderr, "WARNING: DO NOT POWER CYCLE UNTIL COMPLETE!\n"); printf("\nErasing PROM... "); - fflush(stdout); } if (numBytesCurrent == 5000) { printf("complete.\nProgramming:\n0%% "); - fflush(stdout); } // read in a byte of data from the xsvf file *data = (unsigned char)fgetc( in ); @@ -547,11 +545,9 @@ void readByte(unsigned char *data) numBytesCurrent++; if (numBytesCurrent % bytesPerTenth == 0) { printf("\n%d0%% ",(int) (numBytesCurrent/bytesPerTenth)); - fflush(stdout); } else if (numBytesCurrent % bytesPer200th == 0) { printf("."); - fflush(stdout); } } @@ -1968,7 +1964,6 @@ void P3ROC_SPIBulkErase(void) // Send bulk_erase command printf("\nErasing flash ."); - fflush(stdout); dataBuffer[0] = P3_ROC_SPI_OPCODE_BULK_ERASE << P3_ROC_SPI_OPCODE_SHIFT; PRWriteData (proc, P3_ROC_BUS_SPI_SELECT, addr, 1, dataBuffer); P3ROC_SPIWaitForReady(); @@ -2012,7 +2007,6 @@ int verifyP3ROCImage(void) P3ROC_SPIWaitForReady(); printf("\n\nVerifying image:\n0%% "); - fflush(stdout); while (!feof(in)) { for (int i=0; i<64; i++) { @@ -2024,11 +2018,9 @@ int verifyP3ROCImage(void) numBytesCurrent++; if (numBytesCurrent % bytesPerTenth == 0) { printf("\n%d0%% ", (int)(numBytesCurrent / bytesPerTenth)); - fflush(stdout); } else if (numBytesCurrent % bytesPer200th == 0) { printf("."); - fflush(stdout); } } @@ -2064,7 +2056,6 @@ void writeP3ROCImage(void) P3ROC_SPIWaitForReady(); printf("\nProgramming new image:\n0%% "); - fflush(stdout); while (!feof(in)) { for (int i=0; i<64; i++) { if (!feof(in)) { @@ -2073,11 +2064,9 @@ void writeP3ROCImage(void) numBytesCurrent++; if (numBytesCurrent % bytesPerTenth == 0) { printf("\n%d0%% ", (int)(numBytesCurrent / bytesPerTenth)); - fflush(stdout); } else if (numBytesCurrent % bytesPer200th == 0) { printf("."); - fflush(stdout); } } @@ -2255,6 +2244,9 @@ int main( int argc, char** argv ) int i; int iErrorCode; + // Set stdout unbuffered to eliminate need to fflush() + setbuf(stdout, NULL); + iErrorCode = XSVF_ERRORCODE( XSVF_ERROR_NONE ); pzXsvfFileName = NULL;