From 78ed8bbe67998f7ebe934119d1f53b697be8acd3 Mon Sep 17 00:00:00 2001 From: Gerry Stellenberg Date: Mon, 30 Jul 2012 19:22:19 -0500 Subject: [PATCH] Added accelerometer events --- examples/pinproctest/pinproctest.cpp | 49 +++++++++++++++++++++++++--- include/pinproc.h | 5 +++ src/PRDevice.cpp | 35 ++++++++++++++++++-- src/PRHardware.h | 6 +++- 4 files changed, 86 insertions(+), 9 deletions(-) diff --git a/examples/pinproctest/pinproctest.cpp b/examples/pinproctest/pinproctest.cpp index 754cd8e..b07ad86 100644 --- a/examples/pinproctest/pinproctest.cpp +++ b/examples/pinproctest/pinproctest.cpp @@ -80,7 +80,7 @@ void RunLoop(PRHandle proc) const int maxEvents = 16; int i; PREvent events[maxEvents]; - + uint32_t readData[5]; // Create dot array using an array of bytes. Each byte holds 8 dots. Need // space for 4 sub-frames of 128/32 dots. unsigned char dots[4*((128*32)/8)]; @@ -99,10 +99,27 @@ void RunLoop(PRHandle proc) } } + PRReadData(proc, 6, 0x10D, 1, readData); + printf("\nAccel chip id: %x\n", readData[0]); + fflush(stdout); + + readData[0] = 0x3D; + PRWriteData(proc, 6, 0x12A, 1, readData); + + readData[0] = 0x02; + PRWriteData(proc, 6, 0x12B, 1, readData); + + // Enable auto-polling of accelerometer every 128 ms (8 times a sec). + readData[0] = 0x0F; + PRWriteData(proc, 6, 0x000, 1, readData); + PRFlushWriteData(proc); + + int p = 0; + while (runLoopRun) { PRDriverWatchdogTickle(proc); - + int numEvents = PRGetEvents(proc, events, maxEvents); for (int i = 0; i < numEvents; i++) { @@ -122,7 +139,6 @@ void RunLoop(PRHandle proc) struct timeval tv; gettimeofday(&tv, NULL); #endif - switch (event->type) { case kPREventTypeSwitchOpenDebounced: @@ -149,6 +165,29 @@ void RunLoop(PRHandle proc) } break; } + case kPREventTypeAccelerometerX: + { + //readData[0] = event->value & 0x3FFF; + readData[0] = event->value; + break; + } + case kPREventTypeAccelerometerY: + { + //readData[1] = event->value & 0x3FFF; + readData[1] = event->value; + break; + } + case kPREventTypeAccelerometerZ: + { + //readData[2] = event->value & 0x3FFF; + readData[2] = event->value; + printf("\nAccel: X: %x, Y: %x, Z: %x", readData[0], readData[1],readData[2]); + break; + } + default: + { + printf("\nUnknown event: %x:%x", event->type, event->value); + } } } PRFlushWriteData(proc); @@ -211,7 +250,7 @@ int main(int argc, const char **argv) fprintf(stderr, "Unknown machine type: %s\n", machineTypeString.c_str()); return 1; } - + // Finally instantiate the P-ROC device: PRHandle proc = PRCreate(machineType); if (proc == kPRHandleInvalid) @@ -219,7 +258,7 @@ int main(int argc, const char **argv) fprintf(stderr, "Error during PRCreate: %s\n", PRGetLastErrorText()); return 1; } - + PRLogSetLevel (kPRLogInfo); PRReset(proc, kPRResetFlagUpdateDevice); // Reset the device structs and write them into the device. diff --git a/include/pinproc.h b/include/pinproc.h index a8975fe..cb5eeaf 100644 --- a/include/pinproc.h +++ b/include/pinproc.h @@ -376,6 +376,11 @@ typedef enum PREventType { kPREventTypeSwitchClosedNondebounced = 3, /**< The switch has gone from open to closed and the signal has not been debounced. */ kPREventTypeSwitchOpenNondebounced = 4, /**< The switch has gone from closed to open and the signal has not been debounced. */ kPREventTypeDMDFrameDisplayed = 5, /**< A DMD frame has been displayed. */ + kPREventTypeBurstSwitchOpen = 6, /**< A burst switch has gone from closed to open. */ + kPREventTypeBurstSwitchClosed = 7, /**< A burst switch has gone from open to closed. */ + kPREventTypeAccelerometerX = 8, /**< New value from the accelerometer - X plane. */ + kPREventTypeAccelerometerY = 9, /**< New value from the accelerometer - X plane. */ + kPREventTypeAccelerometerZ = 10, /**< New value from the accelerometer - X plane. */ kPREventTypetLast = kPREventTypeSwitchOpenNondebounced } PREventType; diff --git a/src/PRDevice.cpp b/src/PRDevice.cpp index 2ba9584..d626820 100644 --- a/src/PRDevice.cpp +++ b/src/PRDevice.cpp @@ -193,6 +193,7 @@ int PRDevice::GetEvents(PREvent *events, int maxEvents) } + //fprintf(stderr, "\nLibpinproc: event type: %d", type); switch (type) { case P_ROC_EVENT_TYPE_SWITCH: @@ -212,11 +213,39 @@ int PRDevice::GetEvents(PREvent *events, int maxEvents) case P_ROC_EVENT_TYPE_BURST_SWITCH: { - if (open) events[i].type = kPREventTypeSwitchOpenNondebounced; - else events[i].type = kPREventTypeSwitchClosedNondebounced; + //fprintf(stderr, "\nBurst event"); + if (open) events[i].type = kPREventTypeBurstSwitchOpen; + else events[i].type = kPREventTypeBurstSwitchClosed; break; } + case P_ROC_EVENT_TYPE_ACCELEROMETER: + { + events[i].time = events[i].time >> 2; + events[i].value = event_data & 0x00003FFF; + int accel_type = (event_data & 0x00030000) >> 16; + switch (accel_type) + { + case 0: + { + events[i].type = kPREventTypeAccelerometerX; + break; + } + case 1: + { + events[i].type = kPREventTypeAccelerometerY; + break; + } + case 2: + { + events[i].type = kPREventTypeAccelerometerZ; + break; + } + default: events[i].type = kPREventTypeInvalid; + } + break; + } + default: events[i].type = kPREventTypeInvalid; } @@ -1290,7 +1319,7 @@ int32_t PRDevice::CollectReadData() PRResult PRDevice::SortReturningData() { int32_t num_bytes, num_words, rc; - uint32_t rd_buffer[512]; + uint32_t rd_buffer[FTDI_BUFFER_SIZE/4]; num_bytes = CollectReadData(); if (num_bytes < 0) diff --git a/src/PRHardware.h b/src/PRHardware.h index 8fe5a5c..5216fac 100644 --- a/src/PRHardware.h +++ b/src/PRHardware.h @@ -42,7 +42,8 @@ const int32_t FTDI_VENDOR_ID = 0x0403; const int32_t FTDI_FT245RL_PRODUCT_ID = 0x6001; -const int32_t FTDI_BUFFER_SIZE = 2048; +//const int32_t FTDI_BUFFER_SIZE = 2048; +const int32_t FTDI_BUFFER_SIZE = 8192; const uint32_t P_ROC_INIT_PATTERN_A = 0x801F1122; const uint32_t P_ROC_INIT_PATTERN_B = 0x345678AB; const uint32_t P_ROC_CHIP_ID = 0xfeedbeef; @@ -130,6 +131,7 @@ const uint32_t P_ROC_SWITCH_CTRL_DEBOUNCE_BASE_ADDR = 12; const uint32_t P_ROC_EVENT_TYPE_SWITCH = 0; const uint32_t P_ROC_EVENT_TYPE_DMD = 1; const uint32_t P_ROC_EVENT_TYPE_BURST_SWITCH = 2; +const uint32_t P_ROC_EVENT_TYPE_ACCELEROMETER = 3; const uint32_t P_ROC_V1_EVENT_TYPE_MASK = 0xC00; const uint32_t P_ROC_V1_EVENT_TYPE_SHIFT = 10; @@ -150,6 +152,8 @@ const uint32_t P_ROC_V1_EVENT_SWITCH_TIMESTAMP_MASK = 0xFFFFF000; const uint32_t P_ROC_V1_EVENT_SWITCH_TIMESTAMP_SHIFT = 12; const uint32_t P_ROC_V2_EVENT_SWITCH_TIMESTAMP_MASK = 0xFFFF0000; const uint32_t P_ROC_V2_EVENT_SWITCH_TIMESTAMP_SHIFT = 16; +const uint32_t P_ROC_V2_EVENT_ACCEL_TIMESTAMP_MASK = 0xFFFC0000; +const uint32_t P_ROC_V2_EVENT_ACCEL_TIMESTAMP_SHIFT = 18; const uint32_t P_ROC_DRIVER_CTRL_DECODE_SHIFT = 10;