diff Discovery/Src/simulation.c @ 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 f1257a32f2d4
line wrap: on
line diff
--- 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
      */