comparison Discovery/Src/timer.c @ 38:5f11787b4f42

include in ostc4 repository
author heinrichsweikamp
date Sat, 28 Apr 2018 11:52:34 +0200
parents
children f11f0bf6ef2d
comparison
equal deleted inserted replaced
37:ccc45c0e1ea2 38:5f11787b4f42
1 ///////////////////////////////////////////////////////////////////////////////
2 /// -*- coding: UTF-8 -*-
3 ///
4 /// \file Discovery/Src/timer.c
5 /// \brief Contains timer related functionality like stopwatch and security stop
6 /// \author Peter Ryser & heinrichs weikamp gmbh
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
31 long stopWatchTime_Second = 0;
32 _Bool bStopWatch = false;
33 float stopWatchAverageDepth_Meter = 0.0f;
34 long safetyStopCountDown_Second = 0;
35 _Bool bSafetyStop = false;
36
37 void timer_init(void)
38 {
39 stopWatchTime_Second = 0;
40 stopWatchAverageDepth_Meter = 0.0f;
41 bStopWatch = true;
42 safetyStopCountDown_Second = 0;
43
44 }
45
46 void timer_UpdateSecond(_Bool checkOncePerSecond)
47 {
48 static int last_second = -1;
49 static _Bool bSafetyStop = false;
50 static float last_depth_meter = 0;
51 //static _Bool CountDownStarted = false;
52 if(checkOncePerSecond)
53 {
54 int now = current_second();
55 if( last_second == now)
56 return;
57 last_second = now;
58 }
59
60 /** Stopwatch **/
61 if(bStopWatch && stateUsed->lifeData.depth_meter > 1)
62 {
63 if((stopWatchTime_Second == 0) && (stateUsed->lifeData.dive_time_seconds >= 1))
64 {
65 stopWatchTime_Second = stateUsed->lifeData.dive_time_seconds - 1;
66 stopWatchAverageDepth_Meter = stateUsed->lifeData.average_depth_meter * (stopWatchTime_Second - 1) / stopWatchTime_Second;
67 }
68 else
69 {
70 stopWatchAverageDepth_Meter = (stopWatchAverageDepth_Meter * stopWatchTime_Second + stateUsed->lifeData.depth_meter)/ (stopWatchTime_Second + 1);
71 stopWatchTime_Second++;
72 }
73 }
74
75 /** SafetyStop **/
76 float depthToStopSafetyStopCount;
77 if(settingsGetPointer()->safetystopDuration && (stateUsed->lifeData.max_depth_meter > 10.0f) && (stateUsed->lifeData.dive_time_seconds > 60))
78 {
79
80 //No deco when 10 meters are crossed from below => Activate SecurityStop
81 if( last_depth_meter > 10.0f && stateUsed->lifeData.depth_meter <= 10.0f)
82 {
83 if(stateUsed->diveSettings.deco_type.ub.standard == GF_MODE)
84 {
85 if(stateUsed->decolistBuehlmann.output_ndl_seconds > 0)
86 bSafetyStop = true;
87 }
88 else
89 {
90 if(stateUsed->decolistVPM.output_ndl_seconds > 0)
91 bSafetyStop = true;
92 }
93 }
94
95 //Countdown starts at 5 meters
96 if(bSafetyStop && (stateUsed->lifeData.depth_meter - 0.0001f <= (settingsGetPointer()->safetystopDepth) ))
97 {
98 if(safetyStopCountDown_Second == 0)
99 {
100 safetyStopCountDown_Second = (settingsGetPointer()->safetystopDuration) * 60;
101 }
102 else
103 safetyStopCountDown_Second--;
104 }
105
106 // after safetystopDuration minutes or below 3 (2) meter safetyStop is disabled
107 if(settingsGetPointer()->safetystopDepth == 3)
108 depthToStopSafetyStopCount = 1.999f; // instead of 2
109 else
110 depthToStopSafetyStopCount = 2.999f;// instead of 3
111
112 if((safetyStopCountDown_Second == 1) || (stateUsed->lifeData.depth_meter <= depthToStopSafetyStopCount))
113 {
114 bSafetyStop = false;
115 safetyStopCountDown_Second = 0;
116 }
117 }
118 else
119 {
120 bSafetyStop = false;
121 safetyStopCountDown_Second = 0;
122 }
123 last_depth_meter = stateUsed->lifeData.depth_meter;
124 }
125
126
127 void timer_Stopwatch_Restart(void)
128 {
129 stopWatchTime_Second = 1;
130 stopWatchAverageDepth_Meter = stateUsed->lifeData.depth_meter;
131 bStopWatch = true;
132 }
133
134 void timer_Stopwatch_Stop(void)
135 {
136 bStopWatch = false;
137 }
138
139 long timer_Stopwatch_GetTime(void)
140 {
141 return stopWatchTime_Second;
142 }
143
144 float timer_Stopwatch_GetAvarageDepth_Meter(void)
145 {
146 return stopWatchAverageDepth_Meter;
147 }
148
149 long timer_Safetystop_GetCountDown(void)
150 {
151 return safetyStopCountDown_Second;
152 }
153
154 uint8_t timer_Safetystop_GetDepthUpperLimit(void)
155 {
156 if(settingsGetPointer()->safetystopDepth == 3)
157 return 2;
158 else
159 return 3;
160 }
161