Index: firmware/App/Modes/Prime.c =================================================================== diff -u -r24237b8f12f5bb4c9cfa8c51b3b1a92d2d692631 -r11d3b6b7d4ae9f5822998abd4f94792f2d9e1714 --- firmware/App/Modes/Prime.c (.../Prime.c) (revision 24237b8f12f5bb4c9cfa8c51b3b1a92d2d692631) +++ firmware/App/Modes/Prime.c (.../Prime.c) (revision 11d3b6b7d4ae9f5822998abd4f94792f2d9e1714) @@ -96,7 +96,7 @@ static U32 loadcellSteadyVolumeStartTime; ///< Load cell steady volume starting time. static BOOL runBloodCircuitPrimeAgain; ///< Flag indicates HD should run blood circuit prime once more time. static F32 loadcellReadings[ CIRC_BUF_SIZE ]; ///< Storage for 10 seconds of load cell weight readings. -static U32 iteration; ///< Use to calculate the write and the read index of the circular buffer. +static U32 currentBufIndex; ///< Use to calculate the write and the read index of the circular buffer. // ********** private function prototypes ********** @@ -610,13 +610,13 @@ setValvePosition( VBV, VALVE_POSITION_C_CLOSE ); setValveAirTrap( STATE_CLOSED ); - iteration = 0; // initialize to zero before use U32 bufIndex; for ( bufIndex = 0; bufIndex < CIRC_BUF_SIZE; bufIndex++ ) { loadcellReadings[ bufIndex ] = getLoadCellWeight( LOAD_CELL_RESERVOIR_1_PRIMARY ); } + currentBufIndex = 0; // initialize circular buffer read/write index primeDialysateBypassStartTime = getMSTimerCount(); loadcellSteadyVolumeStartTime = getMSTimerCount(); @@ -652,10 +652,10 @@ if ( TRUE == didTimeout( loadcellSteadyVolumeStartTime, LOAD_CELL_STEADY_VOLUME_SAMPLING_TIME ) ) { F32 const currentLoadCellValue = getLoadCellWeight( LOAD_CELL_RESERVOIR_1_PRIMARY ); - F32 const previousLoadCellValue = loadcellReadings[ iteration % CIRC_BUF_SIZE ]; + F32 const previousLoadCellValue = loadcellReadings[ currentBufIndex % CIRC_BUF_SIZE ]; F32 const loadcellDelta = fabs( currentLoadCellValue - previousLoadCellValue ); // delta of the current (now) and the previous (10 seconds ago) - loadcellReadings[ ( iteration + MAX_BUF_INDEX ) % CIRC_BUF_SIZE ] = currentLoadCellValue; - iteration++; + loadcellReadings[ currentBufIndex % CIRC_BUF_SIZE ] = currentLoadCellValue; + currentBufIndex++; if ( ( loadcellDelta < LOAD_CELL_VOLUME_NOISE_TOLERANCE ) && ( currentLoadCellValue < ( PRIME_DIALYZER_BYPASS_EXPECTED_VOLUME + PRIME_EXPECTED_VOLUME_TOLERANCE ) ) && // check the steady state volume of R1 @@ -720,13 +720,14 @@ setDialInPumpTargetFlowRate( DIALYSATE_PUMP_PRIME_FLOW_RATE_ML_MIN, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_OPEN_LOOP ); setDialOutPumpTargetRate( DIALYSATE_PUMP_PRIME_FLOW_RATE_ML_MIN, MOTOR_DIR_FORWARD, PUMP_CONTROL_MODE_OPEN_LOOP ); - iteration = 0; // initialize to zero before use U32 bufIndex; for ( bufIndex = 0; bufIndex < CIRC_BUF_SIZE; bufIndex++ ) { loadcellReadings[ bufIndex ] = getLoadCellWeight( LOAD_CELL_RESERVOIR_2_PRIMARY ); } + currentBufIndex = 0; // initialize circular buffer read/write index + loadcellSteadyVolumeStartTime = getMSTimerCount(); primeDialysateDialyzerStartTime = getMSTimerCount(); state = HD_PRIME_DIALYSATE_DIALYZER_STATE; @@ -757,10 +758,10 @@ if ( TRUE == didTimeout( loadcellSteadyVolumeStartTime, LOAD_CELL_STEADY_VOLUME_SAMPLING_TIME ) ) { F32 const currentLoadCellValue = getLoadCellWeight( LOAD_CELL_RESERVOIR_2_PRIMARY ); - F32 const previousLoadCellValue = loadcellReadings[ iteration % CIRC_BUF_SIZE ]; + F32 const previousLoadCellValue = loadcellReadings[ currentBufIndex % CIRC_BUF_SIZE ]; F32 const loadcellDelta = fabs( currentLoadCellValue - previousLoadCellValue ); - loadcellReadings[ ( iteration + MAX_BUF_INDEX ) % CIRC_BUF_SIZE ] = currentLoadCellValue; - iteration++; + loadcellReadings[ currentBufIndex % CIRC_BUF_SIZE ] = currentLoadCellValue; + currentBufIndex++; if ( ( loadcellDelta < LOAD_CELL_VOLUME_NOISE_TOLERANCE ) && ( currentLoadCellValue < ( PRIME_DIALYZER_EXPECTED_VOLUME + PRIME_EXPECTED_VOLUME_TOLERANCE ) ) && // check the steady state volume of R2