1
0
mirror of https://github.com/preble/libpinproc synced 2026-02-22 18:15:25 +01:00

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.
This commit is contained in:
Tom Collins
2020-06-26 16:00:38 -07:00
committed by Gerry Stellenberg
parent c36d48bc64
commit ed6540858b
2 changed files with 6 additions and 16 deletions

View File

@@ -48,7 +48,6 @@ void ConfigureAccelerometerMotion(PRHandle proc)
PRReadData(proc, P3_ROC_BUS_ACCELEROMETER_SELECT, 0x10D, 1, readData); PRReadData(proc, P3_ROC_BUS_ACCELEROMETER_SELECT, 0x10D, 1, readData);
printf("Accel chip id: %x\n", readData[0]); printf("Accel chip id: %x\n", readData[0]);
fflush(stdout);
// Set FF_MT_COUNT (0x18) // Set FF_MT_COUNT (0x18)
readData[0] = 1; readData[0] = 1;
@@ -96,7 +95,6 @@ void ConfigureAccelerometerTransient(PRHandle proc)
PRReadData(proc, P3_ROC_BUS_ACCELEROMETER_SELECT, 0x10D, 1, readData); PRReadData(proc, P3_ROC_BUS_ACCELEROMETER_SELECT, 0x10D, 1, readData);
printf("Accel chip id: %x\n", readData[0]); printf("Accel chip id: %x\n", readData[0]);
fflush(stdout);
// Set to standby so register changes will take. // Set to standby so register changes will take.
readData[0] = 0x0; readData[0] = 0x0;
@@ -255,9 +253,6 @@ void RunLoop(PRHandle proc)
printf("Unknown event: %x:%x\n", event->type, event->value); printf("Unknown event: %x:%x\n", event->type, event->value);
} }
} }
if (numEvents > 0) {
fflush(stdout);
}
PRFlushWriteData(proc); PRFlushWriteData(proc);
#ifdef _MSC_VER #ifdef _MSC_VER
Sleep(10); Sleep(10);
@@ -292,6 +287,9 @@ int main(int argc, const char **argv)
{ {
int i; 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: // Set a signal handler so that we can exit gracefully on Ctrl-C:
signal(SIGINT, sigint); signal(SIGINT, sigint);
startTime = time(NULL); startTime = time(NULL);

View File

@@ -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, "\n\nUpdating P-ROC. This may take a couple of minutes.\n");
fprintf(stderr, "WARNING: DO NOT POWER CYCLE UNTIL COMPLETE!\n"); fprintf(stderr, "WARNING: DO NOT POWER CYCLE UNTIL COMPLETE!\n");
printf("\nErasing PROM... "); printf("\nErasing PROM... ");
fflush(stdout);
} }
if (numBytesCurrent == 5000) { if (numBytesCurrent == 5000) {
printf("complete.\nProgramming:\n0%% "); printf("complete.\nProgramming:\n0%% ");
fflush(stdout);
} }
// read in a byte of data from the xsvf file // read in a byte of data from the xsvf file
*data = (unsigned char)fgetc( in ); *data = (unsigned char)fgetc( in );
@@ -547,11 +545,9 @@ void readByte(unsigned char *data)
numBytesCurrent++; numBytesCurrent++;
if (numBytesCurrent % bytesPerTenth == 0) { if (numBytesCurrent % bytesPerTenth == 0) {
printf("\n%d0%% ",(int) (numBytesCurrent/bytesPerTenth)); printf("\n%d0%% ",(int) (numBytesCurrent/bytesPerTenth));
fflush(stdout);
} }
else if (numBytesCurrent % bytesPer200th == 0) { else if (numBytesCurrent % bytesPer200th == 0) {
printf("."); printf(".");
fflush(stdout);
} }
} }
@@ -1968,7 +1964,6 @@ void P3ROC_SPIBulkErase(void)
// Send bulk_erase command // Send bulk_erase command
printf("\nErasing flash ."); printf("\nErasing flash .");
fflush(stdout);
dataBuffer[0] = P3_ROC_SPI_OPCODE_BULK_ERASE << P3_ROC_SPI_OPCODE_SHIFT; dataBuffer[0] = P3_ROC_SPI_OPCODE_BULK_ERASE << P3_ROC_SPI_OPCODE_SHIFT;
PRWriteData (proc, P3_ROC_BUS_SPI_SELECT, addr, 1, dataBuffer); PRWriteData (proc, P3_ROC_BUS_SPI_SELECT, addr, 1, dataBuffer);
P3ROC_SPIWaitForReady(); P3ROC_SPIWaitForReady();
@@ -2012,7 +2007,6 @@ int verifyP3ROCImage(void)
P3ROC_SPIWaitForReady(); P3ROC_SPIWaitForReady();
printf("\n\nVerifying image:\n0%% "); printf("\n\nVerifying image:\n0%% ");
fflush(stdout);
while (!feof(in)) { while (!feof(in)) {
for (int i=0; i<64; i++) for (int i=0; i<64; i++)
{ {
@@ -2024,11 +2018,9 @@ int verifyP3ROCImage(void)
numBytesCurrent++; numBytesCurrent++;
if (numBytesCurrent % bytesPerTenth == 0) { if (numBytesCurrent % bytesPerTenth == 0) {
printf("\n%d0%% ", (int)(numBytesCurrent / bytesPerTenth)); printf("\n%d0%% ", (int)(numBytesCurrent / bytesPerTenth));
fflush(stdout);
} }
else if (numBytesCurrent % bytesPer200th == 0) { else if (numBytesCurrent % bytesPer200th == 0) {
printf("."); printf(".");
fflush(stdout);
} }
} }
@@ -2064,7 +2056,6 @@ void writeP3ROCImage(void)
P3ROC_SPIWaitForReady(); P3ROC_SPIWaitForReady();
printf("\nProgramming new image:\n0%% "); printf("\nProgramming new image:\n0%% ");
fflush(stdout);
while (!feof(in)) { while (!feof(in)) {
for (int i=0; i<64; i++) { for (int i=0; i<64; i++) {
if (!feof(in)) { if (!feof(in)) {
@@ -2073,11 +2064,9 @@ void writeP3ROCImage(void)
numBytesCurrent++; numBytesCurrent++;
if (numBytesCurrent % bytesPerTenth == 0) { if (numBytesCurrent % bytesPerTenth == 0) {
printf("\n%d0%% ", (int)(numBytesCurrent / bytesPerTenth)); printf("\n%d0%% ", (int)(numBytesCurrent / bytesPerTenth));
fflush(stdout);
} }
else if (numBytesCurrent % bytesPer200th == 0) { else if (numBytesCurrent % bytesPer200th == 0) {
printf("."); printf(".");
fflush(stdout);
} }
} }
@@ -2255,6 +2244,9 @@ int main( int argc, char** argv )
int i; int i;
int iErrorCode; int iErrorCode;
// Set stdout unbuffered to eliminate need to fflush()
setbuf(stdout, NULL);
iErrorCode = XSVF_ERRORCODE( XSVF_ERROR_NONE ); iErrorCode = XSVF_ERRORCODE( XSVF_ERROR_NONE );
pzXsvfFileName = NULL; pzXsvfFileName = NULL;