/************************************************************************** * * Copyright (c) 2026 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 BPModule.c * * @author (last) Varshini Nagabooshanam * @date (last) 18-May-2026 * * @author (original) Varshini Nagabooshanam * @date (original) 18-May-2026 * ***************************************************************************/ #include "AlarmDefs.h" #include "AlarmMgmtTD.h" #include "BPDriver.h" #include "BPModule.h" #include "FpgaTD.h" /** * @addtogroup BPModule * @{ */ // ********** private definitions ********** #define BP_SYSTOLIC_LOW_LIMIT 90 ///< Low systolic blood pressure threshold in mmHg #define BP_SYSTOLIC_HIGH_LIMIT 180 ///< High systolic blood pressure threshold in mmHG #define BP_HEART_RATE_LOW_LIMIT 40 ///< Low heart rate threshold in BPM #define BP_HEART_RATE_HIGH_LIMIT 180 ///< High heart rate threshold in BPM #define BP_LOW_SYSTOLIC_MAX_COUNT 3 ///< Maximum consecutive low systolic pressure events. /// Blood pressure module states. typedef enum { BP_MODULE_IDLE_STATE = 0, ///< Blood Pressure idle state. BP_MODULE_MEASURE_STATE, ///< Blood pressure measurement state BP_MODULE_CHECK_STATE, ///< Blood pressure validation state NUM_OF_BP_MODULE_STATES ///< Number of blood pressure module states. } BP_MODULE_STATE_T; // ********** private data ********** static BP_MODULE_STATE_T bpModuleState = BP_MODULE_IDLE_STATE; ///< Current blood pressure module state static BP_RESULTS_T bpModuleResults; ///< Latest blood pressure measurement results static U08 lowSystolicCount; ///< Consecutive low systolic pressure counter static BOOL bpSystolicLowDetected; ///< Low systolic blood pressure detection static BOOL bpSystolicLowTooManyDetected; ///< Consecutive low systolic detection static BOOL bpSystolicHighDetected; ///< High systolic blood pressure detection static BOOL bpHeartRateLowDetected; ///< Low heart rate detection static BOOL bpHeartRateHighDetected; ///< High heart rate detection static BOOL bpModuleErrorDetected; ///< Blood pressure module error detection flag. // ********** private function prototypes ********** static void resetBPAlarmFlags( void ); static void checkBloodPressureReading( void ); static void handleBPModuleIdleState( void ); static void handleBPModuleMeasureState( void ); static void handleBPModuleCheckState( void ); /*********************************************************************//** * @brief * The initBPModule function initializes the blood pressure module. * @details \b Inputs: none * @details \b Outputs: bpModuleState * @return none ***************************************************************************/ void initBPModule( void ) { initBPDriver(); bpModuleState = BP_MODULE_IDLE_STATE; resetBPAlarmFlags(); } /*********************************************************************//** * @brief * The resetBPAlarmFlags function clears all blood pressure alarm * detection flags. * @details \b Inputs: none * @details \b Outputs: BP alarm detection flags * @return none ***************************************************************************/ static void resetBPAlarmFlags( void ) { bpSystolicLowDetected = FALSE; bpSystolicLowTooManyDetected = FALSE; bpSystolicHighDetected = FALSE; bpHeartRateLowDetected = FALSE; bpHeartRateHighDetected = FALSE; bpModuleErrorDetected = FALSE; } /*********************************************************************//** * @brief * The execBPModule function executes the blood pressure module state * machine. * @details \b Inputs: bpModuleState * @details \b Outputs: bpModuleState, bpModuleResults * @return none ***************************************************************************/ void execBPModule( void ) { switch ( bpModuleState ) { case BP_MODULE_IDLE_STATE: { handleBPModuleIdleState(); break; } case BP_MODULE_MEASURE_STATE: { handleBPModuleMeasureState(); break; } case BP_MODULE_CHECK_STATE: { handleBPModuleCheckState(); break; } default: { activateAlarm( ALARM_ID_TD_SOFTWARE_FAULT ); bpModuleState = BP_MODULE_IDLE_STATE; break; } } } /*********************************************************************//** * @brief * The initiateAdultBPReading function initiates an adult blood pressure * measurement. * @details \b Inputs: none * @details \b Outputs: bpModuleState * @return none ***************************************************************************/ void initiateAdultBPReading( void ) { startAdultBPMeasurement(); bpModuleState = BP_MODULE_MEASURE_STATE; } /*********************************************************************//** * @brief * The initiatePedsBPReading function initiates a pediatric blood pressure * measurement. * @details \b Inputs: none * @details \b Outputs: bpModuleState * @return none ***************************************************************************/ void initiatePedsBPReading( void ) { startPedsBPMeasurement(); bpModuleState = BP_MODULE_MEASURE_STATE; } /*********************************************************************//** * @brief * The abortBPReading function aborts the active blood pressure * measurement. * @details \b Inputs: none * @details \b Outputs: bpModuleState * @return none ***************************************************************************/ void abortBPReading( void ) { abortBPMeasurement(); bpModuleState = BP_MODULE_IDLE_STATE; } /*********************************************************************//** * @brief * The isBPSystolicLowDetected function returns the systolic low * detection state. * @details \b Inputs: bpSystolicLowDetected * @details \b Outputs: none * @return TRUE if detected, FALSE otherwise. ***************************************************************************/ BOOL isBPSystolicLowDetected( void ) { return bpSystolicLowDetected; } /*********************************************************************//** * @brief * The isBPSystolicLowTooManyDetected function returns the consecutive * systolic low detection state. * @details \b Inputs: bpSystolicLowTooManyDetected * @details \b Outputs: none * @return TRUE if detected, FALSE otherwise. ***************************************************************************/ BOOL isBPSystolicLowTooManyDetected( void ) { return bpSystolicLowTooManyDetected; } /*********************************************************************//** * @brief * The isBPSystolicHighDetected function returns the systolic high * detection state. * @details \b Inputs: bpSystolicHighDetected * @details \b Outputs: none * @return TRUE if detected, FALSE otherwise. ***************************************************************************/ BOOL isBPSystolicHighDetected( void ) { return bpSystolicHighDetected; } /*********************************************************************//** * @brief * The isBPHeartRateLowDetected function returns the heart rate low * detection state. * @details \b Inputs: bpHeartRateLowDetected * @details \b Outputs: none * @return TRUE if detected, FALSE otherwise. ***************************************************************************/ BOOL isBPHeartRateLowDetected( void ) { return bpHeartRateLowDetected; } /*********************************************************************//** * @brief * The isBPHeartRateHighDetected function returns the heart rate high * detection state. * @details \b Inputs: bpHeartRateHighDetected * @details \b Outputs: none * @return TRUE if detected, FALSE otherwise. ***************************************************************************/ BOOL isBPHeartRateHighDetected( void ) { return bpHeartRateHighDetected; } /*********************************************************************//** * @brief * The hasBPModuleError function returns the blood pressure module * error detection state. * @details \b Inputs: bpModuleErrorDetected * @details \b Outputs: none * @return TRUE if error detected, FALSE otherwise. ***************************************************************************/ BOOL hasBPModuleError( void ) { return bpModuleErrorDetected; } /*********************************************************************//** * @brief * The checkBloodPressureReading function validates blood pressure * measurement results against configured thresholds and activates * corresponding blood pressure alarms. * @details \b Inputs: bpModuleResults * @details \b Outputs: bpSystolicLowDetected, bpSystolicLowTooManyDetected * bpSystolicHighDetected, bpHeartRateLowDetected, bpHeartRateHighDetected * @return none ***************************************************************************/ static void checkBloodPressureReading( void ) { resetBPAlarmFlags(); if ( bpModuleResults.systolic <= BP_SYSTOLIC_LOW_LIMIT ) { bpSystolicLowDetected = TRUE; lowSystolicCount++; if ( lowSystolicCount >= BP_LOW_SYSTOLIC_MAX_COUNT ) { bpSystolicLowTooManyDetected = TRUE; activateAlarm( ALARM_ID_TD_BP_SYSTOLIC_LOW_TOO_MANY ); } else { activateAlarm( ALARM_ID_TD_BP_SYSTOLIC_LOW ); } } else { lowSystolicCount = 0; } if ( bpModuleResults.systolic >= BP_SYSTOLIC_HIGH_LIMIT ) { bpSystolicHighDetected = TRUE; activateAlarm( ALARM_ID_TD_BP_SYSTOLIC_HIGH ); } if ( bpModuleResults.heartRate <= BP_HEART_RATE_LOW_LIMIT ) { bpHeartRateLowDetected = TRUE; activateAlarm( ALARM_ID_TD_BP_HEART_RATE_LOW ); } if ( bpModuleResults.heartRate >= BP_HEART_RATE_HIGH_LIMIT ) { bpHeartRateHighDetected = TRUE; activateAlarm( ALARM_ID_TD_BP_HEART_RATE_HIGH ); } } /*********************************************************************//** * @brief * The handleBPModuleIdleState function executes the BP module idle * state handling. * @details \b Inputs: none * @details \b Outputs: none * @return none ***************************************************************************/ static void handleBPModuleIdleState( void ) { // TODO } /*********************************************************************//** * @brief * The handleBPModuleMeasureState function executes the BP module * measurement state handling. * @details \b Inputs: BP driver state/results * @details \b Outputs: bpModuleState * @return none ***************************************************************************/ static void handleBPModuleMeasureState( void ) { execBPDriver(); if ( TRUE == isBPMeasurementReady() ) { getBPResults( &bpModuleResults ); bpModuleState = BP_MODULE_CHECK_STATE; } else if ( TRUE == hasBPDriverError() ) { bpModuleErrorDetected = TRUE; bpModuleState = BP_MODULE_IDLE_STATE; } } /*********************************************************************//** * @brief * The handleBPModuleCheckState function executes the BP module * validation state handling. * @details \b Inputs: bpModuleResults * @details \b Outputs: BP alarm detection flags * @return none ***************************************************************************/ static void handleBPModuleCheckState( void ) { checkBloodPressureReading(); bpModuleState = BP_MODULE_IDLE_STATE; } /**@}*/