Index: firmware/App/Modes/Dialysis.c =================================================================== diff -u -r29150914d74e54bd6476d560a4253b0170c3ff33 -r709def41bcde8e2e59d83c5e9c5a06ab0ee74ec8 --- firmware/App/Modes/Dialysis.c (.../Dialysis.c) (revision 29150914d74e54bd6476d560a4253b0170c3ff33) +++ firmware/App/Modes/Dialysis.c (.../Dialysis.c) (revision 709def41bcde8e2e59d83c5e9c5a06ab0ee74ec8) @@ -26,6 +26,7 @@ #include "ModeTreatment.h" #include "ModeTreatmentParams.h" #include "OperationModes.h" +#include "PresOccl.h" #include "Reservoirs.h" #include "SystemCommMessages.h" #include "TaskGeneral.h" @@ -51,6 +52,20 @@ #define MAX_ACTIVE_LOAD_CELL_CHANGE_G 50.0F ///< Maximum delta between new and previous measured UF volume. +#ifdef DIALYZER_REPRIME_ENABLED +// Dialyzer reprime constants +static const U32 DIALYZER_REPRIME_INTERVAL = ((30 * SEC_PER_MIN * MS_PER_SECOND) / TASK_GENERAL_INTERVAL); +/// Dialyzer reprime interval count. Time between reprime attempts. +#define DIALYZER_REPRIME_REPRIME_DURATION_MS ( 21 * MS_PER_SECOND ) ///< Duration of dialyzer reprime reprime state. +#define DIALYZER_REPRIME_PURGE_PRIOR_1_DURATION_MS ( 10 * MS_PER_SECOND ) ///< Duration of dialyzer reprime purge prior 1 state. +#define DIALYZER_REPRIME_PURGE_PRIOR_2_DURATION_MS ( 6 * MS_PER_SECOND ) ///< Duration of dialyzer reprime purge prior 2 state. +#define DIALYZER_REPRIME_PURGE_LINES_DURATION_MS ( 10 * MS_PER_SECOND ) ///< Duration of dialyzer reprime purge prior 2 state. +#define DIALYZER_REPRIME_BEFORE_TREATEMENT_END_INTERVAL_SEC 300 ///< Time before end of treatment to perform final dialyzer reprime, sec. +#define DP_FAST_PURGE_FLOW_RATE_ML_MIN 500.0F ///< Dialysate pump speed for purging air in dialyzer reprime states, ml/min. +#define DPI_REPRIME_PWM_DC -0.23F ///< Inlet dialysate pump PWM duty cycle (-520 RPM = appx -100 mL/min). +#define DPO_REPRIME_PWM_DC -0.225F ///< Outlet dialysate pump PWM duty cycle (-500 RPM = appx -104 mL/min). +#endif + /// Defined states for the Load Cell cycles. typedef enum Reservoir_Steady_Cycle { @@ -61,9 +76,12 @@ // ********** private data ********** -static DIALYSIS_STATE_T currentDialysisState; ///< Current state of the dialysis sub-mode state machine. -static UF_STATE_T currentUFState; ///< Current state of the ultrafiltration state machine. -static SALINE_BOLUS_STATE_T currentSalineBolusState; ///< Current state of the saline bolus state machine. +static DIALYSIS_STATE_T currentDialysisState; ///< Current state of the dialysis sub-mode state machine. +static UF_STATE_T currentUFState; ///< Current state of the ultrafiltration state machine. +static SALINE_BOLUS_STATE_T currentSalineBolusState; ///< Current state of the saline bolus state machine. +#ifdef DIALYZER_REPRIME_ENABLED +static DIALYZER_REPRIME_STATE_T currentDialyzerReprimeState; ///< Current state of the dialyzer reprime state machine. +#endif static F32 refUFVolume; ///< Current reference volume for ultrafiltration (Where should we be w/r/t ultrafiltration). static F32 measUFVolume; ///< Current total measured volume for ultrafiltration (Where are we w/r/t ultrafiltration). @@ -86,26 +104,45 @@ static U32 salineBolusBroadcastTimerCtr; ///< Saline bolus data broadcast timer counter used to schedule when to transmit data. static BOOL salineBolusStartRequested; ///< Flag indicates a saline bolus start has been requested by user. -static BOOL salineBolusAbortRequested; ///< Flag indicates a saline bolus abort has been requested by user. -static BOOL salineBolusAutoResumeUF = FALSE; ///< Flag indicates UF should be auto-resumed after saline bolus completes. +static BOOL salineBolusAbortRequested; ///< Flag indicates a salien bolus abort has been requested by user. +static BOOL autoResumeUF; ///< Flag indicates UF should be auto-resumed after saline bolus completes. static F32 totalSalineVolumeDelivered_mL; ///< Volume (mL) in total of saline delivered so far (cumulative for all boluses including current one). static F32 bolusSalineVolumeDelivered_mL; ///< Volume (mL) of current bolus delivered so far (calculated from measured blood flow rate). static F32 expectedSalineBolusVolume_mL; ///< Volume (mL) of current bolus delivered so far (calculated from target blood flow rate). static U32 bolusSalineLastVolumeTimeStamp; ///< Time stamp for last saline volume update. +#ifdef DIALYZER_REPRIME_ENABLED +static U32 dialyzerReprimeIntervalTimerCtr; ///< Timer counter to determine when the dialyzer reprime should be done. +static BOOL dialyzerReprimeInProgress; ///< Flag indicates whether the dialyzer reprime has been interrupted. +static BOOL dialyzerReprimeFinalReprimeFinished; ///< Flag indicates whether the final dialyzer reprime has completed. +static U32 dialyzerReprimeStateTimer; ///< General state timer for tracking duration of dialyzer reprime states. +#endif + // ********** private function prototypes ********** static DIALYSIS_STATE_T handleDialysisUltrafiltrationState( void ); static DIALYSIS_STATE_T handleDialysisSalineBolusState( void ); +#ifdef DIALYZER_REPRIME_ENABLED +static DIALYSIS_STATE_T handleDialysisDialyzerReprimeState( void ); +#endif static UF_STATE_T handleUFPausedState( DIALYSIS_STATE_T *dialysisState ); static UF_STATE_T handleUFRunningState( DIALYSIS_STATE_T *dialysisState ); -static SALINE_BOLUS_STATE_T handleSalineBolusIdleState( DIALYSIS_STATE_T *dialysisState ); -static SALINE_BOLUS_STATE_T handleSalineBolusWait4Pumps2Stop( DIALYSIS_STATE_T *dialysisState ); +static SALINE_BOLUS_STATE_T handleSalineBolusIdleState( void ); +static SALINE_BOLUS_STATE_T handleSalineBolusWait4Pumps2Stop( void ); static SALINE_BOLUS_STATE_T handleSalineBolusInProgressState( DIALYSIS_STATE_T *dialysisState ); static SALINE_BOLUS_STATE_T handleSalineBolusMaxDeliveredState( DIALYSIS_STATE_T *dialysisState ); +#ifdef DIALYZER_REPRIME_ENABLED +static DIALYZER_REPRIME_STATE_T handleDialyzerReprimeDialysatePumpsOffState( void ); +static DIALYZER_REPRIME_STATE_T handleDialyzerReprimePurgePrior1State( void ); +static DIALYZER_REPRIME_STATE_T handleDialyzerReprimePurgePrior2State( void ); +static DIALYZER_REPRIME_STATE_T handleDialyzerReprimeRePrimeState( void ); +static DIALYZER_REPRIME_STATE_T handleDialyzerReprimePurgeLinesState( DIALYSIS_STATE_T *dialysisState ); +static void initDialyzerReprime( void ); +#endif + static void checkUFControl( void ); static void updateUFVolumes( void ); @@ -147,7 +184,14 @@ salineBolusBroadcastTimerCtr = 0; totalSalineVolumeDelivered_mL = 0.0; + autoResumeUF = FALSE; +#ifdef DIALYZER_REPRIME_ENABLED + dialyzerReprimeIntervalTimerCtr = 0; + dialyzerReprimeInProgress = FALSE; + dialyzerReprimeFinalReprimeFinished = FALSE; +#endif + for ( i = 0; i < NUM_OF_LOAD_CELLS; i++ ) { lcLastSteadyWeight[RESERVOIR_STEADY_CYCLE_START][i] = LOAD_CELL_ILLEGAL_WEIGHT_VALUE; @@ -185,9 +229,9 @@ { currentSalineBolusState = SALINE_BOLUS_STATE_IDLE; } - if ( TRUE == salineBolusAutoResumeUF ) + if ( TRUE == autoResumeUF ) { - salineBolusAutoResumeUF = FALSE; + autoResumeUF = FALSE; currentUFState = UF_RUNNING_STATE; } } @@ -216,6 +260,8 @@ resetReservoirsVariables(); + signalStartDialysisStartOrResume(); + // Set valves for dialysis setValvePosition( VDI, VALVE_POSITION_B_OPEN ); setValvePosition( VDO, VALVE_POSITION_B_OPEN ); @@ -368,7 +414,6 @@ U32 salineBolusVolume = getTreatmentParameterU32( TREATMENT_PARAM_SALINE_BOLUS_VOLUME ); HD_OP_MODE_T currOpMode = getCurrentOperationMode(); TREATMENT_STATE_T currTreatSubMode = getTreatmentState(); - SALINE_BOLUS_STATE_T currSalineBolusState = getSalineBolusState(); // Must be in treatment mode, dialysis sub-mode, saline bolus in idle state in order to start a saline bolus if ( currOpMode != MODE_TREA ) @@ -383,10 +428,16 @@ { rejReason = REQUEST_REJECT_REASON_SALINE_MAX_VOLUME_REACHED; } - else if ( currSalineBolusState != SALINE_BOLUS_STATE_IDLE ) + else if ( DIALYSIS_SALINE_BOLUS_STATE == currentDialysisState ) { rejReason = REQUEST_REJECT_REASON_SALINE_BOLUS_IN_PROGRESS; } +#ifdef DIALYZER_REPRIME_ENABLED + else if ( DIALYSIS_DIALYZER_REPRIME_STATE == currentDialysisState ) + { + rejReason = REQUEST_REJECT_REASON_DIALYZER_REPRIME_IN_PROGRESS; + } +#endif else { accept = TRUE; @@ -473,8 +524,22 @@ return currentSalineBolusState; } +#ifdef DIALYZER_REPRIME_ENABLED /*********************************************************************//** * @brief + * The getDialyzerRePrimeState function gets the current dialyzer re-prime state. + * @details Inputs: currentDialyzerReprimeState + * @details Outputs: none + * @return currentDialyzerReprimeState + *************************************************************************/ +DIALYZER_REPRIME_STATE_T getDialyzerRePrimeState( void ) +{ + return currentDialyzerReprimeState; +} +#endif + +/*********************************************************************//** + * @brief * The getUltrafiltrationVolumeCollected function gets the current ultrafiltration * volume collected so far for current treatment. * @details Inputs: measUFVolume @@ -638,7 +703,11 @@ case DIALYSIS_SALINE_BOLUS_STATE: currentDialysisState = handleDialysisSalineBolusState(); break; - +#ifdef DIALYZER_REPRIME_ENABLED + case DIALYSIS_DIALYZER_REPRIME_STATE: + currentDialysisState = handleDialysisDialyzerReprimeState(); + break; +#endif default: SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_DIALYSIS_INVALID_STATE, currentDialysisState ) break; @@ -677,6 +746,27 @@ setHeparinCompleted(); } +#ifdef DIALYZER_REPRIME_ENABLED + // Check whether it is time for dialyzer reprime + // Either a fixed interval or some interval before treatment end. + if ( ( ++dialyzerReprimeIntervalTimerCtr >= DIALYZER_REPRIME_INTERVAL) || + ( ( FALSE == dialyzerReprimeFinalReprimeFinished ) && + ( getTreatmentTimeRemainingSecs() <= DIALYZER_REPRIME_BEFORE_TREATEMENT_END_INTERVAL_SEC ) + ) + ) + { + initDialyzerReprime(); + if ( UF_RUNNING_STATE == currentUFState ) + { + autoResumeUF = TRUE; + } + currentUFState = UF_PAUSED_STATE; + result = DIALYSIS_DIALYZER_REPRIME_STATE; + } + else + { +#endif + // Handle current ultrafiltration state switch ( currentUFState ) { @@ -694,6 +784,10 @@ break; } +#ifdef DIALYZER_REPRIME_ENABLED + } +#endif + if ( priorSubState != currentUFState ) { SEND_EVENT_WITH_2_U32_DATA( HD_EVENT_SUB_STATE_CHANGE, priorSubState, currentUFState ); @@ -704,8 +798,8 @@ /*********************************************************************//** * @brief - * The handleDialysisSolutionInfusionState function handles the solution - * infustion state of the Dialysis state machine. + * The handleDialysisSalineBolusState function handles the saline bolus + * state of the Dialysis state machine. * @details Inputs: currentSalineBolusState * @details Outputs: currentSalineBolusState * @return next Dialysis state. @@ -719,11 +813,11 @@ switch ( currentSalineBolusState ) { case SALINE_BOLUS_STATE_IDLE: - currentSalineBolusState = handleSalineBolusIdleState( &result ); + currentSalineBolusState = handleSalineBolusIdleState(); break; case SALINE_BOLUS_STATE_WAIT_FOR_PUMPS_STOP: - currentSalineBolusState = handleSalineBolusWait4Pumps2Stop( &result ); + currentSalineBolusState = handleSalineBolusWait4Pumps2Stop(); break; case SALINE_BOLUS_STATE_IN_PROGRESS: @@ -748,8 +842,59 @@ return result; } +#ifdef DIALYZER_REPRIME_ENABLED /*********************************************************************//** * @brief + * The handleDialysisDialyzerReprimeState function handles the dialyzer reprime + * state of the Dialysis state machine. + * @details Inputs: prior currentDialyzerReprimeState + * @details Outputs: new currentDialyzerReprimeState + * @return next Dialysis state. + *************************************************************************/ +static DIALYSIS_STATE_T handleDialysisDialyzerReprimeState( void ) +{ + DIALYSIS_STATE_T result = DIALYSIS_DIALYZER_REPRIME_STATE; + DIALYZER_REPRIME_STATE_T priorSubState = currentDialyzerReprimeState; + + switch ( currentDialyzerReprimeState ) + { + case DIALYZER_REPRIME_STATE_DIALYSATE_PUMPS_OFF: + currentDialyzerReprimeState = handleDialyzerReprimeDialysatePumpsOffState(); + break; + + case DIALYZER_REPRIME_STATE_PURGE_PRIOR_1: + currentDialyzerReprimeState = handleDialyzerReprimePurgePrior1State(); + break; + + case DIALYZER_REPRIME_STATE_PURGE_PRIOR_2: + currentDialyzerReprimeState = handleDialyzerReprimePurgePrior2State(); + break; + + case DIALYZER_REPRIME_STATE_REPRIME: + currentDialyzerReprimeState = handleDialyzerReprimeRePrimeState(); + break; + + case DIALYZER_REPRIME_STATE_PURGE_LINES: + currentDialyzerReprimeState = handleDialyzerReprimePurgeLinesState( &result ); + break; + + default: + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_DIALYSIS_INVALID_DIALYZER_REPRIME_STATE, currentDialyzerReprimeState ) + currentDialyzerReprimeState = DIALYZER_REPRIME_STATE_DIALYSATE_PUMPS_OFF; + break; + } + + if ( priorSubState != currentDialyzerReprimeState ) + { + SEND_EVENT_WITH_2_U32_DATA( HD_EVENT_SUB_STATE_CHANGE, priorSubState, currentDialyzerReprimeState ); + } + + return result; +} +#endif + +/*********************************************************************//** + * @brief * The handleUFPausedState function handles the Paused state of the * ultrafiltration state machine. * @details Inputs: salineBolusStartRequested @@ -764,7 +909,7 @@ // Handle saline bolus start request from user if ( TRUE == salineBolusStartRequested ) { - salineBolusAutoResumeUF = FALSE; + autoResumeUF = FALSE; // Go to saline bolus state if we can if ( SALINE_BOLUS_STATE_IDLE == currentSalineBolusState ) { @@ -815,7 +960,7 @@ if ( SALINE_BOLUS_STATE_IDLE == currentSalineBolusState ) { // Since we were doing UF prior to saline bolus, we want to auto-resume when done - salineBolusAutoResumeUF = TRUE; + autoResumeUF = TRUE; // Go to UF paused state result = UF_PAUSED_STATE; // Go to saline bolus state @@ -836,10 +981,9 @@ * saline bolus state machine. * @details Inputs: none * @details Outputs: - * @param dialysisState next dialysis state * @return next saline bolus state *************************************************************************/ -static SALINE_BOLUS_STATE_T handleSalineBolusIdleState( DIALYSIS_STATE_T *dialysisState ) +static SALINE_BOLUS_STATE_T handleSalineBolusIdleState( void ) { SALINE_BOLUS_STATE_T result = SALINE_BOLUS_STATE_IDLE; @@ -865,10 +1009,9 @@ * to stop state of the saline bolus state machine. * @details Inputs: none * @details Outputs: - * @param dialysisState next dialysis state * @return next saline bolus state *************************************************************************/ -static SALINE_BOLUS_STATE_T handleSalineBolusWait4Pumps2Stop( DIALYSIS_STATE_T *dialysisState ) +static SALINE_BOLUS_STATE_T handleSalineBolusWait4Pumps2Stop( void ) { SALINE_BOLUS_STATE_T result = SALINE_BOLUS_STATE_WAIT_FOR_PUMPS_STOP; @@ -978,9 +1121,9 @@ if ( FALSE == errorFound ) { // Resume UF if appropriate - if ( TRUE == salineBolusAutoResumeUF ) + if ( TRUE == autoResumeUF ) { - salineBolusAutoResumeUF = FALSE; + autoResumeUF = FALSE; currentUFState = UF_RUNNING_STATE; } // Resume dialysis @@ -1011,8 +1154,185 @@ return result; } +#ifdef DIALYZER_REPRIME_ENABLED /*********************************************************************//** * @brief + * The initDialyzerReprimeState function performs the steps to initiate + * the dialyzer reprime task. + * @details Inputs: + * @details Outputs: currentDialyzerReprimeState + * @return none. + *************************************************************************/ +static void initDialyzerReprime( void ) +{ + // Give commands to stop pumps + setDialInPumpTargetFlowRate( 0, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_CLOSED_LOOP ); + setDialOutPumpTargetRate( 0, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_CLOSED_LOOP ); + currentDialyzerReprimeState = DIALYZER_REPRIME_STATE_DIALYSATE_PUMPS_OFF; +} + +/*********************************************************************//** + * @brief + * The handleDialysatePumpsOffState function handles the dialysate pumps off state of the + * dialyzer reprime state machine. + * @details Inputs: isDialInPumpRunning(), isDialOutPumpRunning(), dialyzerReprimeInProgress flag + * @details Outputs: next dialyzer reprime state, dialyzerReprimeInProgress flag + * @return next dialyzer reprime state + *************************************************************************/ +static DIALYZER_REPRIME_STATE_T handleDialyzerReprimeDialysatePumpsOffState( void ) +{ + DIALYZER_REPRIME_STATE_T result = DIALYZER_REPRIME_STATE_DIALYSATE_PUMPS_OFF; + + if ( ( FALSE == isDialInPumpRunning() ) && ( FALSE == isDialOutPumpRunning() ) ) + { + // Decide which state to transition to + if ( TRUE == dialyzerReprimeInProgress ) + { // the reprime was interrupted, so it is necessary to purge any air that may have been left in the lines + setDialInPumpTargetFlowRate( DP_FAST_PURGE_FLOW_RATE_ML_MIN, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_OPEN_LOOP ); + setValvePosition( VDI, VALVE_POSITION_C_CLOSE ); + setValvePosition( VDO, VALVE_POSITION_C_CLOSE ); + result = DIALYZER_REPRIME_STATE_PURGE_PRIOR_1; + } + else + { + // Set valves to flow through dialyzer + setValvePosition( VDI, VALVE_POSITION_B_OPEN ); + setValvePosition( VDO, VALVE_POSITION_B_OPEN ); + testSetDialInPumpTargetDutyCycle( DPI_REPRIME_PWM_DC ); + testSetDialOutPumpTargetDutyCycle( DPO_REPRIME_PWM_DC ); + dialyzerReprimeInProgress = TRUE; + result = DIALYZER_REPRIME_STATE_REPRIME; + } + dialyzerReprimeStateTimer = getMSTimerCount(); + } + + return result; +} + +/*********************************************************************//** + * @brief + * The handleState function handles the purge prior 1 state of the dialyzer reprime + * state machine. + * @details Inputs: dialyzerReprimeStateTimer + * @details Outputs: dialyzerReprimeStateTimer, next dialyzer reprime state + * @return next dialyzer reprime state + *************************************************************************/ +static DIALYZER_REPRIME_STATE_T handleDialyzerReprimePurgePrior1State( void ) +{ + DIALYZER_REPRIME_STATE_T result = DIALYZER_REPRIME_STATE_PURGE_PRIOR_1; + + if ( ( TRUE == didTimeout( dialyzerReprimeStateTimer, DIALYZER_REPRIME_PURGE_PRIOR_1_DURATION_MS ) ) ) + { + // Stop the dialysate in pump before opening valves + signalDialInPumpHardStop(); + setValvePosition( VDI, VALVE_POSITION_B_OPEN ); + setValvePosition( VDO, VALVE_POSITION_B_OPEN ); + // Start the two dialysate pumps together. + setDialInPumpTargetFlowRate( DP_FAST_PURGE_FLOW_RATE_ML_MIN, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_OPEN_LOOP ); + setDialOutPumpTargetRate( DP_FAST_PURGE_FLOW_RATE_ML_MIN, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_OPEN_LOOP ); + dialyzerReprimeStateTimer = getMSTimerCount(); + result = DIALYZER_REPRIME_STATE_PURGE_PRIOR_2; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The handlePurgePrior2State function handles the purge prior 2 state of the dialyzer reprime + * state machine. + * @details Inputs: dialyzerReprimeStateTimer + * @details Outputs: next dialyzer reprime state + * @return next dialyzer reprime state + *************************************************************************/ +static DIALYZER_REPRIME_STATE_T handleDialyzerReprimePurgePrior2State( void ) +{ + DIALYZER_REPRIME_STATE_T result = DIALYZER_REPRIME_STATE_PURGE_PRIOR_2; + + if ( ( TRUE == didTimeout( dialyzerReprimeStateTimer, DIALYZER_REPRIME_PURGE_PRIOR_2_DURATION_MS ) ) ) + { + // ramp down the pumps to zero before turning them on in reverse + setDialInPumpTargetFlowRate( 0, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_CLOSED_LOOP ); + setDialOutPumpTargetRate( 0, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_CLOSED_LOOP ); + dialyzerReprimeInProgress = FALSE; // This will cause a transition to reprime state after pumps stop + result = DIALYZER_REPRIME_STATE_DIALYSATE_PUMPS_OFF; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The handleState function handles the reprime state of the dialyzer reprime + * state machine. + * @details Inputs: dialyzerReprimeStateTimer + * @details Outputs: dialyzerReprimeStateTimer, next dialyzer reprime state + * @return next dialyzer reprime state + *************************************************************************/ +static DIALYZER_REPRIME_STATE_T handleDialyzerReprimeRePrimeState( void ) +{ + DIALYZER_REPRIME_STATE_T result = DIALYZER_REPRIME_STATE_REPRIME; + + if ( ( TRUE == didTimeout( dialyzerReprimeStateTimer, DIALYZER_REPRIME_REPRIME_DURATION_MS ) ) ) + { + signalDialInPumpHardStop(); + signalDialOutPumpHardStop(); + setDialInPumpTargetFlowRate( DP_FAST_PURGE_FLOW_RATE_ML_MIN, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_OPEN_LOOP ); + setValvePosition( VDI, VALVE_POSITION_C_CLOSE ); + setValvePosition( VDO, VALVE_POSITION_C_CLOSE ); + dialyzerReprimeStateTimer = getMSTimerCount(); + result = DIALYZER_REPRIME_STATE_PURGE_LINES; + } + + return result; +} + +/*********************************************************************//** + * @brief + * The handleState function handles the purge lines state of the dialyzer reprime + * state machine. + * @details Inputs: dialyzerReprimeStateTimer + * @details Outputs: dialyzerReprimeStateTimer, dialyzerReprimeInProgress, + * dialyzerReprimeIntervalTimerCtr, next dialyzer reprime state + * @param dialysis Dialysis state + * @return next dialyzer reprime state + *************************************************************************/ +static DIALYZER_REPRIME_STATE_T handleDialyzerReprimePurgeLinesState( DIALYSIS_STATE_T *dialysisState ) +{ + DIALYZER_REPRIME_STATE_T result = DIALYZER_REPRIME_STATE_PURGE_LINES; + + if ( ( TRUE == didTimeout( dialyzerReprimeStateTimer, DIALYZER_REPRIME_PURGE_LINES_DURATION_MS ) ) ) + { + signalDialInPumpHardStop(); + signalDialOutPumpHardStop(); + // Delay 1 more second before resuming treatment + if ( TRUE == didTimeout( dialyzerReprimeStateTimer, DIALYZER_REPRIME_PURGE_LINES_DURATION_MS + MS_PER_SECOND ) ) + { + setValvePosition( VDI, VALVE_POSITION_B_OPEN ); + setValvePosition( VDO, VALVE_POSITION_B_OPEN ); + // Reset the timer for reprime interval and the reprime in progress flag. + dialyzerReprimeIntervalTimerCtr = 0; + dialyzerReprimeInProgress = FALSE; + if ( getTreatmentTimeRemainingSecs() <= DIALYZER_REPRIME_BEFORE_TREATEMENT_END_INTERVAL_SEC) + { + dialyzerReprimeFinalReprimeFinished = TRUE; + } + // Dialysis back to UF state + *dialysisState = DIALYSIS_UF_STATE; + // This handles resuming UF if it was paused by re-prime + resetSalineBolus(); + // Resume dialysis + transitionToDialysis(); + result = DIALYZER_REPRIME_STATE_DIALYSATE_PUMPS_OFF; + } + } + + return result; +} +#endif + +/*********************************************************************//** + * @brief * The publishSalineBolusData function handles the max saline delivered * state of the saline bolus state machine. This is a terminal state. * @details Inputs: none