Index: firmware/App/Controllers/BalancingChamber.c =================================================================== diff -u -rd748813399d38ef5b71d760e327e368cc82d7a38 -re79eb4fbdacbc729b0f611f7d27b03ef004dc3da --- firmware/App/Controllers/BalancingChamber.c (.../BalancingChamber.c) (revision d748813399d38ef5b71d760e327e368cc82d7a38) +++ firmware/App/Controllers/BalancingChamber.c (.../BalancingChamber.c) (revision e79eb4fbdacbc729b0f611f7d27b03ef004dc3da) @@ -73,6 +73,9 @@ static F32 lastTdDialysateFlowrate; ///< Previous TD dialysate flow rate static F32 freshDialPressure; ///< Fresh side dialysate pressure static F32 spentDialPressure; ///< Spent side dialysate pressure +static BOOL isBalChamberSwitchingActive; ///< Flag indicating balancing chamber switching is active or not. +static BOOL isBalChamberSwitchingRequest; ///< Flag indicating if there is a request to either activate or deactivate the balancing chamber switching +static BOOL balChamberSwitchingRequestAction; ///< Flag indicating balancing chamber request action. If TRUE then activate the switching, else stay in IDLE state. //TODO: remove later once level sensor working static U32 bicarbChamberPeriodicFillCounter; ///< Counter for checking the timeout for drybicart chamber fill request. @@ -86,6 +89,8 @@ static BAL_CHAMBER_EXEC_STATE_T handleBalChamberState2FillStart( void ); static BAL_CHAMBER_EXEC_STATE_T handleBalChamberState2ValvesClose( void ); static BAL_CHAMBER_EXEC_STATE_T handleBalChamberState2FillEnd(void); +static BAL_CHAMBER_EXEC_STATE_T handleBalChamberStateIdle( void ); +static BAL_CHAMBER_EXEC_STATE_T processBalChamberSwitchingRequest( void ); static void publishBalChamberData( void ); static U32 getBalChamberDataPublishInterval( void ); @@ -255,6 +260,10 @@ balChamberExecState = handleBalChamberState2FillEnd(); break; + case BAL_CHAMBER_STATE_IDLE: + balChamberExecState = handleBalChamberStateIdle(); + break; + default: SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DD_SOFTWARE_FAULT, SW_FAULT_ID_BAL_CHAMBER_INVALID_EXEC_STATE, balChamberExecState ) balChamberExecState = BAL_CHAMBER_STATE_START; @@ -269,6 +278,42 @@ /*********************************************************************//** * @brief + * The activateBalChamberSwitching function activates the balancing chamber switching. + * The balancing chamber state moves from start to end and keeps switching between + * state1 and state2 until deactivated. + * @details \b Inputs: none + * @details \b Outputs: isBalChamberSwitchingRequest, balChamberSwitchingRequestAction + * @return none. + *************************************************************************/ +void activateBalChamberSwitching( void ) +{ + // Set flag to indicate that a request was made for balancing chamber + isBalChamberSwitchingRequest = TRUE; + + // Set flag to indicate that the request made was to activate the balancing chamber switching + balChamberSwitchingRequestAction = TRUE; +} + +/*********************************************************************//** + * @brief + * The deactivateBalChamberSwitching function deactivates the balancing chamber switching. + * The balancing chamber state remains in the idle state until activated again. + * @details \b Inputs: + * @details \b Outputs: isBalChamberSwitchingRequest, balChamberSwitchingRequestAction + * @return none. + *************************************************************************/ +void deactivateBalChamberSwitching( void ) +{ + // Set flag to indicate that a request was made for balancing chamber + isBalChamberSwitchingRequest = TRUE; + + // Set flag to indicate that the request made was to deactivate the balancing chamber switching + // and stay in the idle state + balChamberSwitchingRequestAction = FALSE; +} + +/*********************************************************************//** + * @brief * The valveControlForBCClosedState function closes the all balancing * chamber valves. * @details \b Inputs: none @@ -713,6 +758,9 @@ } } + // Process Balancing Chamber Switching Request + state = processBalChamberSwitchingRequest(); + return state; } @@ -883,11 +931,73 @@ } } + // Process Balancing Chamber Switching Request + state = processBalChamberSwitchingRequest(); + return state; } /*********************************************************************//** * @brief + * The handleBalChamberStateIdle function check for balancing chamber switching + * activation or deactivation request and then change state + * @details \b Inputs: isBalChamberSwitchingRequest, balChamberSwitchingRequestAction + * @details \b Outputs: isBalChamberSwitchingActive + * @return next balancing chamber state. + *************************************************************************/ +static BAL_CHAMBER_EXEC_STATE_T handleBalChamberStateIdle( void ) +{ + BAL_CHAMBER_EXEC_STATE_T state = processBalChamberSwitchingRequest(); + return state; +} + +/*********************************************************************//** + * @brief + * The processBalChamberSwitchingRequest function processes requests for + * activating or deactivating balancing chamber switching and then change state + * @details \b Inputs: isBalChamberSwitchingRequest, balChamberSwitchingRequestAction + * @details \b Outputs: isBalChamberSwitchingActive + * @return next balancing chamber state. + *************************************************************************/ +static BAL_CHAMBER_EXEC_STATE_T processBalChamberSwitchingRequest( void ) +{ + BAL_CHAMBER_EXEC_STATE_T state = balChamberExecState; + + // Check if there was a request made to the balancing chamber + if ( TRUE == isBalChamberSwitchingRequest ) + { + // Clear the request flag + isBalChamberSwitchingRequest = FALSE; + + // Check if the request made was to activate the switching and it is currently inactive + if ( ( TRUE == balChamberSwitchingRequestAction ) && ( FALSE == isBalChamberSwitchingActive ) ) + { + // Set flag to indicate that balancing chamber switching is active + isBalChamberSwitchingActive = TRUE; + + // Move to the start state + state = BAL_CHAMBER_STATE_START; + } + // Check if the request made was to deactivate the switching and it is currently active + else if ( ( FALSE == balChamberSwitchingRequestAction ) && ( TRUE == isBalChamberSwitchingActive ) ) + { + // Set flag to indicate that balancing chamber switching is inactive + isBalChamberSwitchingActive = FALSE; + + // Move to the idle state + state = BAL_CHAMBER_STATE_IDLE; + } + else + { + // Do nothing and continue to execute the current state as the request made was invalid. + } + } + + return state; +} + +/*********************************************************************//** + * @brief * The getBalChamberDataPublishInterval function gets the balancing chamber * data publish interval. * @details \b Inputs: balChamberDataPublishInterval @@ -930,6 +1040,7 @@ data.currentBalChamberSwitchingCounter = currentBalChamberSwitchingCounter; data.isPressureStabilizedDuringFill = isPressureStabilizedDuringFill; data.balChamberSWOnlyState = balanceChamberSwitchingOnly; + data.isBalChamberSwitchingActive = isBalChamberSwitchingActive; broadcastData( MSG_ID_DD_BAL_CHAMBER_DATA, COMM_BUFFER_OUT_CAN_DD_BROADCAST, (U08*)&data, sizeof( BAL_CHAMBER_DATA_T ) );