diff Discovery/Src/timer.c @ 312:e84a2486933e

Merged in janlmulder/ostc4/cleanup-4 (pull request #25) Fix stopwatch and divetime not in sync
author heinrichsweikamp <bitbucket@heinrichsweikamp.com>
date Fri, 31 May 2019 06:22:50 +0000
parents ddbe8bed5096
children 2c49561c8062
line wrap: on
line diff
--- a/Discovery/Src/timer.c	Fri May 17 06:38:56 2019 +0000
+++ b/Discovery/Src/timer.c	Fri May 31 06:22:50 2019 +0000
@@ -28,18 +28,17 @@
 
 #include "data_central.h"
 
-static long stopWatchTime_Second = 0;
 static _Bool bStopWatch = false;
 static float stopWatchAverageDepth_Meter = 0.0f;
 static long safetyStopCountDown_Second = 0;
+static long stopWatchOffset = 0;
 
 void timer_init(void)
 {
-  stopWatchTime_Second = 0;
   stopWatchAverageDepth_Meter = 0.0f;
   bStopWatch = true;
   safetyStopCountDown_Second = 0;
-
+  stopWatchOffset = 0;
 }
 
 void timer_UpdateSecond(_Bool checkOncePerSecond)
@@ -47,6 +46,7 @@
     static int last_second = -1;
     static _Bool bSafetyStop = false;
     static float last_depth_meter = 0;
+    long stopWatchTime_Second = 0;
 
     if(checkOncePerSecond)
     {
@@ -57,18 +57,13 @@
     }
 
     /** Stopwatch **/
-    if(bStopWatch && stateUsed->lifeData.depth_meter > 1)
+    stopWatchTime_Second = stateUsed->lifeData.dive_time_seconds_without_surface_time - stopWatchOffset;
+    if(bStopWatch && !is_ambient_pressure_close_to_surface(&stateUsedWrite->lifeData))
     {
-        if((stopWatchTime_Second == 0) && (stateUsed->lifeData.dive_time_seconds >= 1))
-        {
-            stopWatchTime_Second = stateUsed->lifeData.dive_time_seconds - 1;
-            stopWatchAverageDepth_Meter = stateUsed->lifeData.average_depth_meter * (stopWatchTime_Second - 1) / stopWatchTime_Second;
-        }
+        if(stopWatchTime_Second == 0)
+            stopWatchAverageDepth_Meter = stateUsed->lifeData.depth_meter;
         else
-        {
             stopWatchAverageDepth_Meter = (stopWatchAverageDepth_Meter * stopWatchTime_Second + stateUsed->lifeData.depth_meter)/ (stopWatchTime_Second + 1);
-            stopWatchTime_Second++;
-        }
     }
 
     /** SafetyStop **/
@@ -125,9 +120,9 @@
 
 void timer_Stopwatch_Restart(void)
 {
-  stopWatchTime_Second = 1;
   stopWatchAverageDepth_Meter = stateUsed->lifeData.depth_meter;
   bStopWatch = true;
+  stopWatchOffset = stateUsed->lifeData.dive_time_seconds_without_surface_time;
 }
 
 void timer_Stopwatch_Stop(void)
@@ -137,7 +132,7 @@
 
 long timer_Stopwatch_GetTime(void)
 {
-  return stopWatchTime_Second;
+  return stateUsed->lifeData.dive_time_seconds_without_surface_time - stopWatchOffset;
 }
 
 float timer_Stopwatch_GetAvarageDepth_Meter(void)