changeset 308:1203255481e4 cleanup-4

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>
author Jan Mulder <jlmulder@xs4all.nl>
date Fri, 24 May 2019 09:02:46 +0200
parents b6436edfb2c0
children b0045281cb2d
files Discovery/Inc/data_exchange_main.h Discovery/Src/data_exchange_main.c Discovery/Src/simulation.c
diffstat 3 files changed, 9 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- 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);
 
--- 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;
--- 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
      */