/************************************************************************** * * Copyright (c) 2025-2025 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 ModePreGenDialysate.c * * @author (last) Vinayakam Mani * @date (last) 17-Apr-2025 * * @author (original) Vinayakam Mani * @date (original) 17-Apr-2025 * ***************************************************************************/ #include "ConcentratePumps.h" #include "DialysatePumps.h" #include "Heaters.h" #include "ModePreGenDialysate.h" #include "ModeFault.h" #include "Messaging.h" #include "OperationModes.h" #include "TaskGeneral.h" #include "Utilities.h" #include "Valves.h" /** * @addtogroup DDPreGenDialysateMode * @{ */ // ********** private definitions ********** /// Interval (ms/task time) at which the pre-treatment state data is published on the CAN bus. #define PRE_GEN_DIALYSATE_DATA_PUB_INTERVAL ( 250 / TASK_GENERAL_INTERVAL ) // ********** private data ********** static DD_PRE_GEN_DAILYSATE_STATE_T preGenDialysateState; ///< Currently active pre-gen dialysate state. static OVERRIDE_U32_T pendingStartDDGenDialRequest; ///< Flag indicating TD has requested DD start the generation dialysate request(Overridable). static OVERRIDE_U32_T pendingStopDDPreGenDialRequest; ///< Flag indicating TD has requested DD stop the pre generation dialysate (Overridable). static U32 preGenDialysatePublishTimerCounter; ///< Pre-Gen Dialysate data broadcast timer counter used to schedule when to transmit data. static OVERRIDE_U32_T preGenDialysateModePublishInterval; ///< Interval (in task intervals) at which to publish pre-gen dialysate mode data to CAN bus. // ********** private function prototypes ********** static void publishPreGenDialysateState( void ); static DD_PRE_GEN_DAILYSATE_STATE_T handlePreGenDialysateFillCompleteCheckState( void ); /*********************************************************************//** * @brief * The initPreGenDialysateMode function initializes the Pre-Generation * Dialysate Mode unit. * @details \b Inputs : none * @details \b Outputs: Pre-Gen Dialysate mode unit initialized * @return none *************************************************************************/ void initPreGenDialysateMode( void ) { preGenDialysateState = DD_PRE_GEN_DIALYSATE_HYD_CHAMBER_FILL_CHECK; pendingStartDDGenDialRequest.data = FALSE; pendingStartDDGenDialRequest.ovData = FALSE; pendingStartDDGenDialRequest.ovInitData = FALSE; pendingStartDDGenDialRequest.override = OVERRIDE_RESET; pendingStopDDPreGenDialRequest.data = FALSE; pendingStopDDPreGenDialRequest.ovData = FALSE; pendingStopDDPreGenDialRequest.ovInitData = FALSE; pendingStopDDPreGenDialRequest.override = OVERRIDE_RESET; preGenDialysateModePublishInterval.data = PRE_GEN_DIALYSATE_DATA_PUB_INTERVAL; preGenDialysateModePublishInterval.ovData = PRE_GEN_DIALYSATE_DATA_PUB_INTERVAL; preGenDialysateModePublishInterval.ovInitData = 0; preGenDialysateModePublishInterval.override = OVERRIDE_RESET; } /*********************************************************************//** * @brief * The transitionToPreGenDialysateMode function prepares for transition * to Pre-Gen Dialysate mode from standby mode. * @details \b Inputs: none * @details \b Outputs: none * @return initial state *************************************************************************/ U32 transitionToPreGenDialysateMode( void ) { initPreGenDialysateMode(); deenergizeActuators( PARK_CONC_PUMPS ); setCurrentSubState( NO_SUB_STATE ); return preGenDialysateState; } /*********************************************************************//** * @brief * The execPreGenDialysateMode function executes the Pre-Gen Dialysate mode * state machine. * @details \b Inputs: preGenDialysateState * @details \b Outputs: Pre-Gen Dialysate mode state machine executed * @details \b Alarm: ALARM_ID_DD_SOFTWARE_FAULT when wrong pre gen dialysate * state invoked * @return current state of Pre-Gen Dialysate Mode *************************************************************************/ U32 execPreGenDialysateMode( void ) { //TODO: To be placed in correct states once states defined. if ( TRUE == getU32OverrideValue( &pendingStartDDGenDialRequest ) ) { pendingStartDDGenDialRequest.data = FALSE; requestNewOperationMode( DD_MODE_GEND ); } //TODO: Transition to post gen dialysate if concentrate priming has been started else if ( TRUE == getU32OverrideValue( &pendingStopDDPreGenDialRequest ) ) { pendingStopDDPreGenDialRequest.data = FALSE; requestNewOperationMode( DD_MODE_STAN ); } else { // execute current pre-gen dialysate state switch ( preGenDialysateState ) { case DD_PRE_GEN_DIALYSATE_HYD_CHAMBER_FILL_CHECK: preGenDialysateState = handlePreGenDialysateFillCompleteCheckState(); break; case DD_PRE_GEN_DIALYSATE_WAIT_FOR_GEND: // TODO : handle wait for Gen dialysate break; default: SET_ALARM_WITH_2_U32_DATA( ALARM_ID_DD_SOFTWARE_FAULT, SW_FAULT_ID_PRE_GEND_MODE_INVALID_EXEC_STATE, preGenDialysateState ) break; } } publishPreGenDialysateState(); return preGenDialysateState; } /*********************************************************************//** * @brief * The getCurrentPreGenDialysateState function returns the current state * of the Pre-Gen Dialysate mode. * @details \b Inputs: preGenDialysateState * @details \b Outputs: none * @return current state of PreGenDialysate mode *************************************************************************/ DD_PRE_GEN_DAILYSATE_STATE_T getCurrentPreGenDialysateState( void ) { return preGenDialysateState; } /*********************************************************************//** * @brief * The requestDDGenDialStart function handles an TD request to start * generation dialysis mode. * @details \b Inputs: Pre generation dailysate state * @details \b Outputs: pendingStartDDGenDialRequest * @return TRUE if request accepted, FALSE if not. *************************************************************************/ BOOL requestDDGenDialStart( void ) { BOOL result = FALSE; if ( DD_PRE_GEN_DIALYSATE_WAIT_FOR_GEND == preGenDialysateState ) { result = TRUE; pendingStartDDGenDialRequest.data = TRUE; } return result; } /*********************************************************************//** * @brief * The requestDDPreGenStop function handles an TD request to stop * pre-generation dialysis mode. * @details \b Inputs: Pre generation dialysate mode / state * @details \b Outputs: pendingStopDDPreGenDialRequest * @return TRUE if request accepted, FALSE if not. *************************************************************************/ BOOL requestDDPreGenStop( void ) { BOOL status = FALSE; if ( DD_MODE_PREG == getCurrentOperationMode() ) { pendingStopDDPreGenDialRequest.data = TRUE; status = TRUE; } return status; } /*********************************************************************//** * @brief * The handlePreGenDialysateFillCompleteCheckState function checks the * hydrochamber fill and transition to next state. * @details \b Inputs: state * @details \b Outputs: none * @return current state of PreGenDialysate mode *************************************************************************/ static DD_PRE_GEN_DAILYSATE_STATE_T handlePreGenDialysateFillCompleteCheckState( void ) { //TODO: define hydroblock fill complete process DD_PRE_GEN_DAILYSATE_STATE_T state = DD_PRE_GEN_DIALYSATE_WAIT_FOR_GEND; return state; } /*********************************************************************//** * @brief * The publishPreGenDialysateState function publishes Pre-Generation dialysate * mode data. * @details \b Inputs: preGenDialysatePublishTimerCounter, preGenDialysateModePublishInterval * @details \b Outputs: preGenDialysatePublishTimerCounter * @details \b Message \Sent: MSG_ID_DD_PRE_GEN_DIALYSATE_STATE_DATA to publish the * pre generate dialysate mode data. * @return none *************************************************************************/ static void publishPreGenDialysateState( void ) { // publish data at periodic interval if ( ++preGenDialysatePublishTimerCounter >= getU32OverrideValue( &preGenDialysateModePublishInterval ) ) { PRE_GEN_DIALYSATE_STATE_DATA_T preGenDialysateData; preGenDialysateData.preGenDialysateExecState = preGenDialysateState; broadcastData( MSG_ID_DD_PRE_GEN_DIALYSATE_STATE_DATA, COMM_BUFFER_OUT_CAN_DD_BROADCAST, (U08*)&preGenDialysateData, sizeof( PRE_GEN_DIALYSATE_STATE_DATA_T ) ); preGenDialysatePublishTimerCounter = 0; } } /************************************************************************* * TEST SUPPORT FUNCTIONS *************************************************************************/ /*********************************************************************//** * @brief * The testDDPreGenDialysateDataPublishIntervalOverride function overrides the * DD pre generate dialysate mode data publish interval. * @details \b Inputs: preGenDialysateModePublishInterval * @details \b Outputs: preGenDialysateModePublishInterval * @param Override message from Dialin which includes the interval * (in ms) to override the DD pre generate dialysate data publish interval to. * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testDDPreGenDialysateDataPublishIntervalOverride( MESSAGE_T *message ) { BOOL result = u32BroadcastIntervalOverride( message, &preGenDialysateModePublishInterval, TASK_GENERAL_INTERVAL ); return result; } /*********************************************************************//** * @brief * The testDDStartGenDialysateOverride function sets the override value * to start the gen dialysate operation mode. * @details Inputs: pendingStartDDGenDialRequest * @details Outputs: pendingStartDDGenDialRequest * @param message Override message from Dialin which includes the flag * to override the operation mode. * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testDDStartGenDialysateOverride( MESSAGE_T *message ) { BOOL result = u32Override( message, &pendingStartDDGenDialRequest, FALSE, TRUE ); return result; } /*********************************************************************//** * @brief * The testDDStopPreGenDialysateOverride function sets the override value * to stop the pre gen dialysate operation mode. * @details Inputs: pendingStopDDPreGenDialRequest * @details Outputs: pendingStopDDPreGenDialRequest * @param message Override message from Dialin which includes the flag * to override the operation mode. * @return TRUE if override successful, FALSE if not *************************************************************************/ BOOL testDDStopPreGenDialysateOverride( MESSAGE_T *message ) { BOOL result = u32Override( message, &pendingStopDDPreGenDialRequest, FALSE, TRUE ); return result; } /**@}*/