/************************************************************************** * * Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. * * THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN * WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. * * @file TreatmentRecirc.c * * @author (last) Sean Nash * @date (last) 14-Jan-2021 * * @author (original) Sean * @date (original) 14-Jan-2021 * ***************************************************************************/ #include "AirTrap.h" #include "BloodFlow.h" #include "Buttons.h" #include "DGInterface.h" #include "DialInFlow.h" #include "DialOutFlow.h" #include "ModeTreatment.h" #include "OperationModes.h" #include "SystemCommMessages.h" #include "TaskGeneral.h" #include "TreatmentRecirc.h" #include "Valves.h" /** * @addtogroup TreatmentRecirculate * @{ */ // ********** private definitions ********** /// Alarm if re-circulation is running for this much time. TODO - finalize these times w/ Systems #define RECIRC_TIMEOUT_MS ( ( 15 * 60 * MS_PER_SECOND ) / TASK_GENERAL_INTERVAL ) /// Alarm if re-circulation is stopped for this much time. #define RECIRC_STOP_TIMEOUT_MS ( ( 15 * 60 * MS_PER_SECOND ) / TASK_GENERAL_INTERVAL ) // ********** private data ********** static TREATMENT_RECIRC_STATE_T treatmentRecircState; ///< Current state of the treatment re-circulate sub-mode. static U32 recircTimerCtr; ///< Timer counter (in GP task intervals) that re-circulation is running. static U32 recircStoppedTimerCtr; ///< Timer counter (in GP task intervals) that re-circulation is stopped. static BOOL recircStopRequested; ///< Flag indicates alarm requesting to stop rinseback. static BOOL recircReconnectRequested; ///< Flag indicates user requesting re-circulate stop so they can re-connect. static BOOL recircBackToTreatmenRequested; ///< Flag indicates user requesting to go back to treatment (confirming re-connected). static BOOL recircResumeRequested; ///< Flag indicates user requesting resumption of re-circulating. static BOOL recircEndTreatmentRequested; ///< Flag indicates user requesting end of treatment. // ********** private function prototypes ********** static void resetTreatmentRecircFlags( void ); static TREATMENT_RECIRC_STATE_T handleRecircRecircState( void ); static TREATMENT_RECIRC_STATE_T handleRecircStoppedState( void ); static BOOL handleRecircReconnectUserAction( REQUEST_REJECT_REASON_CODE_T *rejReason ); static BOOL handleRecicConfirmReconnectUserAction( REQUEST_REJECT_REASON_CODE_T *rejReason ); static BOOL handleRecircResumeUserAction( REQUEST_REJECT_REASON_CODE_T *rejReason ); static BOOL handleRecircEndTreatmentUserAction( REQUEST_REJECT_REASON_CODE_T *rejReason ); /*********************************************************************//** * @brief * The initTreatmentRecirc function initializes the Treatment Re-circulate sub-mode * module. * @details Inputs: none * @details Outputs: Treatment Re-circulate sub-mode module initialized. * @return none *************************************************************************/ void initTreatmentRecirc( void ) { treatmentRecircState = TREATMENT_RECIRC_RECIRC_STATE; recircTimerCtr = 0; recircStoppedTimerCtr = 0; resetTreatmentRecircFlags(); } /*********************************************************************//** * @brief * The resetRinsebackFlags function resets the Rinseback request flags. * @details Inputs: none * @details Outputs: Rinseback request flags reset to FALSE. * @return none *************************************************************************/ static void resetTreatmentRecircFlags( void ) { recircStopRequested = FALSE; recircReconnectRequested = FALSE; recircBackToTreatmenRequested = FALSE; recircResumeRequested = FALSE; recircEndTreatmentRequested = FALSE; } /*********************************************************************//** * @brief * The transitionToTreatmentRecirc function prepares for transition to treatment * re-circulate sub-mode. * @details Inputs: none * @details Outputs: none * @return none *************************************************************************/ void transitionToTreatmentRecirc( void ) { initTreatmentRecirc(); // Set valves to safe state setValvePosition( VDI, VALVE_POSITION_C_CLOSE ); setValvePosition( VDO, VALVE_POSITION_C_CLOSE ); setValvePosition( VBA, VALVE_POSITION_C_CLOSE ); setValvePosition( VBV, VALVE_POSITION_C_CLOSE ); // Reset saline bolus state in case alarm interrupted one resetSalineBolus(); // Start air trap control startAirTrapControl(); // Should always have stopped alarm active in treatment stop sub-mode so that user can take action activateAlarmNoData( ALARM_ID_TREATMENT_STOPPED_BY_USER ); // Set user alarm recovery actions allowed in this sub-mode setAlarmUserActionEnabled( ALARM_USER_ACTION_RESUME, FALSE ); setAlarmUserActionEnabled( ALARM_USER_ACTION_RINSEBACK, FALSE ); setAlarmUserActionEnabled( ALARM_USER_ACTION_END_TREATMENT, FALSE ); } /*********************************************************************//** * @brief * The execTreatmentRecirc function executes the Treatment circulate sub-mode * state machine. * @details Inputs: none * @details Outputs: none * @return none *************************************************************************/ void execTreatmentRecirc( void ) { switch ( treatmentRecircState ) { case TREATMENT_RECIRC_RECIRC_STATE: treatmentRecircState = handleRecircRecircState(); break; case TREATMENT_RECIRC_STOPPED_STATE: treatmentRecircState = handleRecircStoppedState(); break; default: // TODO - s/w fault break; } // Re-circulate flags should be handled by now - reset in case not handled by current state resetTreatmentRecircFlags(); } /*********************************************************************//** * @brief * The handleRecircRecircState function handles the re-circulating state * operations. * @details Inputs: none * @details Outputs: none * @return next treatment re-circulation state *************************************************************************/ static TREATMENT_RECIRC_STATE_T handleRecircRecircState( void ) { TREATMENT_RECIRC_STATE_T result = TREATMENT_RECIRC_RECIRC_STATE; // TODO - update timer on this state recircTimerCtr++; // is stop or reconnect requested or max re-circ time exceeded? if ( ( TRUE == recircStopRequested ) || ( TRUE == recircReconnectRequested ) || ( recircTimerCtr > RECIRC_TIMEOUT_MS ) ) { // TODO stop BP, close VBA and VBV result = TREATMENT_RECIRC_STOPPED_STATE; } // is end treatment requested? else if ( TRUE == recircEndTreatmentRequested ) { // TODO stop BP, close VBA and VBV endAirTrapControl(); // TODO - signal end Tx sub-mode } return result; } /*********************************************************************//** * @brief * The handleRecircStoppedState function handles the re-circulation stopped * state operations. * @details Inputs: none * @details Outputs: none * @return next Treatment re-circulation state *************************************************************************/ static TREATMENT_RECIRC_STATE_T handleRecircStoppedState( void ) { TREATMENT_RECIRC_STATE_T result = TREATMENT_RECIRC_STOPPED_STATE; // TODO - update timer on this state recircStoppedTimerCtr++; // is back to treatment requested? if ( TRUE == recircBackToTreatmenRequested ) { // TODO - signal return to treatment stop sub-mode } // is end treatment requested? else if ( TRUE == recircEndTreatmentRequested ) { // TODO - signal end Tx sub-mode } // is re-circ resume requested? else if ( TRUE == recircResumeRequested ) { // TODO start BP, open VBA and VBV startAirTrapControl(); result = TREATMENT_RECIRC_RECIRC_STATE; } else { // is max re-circ stopped time exceeded? if ( recircStoppedTimerCtr > RECIRC_STOP_TIMEOUT_MS ) { // TODO - alarm } } return result; } /*********************************************************************//** * @brief * The signalStopTreatmentRecirc function signals the treatment re-circulate * sub-mode to stop per an active alarm. * @details Inputs: none * @details Outputs: none * @return none *************************************************************************/ void signalStopTreatmentRecirc( void ) { recircStopRequested = TRUE; } /*********************************************************************//** * @brief * The signalTreatmentRecircUserAction function signals a re-circ user action * has been requested. The request is handled and responded to. * @details Inputs: none * @details Outputs: none * @param action User action requested * @return none *************************************************************************/ void signalTreatmentRecircUserAction( REQUESTED_TREATMENT_RECIRC_USER_ACTIONS_T action ) { BOOL accepted = FALSE; REQUEST_REJECT_REASON_CODE_T rejReason = REQUEST_REJECT_REASON_NONE; // Reject user action requests if any alarm is currently active. User must clear alarm first. if ( FALSE == isAnyAlarmActive() ) { switch ( action ) { case REQUESTED_USER_ACTION_TX_RECIRC_RECONNECT: accepted = handleRecircReconnectUserAction( &rejReason ); break; case REQUESTED_USER_ACTION_TX_RECIRC_CONFIRM_RECONNECT: accepted = handleRecicConfirmReconnectUserAction( &rejReason ); break; case REQUESTED_USER_ACTION_TX_RECIRC_RESUME_RC: accepted = handleRecircResumeUserAction( &rejReason ); break; case REQUESTED_USER_ACTION_TX_RECIRC_END_TREATMENT: accepted = handleRecircEndTreatmentUserAction( &rejReason ); break; default: rejReason = REQUEST_REJECT_REASON_INVALID_COMMAND; break; } } else { rejReason = REQUEST_REJECT_REASON_ALARM_IS_ACTIVE; } // Respond to user action request sendRinsebackCmdResponse( accepted, (U32)rejReason ); } /*********************************************************************//** * @brief * The handleRecircReconnectUserAction function handles a re-circulate re-connect * user action request. It is assumed that the calling function will set * the reject reason parameter to None beforehand. * @details Inputs: none * @details Outputs: none * @param rejReason Code indicating reason for rejection * @return TRUE if user action accepted, FALSE if not *************************************************************************/ static BOOL handleRecircReconnectUserAction( REQUEST_REJECT_REASON_CODE_T *rejReason ) { BOOL result = FALSE; if ( TREATMENT_RECIRC_RECIRC_STATE == treatmentRecircState ) { result = TRUE; recircReconnectRequested = TRUE; } else { *rejReason = REQUEST_REJECT_REASON_ACTION_DISABLED_IN_CURRENT_STATE; } return result; } /*********************************************************************//** * @brief * The handleRecicConfirmReconnectUserAction function handles a re-circulate * back to treatment user action request. It is assumed that the calling * function will set the reject reason parameter to None beforehand. * @details Inputs: none * @details Outputs: none * @param rejReason Code indicating reason for rejection * @return TRUE if user action accepted, FALSE if not *************************************************************************/ static BOOL handleRecicConfirmReconnectUserAction( REQUEST_REJECT_REASON_CODE_T *rejReason ) { BOOL result = FALSE; if ( TREATMENT_RECIRC_STOPPED_STATE == treatmentRecircState ) { result = TRUE; recircBackToTreatmenRequested = TRUE; } else { *rejReason = REQUEST_REJECT_REASON_ACTION_DISABLED_IN_CURRENT_STATE; } return result; } /*********************************************************************//** * @brief * The handleRecircResumeUserAction function handles a re-circulate resume * user action request. It is assumed that the calling function will set * the reject reason parameter to None beforehand. * @details Inputs: none * @details Outputs: none * @param rejReason Code indicating reason for rejection * @return TRUE if user action accepted, FALSE if not *************************************************************************/ static BOOL handleRecircResumeUserAction( REQUEST_REJECT_REASON_CODE_T *rejReason ) { BOOL result = FALSE; if ( TREATMENT_RECIRC_STOPPED_STATE == treatmentRecircState ) { result = TRUE; recircResumeRequested = TRUE; } else { *rejReason = REQUEST_REJECT_REASON_ACTION_DISABLED_IN_CURRENT_STATE; } return result; } /*********************************************************************//** * @brief * The handleRecircEndTreatmentUserAction function handles an e reatment * user action request. It is assumed that the calling function will set * the reject reason parameter to None beforehand. * @details Inputs: none * @details Outputs: none * @param rejReason Code indicating reason for rejection * @return TRUE if user action accepted, FALSE if not *************************************************************************/ static BOOL handleRecircEndTreatmentUserAction( REQUEST_REJECT_REASON_CODE_T *rejReason ) { BOOL result = FALSE; if ( ( TREATMENT_RECIRC_RECIRC_STATE == treatmentRecircState ) || ( TREATMENT_RECIRC_STOPPED_STATE == treatmentRecircState ) ) { result = TRUE; recircEndTreatmentRequested = TRUE; } else { *rejReason = REQUEST_REJECT_REASON_ACTION_DISABLED_IN_CURRENT_STATE; } return result; } /*********************************************************************//** * @brief * The getCurrentTreatmentRecircState function returns the current state of the * treatment re-circulate sub-mode. * @details Inputs: treatmentRecircState * @details Outputs: none * @return treatmentRecircState *************************************************************************/ TREATMENT_RECIRC_STATE_T getCurrentTreatmentRecircState( void ) { return treatmentRecircState; } /**@}*/