Mercurial > public > ostc4
annotate Discovery/Src/timer.c @ 778:74253a41cf80
Zusammenf?hren
author | heinrichsweikamp |
---|---|
date | Mon, 22 May 2023 09:15:09 +0200 |
parents | 9660df857c57 |
children |
rev | line source |
---|---|
38 | 1 /////////////////////////////////////////////////////////////////////////////// |
2 /// -*- coding: UTF-8 -*- | |
3 /// | |
4 /// \file Discovery/Src/timer.c | |
5 /// \brief Contains timer related functionality like stopwatch and security stop | |
766
9660df857c57
change button default responsiveness to 100
heinrichsweikamp
parents:
763
diff
changeset
|
6 /// \author heinrichs weikamp gmbh |
38 | 7 /// \date 5. Feb.2015 (maybe) |
8 /// | |
9 /// \details | |
10 /// | |
11 /// $Id$ | |
12 /////////////////////////////////////////////////////////////////////////////// | |
13 /// \par Copyright (c) 2014-2018 Heinrichs Weikamp gmbh | |
14 /// | |
15 /// This program is free software: you can redistribute it and/or modify | |
16 /// it under the terms of the GNU General Public License as published by | |
17 /// the Free Software Foundation, either version 3 of the License, or | |
18 /// (at your option) any later version. | |
19 /// | |
20 /// This program is distributed in the hope that it will be useful, | |
21 /// but WITHOUT ANY WARRANTY; without even the implied warranty of | |
22 /// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
23 /// GNU General Public License for more details. | |
24 /// | |
25 /// You should have received a copy of the GNU General Public License | |
26 /// along with this program. If not, see <http://www.gnu.org/licenses/>. | |
27 ////////////////////////////////////////////////////////////////////////////// | |
28 | |
29 #include "data_central.h" | |
30 | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
31 static _Bool bStopWatch = false; |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
32 static float stopWatchAverageDepth_Meter = 0.0f; |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
33 static long safetyStopCountDown_Second = 0; |
311
ddbe8bed5096
bugfix: make stopwatch and divetime run in sync
Jan Mulder <jlmulder@xs4all.nl>
parents:
303
diff
changeset
|
34 static long stopWatchOffset = 0; |
38 | 35 |
36 void timer_init(void) | |
37 { | |
38 stopWatchAverageDepth_Meter = 0.0f; | |
39 bStopWatch = true; | |
40 safetyStopCountDown_Second = 0; | |
311
ddbe8bed5096
bugfix: make stopwatch and divetime run in sync
Jan Mulder <jlmulder@xs4all.nl>
parents:
303
diff
changeset
|
41 stopWatchOffset = 0; |
38 | 42 } |
43 | |
44 void timer_UpdateSecond(_Bool checkOncePerSecond) | |
45 { | |
46 static int last_second = -1; | |
47 static _Bool bSafetyStop = false; | |
48 static float last_depth_meter = 0; | |
519
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
49 static uint8_t safetyStopResetCnt = 0; |
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
50 static uint16_t safetyStopRestartCnt = 0; |
311
ddbe8bed5096
bugfix: make stopwatch and divetime run in sync
Jan Mulder <jlmulder@xs4all.nl>
parents:
303
diff
changeset
|
51 long stopWatchTime_Second = 0; |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
52 |
38 | 53 if(checkOncePerSecond) |
54 { | |
55 int now = current_second(); | |
56 if( last_second == now) | |
57 return; | |
58 last_second = now; | |
59 } | |
60 | |
61 /** Stopwatch **/ | |
311
ddbe8bed5096
bugfix: make stopwatch and divetime run in sync
Jan Mulder <jlmulder@xs4all.nl>
parents:
303
diff
changeset
|
62 stopWatchTime_Second = stateUsed->lifeData.dive_time_seconds_without_surface_time - stopWatchOffset; |
303
90e65971f15d
bugfix, cleanup: simplify stopwatch logic and fix fallout
Jan Mulder <jlmulder@xs4all.nl>
parents:
186
diff
changeset
|
63 if(bStopWatch && !is_ambient_pressure_close_to_surface(&stateUsedWrite->lifeData)) |
38 | 64 { |
303
90e65971f15d
bugfix, cleanup: simplify stopwatch logic and fix fallout
Jan Mulder <jlmulder@xs4all.nl>
parents:
186
diff
changeset
|
65 if(stopWatchTime_Second == 0) |
90e65971f15d
bugfix, cleanup: simplify stopwatch logic and fix fallout
Jan Mulder <jlmulder@xs4all.nl>
parents:
186
diff
changeset
|
66 stopWatchAverageDepth_Meter = stateUsed->lifeData.depth_meter; |
38 | 67 else |
68 stopWatchAverageDepth_Meter = (stopWatchAverageDepth_Meter * stopWatchTime_Second + stateUsed->lifeData.depth_meter)/ (stopWatchTime_Second + 1); | |
69 } | |
70 | |
71 /** SafetyStop **/ | |
72 float depthToStopSafetyStopCount; | |
73 if(settingsGetPointer()->safetystopDuration && (stateUsed->lifeData.max_depth_meter > 10.0f) && (stateUsed->lifeData.dive_time_seconds > 60)) | |
74 { | |
75 | |
76 //No deco when 10 meters are crossed from below => Activate SecurityStop | |
77 if( last_depth_meter > 10.0f && stateUsed->lifeData.depth_meter <= 10.0f) | |
78 { | |
79 if(stateUsed->diveSettings.deco_type.ub.standard == GF_MODE) | |
80 { | |
81 if(stateUsed->decolistBuehlmann.output_ndl_seconds > 0) | |
82 bSafetyStop = true; | |
83 } | |
84 else | |
85 { | |
86 if(stateUsed->decolistVPM.output_ndl_seconds > 0) | |
87 bSafetyStop = true; | |
88 } | |
89 } | |
90 | |
519
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
91 /* Has the diver left safety stop depth (descend)? => need to restart safety stop timer? */ |
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
92 if(safetyStopCountDown_Second != 0) |
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
93 { |
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
94 if(stateUsed->lifeData.depth_meter >= (settingsGetPointer()->safetystopDepth + 2.0)) |
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
95 { |
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
96 safetyStopRestartCnt = safetyStopCountDown_Second; |
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
97 safetyStopCountDown_Second = 0; |
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
98 safetyStopResetCnt = 60; /* restart safety stop from scratch if depth is left for more than one minute */ |
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
99 } |
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
100 } |
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
101 else if(safetyStopResetCnt) |
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
102 { |
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
103 safetyStopResetCnt--; |
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
104 if(safetyStopResetCnt == 0) |
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
105 { |
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
106 safetyStopRestartCnt = 0; |
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
107 } |
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
108 } |
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
109 |
38 | 110 //Countdown starts at 5 meters |
111 if(bSafetyStop && (stateUsed->lifeData.depth_meter - 0.0001f <= (settingsGetPointer()->safetystopDepth) )) | |
112 { | |
113 if(safetyStopCountDown_Second == 0) | |
114 { | |
519
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
115 if(safetyStopRestartCnt) /* just a short interrupt of the safetystop => continue using old count */ |
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
116 { |
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
117 safetyStopCountDown_Second = safetyStopRestartCnt; |
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
118 safetyStopRestartCnt = 0; |
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
119 } |
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
120 else /* setup safety stop duration */ |
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
121 { |
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
122 safetyStopCountDown_Second = (settingsGetPointer()->safetystopDuration) * 60; |
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
123 } |
38 | 124 } |
125 else | |
519
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
126 { |
38 | 127 safetyStopCountDown_Second--; |
519
2c49561c8062
Reset Safety Stop display in case of a new descend:
Ideenmodellierer
parents:
311
diff
changeset
|
128 } |
38 | 129 } |
130 | |
131 // after safetystopDuration minutes or below 3 (2) meter safetyStop is disabled | |
132 if(settingsGetPointer()->safetystopDepth == 3) | |
133 depthToStopSafetyStopCount = 1.999f; // instead of 2 | |
134 else | |
135 depthToStopSafetyStopCount = 2.999f;// instead of 3 | |
136 | |
137 if((safetyStopCountDown_Second == 1) || (stateUsed->lifeData.depth_meter <= depthToStopSafetyStopCount)) | |
138 { | |
139 bSafetyStop = false; | |
140 safetyStopCountDown_Second = 0; | |
141 } | |
142 } | |
143 else | |
144 { | |
145 bSafetyStop = false; | |
146 safetyStopCountDown_Second = 0; | |
147 } | |
148 last_depth_meter = stateUsed->lifeData.depth_meter; | |
149 } | |
150 | |
151 | |
152 void timer_Stopwatch_Restart(void) | |
153 { | |
154 stopWatchAverageDepth_Meter = stateUsed->lifeData.depth_meter; | |
155 bStopWatch = true; | |
311
ddbe8bed5096
bugfix: make stopwatch and divetime run in sync
Jan Mulder <jlmulder@xs4all.nl>
parents:
303
diff
changeset
|
156 stopWatchOffset = stateUsed->lifeData.dive_time_seconds_without_surface_time; |
38 | 157 } |
158 | |
159 void timer_Stopwatch_Stop(void) | |
160 { | |
161 bStopWatch = false; | |
162 } | |
163 | |
164 long timer_Stopwatch_GetTime(void) | |
165 { | |
311
ddbe8bed5096
bugfix: make stopwatch and divetime run in sync
Jan Mulder <jlmulder@xs4all.nl>
parents:
303
diff
changeset
|
166 return stateUsed->lifeData.dive_time_seconds_without_surface_time - stopWatchOffset; |
38 | 167 } |
168 | |
169 float timer_Stopwatch_GetAvarageDepth_Meter(void) | |
170 { | |
171 return stopWatchAverageDepth_Meter; | |
172 } | |
173 | |
174 long timer_Safetystop_GetCountDown(void) | |
175 { | |
176 return safetyStopCountDown_Second; | |
177 } | |
178 | |
179 uint8_t timer_Safetystop_GetDepthUpperLimit(void) | |
180 { | |
181 if(settingsGetPointer()->safetystopDepth == 3) | |
182 return 2; | |
183 else | |
184 return 3; | |
185 } | |
186 |