Index: firmware/App/Controllers/Switches.c =================================================================== diff -u -rc2fe204db1b8926994b5eee78afa1b516c97d02c -rb7c30522ec6cde3ca515f36a053a1fff4e14159c --- firmware/App/Controllers/Switches.c (.../Switches.c) (revision c2fe204db1b8926994b5eee78afa1b516c97d02c) +++ firmware/App/Controllers/Switches.c (.../Switches.c) (revision b7c30522ec6cde3ca515f36a053a1fff4e14159c) @@ -4,6 +4,7 @@ #include "Switches.h" #include "SystemCommMessages.h" #include "TaskGeneral.h" +#include "Timers.h" /** * @addtogroup Switches @@ -12,17 +13,21 @@ // ********** private definitions ********** -#define SWITCHES_DATA_PUB_INTERVAL ( MS_PER_SECOND / TASK_GENERAL_INTERVAL ) ///< Interval (ms/task time) at which the switches data is published on the CAN bus. -#define SWITCHES_FPGA_STATUS_CHECK_INTERVAL ( MS_PER_SECOND / ( 5 * TASK_GENERAL_INTERVAL ) ) ///< Switches FPGA status check interval. +#define SWITCHES_DATA_PUB_INTERVAL ( MS_PER_SECOND / TASK_GENERAL_INTERVAL ) ///< Interval (ms/task time) at which the switches data is published on the CAN bus. +#define SWITCHES_DEBOUNCE_TIME_MS ( MS_PER_SECOND / 4 ) ///< Switches debounce time in milliseconds. +typedef struct +{ + OVERRIDE_U32_T status; ///< Switch status. + U32 debounceStartTime; ///< Debounce start time. +} SWITCH_STATUS_T; + // ********** private data ********** -static U32 switchesDataPublicationCounter; ///< Switches data publication counter. +static U32 switchesDataPublicationCounter; ///< Switches data publication counter. static OVERRIDE_U32_T switchesDataPublishInterval = { SWITCHES_DATA_PUB_INTERVAL, - SWITCHES_DATA_PUB_INTERVAL, - 0, 0 }; ///< Interval (in ms) at which to publish switches data to CAN bus. -static OVERRIDE_U32_T switchStatus[ NUM_OF_DOORS_AND_CAPS ]; ///< Switches status array. -static U32 switchesFPGAStatusCheckCounter; ///< Switches FPGA status check counter. + SWITCHES_DATA_PUB_INTERVAL, 0, 0 }; ///< Interval (in ms) at which to publish switches data to CAN bus. +static SWITCH_STATUS_T switchesStatus[ NUM_OF_DOORS_AND_CAPS ]; ///< Switches status array. // ********** private function prototypes ********** @@ -41,12 +46,15 @@ U08 i; switchesDataPublicationCounter = 0; - switchesFPGAStatusCheckCounter = 0; // Initialize all the switches for ( i = 0; i < NUM_OF_DOORS_AND_CAPS; i++ ) { - switchStatus[ i ].data = (U32)STATE_CLOSED; + switchesStatus[ i ].status.data = (U32)STATE_CLOSED; + switchesStatus[ i ].status.ovData = (U32)STATE_CLOSED; + switchesStatus[ i ].status.ovInitData = (U32)STATE_CLOSED; + switchesStatus[ i ].status.override = OVERRIDE_RESET; + switchesStatus[ i ].debounceStartTime = 0; } } @@ -59,13 +67,51 @@ *************************************************************************/ void execSwitches( void ) { - if ( ++switchesFPGAStatusCheckCounter > SWITCHES_FPGA_STATUS_CHECK_INTERVAL ) + U08 i; + + U08 currentSwitchStatus = 0; + + for ( i = 0; i < NUM_OF_DOORS_AND_CAPS; i++ ) { - switchStatus[ CONCENTRATE_CAP ].data = (U32)( getFPGAGFluidDoorStatus() != 0 ? STATE_CLOSED : STATE_OPEN ); - switchStatus[ DIALYSATE_CAP ].data = (U32)( getFPGADialysateCapStatus() != 0 ? STATE_CLOSED : STATE_OPEN ); - switchStatus[ FLUID_DOOR ].data = (U32)( getFPGAConcentrateCapStatus() != 0 ? STATE_CLOSED : STATE_OPEN ); + // Get the current switch status + switch ( i ) + { + case CONCENTRATE_CAP: + currentSwitchStatus = getFPGAConcentrateCapStatus(); + break; - switchesFPGAStatusCheckCounter = 0; + case DIALYSATE_CAP: + currentSwitchStatus = getFPGADialysateCapStatus(); + break; + + case FLUID_DOOR: + currentSwitchStatus = getFPGAGFluidDoorStatus(); + break; + + default: + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DG_SOFTWARE_FAULT, SW_FAULT_ID_DG_INVALID_SWITCH_ID, i ) + break; + } + + // Check if the current switch status is not the same as the recorded data + if ( currentSwitchStatus != switchesStatus[ i ].status.data ) + { + // If the debounce time is 0, start the timer + if ( 0 == switchesStatus[ i ].debounceStartTime ) + { + switchesStatus[ i ].debounceStartTime = getMSTimerCount(); + } + // If the debounce time has been elapsed, update the switch status to the new status + else if ( TRUE == didTimeout( switchesStatus[ i ].debounceStartTime, SWITCHES_DEBOUNCE_TIME_MS ) ) + { + switchesStatus[ i ].debounceStartTime = 0; + switchesStatus[ i ].status.data = currentSwitchStatus; + } + } + else + { + switchesStatus[ i ].debounceStartTime = 0; + } } publishSwitchesData(); @@ -85,13 +131,13 @@ if ( switchId < NUM_OF_DOORS_AND_CAPS ) { - if ( OVERRIDE_KEY == switchStatus[ switchId ].override ) + if ( OVERRIDE_KEY == switchesStatus[ switchId ].status.override ) { - status = switchStatus[ switchId ].ovData; + status = switchesStatus[ switchId ].status.ovData; } else { - status = switchStatus[ switchId ].data; + status = switchesStatus[ switchId ].status.data; } } else @@ -220,15 +266,14 @@ if ( isTestingActivated() ) { result = TRUE; - switchStatus[ switchId ].ovData = status; - switchStatus[ switchId ].override = OVERRIDE_KEY; + switchesStatus[ switchId ].status.ovData = status; + switchesStatus[ switchId ].status.override = OVERRIDE_KEY; } } return result; } - /*********************************************************************//** * @brief * The testResetSwitchesStatusOverride function resets the override @@ -247,8 +292,8 @@ if ( isTestingActivated() ) { result = TRUE; - switchStatus[ switchId ].override = OVERRIDE_RESET; - switchStatus[ switchId ].ovData = switchStatus[ switchId ].ovInitData; + switchesStatus[ switchId ].status.override = OVERRIDE_RESET; + switchesStatus[ switchId ].status.ovData = switchesStatus[ switchId ].status.ovInitData; } }