annotate Discovery/Src/timer.c @ 504:8428195a4967

Added worst case plausibility check (disabled by compile switch) Just used in case of development activities which may cause negativ values resulting in errors / timeout in the deco calculation
author Ideenmodellierer
date Mon, 24 Aug 2020 19:59:28 +0200
parents ddbe8bed5096
children 2c49561c8062
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 _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
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 stopWatchAverageDepth_Meter = 0.0f;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
39 bStopWatch = true;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
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
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 void timer_UpdateSecond(_Bool checkOncePerSecond)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
45 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
46 static int last_second = -1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
47 static _Bool bSafetyStop = false;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
48 static float last_depth_meter = 0;
311
ddbe8bed5096 bugfix: make stopwatch and divetime run in sync
Jan Mulder <jlmulder@xs4all.nl>
parents: 303
diff changeset
49 long stopWatchTime_Second = 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 **/
311
ddbe8bed5096 bugfix: make stopwatch and divetime run in sync
Jan Mulder <jlmulder@xs4all.nl>
parents: 303
diff changeset
60 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
61 if(bStopWatch && !is_ambient_pressure_close_to_surface(&stateUsedWrite->lifeData))
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
62 {
303
90e65971f15d bugfix, cleanup: simplify stopwatch logic and fix fallout
Jan Mulder <jlmulder@xs4all.nl>
parents: 186
diff changeset
63 if(stopWatchTime_Second == 0)
90e65971f15d bugfix, cleanup: simplify stopwatch logic and fix fallout
Jan Mulder <jlmulder@xs4all.nl>
parents: 186
diff changeset
64 stopWatchAverageDepth_Meter = stateUsed->lifeData.depth_meter;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
65 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
66 stopWatchAverageDepth_Meter = (stopWatchAverageDepth_Meter * stopWatchTime_Second + stateUsed->lifeData.depth_meter)/ (stopWatchTime_Second + 1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
67 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
68
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
69 /** SafetyStop **/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
70 float depthToStopSafetyStopCount;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
71 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
72 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
73
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
74 //No deco when 10 meters are crossed from below => Activate SecurityStop
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
75 if( last_depth_meter > 10.0f && stateUsed->lifeData.depth_meter <= 10.0f)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
76 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
77 if(stateUsed->diveSettings.deco_type.ub.standard == GF_MODE)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
78 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
79 if(stateUsed->decolistBuehlmann.output_ndl_seconds > 0)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
80 bSafetyStop = true;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
81 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
82 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
83 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
84 if(stateUsed->decolistVPM.output_ndl_seconds > 0)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
85 bSafetyStop = true;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
86 }
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 //Countdown starts at 5 meters
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
90 if(bSafetyStop && (stateUsed->lifeData.depth_meter - 0.0001f <= (settingsGetPointer()->safetystopDepth) ))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
91 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
92 if(safetyStopCountDown_Second == 0)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
93 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
94 safetyStopCountDown_Second = (settingsGetPointer()->safetystopDuration) * 60;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
95 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
96 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
97 safetyStopCountDown_Second--;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
98 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
99
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
100 // after safetystopDuration minutes or below 3 (2) meter safetyStop is disabled
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
101 if(settingsGetPointer()->safetystopDepth == 3)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
102 depthToStopSafetyStopCount = 1.999f; // instead of 2
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
103 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
104 depthToStopSafetyStopCount = 2.999f;// instead of 3
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
105
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
106 if((safetyStopCountDown_Second == 1) || (stateUsed->lifeData.depth_meter <= depthToStopSafetyStopCount))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
107 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
108 bSafetyStop = false;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
109 safetyStopCountDown_Second = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
110 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
111 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
112 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
113 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
114 bSafetyStop = false;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
115 safetyStopCountDown_Second = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
116 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
117 last_depth_meter = stateUsed->lifeData.depth_meter;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
118 }
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 void timer_Stopwatch_Restart(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
122 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
123 stopWatchAverageDepth_Meter = stateUsed->lifeData.depth_meter;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
124 bStopWatch = true;
311
ddbe8bed5096 bugfix: make stopwatch and divetime run in sync
Jan Mulder <jlmulder@xs4all.nl>
parents: 303
diff changeset
125 stopWatchOffset = stateUsed->lifeData.dive_time_seconds_without_surface_time;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
126 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
127
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
128 void timer_Stopwatch_Stop(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
129 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
130 bStopWatch = false;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
131 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
132
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
133 long timer_Stopwatch_GetTime(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
134 {
311
ddbe8bed5096 bugfix: make stopwatch and divetime run in sync
Jan Mulder <jlmulder@xs4all.nl>
parents: 303
diff changeset
135 return stateUsed->lifeData.dive_time_seconds_without_surface_time - stopWatchOffset;
38
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
136 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
137
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
138 float timer_Stopwatch_GetAvarageDepth_Meter(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
139 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
140 return stopWatchAverageDepth_Meter;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
141 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
142
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
143 long timer_Safetystop_GetCountDown(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
144 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
145 return safetyStopCountDown_Second;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
146 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
147
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
148 uint8_t timer_Safetystop_GetDepthUpperLimit(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
149 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
150 if(settingsGetPointer()->safetystopDepth == 3)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
151 return 2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
152 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
153 return 3;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
154 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
155