Index: firmware/App/Modes/ModeTreatment.c =================================================================== diff -u -r9ded16038a0d6dd844da697aaf03505ec3ed335a -r21c533a2c6f4cfc840a0297536c7f10a5a9fc274 --- firmware/App/Modes/ModeTreatment.c (.../ModeTreatment.c) (revision 9ded16038a0d6dd844da697aaf03505ec3ed335a) +++ firmware/App/Modes/ModeTreatment.c (.../ModeTreatment.c) (revision 21c533a2c6f4cfc840a0297536c7f10a5a9fc274) @@ -18,6 +18,7 @@ #include "AirTrap.h" #include "AlarmLamp.h" #include "BloodFlow.h" +#include "BloodLeak.h" #include "Buttons.h" #include "DGInterface.h" #include "DialInFlow.h" @@ -79,7 +80,8 @@ /// Blood leak zeroing states typedef enum BloodLeakZeroingState { - BLD_ZEROING_FLUSH = 0, ///< Blood leak zeroing flush. + BLD_ZEROING_SET_TO_BYPASS = 0, ///< Blood leak zeroing set to bypass. + BLD_ZEROING_FLUSH, ///< Blood leak zeroing flush. BLD_ZEROING_ZERO, ///< Blood leak zeroing zero. BLD_ZEROING_VERIFY_ZEROING, ///< Blood leak zeroing verify zeroing. BLD_ZEROING_COMPLETE, ///< Blood leak zeroing complete. @@ -90,8 +92,9 @@ typedef struct { F32 DPiToBLDFlushedVolML; ///< Dialysate inlet pump to blood leak flushed volume in milliliters. - BOOL hasBloodLeakZeroingBeenRequested; ///< Flag to indicate blood leak zeroing has been requested. + BOOL hasBloodLeakZeroingBeenCompleted; ///< Flag to indicate blood leak zeroing has been completed successfully. BLOOD_LEAK_ZEROING_STATE_T bloodLeakZeroingState; ///< Blood leak zeroing state. + TREATMENT_STATE_T prevTreatmentState; ///< Previous treatment state prior to transitioning to zeroing state. } BLOOD_LEAK_ZEROING_T ; // ********** private data ********** @@ -152,7 +155,7 @@ static U32 treatmentStartTimeStamp; ///< Treatment start timestampt for logging purpose. static U32 treatmentEndTimeStamp; ///< Treatment end timestampt for logging purpose. -static BLOOD_LEAK_ZEROING_T bloodLeakZeroing; +static BLOOD_LEAK_ZEROING_T bloodLeakZeroing; ///< Blood leak zeroing. // ********** private function prototypes ********** @@ -164,6 +167,7 @@ static TREATMENT_STATE_T handleTreatmentDialysisState( void ); static TREATMENT_STATE_T handleTreatmentStopState( void ); static TREATMENT_STATE_T handleTreatmentRinsebackState( void ); +static TREATMENT_STATE_T handleTreatmentZeroBloodLeakState( void ); static TREATMENT_STATE_T handleTreatmentRecircState( void ); static TREATMENT_STATE_T handleTreatmentEndState( void ); static void resetSignalFlags( void ); @@ -221,6 +225,8 @@ treatmentStartTimeStamp = getRTCTimestamp(); treatmentEndTimeStamp = 0; + memset( &bloodLeakZeroing, 0x0, sizeof( BLOOD_LEAK_ZEROING_T ) ); + // reset dialysate temperature alarm persistences prior to starting a treatment. resetPersistentAlarmTimer( ALARM_ID_HD_DIALYSATE_TEMP_ABOVE_SAFETY_TEMP ); resetPersistentAlarmTimer( ALARM_ID_HD_DIALYSATE_TEMP_ABOVE_TARGET_TEMP ); @@ -664,6 +670,13 @@ resumeBlockedByAlarm = TRUE; } + if ( TRUE == isBloodLeakZeroingNeeded() ) + { + requestBloodLeakZeroing(); + currentTreatmentState = TREATMENT_ZERO_BLOOD_LEAK_STATE; // TODO is this the right thing to change the tx state? anything else? + // TODO when we go to zeroing, shouldn't we pause the treatment time? + } + // Check dialysate temperature during treatment mode (except end state where treatment is over, dialyzer is bypassed and temp is no longer an issue) if ( currentTreatmentState != TREATMENT_END_STATE ) { @@ -693,6 +706,10 @@ currentTreatmentState = handleTreatmentRinsebackState(); break; + case TREATMENT_ZERO_BLOOD_LEAK_STATE: + currentTreatmentState = handleTreatmentZeroBloodLeakState(); + break; + case TREATMENT_RECIRC_STATE: currentTreatmentState = handleTreatmentRecircState(); break; @@ -965,6 +982,48 @@ /*********************************************************************//** * @brief + * The handleTreatmentZeroBloodLeakState function executes the zero blood leak + * state of the state machine. + * @details Inputs: TODO fill up + * @details Outputs: TODO fill up + * @return next treatment mode state + *************************************************************************/ +static TREATMENT_STATE_T handleTreatmentZeroBloodLeakState( void ) +{ + TREATMENT_STATE_T result = TREATMENT_ZERO_BLOOD_LEAK_STATE; + + // If user requests treatment end, end treatment + if ( TRUE == endTreatmentAlarmResponseRequest ) + { + sendLastTreatmentPeriodicData = TRUE; + requestNewOperationMode( MODE_POST ); + } + else + { + // If user requests resumption of treatment, resume + if ( TRUE == resumeTreatmentAlarmResponseRequest ) + { + resumeTreatmentAlarmResponseRequest = FALSE; + //signalRinsebackAlarmResumeUserAction(); // TODO do we need one of this for zeroing? + } + execBloodLeakZeroing(); + } + + // TODO other transitions are needed too? + if ( TRUE == bloodLeakZeroing.hasBloodLeakZeroingBeenCompleted ) + { + result = bloodLeakZeroing.prevTreatmentState; + } + else if ( TRUE == endTreatmentRequest ) + { + requestNewOperationMode( MODE_POST ); + } + + return result; +} + +/*********************************************************************//** + * @brief * The handleTreatmentRecircState function executes the re-circulate state of the * Treatment Mode state machine. * @details Inputs: endTreatmentAlarmResponseRequest, rinsebackToStoppedRequest, @@ -1536,8 +1595,18 @@ BOOL status = FALSE; SELF_TEST_STATUS_T zeroStatus = getBloodLeakSelfTestStatus(); - switch( bloodLeakZeroing.bloodLeakZeroingState ) // TODO we need handlers for this state machine + // TODO investigate whether we need to stop the before going to bypass + + switch( bloodLeakZeroing.bloodLeakZeroingState ) // TODO we need handlers for this state machine? { + case BLD_ZEROING_SET_TO_BYPASS: + // Set Dialysate valves to bypass filter for recirculation + signalDialOutPumpHardStop(); + setValvePosition( VDI, VALVE_POSITION_C_CLOSE ); + setValvePosition( VDO, VALVE_POSITION_C_CLOSE ); + bloodLeakZeroing.bloodLeakZeroingState = BLD_ZEROING_FLUSH; + break; + case BLD_ZEROING_FLUSH: status = hasDPiToBLDVolumeBeenFlushed(); bloodLeakZeroing.bloodLeakZeroingState = ( TRUE == status ? BLD_ZEROING_ZERO : BLD_ZEROING_FLUSH ); @@ -1567,19 +1636,14 @@ break; case BLD_ZEROING_COMPLETE: - // Do nothing done with zeroing + bloodLeakZeroing.hasBloodLeakZeroingBeenCompleted = TRUE; break; default: - // TODO software fault + SET_ALARM_WITH_2_U32_DATA( ALARM_ID_HD_SOFTWARE_FAULT, SW_FAULT_ID_MODE_TREATMENT_INVALID_BLOOD_LEAK_ZEROING_STATE, + bloodLeakZeroing.bloodLeakZeroingState ) break; } - - if ( TRUE == bloodLeakZeroing.hasBloodLeakZeroingBeenRequested ) - { - bloodLeakZeroing.bloodLeakZeroingState = BLD_ZEROING_FLUSH; - bloodLeakZeroing.hasBloodLeakZeroingBeenRequested = FALSE; - } } /*********************************************************************//** @@ -1592,7 +1656,10 @@ *************************************************************************/ void requestBloodLeakZeroing( void ) { - bloodLeakZeroing.hasBloodLeakZeroingBeenRequested = TRUE; + bloodLeakZeroing.DPiToBLDFlushedVolML = 0.0F; + bloodLeakZeroing.bloodLeakZeroingState = BLD_ZEROING_SET_TO_BYPASS; + bloodLeakZeroing.hasBloodLeakZeroingBeenCompleted = FALSE; + bloodLeakZeroing.prevTreatmentState = currentTreatmentState; } /*********************************************************************//** @@ -1763,7 +1830,6 @@ { bloodLeakZeroing.DPiToBLDFlushedVolML += ( measuredDPiMLPM * TASK_GENERAL_INTERVAL ) / ( SEC_PER_MIN * MS_PER_SECOND ); status = ( ( bloodLeakZeroing.DPiToBLDFlushedVolML - DPI_TO_BLD_VOLUME_ML ) < NEARLY_ZERO ? TRUE : FALSE ); - bloodLeakZeroing.DPiToBLDFlushedVolML = 0.0F; } return status;