comparison Discovery/Src/timer.c @ 311:ddbe8bed5096 cleanup-4

bugfix: make stopwatch and divetime run in sync And this shows the fundamental issue in the difference between dive time and stopwatch time. The dive time is constructed on the RTE, and rather independently, the stopwatch time is constructed on CPU1. This works rather well, but not perfect. This commit fixes things in a relatively straightforward way. Instead of incrementing the stopwatch locally on CPU1, simply use the same time data that is coming from the RTE. Some logic was added to make this stopwatch resettable again. Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
author Jan Mulder <jlmulder@xs4all.nl>
date Wed, 29 May 2019 14:02:27 +0200
parents 90e65971f15d
children 2c49561c8062
comparison
equal deleted inserted replaced
310:95928ef3986f 311:ddbe8bed5096
26 /// along with this program. If not, see <http://www.gnu.org/licenses/>. 26 /// along with this program. If not, see <http://www.gnu.org/licenses/>.
27 ////////////////////////////////////////////////////////////////////////////// 27 //////////////////////////////////////////////////////////////////////////////
28 28
29 #include "data_central.h" 29 #include "data_central.h"
30 30
31 static long stopWatchTime_Second = 0;
32 static _Bool bStopWatch = false; 31 static _Bool bStopWatch = false;
33 static float stopWatchAverageDepth_Meter = 0.0f; 32 static float stopWatchAverageDepth_Meter = 0.0f;
34 static long safetyStopCountDown_Second = 0; 33 static long safetyStopCountDown_Second = 0;
34 static long stopWatchOffset = 0;
35 35
36 void timer_init(void) 36 void timer_init(void)
37 { 37 {
38 stopWatchTime_Second = 0;
39 stopWatchAverageDepth_Meter = 0.0f; 38 stopWatchAverageDepth_Meter = 0.0f;
40 bStopWatch = true; 39 bStopWatch = true;
41 safetyStopCountDown_Second = 0; 40 safetyStopCountDown_Second = 0;
42 41 stopWatchOffset = 0;
43 } 42 }
44 43
45 void timer_UpdateSecond(_Bool checkOncePerSecond) 44 void timer_UpdateSecond(_Bool checkOncePerSecond)
46 { 45 {
47 static int last_second = -1; 46 static int last_second = -1;
48 static _Bool bSafetyStop = false; 47 static _Bool bSafetyStop = false;
49 static float last_depth_meter = 0; 48 static float last_depth_meter = 0;
49 long stopWatchTime_Second = 0;
50 50
51 if(checkOncePerSecond) 51 if(checkOncePerSecond)
52 { 52 {
53 int now = current_second(); 53 int now = current_second();
54 if( last_second == now) 54 if( last_second == now)
55 return; 55 return;
56 last_second = now; 56 last_second = now;
57 } 57 }
58 58
59 /** Stopwatch **/ 59 /** Stopwatch **/
60 stopWatchTime_Second = stateUsed->lifeData.dive_time_seconds_without_surface_time - stopWatchOffset;
60 if(bStopWatch && !is_ambient_pressure_close_to_surface(&stateUsedWrite->lifeData)) 61 if(bStopWatch && !is_ambient_pressure_close_to_surface(&stateUsedWrite->lifeData))
61 { 62 {
62 if(stopWatchTime_Second == 0) 63 if(stopWatchTime_Second == 0)
63 stopWatchAverageDepth_Meter = stateUsed->lifeData.depth_meter; 64 stopWatchAverageDepth_Meter = stateUsed->lifeData.depth_meter;
64 else 65 else
65 stopWatchAverageDepth_Meter = (stopWatchAverageDepth_Meter * stopWatchTime_Second + stateUsed->lifeData.depth_meter)/ (stopWatchTime_Second + 1); 66 stopWatchAverageDepth_Meter = (stopWatchAverageDepth_Meter * stopWatchTime_Second + stateUsed->lifeData.depth_meter)/ (stopWatchTime_Second + 1);
66
67 stopWatchTime_Second++;
68 } 67 }
69 68
70 /** SafetyStop **/ 69 /** SafetyStop **/
71 float depthToStopSafetyStopCount; 70 float depthToStopSafetyStopCount;
72 if(settingsGetPointer()->safetystopDuration && (stateUsed->lifeData.max_depth_meter > 10.0f) && (stateUsed->lifeData.dive_time_seconds > 60)) 71 if(settingsGetPointer()->safetystopDuration && (stateUsed->lifeData.max_depth_meter > 10.0f) && (stateUsed->lifeData.dive_time_seconds > 60))
119 } 118 }
120 119
121 120
122 void timer_Stopwatch_Restart(void) 121 void timer_Stopwatch_Restart(void)
123 { 122 {
124 stopWatchTime_Second = 0;
125 stopWatchAverageDepth_Meter = stateUsed->lifeData.depth_meter; 123 stopWatchAverageDepth_Meter = stateUsed->lifeData.depth_meter;
126 bStopWatch = true; 124 bStopWatch = true;
125 stopWatchOffset = stateUsed->lifeData.dive_time_seconds_without_surface_time;
127 } 126 }
128 127
129 void timer_Stopwatch_Stop(void) 128 void timer_Stopwatch_Stop(void)
130 { 129 {
131 bStopWatch = false; 130 bStopWatch = false;
132 } 131 }
133 132
134 long timer_Stopwatch_GetTime(void) 133 long timer_Stopwatch_GetTime(void)
135 { 134 {
136 return stopWatchTime_Second; 135 return stateUsed->lifeData.dive_time_seconds_without_surface_time - stopWatchOffset;
137 } 136 }
138 137
139 float timer_Stopwatch_GetAvarageDepth_Meter(void) 138 float timer_Stopwatch_GetAvarageDepth_Meter(void)
140 { 139 {
141 return stopWatchAverageDepth_Meter; 140 return stopWatchAverageDepth_Meter;