annotate Discovery/Src/timer.c @ 232:f0069f002c55 div-fixes-4-1

Bugfix: make date/time setting work over reboots Setting the time/date over the UART interface or by the menu, seems to work, but a reboot of the RTE brings back strange, seemingly random, time. The reason for this is rather simple. In the settings, a time is stored, based on some flawed logic, and that time was restored on reboot. There is no reason to store any time, when the moment of restoring it is unrelated in time. So, the fix is simple: do not set time (in the RTC) based on some time from the past. The whole idea of a RTC is that it does preserve the time for you, as long its powered. Any attempt to do things better using stored time data is futile (and nonsense). And while working on his, also kick out some useless code from the RTE. There is no reason to initialize the time on the RTC to some random time/date in the past. A zero data/time is as good and any random date. Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
author Jan Mulder <jlmulder@xs4all.nl>
date Wed, 03 Apr 2019 21:11:56 +0200
parents f11f0bf6ef2d
children 90e65971f15d
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 **/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
60 if(bStopWatch && stateUsed->lifeData.depth_meter > 1)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
61 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
62 if((stopWatchTime_Second == 0) && (stateUsed->lifeData.dive_time_seconds >= 1))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
63 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
64 stopWatchTime_Second = stateUsed->lifeData.dive_time_seconds - 1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
65 stopWatchAverageDepth_Meter = stateUsed->lifeData.average_depth_meter * (stopWatchTime_Second - 1) / stopWatchTime_Second;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
66 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
67 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
68 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
69 stopWatchAverageDepth_Meter = (stopWatchAverageDepth_Meter * stopWatchTime_Second + stateUsed->lifeData.depth_meter)/ (stopWatchTime_Second + 1);
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
70 stopWatchTime_Second++;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
71 }
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 /** SafetyStop **/
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
75 float depthToStopSafetyStopCount;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
76 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
77 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
78
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
79 //No deco when 10 meters are crossed from below => Activate SecurityStop
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
80 if( last_depth_meter > 10.0f && stateUsed->lifeData.depth_meter <= 10.0f)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
81 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
82 if(stateUsed->diveSettings.deco_type.ub.standard == GF_MODE)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
83 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
84 if(stateUsed->decolistBuehlmann.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 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
88 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
89 if(stateUsed->decolistVPM.output_ndl_seconds > 0)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
90 bSafetyStop = true;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
91 }
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 //Countdown starts at 5 meters
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
95 if(bSafetyStop && (stateUsed->lifeData.depth_meter - 0.0001f <= (settingsGetPointer()->safetystopDepth) ))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
96 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
97 if(safetyStopCountDown_Second == 0)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
98 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
99 safetyStopCountDown_Second = (settingsGetPointer()->safetystopDuration) * 60;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
100 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
101 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
102 safetyStopCountDown_Second--;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
103 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
104
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
105 // after safetystopDuration minutes or below 3 (2) meter safetyStop is disabled
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
106 if(settingsGetPointer()->safetystopDepth == 3)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
107 depthToStopSafetyStopCount = 1.999f; // instead of 2
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
108 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
109 depthToStopSafetyStopCount = 2.999f;// instead of 3
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
110
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
111 if((safetyStopCountDown_Second == 1) || (stateUsed->lifeData.depth_meter <= depthToStopSafetyStopCount))
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
112 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
113 bSafetyStop = false;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
114 safetyStopCountDown_Second = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
115 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
116 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
117 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
118 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
119 bSafetyStop = false;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
120 safetyStopCountDown_Second = 0;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
121 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
122 last_depth_meter = stateUsed->lifeData.depth_meter;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
123 }
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 void timer_Stopwatch_Restart(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
127 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
128 stopWatchTime_Second = 1;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
129 stopWatchAverageDepth_Meter = stateUsed->lifeData.depth_meter;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
130 bStopWatch = true;
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 void timer_Stopwatch_Stop(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
134 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
135 bStopWatch = false;
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 long timer_Stopwatch_GetTime(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
139 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
140 return stopWatchTime_Second;
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 float timer_Stopwatch_GetAvarageDepth_Meter(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
144 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
145 return stopWatchAverageDepth_Meter;
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 long timer_Safetystop_GetCountDown(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
149 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
150 return safetyStopCountDown_Second;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
151 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
152
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
153 uint8_t timer_Safetystop_GetDepthUpperLimit(void)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
154 {
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
155 if(settingsGetPointer()->safetystopDepth == 3)
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
156 return 2;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
157 else
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
158 return 3;
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
159 }
5f11787b4f42 include in ostc4 repository
heinrichsweikamp
parents:
diff changeset
160