Index: firmware/App/Modes/TreatmentRecirc.c =================================================================== diff -u -rfa356a2bce909141f45c6832659fa1ceea5bfbba -r49dba1e95bb3763b4c150e7a80b84a65264a7ca8 --- firmware/App/Modes/TreatmentRecirc.c (.../TreatmentRecirc.c) (revision fa356a2bce909141f45c6832659fa1ceea5bfbba) +++ firmware/App/Modes/TreatmentRecirc.c (.../TreatmentRecirc.c) (revision 49dba1e95bb3763b4c150e7a80b84a65264a7ca8) @@ -23,6 +23,8 @@ #include "DialOutFlow.h" #include "ModeTreatment.h" #include "OperationModes.h" +#include "SystemCommMessages.h" +#include "TaskGeneral.h" #include "TreatmentRecirc.h" #include "Valves.h" @@ -31,15 +33,38 @@ * @{ */ +// ********** 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 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 @@ -51,10 +76,29 @@ 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 @@ -72,8 +116,8 @@ setValvePosition( VBV, VALVE_POSITION_C_CLOSE ); // Reset saline bolus state in case alarm interrupted one resetSalineBolus(); - // Stop air trap control - endAirTrapControl(); + // 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 ); @@ -107,19 +151,84 @@ // 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; } @@ -133,11 +242,168 @@ *************************************************************************/ 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