# HG changeset patch
# User Jan Mulder <jlmulder@xs4all.nl>
# Date 1558681366 -7200
# Node ID 1203255481e4a8c71eda914ba75292fa4342eee2
# Parent  b6436edfb2c0224b61afb150da277fc45bd1a427
cleanup: introduce function setAvgDepth

The simulator and the realtime code shared a literally identical piece
of code to compute a running depth average. This is simply poor coding
style, so factor this out and create a function to do this work.

Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>

diff -r b6436edfb2c0 -r 1203255481e4 Discovery/Inc/data_exchange_main.h
--- a/Discovery/Inc/data_exchange_main.h	Wed May 22 22:22:15 2019 +0200
+++ b/Discovery/Inc/data_exchange_main.h	Fri May 24 09:02:46 2019 +0200
@@ -48,7 +48,7 @@
 uint32_t DataEX_lost_connection_count(void);
 void DataEX_control_connection_while_asking_for_sleep(void);
 uint8_t DataEX_check_RTE_version__needs_update(void);
-
+void setAvgDepth(SDiveState *pStateReal);
 
 SDataReceiveFromMaster * dataOutGetPointer(void);
 
diff -r b6436edfb2c0 -r 1203255481e4 Discovery/Src/data_exchange_main.c
--- a/Discovery/Src/data_exchange_main.c	Wed May 22 22:22:15 2019 +0200
+++ b/Discovery/Src/data_exchange_main.c	Fri May 24 09:02:46 2019 +0200
@@ -994,15 +994,18 @@
 		}
 	}
 
-	/* average depth 
-	 */
+	setAvgDepth(pStateReal);
+}
+
+void setAvgDepth(SDiveState *pStateReal) {
+
 	float *AvgDepthValue = &pStateReal->lifeData.average_depth_meter;
 	float	DepthNow = pStateReal->lifeData.depth_meter; 
 	uint32_t *AvgDepthCount = &pStateReal->lifeData.internal.average_depth_meter_Count;
 	uint32_t *AvgDepthTimer = &pStateReal->lifeData.internal.average_depth_last_update_dive_time_seconds_without_surface_time;
 	uint32_t AvgSecondsSinceLast;
 	uint32_t DiveTime = pStateReal->lifeData.dive_time_seconds_without_surface_time;
-	
+
 	if(pStateReal->lifeData.boolResetAverageDepth)
 	{
 		*AvgDepthValue = DepthNow;
diff -r b6436edfb2c0 -r 1203255481e4 Discovery/Src/simulation.c
--- a/Discovery/Src/simulation.c	Wed May 22 22:22:15 2019 +0200
+++ b/Discovery/Src/simulation.c	Fri May 24 09:02:46 2019 +0200
@@ -37,6 +37,7 @@
 #include "decom.h"
 #include "calc_crush.h"
 #include "data_exchange.h"
+#include "data_exchange_main.h"
 #include "timer.h"
 #include "check_warning.h"
 #include "vpm.h"
@@ -204,34 +205,7 @@
         }
     }
 
-    /* average depth
-     */
-    float *AvgDepthValue = &pDiveState->lifeData.average_depth_meter;
-    float	DepthNow = pDiveState->lifeData.depth_meter;
-    uint32_t *AvgDepthCount = &pDiveState->lifeData.internal.average_depth_meter_Count;
-    uint32_t *AvgDepthTimer = &pDiveState->lifeData.internal.average_depth_last_update_dive_time_seconds_without_surface_time;
-    uint32_t AvgSecondsSinceLast;
-    uint32_t DiveTime = pDiveState->lifeData.dive_time_seconds_without_surface_time;
-
-    if(pDiveState->lifeData.boolResetAverageDepth)
-    {
-        *AvgDepthValue = DepthNow;
-        *AvgDepthCount = 1;
-        *AvgDepthTimer = DiveTime;
-        pDiveState->lifeData.boolResetAverageDepth = 0;
-    }
-    else if (DiveTime > *AvgDepthTimer)
-    {
-        AvgSecondsSinceLast = DiveTime - *AvgDepthTimer;
-        for(int i=0;i<AvgSecondsSinceLast;i++)
-        {
-            *AvgDepthValue = (*AvgDepthValue * *AvgDepthCount + DepthNow) / (*AvgDepthCount + 1);
-            *AvgDepthCount += 1;
-        }
-        *AvgDepthTimer = DiveTime;
-    }
-    if(*AvgDepthCount == 0)
-        *AvgDepthValue = 0;
+    setAvgDepth(pDiveState);
 
     /* Exposure Tissues
      */