/************************************************************************** * * Copyright (c) 2024-2024 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 AirPump.c * * @author (last) Sean * @date (last) 17-Sep-2024 * * @author (original) Sean * @date (original) 17-Sep-2024 * ***************************************************************************/ #include "AirPump.h" #include "AlarmMgmtTD.h" #include "Messaging.h" #include "OperationModes.h" #include "PersistentAlarm.h" #include "TaskGeneral.h" #include "Timers.h" /** * @addtogroup AirPump * @{ */ // ********** private definitions ********** #define AIR_PUMP_DATA_PUB_INTERVAL ( MS_PER_SECOND / TASK_GENERAL_INTERVAL ) ///< Air pump data publish interval. #define DATA_PUBLISH_COUNTER_START_COUNT 13 ///< Air pump data publish start counter. // ********** private data ********** static AIR_PUMP_STATE_T currentAirPumpState; ///< Current air pump control state. static U32 airPumpDataPublicationTimerCounter; ///< Air pump data broadcast timer counter. static OVERRIDE_U32_T airPumpDataPublishInterval; ///< Air pump data broadcast interval (in ms). // ********** private function prototypes ********** static AIR_PUMP_STATE_T handleAirPumpStartState( void ); static AIR_PUMP_STATE_T handleAirPumpOffState( void ); static AIR_PUMP_STATE_T handleAirPumpOnState ( void ); static void publishAirPumpData( void ); /*********************************************************************//** * @brief * The initAirPump function initializes the air pump driver. * @details \b Inputs: none * @details \b Outputs: Air pump driver unit initialized * @return none *************************************************************************/ void initAirPump(void) { // Initialize driver initGasLiqXferPumpDriver(); // Initialize controller variables airPumpDataPublicationTimerCounter = DATA_PUBLISH_COUNTER_START_COUNT; currentAirPumpState = AIR_PUMP_STATE_INIT; airPumpDataPublishInterval.data = AIR_PUMP_DATA_PUB_INTERVAL; airPumpDataPublishInterval.ovData = AIR_PUMP_DATA_PUB_INTERVAL; airPumpDataPublishInterval.ovInitData = 0; airPumpDataPublishInterval.override = OVERRIDE_RESET; } /*********************************************************************//** * @brief * The setAirPumpState function sets the current air pump state machine state. * @details \b Alarm: ALARM_ID_TD_SOFTWARE_FAULT if invalid state given. * @details \b Inputs: currentAirPumpState * @details \b Outputs: currentAirPumpState * @param state Air pump state to set * @return none *************************************************************************/ void setAirPumpState( AIR_PUMP_STATE_T state ) { if ( state < NUM_OF_AIR_PUMP_STATES ) { currentAirPumpState = state; } else { SET_ALARM_WITH_2_U32_DATA( ALARM_ID_TD_SOFTWARE_FAULT, SW_FAULT_ID_TD_AIR_PUMP_INVALID_STATE1, (U32)state ) } } /*********************************************************************//** * @brief * The getAirPumpState function returns the current air pump state machine. * @details \b Inputs: currentAirPumpState * @details \b Outputs: currentAirPumpState * @return current state of the air pump state machine. *************************************************************************/ AIR_PUMP_STATE_T getAirPumpState( void ) { return currentAirPumpState; } /*********************************************************************//** * @brief * The execAirPumpController function executes the air pump state machine. * @details \b Alarm: ALARM_ID_TD_SOFTWARE_FAULT if the current state is invalid. * @details \b Inputs: currentAirPumpState * @details \b Outputs: currentAirPumpState * @return none *************************************************************************/ void execAirPumpController( void ) { switch( currentAirPumpState ) { case AIR_PUMP_STATE_INIT: currentAirPumpState = handleAirPumpStartState(); break; case AIR_PUMP_STATE_OFF: currentAirPumpState = handleAirPumpOffState(); break; case AIR_PUMP_STATE_ON: currentAirPumpState = handleAirPumpOnState(); break; default: SET_ALARM_WITH_2_U32_DATA( ALARM_ID_TD_SOFTWARE_FAULT, SW_FAULT_ID_TD_AIR_PUMP_INVALID_STATE2, (U32)currentAirPumpState ) break; } publishAirPumpData(); } /*********************************************************************//** * @brief * The handleAirPumpStartState function starts the air pump state machine. * @details \b Inputs: none * @details \b Outputs: none * @return next state of the air pump state machine *************************************************************************/ static AIR_PUMP_STATE_T handleAirPumpStartState( void ) { AIR_PUMP_STATE_T state = AIR_PUMP_STATE_OFF; return state; } /*********************************************************************//** * @brief * The handleAirPumpOffState function stops the air pump * @details \b Inputs: none * @details \b Outputs: Air pump motor turned off * @return next state of the air pump state machine *************************************************************************/ static AIR_PUMP_STATE_T handleAirPumpOffState( void ) { AIR_PUMP_STATE_T state = AIR_PUMP_STATE_OFF; setAirPumpMotorState( AIR_PUMP_MOTOR_OFF ); return state; } /*********************************************************************//** * @brief * The handleAirPumpOnState function starts the air pump * @details \b Inputs: none * @details \b Outputs: Air pump motor turned on. * @return next state of the air pump state machine *************************************************************************/ static AIR_PUMP_STATE_T handleAirPumpOnState( void ) { AIR_PUMP_STATE_T state = AIR_PUMP_STATE_ON; setAirPumpMotorState( AIR_PUMP_MOTOR_ON ); return state; } /*********************************************************************//** * @brief * The publishAirPumpData function constructs and sends the air pump data * broadcast message. * @details \b Message \b Sent: MSG_ID_TD_AIR_PUMP_DATA * @details \b Inputs: airPumpDataPublicationTimerCounter, currentAirPumpState * @details \b Outputs: airPumpDataPublicationTimerCounter * @return none *************************************************************************/ static void publishAirPumpData( void ) { if (++airPumpDataPublicationTimerCounter >= getU32OverrideValue( &airPumpDataPublishInterval ) ) { AIR_PUMP_PAYLOAD_T data; data.airPumpStateStatus = getAirPumpState(); broadcastData( MSG_ID_TD_AIR_PUMP_DATA, COMM_BUFFER_OUT_CAN_TD_BROADCAST, (U08*)&data, sizeof( AIR_PUMP_PAYLOAD_T ) ); airPumpDataPublicationTimerCounter = 0; } } /************************************************************************* * TEST SUPPORT FUNCTIONS *************************************************************************/ /*********************************************************************//** * @brief * The testAirPumpDataPublishIntervalOverride function overrides the interval * at which the TD air pump data is published. * @details \b Inputs: none * @details \b Outputs: airPumpDataPublishInterval * @param message Override message from Dialin which includes the interval * (in ms) to override the air pump broadcast interval to. * @return TRUE if override request is successful, FALSE if not *************************************************************************/ BOOL testAirPumpDataPublishIntervalOverride( MESSAGE_T *message ) { BOOL result = u32BroadcastIntervalOverride( message, &airPumpDataPublishInterval, TASK_GENERAL_INTERVAL ); return result; } /**@}*/