From 06442264a1ddee9d8fafcedf778435a654dea9c4 Mon Sep 17 00:00:00 2001 From: gstellenberg Date: Wed, 27 May 2009 19:10:44 -0500 Subject: [PATCH] Made PRDevice::Reset public for use in pinproctest and added a parameter to allow the disabled driver and switch rule structures to be written to the P-ROC. --- examples/pinproctest/pinproctest.cpp | 4 ++ include/pinproc.h | 5 +++ src/PRDevice.cpp | 56 ++++++++++++---------------- src/PRDevice.h | 3 +- src/pinproc.cpp | 6 +++ 5 files changed, 39 insertions(+), 35 deletions(-) diff --git a/examples/pinproctest/pinproctest.cpp b/examples/pinproctest/pinproctest.cpp index 4582a73..8e2f198 100644 --- a/examples/pinproctest/pinproctest.cpp +++ b/examples/pinproctest/pinproctest.cpp @@ -415,6 +415,10 @@ int main(int argc, const char **argv) RunLoop(proc); + // Clean up P-ROC. + printf("Disabling P-ROC drivers and switch rules...\n"); + PRReset(proc,true); // Reset the device structs and write them into the device. + // Destroy the P-ROC device handle: PRDelete(proc); proc = kPRHandleInvalid; diff --git a/include/pinproc.h b/include/pinproc.h index 71a56a2..fc791ab 100644 --- a/include/pinproc.h +++ b/include/pinproc.h @@ -90,6 +90,7 @@ typedef enum PRMachineType { PR_EXPORT PRHandle PRCreate(PRMachineType machineType); /**< Create a new P-ROC device handle. Only one handle per device may be created. This handle must be destroyed with PRDelete() when it is no longer needed. Returns #kPRHandleInvalid if an error occurred. */ PR_EXPORT void PRDelete(PRHandle handle); /**< Destroys an existing P-ROC device handle. */ +PR_EXPORT void PRReset(PRHandle handle, bool updateDevice); /**< Resets internally maintained driver and switch rule structures and optionally writes those to the P-ROC to turn off drivers and switch rules. */ /** @} */ // End of Device Creation & Deletion @@ -98,6 +99,9 @@ PR_EXPORT void PRDelete(PRHandle handle); /**< Destroys an existin * @{ */ +#define kPRDriverGroupsMax (26) /**< Number of available driver groups. */ +#define kPRDriverCount (256) /**< Total number of drivers */ + typedef struct PRDriverGlobalConfig { bool_t enableOutputs; // Formerly enable_direct_outputs bool_t globalPolarity; @@ -235,6 +239,7 @@ PR_EXPORT int PRGetEvents(PRHandle handle, PREvent *eventsOut, int maxEvents); #define kPRSwitchPhysicalLast (223) /**< Switch number of the last physical switch. */ #define kPRSwitchVirtualFirst (224) /**< Switch number of the first virtual switch. */ #define kPRSwitchVirtualLast (255) /**< Switch number of the last virtual switch. */ +#define kPRSwitchRulesCount ((kPRSwitchVirtualLast + 1) << 2) /**< Total number of available switch rules. */ typedef struct PRSwitchConfig { bool_t clear; // Drive the clear output diff --git a/src/PRDevice.cpp b/src/PRDevice.cpp index fb5fe48..ae69440 100644 --- a/src/PRDevice.cpp +++ b/src/PRDevice.cpp @@ -32,12 +32,12 @@ PRDevice::PRDevice(PRMachineType machineType) : machineType(machineType) { - Reset(); + // Reset internally maintainted driver and switch structures, but do not update the device. + Reset(false); } PRDevice::~PRDevice() { - Shutdown(); Close(); } @@ -58,34 +58,50 @@ PRDevice* PRDevice::Create(PRMachineType machineType) return NULL; } - dev->Reset(); + // Reset internally maintainted driver and switch structures, but do not update the device. + dev->Reset(false); return dev; } -void PRDevice::Reset() +void PRDevice::Reset(bool updateDevice) { bool defaultPolarity = machineType != kPRMachineWPC; int i; memset(&driverGlobalConfig, 0x00, sizeof(PRDriverGlobalConfig)); - for (i = 0; i < maxDrivers; i++) + for (i = 0; i < kPRDriverCount; i++) { PRDriverState *driver = &drivers[i]; memset(driver, 0x00, sizeof(PRDriverState)); driver->driverNum = i; driver->polarity = defaultPolarity; + if (updateDevice) DriverUpdateState(driver); } - for (i = 0; i < maxDriverGroups; i++) + for (i = 0; i < kPRDriverGroupsMax; i++) { PRDriverGroupConfig *group = &driverGroups[i]; memset(group, 0x00, sizeof(PRDriverGroupConfig)); group->groupNum = i; group->polarity = defaultPolarity; } - for (i = 0; i < maxSwitchRules; i++) + + // Create empty switch rule for clearing the rules in the device. + PRSwitchRule emptySwitchRule; + memset(&emptySwitchRule, 0x00, sizeof(PRSwitchRule)); + + for (i = 0; i < kPRSwitchRulesCount; i++) { PRSwitchRuleInternal *switchRule = &switchRules[i]; memset(switchRule, 0x00, sizeof(PRSwitchRule)); + + // Send blank rule for each event type to Device if necessary + if (updateDevice && i <= kPRSwitchPhysicalLast) { + SwitchUpdateRule(i, kPREventTypeSwitchOpenDebounced, &emptySwitchRule, NULL, 0); + SwitchUpdateRule(i, kPREventTypeSwitchClosedDebounced, &emptySwitchRule, NULL, 0); + SwitchUpdateRule(i, kPREventTypeSwitchOpenNondebounced, &emptySwitchRule, NULL, 0); + SwitchUpdateRule(i, kPREventTypeSwitchClosedNondebounced, &emptySwitchRule, NULL, 0); + } + uint16_t ruleIndex = i; ParseSwitchRuleIndex(ruleIndex, &switchRule->switchNum, &switchRule->eventType); switchRule->driver.polarity = defaultPolarity; @@ -100,32 +116,6 @@ void PRDevice::Reset() // TODO: Assign defaults based on machineType. Some may have already been done above. } -void PRDevice::Shutdown() -{ - int i; - PRDriverState driverState; - PRSwitchRule switchRule; - - // Deactivate all drivers - for (i = 0; i < maxDrivers; i++) - { - // Get each driver's current state just in case polarity was changed from the default. - DriverGetState(i, &driverState); - driverState.state = false; - DriverUpdateState(&driverState); - } - - // Deactivate all switch rules - switchRule.notifyHost = false; - for (i = kPRSwitchPhysicalFirst; i < kPRSwitchPhysicalLast; i++) - { - SwitchUpdateRule(i, kPREventTypeSwitchOpenDebounced, &switchRule, NULL, 0); - SwitchUpdateRule(i, kPREventTypeSwitchClosedDebounced, &switchRule, NULL, 0); - SwitchUpdateRule(i, kPREventTypeSwitchOpenNondebounced, &switchRule, NULL, 0); - SwitchUpdateRule(i, kPREventTypeSwitchClosedNondebounced, &switchRule, NULL, 0); - } -} - int PRDevice::GetEvents(PREvent *events, int maxEvents) { SortReturningData(); diff --git a/src/PRDevice.h b/src/PRDevice.h index 3292be1..cd4e20a 100644 --- a/src/PRDevice.h +++ b/src/PRDevice.h @@ -44,6 +44,7 @@ class PRDevice public: static PRDevice *Create(PRMachineType machineType); ~PRDevice(); + void Reset(bool updateDevice); protected: PRDevice(PRMachineType machineType); @@ -121,8 +122,6 @@ protected: // Local Device State - void Shutdown(); - void Reset(); PRMachineType machineType; PRDriverGlobalConfig driverGlobalConfig; PRDriverGroupConfig driverGroups[maxDriverGroups]; diff --git a/src/pinproc.cpp b/src/pinproc.cpp index 91fc8f5..9d57655 100644 --- a/src/pinproc.cpp +++ b/src/pinproc.cpp @@ -72,6 +72,12 @@ PR_EXPORT void PRDelete(PRHandle handle) delete (PRDevice*)handle; } +/** Resets internally maintained driver and switch rule structures and optionally writes those to the P-ROC device. */ +PR_EXPORT void PRReset(PRHandle handle, bool updateDevice) +{ + return handleAsDevice->Reset(updateDevice); +} + // Events /** Get all of the available events that have been received. */