Index: firmware/App/Controllers/BloodLeak.c =================================================================== diff -u -r55652a393b0aa2f9d9a45994f4f15628bdb2b8d1 -r33eb95dbdfee4c63f6f88a8972a5e08d2ce30bad --- firmware/App/Controllers/BloodLeak.c (.../BloodLeak.c) (revision 55652a393b0aa2f9d9a45994f4f15628bdb2b8d1) +++ firmware/App/Controllers/BloodLeak.c (.../BloodLeak.c) (revision 33eb95dbdfee4c63f6f88a8972a5e08d2ce30bad) @@ -14,6 +14,7 @@ * @date (original) 18-Mar-2021 * ***************************************************************************/ +#include // For sprintf #include "AlarmMgmt.h" #include "BloodLeak.h" @@ -37,9 +38,11 @@ #define BLOOD_LEAK_START_COMM_CTRL_U_ASCII 21 ///< Blood leak start communication command, ^U (Ctrl-U) in ascii. #define BLOOD_LEAK_UART_COMM_ACTIVE_LOW 0 ///< Blood leak UART communication active low command. #define BLOOD_LEAK_UART_COMM_ACTIVE_HIGH 1 ///< Blood leak UART communication active high command. -#define BLOOD_LEAK_STARTUP_SEQUENCE_LENGTH 5 ///< Blood leak start up sequence array length. +#define BLOOD_LEAK_STARTUP_SEQ_LENGTH 5 ///< Blood leak start up sequence array length. -#define BLOOD_LEAK_SET_POINT_START_CHAR 'S' +#define BLOOD_LEAK_SET_POINT_MAX_CHAR_LENGTH 4 +#define BLOOD_LEAK_SET_POINT_START_CHAR_ASCII 83 +#define BLOOD_LEAK_SET_POINT_START_CHAR_INDEX 0 #define BLOOD_LEAK_CARRIAGE_RETURN_ASCII 13 #define BLOOD_LEAK_SET_POINT_SEQ_MAX_LENGTH 7 @@ -75,18 +78,18 @@ static U32 bloodLeakUARTCmdIndex; ///< Blood leak UART command index. -static U32 bloodLeakSetPointSequenceActualLength = 0; ///< Blood leak set point sequence actual length. +static U32 bloodLeakSetPointSeqLength = 0; ///< Blood leak set point sequence actual length. static U08 bloodLeakSetPointSequence[ BLOOD_LEAK_SET_POINT_SEQ_MAX_LENGTH ]; ///< Blood leak set point sequence array. /// Blood leak start up sequence array. -static const U08 BLOOD_LEAK_START_UP_SEQUENCE[ BLOOD_LEAK_STARTUP_SEQUENCE_LENGTH ] = { BLOOD_LEAK_RESET_TX_FIFO, - BLOOD_LEAK_UART_COMM_ACTIVE_LOW, - BLOOD_LEAK_START_COMM_CTRL_U_ASCII, - BLOOD_LEAK_UART_COMM_ACTIVE_HIGH, - BLOOD_LEAK_UART_COMM_ACTIVE_LOW }; -static const U32 REMOVE_LATER_SET_POINT = 20; // TODO remove this later -static U32 test = 23; +static const U08 BLOOD_LEAK_START_UP_SEQUENCE[ BLOOD_LEAK_STARTUP_SEQ_LENGTH ] = { BLOOD_LEAK_RESET_TX_FIFO, + BLOOD_LEAK_UART_COMM_ACTIVE_LOW, + BLOOD_LEAK_START_COMM_CTRL_U_ASCII, + BLOOD_LEAK_UART_COMM_ACTIVE_HIGH, + BLOOD_LEAK_UART_COMM_ACTIVE_LOW }; +static const U32 REMOVE_LATER_SET_POINT = 38; // TODO remove this later +static U32 test = 38; // ********** private function prototypes ********** @@ -99,6 +102,7 @@ static BLOOD_LEAK_STATES_T handleBloodLeakSelfTestState( void ); static BLOOD_LEAK_STATES_T handleBloodLeakNormalState( void ); +static void prepareSetPointSeq( void ); static void publishBloodLeakData( void ); /*********************************************************************//** @@ -122,9 +126,10 @@ bloodLeakZeroStartTime = 0; bloodLeakSelfTestStartTime = 0; bloodLeakUARTCmdIndex = 0; + bloodLeakSetPointSeqLength = 0; // Set the blood leak set pint sequence to 0 to be initialized - memset( bloodLeakSetPointSequence, 0x0, BLOOD_LEAK_SET_POINT_SEQ_MAX_LENGTH); + memset( bloodLeakSetPointSequence, 0x0, BLOOD_LEAK_SET_POINT_SEQ_MAX_LENGTH ); } /*********************************************************************//** @@ -213,10 +218,10 @@ } else { - setFPGABloodLeakUARTControl( command ); + setFPGABloodLeakUARTControl( (U08)command ); } - if ( bloodLeakUARTCmdIndex > BLOOD_LEAK_STARTUP_SEQUENCE_LENGTH - 1 ) + if ( bloodLeakUARTCmdIndex > BLOOD_LEAK_STARTUP_SEQ_LENGTH - 1 ) { bloodLeakUARTCmdIndex = 0; state = BLOOD_LEAK_CHECK_SET_POINT_STATE; @@ -234,7 +239,7 @@ * The handleBloodLeakCheckSetPointState function handles the check set point * state to ensure the set point is set correctly. * @details Inputs: none - * @details Outputs: none + * @details Outputs: bloodLeakUARTCmdIndex * @return next state *************************************************************************/ static BLOOD_LEAK_STATES_T handleBloodLeakCheckSetPointState( void ) @@ -243,8 +248,11 @@ U32 bloodLeakSetPoint = (U32)getFPGABloodLeakDetectSetPoint(); - if ( REMOVE_LATER_SET_POINT == bloodLeakSetPoint ) + if ( REMOVE_LATER_SET_POINT != bloodLeakSetPoint ) { + prepareSetPointSeq(); + + bloodLeakUARTCmdIndex = 0; state = BLOOD_LEAK_SET_SET_POINT_STATE; } else @@ -258,17 +266,40 @@ /*********************************************************************//** * @brief * The handleBloodLeakSetSetPointState function handles the set set point state. - * @details Inputs: none - * @details Outputs: TODO fill up + * @details Inputs: bloodLeakUARTCmdIndex + * @details Outputs: bloodLeakUARTCmdIndex, bloodLeakSetPointSeqLength * @return next state *************************************************************************/ static BLOOD_LEAK_STATES_T handleBloodLeakSetSetPointState( void ) { BLOOD_LEAK_STATES_T state = BLOOD_LEAK_SET_SET_POINT_STATE; - // TODO implement the set set point state - state = BLOOD_LEAK_INIT_STATE; + U32 command = bloodLeakSetPointSequence[ bloodLeakUARTCmdIndex ]; + // Check if the current buffer index is less than the buffer length + if( bloodLeakUARTCmdIndex < bloodLeakSetPointSeqLength ) + { + // The active high index is the length - 2 they are the last two elements - 1 since the + // index starts from 0 + U32 activeHighIndex = bloodLeakSetPointSeqLength - 2; + // Check if the current index towards the end of the buffer which are 1 and 0 + if( activeHighIndex == bloodLeakUARTCmdIndex ) + { + setFPGABloodLeakUARTTransmit( (U08)command ); + } + else + { + setFPGABloodLeakUARTControl( (U08)command ); + } + + bloodLeakUARTCmdIndex++; + } + else + { + // Done with writing the set point + state = BLOOD_LEAK_INIT_STATE; + } + return state; } @@ -395,7 +426,7 @@ } // Check status reading and act upon - if ( ( BLOOD_LEAK_DETECTED == getBloodLeakStatus() ) && ( getCurrentOperationMode() == MODE_TREA ) ) + if ( ( BLOOD_LEAK_DETECTED == getBloodLeakStatus() ) && ( MODE_TREA == getCurrentOperationMode() ) ) { if ( ++bloodLeakPersistenceCtr > BLOOD_LEAK_PERSISTENCE ) { @@ -466,9 +497,70 @@ /*********************************************************************//** * @brief + * The prepareSetPointSeq function prepares the set point sequence to be + * written to the blood leak sensor. + * @details Inputs: none + * @details Outputs: bloodLeakSetPointSequence, bloodLeakSetPointSeqLength + * @return none + *************************************************************************/ +static void prepareSetPointSeq( void ) +{ + U08 i; + // Set the local variables. Create a local char buffer with the maximum possible length + // The set point can be maximum 3 digits and when it is converted to string, the null character + // is the extra character at the end of the buffer so it maximum length is 4 bytes. + U32 digitCount = 0; + U32 setPoint = test; // TODO make the origin of the number right + U32 bufferIndex = BLOOD_LEAK_SET_POINT_START_CHAR_INDEX; + char tempCharBuffer[ BLOOD_LEAK_SET_POINT_MAX_CHAR_LENGTH ]; + memset( tempCharBuffer, 0x0, BLOOD_LEAK_SET_POINT_MAX_CHAR_LENGTH ); + + while ( setPoint != 0 ) + { + // Calculate the number of digits of the set point + // Keep dividing the set point to 10 and count the digits until the set point + // is 0. This value is an unsigned integer. + setPoint = setPoint / 10; + digitCount++; + } + + // + setPoint = test; + + // Convert the set point number to the equivalent ASCII number with the unsigned integer data type + sprintf( tempCharBuffer, "%u", setPoint ); + + // Set the first item to the ASCII character of S. The format to set the set point is + // SXXXCR10. It starts with S followed by the characters up to 3 digits, carriage return and a 1 and a 0 to write to the buffer. + bloodLeakSetPointSequence[ bufferIndex ] = BLOOD_LEAK_SET_POINT_START_CHAR_ASCII; + // Increment the buffer index + bufferIndex++; + + // Loop through the number of digits and get each ASCII value + for ( i = 0; i < digitCount; i++ ) + { + // Write the characters + bloodLeakSetPointSequence[ bufferIndex ] = tempCharBuffer[ i ]; + bufferIndex++; + } + + // After the characters, insert the carriage return into the buffer + bloodLeakSetPointSequence[ bufferIndex ] = BLOOD_LEAK_CARRIAGE_RETURN_ASCII; + bufferIndex++; + // Set active high and active low into the buffer + bloodLeakSetPointSequence[ bufferIndex ] = BLOOD_LEAK_UART_COMM_ACTIVE_HIGH; + bufferIndex++; + + bloodLeakSetPointSequence[ bufferIndex ] = BLOOD_LEAK_UART_COMM_ACTIVE_LOW; + // Update the sequence length for writing to the sensor + bloodLeakSetPointSeqLength = bufferIndex + 1; +} + +/*********************************************************************//** + * @brief * The publishBloodLeakData function publishes blood leak data at the set interval. - * @details Inputs: bloodLeakStatus, bloodLeakState - * @details Outputs: if broadcast is due, send blood leak data + * @details Inputs: bloodLeakDataPublicationTimerCounter + * @details Outputs: bloodLeakDataPublicationTimerCounter * @return none *************************************************************************/ static void publishBloodLeakData( void ) @@ -518,7 +610,7 @@ U32 intvl = value / TASK_PRIORITY_INTERVAL; result = TRUE; - bloodLeakDataPublishInterval.ovData = intvl; + bloodLeakDataPublishInterval.ovData = intvl; bloodLeakDataPublishInterval.override = OVERRIDE_KEY; } @@ -541,7 +633,7 @@ { result = TRUE; bloodLeakDataPublishInterval.override = OVERRIDE_RESET; - bloodLeakDataPublishInterval.ovData = bloodLeakDataPublishInterval.ovInitData; + bloodLeakDataPublishInterval.ovData = bloodLeakDataPublishInterval.ovInitData; } return result; @@ -566,7 +658,7 @@ if ( TRUE == isTestingActivated() ) { result = TRUE; - bloodLeakStatus.ovData = (U32)status; + bloodLeakStatus.ovData = (U32)status; bloodLeakStatus.override = OVERRIDE_KEY; } } @@ -591,7 +683,7 @@ { result = TRUE; bloodLeakStatus.override = OVERRIDE_RESET; - bloodLeakStatus.ovData = bloodLeakStatus.ovInitData; + bloodLeakStatus.ovData = bloodLeakStatus.ovInitData; } return result;