annotate Discovery/Src/timer.c @ 91:8b0fadd413c6 kittz

First stable. With minimal button sensitivity fix(for broken buttons)
author Dmitry Romanov <kitt@bk.ru>
date Mon, 26 Nov 2018 10:13:02 +0300
parents 5f11787b4f42
children f11f0bf6ef2d
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
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
31 long stopWatchTime_Second = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
32 _Bool bStopWatch = false;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
33 float stopWatchAverageDepth_Meter = 0.0f;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
34 long safetyStopCountDown_Second = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
35 _Bool bSafetyStop = false;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
36
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
37 void timer_init(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
38 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
39 stopWatchTime_Second = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
40 stopWatchAverageDepth_Meter = 0.0f;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
41 bStopWatch = true;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
42 safetyStopCountDown_Second = 0;
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
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
46 void timer_UpdateSecond(_Bool checkOncePerSecond)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
47 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
48 static int last_second = -1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
49 static _Bool bSafetyStop = false;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
50 static float last_depth_meter = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
51 //static _Bool CountDownStarted = false;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
52 if(checkOncePerSecond)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
53 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
54 int now = current_second();
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
55 if( last_second == now)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
56 return;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
57 last_second = now;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
58 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
59
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
60 /** Stopwatch **/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
61 if(bStopWatch && stateUsed->lifeData.depth_meter > 1)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
62 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
63 if((stopWatchTime_Second == 0) && (stateUsed->lifeData.dive_time_seconds >= 1))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
64 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
65 stopWatchTime_Second = stateUsed->lifeData.dive_time_seconds - 1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
66 stopWatchAverageDepth_Meter = stateUsed->lifeData.average_depth_meter * (stopWatchTime_Second - 1) / stopWatchTime_Second;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
67 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
68 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
69 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
70 stopWatchAverageDepth_Meter = (stopWatchAverageDepth_Meter * stopWatchTime_Second + stateUsed->lifeData.depth_meter)/ (stopWatchTime_Second + 1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
71 stopWatchTime_Second++;
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
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
75 /** SafetyStop **/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
76 float depthToStopSafetyStopCount;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
77 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
78 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
79
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
80 //No deco when 10 meters are crossed from below => Activate SecurityStop
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
81 if( last_depth_meter > 10.0f && stateUsed->lifeData.depth_meter <= 10.0f)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
82 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
83 if(stateUsed->diveSettings.deco_type.ub.standard == GF_MODE)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
84 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
85 if(stateUsed->decolistBuehlmann.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 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
89 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
90 if(stateUsed->decolistVPM.output_ndl_seconds > 0)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
91 bSafetyStop = true;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
92 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
93 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
94
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
95 //Countdown starts at 5 meters
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
96 if(bSafetyStop && (stateUsed->lifeData.depth_meter - 0.0001f <= (settingsGetPointer()->safetystopDepth) ))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
97 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
98 if(safetyStopCountDown_Second == 0)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
99 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
100 safetyStopCountDown_Second = (settingsGetPointer()->safetystopDuration) * 60;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
101 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
102 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
103 safetyStopCountDown_Second--;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
104 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
105
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
106 // after safetystopDuration minutes or below 3 (2) meter safetyStop is disabled
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
107 if(settingsGetPointer()->safetystopDepth == 3)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
108 depthToStopSafetyStopCount = 1.999f; // instead of 2
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
109 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
110 depthToStopSafetyStopCount = 2.999f;// instead of 3
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
111
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
112 if((safetyStopCountDown_Second == 1) || (stateUsed->lifeData.depth_meter <= depthToStopSafetyStopCount))
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 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
118 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
119 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
120 bSafetyStop = false;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
121 safetyStopCountDown_Second = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
122 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
123 last_depth_meter = stateUsed->lifeData.depth_meter;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
124 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
125
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
126
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
127 void timer_Stopwatch_Restart(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
128 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
129 stopWatchTime_Second = 1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
130 stopWatchAverageDepth_Meter = stateUsed->lifeData.depth_meter;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
131 bStopWatch = true;
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 void timer_Stopwatch_Stop(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
135 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
136 bStopWatch = false;
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 long timer_Stopwatch_GetTime(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
140 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
141 return stopWatchTime_Second;
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 float timer_Stopwatch_GetAvarageDepth_Meter(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
145 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
146 return stopWatchAverageDepth_Meter;
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 long timer_Safetystop_GetCountDown(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
150 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
151 return safetyStopCountDown_Second;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
152 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
153
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
154 uint8_t timer_Safetystop_GetDepthUpperLimit(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
155 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
156 if(settingsGetPointer()->safetystopDepth == 3)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
157 return 2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
158 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
159 return 3;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
160 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
161