From 954084d348ec1ce1aeea7bbc55f54738d590a16e Mon Sep 17 00:00:00 2001 From: Tom Collins Date: Thu, 25 Jun 2020 23:28:13 -0700 Subject: [PATCH] pinproctest: code cleanup Includes check to limit accelerometer-related code to P3-ROC since it fails on the P-ROC (which doesn't have an accelerometer). --- examples/pinproctest/drivers.cpp | 2 - examples/pinproctest/pinproctest.cpp | 107 +++++++++++++++------------ examples/pinproctest/switches.cpp | 12 ++- 3 files changed, 64 insertions(+), 57 deletions(-) diff --git a/examples/pinproctest/drivers.cpp b/examples/pinproctest/drivers.cpp index 9cf7164..134ca37 100644 --- a/examples/pinproctest/drivers.cpp +++ b/examples/pinproctest/drivers.cpp @@ -222,8 +222,6 @@ void ConfigureDriverGlobals(PRHandle proc, bool driverPolarity) void ConfigureDrivers(PRHandle proc) { - int i; - // First set up a bunch of constants to use later: // The driverPolarity determines when the drivers go high or low when diff --git a/examples/pinproctest/pinproctest.cpp b/examples/pinproctest/pinproctest.cpp index 10d2075..cf4bb63 100644 --- a/examples/pinproctest/pinproctest.cpp +++ b/examples/pinproctest/pinproctest.cpp @@ -27,115 +27,124 @@ * libpinproc */ #include "pinproctest.h" +uint32_t board_id = 0; PRMachineType machineType = kPRMachineInvalid; /** Demonstration of the custom logging callback. */ void TestLogger(PRLogLevel level, const char *text) { - fprintf(stderr, "TEST: %s", text); + printf("TEST: %s", text); } void ConfigureAccelerometerMotion(PRHandle proc) { uint32_t readData[5]; - PRReadData(proc, 6, 0x10D, 1, readData); + // Only the P3-ROC has an accelerometer. + if (board_id != P3_ROC_CHIP_ID) { + return; + } + + PRReadData(proc, P3_ROC_BUS_ACCELEROMETER_SELECT, 0x10D, 1, readData); printf("\nAccel chip id: %x\n", readData[0]); fflush(stdout); // Set FF_MT_COUNT (0x18) readData[0] = 1; - PRWriteData(proc, 6, 0x118, 1, readData); + PRWriteData(proc, P3_ROC_BUS_ACCELEROMETER_SELECT, 0x118, 1, readData); // Set FF_MT_THRESH (0x17) readData[0] = 1; - PRWriteData(proc, 6, 0x117, 1, readData); + PRWriteData(proc, P3_ROC_BUS_ACCELEROMETER_SELECT, 0x117, 1, readData); // Set FF_MT_CONFIG (0x15) readData[0] = 0xD8; - PRWriteData(proc, 6, 0x115, 1, readData); + PRWriteData(proc, P3_ROC_BUS_ACCELEROMETER_SELECT, 0x115, 1, readData); // Enable Motion interrupts readData[0] = 0x04; - PRWriteData(proc, 6, 0x12D, 1, readData); + PRWriteData(proc, P3_ROC_BUS_ACCELEROMETER_SELECT, 0x12D, 1, readData); // Direct motion interrupt to int0 pin (default) readData[0] = 0x04; - PRWriteData(proc, 6, 0x12E, 1, readData); + PRWriteData(proc, P3_ROC_BUS_ACCELEROMETER_SELECT, 0x12E, 1, readData); readData[0] = 0x3D; - PRWriteData(proc, 6, 0x12A, 1, readData); + PRWriteData(proc, P3_ROC_BUS_ACCELEROMETER_SELECT, 0x12A, 1, readData); readData[0] = 0x02; - PRWriteData(proc, 6, 0x12B, 1, readData); + PRWriteData(proc, P3_ROC_BUS_ACCELEROMETER_SELECT, 0x12B, 1, readData); // Enable auto-polling of accelerometer every 128 ms (8 times a sec). //readData[0] = 0x0F; // Enable polling, 8 times a second. readData[0] = 0x00; // Disable polling readData[0] = readData[0] | 0x1600; // Set IRQ status addr (FF_MT_SRC) - PRWriteData(proc, 6, 0x000, 1, readData); + PRWriteData(proc, P3_ROC_BUS_ACCELEROMETER_SELECT, 0x000, 1, readData); PRFlushWriteData(proc); - } void ConfigureAccelerometerTransient(PRHandle proc) { uint32_t readData[5]; - PRReadData(proc, 6, 0x10D, 1, readData); + // Only the P3-ROC has an accelerometer. + if (board_id != P3_ROC_CHIP_ID) { + return; + } + + PRReadData(proc, P3_ROC_BUS_ACCELEROMETER_SELECT, 0x10D, 1, readData); printf("\nAccel chip id: %x\n", readData[0]); fflush(stdout); // Set to standby so register changes will take. readData[0] = 0x0; - PRWriteData(proc, 6, 0x12A, 1, readData); + PRWriteData(proc, P3_ROC_BUS_ACCELEROMETER_SELECT, 0x12A, 1, readData); - // Set HPF_OUT bit in XYZ_DATA_CFG (0xOE) + // Set HPF_OUT bit in XYZ_DATA_CFG (0x0E) //readData[0] = 0x10; - //PRWriteData(proc, 6, 0x10E, 1, readData); + //PRWriteData(proc, P3_ROC_BUS_ACCELEROMETER_SELECT, 0x10E, 1, readData); // Set HP_FILTER_CUTOFF (0x0F) readData[0] = 0x03; - PRWriteData(proc, 6, 0x10F, 1, readData); + PRWriteData(proc, P3_ROC_BUS_ACCELEROMETER_SELECT, 0x10F, 1, readData); // Set FF_TRANSIENT_COUNT (0x20) readData[0] = 1; - PRWriteData(proc, 6, 0x120, 1, readData); + PRWriteData(proc, P3_ROC_BUS_ACCELEROMETER_SELECT, 0x120, 1, readData); // Set FF_TRANSIENT_THRESH (0x1F) readData[0] = 1; - PRWriteData(proc, 6, 0x11F, 1, readData); + PRWriteData(proc, P3_ROC_BUS_ACCELEROMETER_SELECT, 0x11F, 1, readData); // Set FF_TRANSIENT_CONFIG (0x1D) readData[0] = 0x1E; - PRWriteData(proc, 6, 0x11D, 1, readData); + PRWriteData(proc, P3_ROC_BUS_ACCELEROMETER_SELECT, 0x11D, 1, readData); // Enable Motion interrupts readData[0] = 0x20; - PRWriteData(proc, 6, 0x12D, 1, readData); + PRWriteData(proc, P3_ROC_BUS_ACCELEROMETER_SELECT, 0x12D, 1, readData); // Direct motion interrupt to int0 pin (default) readData[0] = 0x20; - PRWriteData(proc, 6, 0x12E, 1, readData); + PRWriteData(proc, P3_ROC_BUS_ACCELEROMETER_SELECT, 0x12E, 1, readData); //readData[0] = 0x3D; readData[0] = 0x05; - PRWriteData(proc, 6, 0x12A, 1, readData); + PRWriteData(proc, P3_ROC_BUS_ACCELEROMETER_SELECT, 0x12A, 1, readData); readData[0] = 0x02; - PRWriteData(proc, 6, 0x12B, 1, readData); + PRWriteData(proc, P3_ROC_BUS_ACCELEROMETER_SELECT, 0x12B, 1, readData); // Enable auto-polling of accelerometer every 128 ms (8 times a sec). //readData[0] = 0x0F; // Enable polling, 8 times a second. readData[0] = 0x00; // Disable polling readData[0] = readData[0] | 0x1E00; // Set IRQ status addr (FF_MT_SRC) - PRWriteData(proc, 6, 0x000, 1, readData); + PRWriteData(proc, P3_ROC_BUS_ACCELEROMETER_SELECT, 0x000, 1, readData); PRFlushWriteData(proc); - } time_t startTime; @@ -174,19 +183,6 @@ void RunLoop(PRHandle proc) { PRDriverWatchdogTickle(proc); -// PRReadData(proc, 6, 0x115, 1, readData); -// printf("\n\n\nAccel chip id: %x\n", readData[0]); -// PRReadData(proc, 6, 0x116, 1, readData); -// printf("\nAccel chip id: %x\n", readData[0]); -// PRReadData(proc, 6, 0x117, 1, readData); -// printf("\nAccel chip id: %x\n", readData[0]); -// PRReadData(proc, 6, 0x118, 1, readData); -// printf("\nAccel chip id: %x\n", readData[0]); -// PRReadData(proc, 6, 0x12D, 1, readData); -// printf("\nAccel chip id: %x\n", readData[0]); -// PRReadData(proc, 6, 0x12E, 1, readData); -// printf("\nAccel chip id: %x\n", readData[0]); - int numEvents = PRGetEvents(proc, events, maxEvents); // if (numEvents > 0) printf("\nNum events: %x\n", numEvents); for (int i = 0; i < numEvents; i++) @@ -202,7 +198,7 @@ void RunLoop(PRHandle proc) } #ifdef _MSC_VER struct _timeb tv; - _ftime(&tv); + _ftime_s(&tv); #else struct timeval tv; gettimeofday(&tv, NULL); @@ -215,9 +211,9 @@ void RunLoop(PRHandle proc) case kPREventTypeSwitchClosedNondebounced: { #ifdef _MSC_VER - printf("%d.%03d switch % 3d: %s\n", tv.time-startTime, tv.millitm, event->value, stateText); + printf("%d.%03d switch %3d: %s\n", (int)(tv.time-startTime), tv.millitm, event->value, stateText); #else - printf("%d.%03d switch % 3d: %s\n", (int)(tv.tv_sec-startTime), (int)tv.tv_usec/1000, event->value, stateText); + printf("%d.%03d switch %3d: %s\n", (int)(tv.tv_sec-startTime), (int)tv.tv_usec/1000, event->value, stateText); #endif UpdateSwitchState( event ); break; @@ -265,6 +261,9 @@ void RunLoop(PRHandle proc) } } } + if (numEvents > 0) { + fflush(stdout); + } PRFlushWriteData(proc); #ifdef _MSC_VER Sleep(10); @@ -328,22 +327,35 @@ int main(int argc, const char **argv) // Finally instantiate the P-ROC device: PRHandle proc = PRCreate(machineType); - if (proc == kPRHandleInvalid) - { - fprintf(stderr, "Error during PRCreate: %s\n", PRGetLastErrorText()); + if (proc == kPRHandleInvalid) { + printf("Error during PRCreate: %s\n", PRGetLastErrorText()); return 1; } + PRReadData(proc, P_ROC_MANAGER_SELECT, P_ROC_REG_CHIP_ID_ADDR, 1, &board_id); + if (board_id == P_ROC_CHIP_ID) { + printf("Connected to P-ROC\n"); + } + else if (board_id == P3_ROC_CHIP_ID) { + printf("Connected to P3-ROC\n"); + } + else { + printf("Warning: unrecognized board ID 0x%08X\n", board_id); + } - PRLogSetLevel (kPRLogInfo); + PRLogSetLevel(kPRLogInfo); PRReset(proc, kPRResetFlagUpdateDevice); // Reset the device structs and write them into the device. // Even if WPCAlphanumeric, configure the DMD at least to get frame events for // timing purposes. ConfigureDMD(proc); - if (machineType == kPRMachineCustom) ConfigureDrivers(proc); + if (machineType == kPRMachineCustom) { + ConfigureDrivers(proc); + } ConfigureSwitches(proc); // Notify host for all debounced switch events. - if (machineType == kPRMachineWPCAlphanumeric) UpdateAlphaDisplay(proc, 0); + if (machineType == kPRMachineWPCAlphanumeric) { + UpdateAlphaDisplay(proc, 0); + } // Pulse a coil for testing purposes. PRDriverPulse(proc, 47, 30); @@ -385,7 +397,6 @@ int main(int argc, const char **argv) */ PRFlushWriteData(proc); - printf("Running. Hit Ctrl-C to exit.\n"); RunLoop(proc); diff --git a/examples/pinproctest/switches.cpp b/examples/pinproctest/switches.cpp index 75a99d5..641e560 100644 --- a/examples/pinproctest/switches.cpp +++ b/examples/pinproctest/switches.cpp @@ -151,7 +151,7 @@ void LoadSwitchStates( PRHandle proc ) // Get all of the switch states from the P-ROC. if (PRSwitchGetStates( proc, procSwitchStates, kPRSwitchPhysicalLast + 1 ) == kPRFailure) { - fprintf(stderr, "Error: Unable to retrieve switch states\n"); + printf("Error: Unable to retrieve switch states\n"); } else { @@ -162,17 +162,15 @@ void LoadSwitchStates( PRHandle proc ) } int zero = 0; - fprintf(stderr, "\nCurrent Switch States: %3d : ", zero); for (i = 0; i < kPRSwitchPhysicalLast + 1; i++) { - fprintf(stderr, "%d ", switches[i].state); - if ((i + 1) % 32 == 0) - { + if (i % 32 == 0) { printf("\n"); if (i != kPRSwitchPhysicalLast) - fprintf(stderr, "Current Switch States: %3d : ", i+1); + printf("Current Switch States: %3d: ", i); } + printf("%d", switches[i].state); } - fprintf(stderr, "\n"); + printf("\n"); } }