annotate Discovery/Src/timer.c @ 303:90e65971f15d cleanup-4

bugfix, cleanup: simplify stopwatch logic and fix fallout The previous 2 commits (making the depth switch between surface and diving consistent) increased the time difference (in the simulator) to about 4 seconds. This commit fixes this again, and we are back at 1 sec. difference between the 2 timers (notice: in the simulator). Still not the wanted 0 sec. difference, but the old stopwatch logic logic was rather convoluted. Resetting to 1 second (instead of 0), and second-1 logic. Basically, this feels like a bug fixed with a second bug on top to mask it. The code is now much more logic and consistent (despite the fact that the real reason for the 1 sec. difference is not yet found). Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
author Jan Mulder <jlmulder@xs4all.nl>
date Mon, 20 May 2019 12:57:31 +0200
parents f11f0bf6ef2d
children ddbe8bed5096
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
1 ///////////////////////////////////////////////////////////////////////////////
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
2 /// -*- coding: UTF-8 -*-
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
3 ///
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
4 /// \file Discovery/Src/timer.c
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
5 /// \brief Contains timer related functionality like stopwatch and security stop
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
6 /// \author Peter Ryser & heinrichs weikamp gmbh
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
7 /// \date 5. Feb.2015 (maybe)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
8 ///
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
9 /// \details
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
10 ///
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
11 /// $Id$
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
12 ///////////////////////////////////////////////////////////////////////////////
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
13 /// \par Copyright (c) 2014-2018 Heinrichs Weikamp gmbh
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
14 ///
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
15 /// This program is free software: you can redistribute it and/or modify
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
16 /// it under the terms of the GNU General Public License as published by
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
17 /// the Free Software Foundation, either version 3 of the License, or
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
18 /// (at your option) any later version.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
19 ///
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
20 /// This program is distributed in the hope that it will be useful,
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
21 /// but WITHOUT ANY WARRANTY; without even the implied warranty of
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
22 /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
23 /// GNU General Public License for more details.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
24 ///
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
25 /// You should have received a copy of the GNU General Public License
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
26 /// along with this program. If not, see <http://www.gnu.org/licenses/>.
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
27 //////////////////////////////////////////////////////////////////////////////
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
28
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
29 #include "data_central.h"
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
30
186
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
31 static long stopWatchTime_Second = 0;
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
32 static _Bool bStopWatch = false;
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
33 static float stopWatchAverageDepth_Meter = 0.0f;
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
34 static long safetyStopCountDown_Second = 0;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
35
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
36 void timer_init(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
37 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
38 stopWatchTime_Second = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
39 stopWatchAverageDepth_Meter = 0.0f;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
40 bStopWatch = true;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
41 safetyStopCountDown_Second = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
42
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
43 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
44
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
45 void timer_UpdateSecond(_Bool checkOncePerSecond)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
46 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
47 static int last_second = -1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
48 static _Bool bSafetyStop = false;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
49 static float last_depth_meter = 0;
186
f11f0bf6ef2d cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents: 38
diff changeset
50
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
51 if(checkOncePerSecond)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
52 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
53 int now = current_second();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
54 if( last_second == now)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
55 return;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
56 last_second = now;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
57 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
58
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
59 /** Stopwatch **/
303
90e65971f15d bugfix, cleanup: simplify stopwatch logic and fix fallout
Jan Mulder <jlmulder@xs4all.nl>
parents: 186
diff changeset
60 if(bStopWatch && !is_ambient_pressure_close_to_surface(&stateUsedWrite->lifeData))
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
61 {
303
90e65971f15d bugfix, cleanup: simplify stopwatch logic and fix fallout
Jan Mulder <jlmulder@xs4all.nl>
parents: 186
diff changeset
62 if(stopWatchTime_Second == 0)
90e65971f15d bugfix, cleanup: simplify stopwatch logic and fix fallout
Jan Mulder <jlmulder@xs4all.nl>
parents: 186
diff changeset
63 stopWatchAverageDepth_Meter = stateUsed->lifeData.depth_meter;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
64 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
65 stopWatchAverageDepth_Meter = (stopWatchAverageDepth_Meter * stopWatchTime_Second + stateUsed->lifeData.depth_meter)/ (stopWatchTime_Second + 1);
303
90e65971f15d bugfix, cleanup: simplify stopwatch logic and fix fallout
Jan Mulder <jlmulder@xs4all.nl>
parents: 186
diff changeset
66
90e65971f15d bugfix, cleanup: simplify stopwatch logic and fix fallout
Jan Mulder <jlmulder@xs4all.nl>
parents: 186
diff changeset
67 stopWatchTime_Second++;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
68 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
69
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
70 /** SafetyStop **/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
71 float depthToStopSafetyStopCount;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
72 if(settingsGetPointer()->safetystopDuration && (stateUsed->lifeData.max_depth_meter > 10.0f) && (stateUsed->lifeData.dive_time_seconds > 60))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
73 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
74
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
75 //No deco when 10 meters are crossed from below => Activate SecurityStop
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
76 if( last_depth_meter > 10.0f && stateUsed->lifeData.depth_meter <= 10.0f)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
77 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
78 if(stateUsed->diveSettings.deco_type.ub.standard == GF_MODE)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
79 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
80 if(stateUsed->decolistBuehlmann.output_ndl_seconds > 0)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
81 bSafetyStop = true;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
82 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
83 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
84 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
85 if(stateUsed->decolistVPM.output_ndl_seconds > 0)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
86 bSafetyStop = true;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
87 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
88 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
89
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
90 //Countdown starts at 5 meters
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
91 if(bSafetyStop && (stateUsed->lifeData.depth_meter - 0.0001f <= (settingsGetPointer()->safetystopDepth) ))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
92 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
93 if(safetyStopCountDown_Second == 0)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
94 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
95 safetyStopCountDown_Second = (settingsGetPointer()->safetystopDuration) * 60;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
96 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
97 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
98 safetyStopCountDown_Second--;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
99 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
100
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
101 // after safetystopDuration minutes or below 3 (2) meter safetyStop is disabled
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
102 if(settingsGetPointer()->safetystopDepth == 3)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
103 depthToStopSafetyStopCount = 1.999f; // instead of 2
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
104 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
105 depthToStopSafetyStopCount = 2.999f;// instead of 3
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
106
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
107 if((safetyStopCountDown_Second == 1) || (stateUsed->lifeData.depth_meter <= depthToStopSafetyStopCount))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
108 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
109 bSafetyStop = false;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
110 safetyStopCountDown_Second = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
111 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
112 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
113 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
114 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
115 bSafetyStop = false;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
116 safetyStopCountDown_Second = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
117 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
118 last_depth_meter = stateUsed->lifeData.depth_meter;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
119 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
120
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
121
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
122 void timer_Stopwatch_Restart(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
123 {
303
90e65971f15d bugfix, cleanup: simplify stopwatch logic and fix fallout
Jan Mulder <jlmulder@xs4all.nl>
parents: 186
diff changeset
124 stopWatchTime_Second = 0;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
125 stopWatchAverageDepth_Meter = stateUsed->lifeData.depth_meter;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
126 bStopWatch = true;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
127 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
128
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
129 void timer_Stopwatch_Stop(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
130 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
131 bStopWatch = false;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
132 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
133
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
134 long timer_Stopwatch_GetTime(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
135 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
136 return stopWatchTime_Second;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
137 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
138
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
139 float timer_Stopwatch_GetAvarageDepth_Meter(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
140 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
141 return stopWatchAverageDepth_Meter;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
142 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
143
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
144 long timer_Safetystop_GetCountDown(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
145 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
146 return safetyStopCountDown_Second;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
147 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
148
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
149 uint8_t timer_Safetystop_GetDepthUpperLimit(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
150 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
151 if(settingsGetPointer()->safetystopDepth == 3)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
152 return 2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
153 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
154 return 3;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
155 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
156