comparison Discovery/Src/simulation.c @ 308:1203255481e4 cleanup-4

cleanup: introduce function setAvgDepth The simulator and the realtime code shared a literally identical piece of code to compute a running depth average. This is simply poor coding style, so factor this out and create a function to do this work. Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
author Jan Mulder <jlmulder@xs4all.nl>
date Fri, 24 May 2019 09:02:46 +0200
parents b6436edfb2c0
children f1257a32f2d4
comparison
equal deleted inserted replaced
307:b6436edfb2c0 308:1203255481e4
35 #include "simulation.h" 35 #include "simulation.h"
36 36
37 #include "decom.h" 37 #include "decom.h"
38 #include "calc_crush.h" 38 #include "calc_crush.h"
39 #include "data_exchange.h" 39 #include "data_exchange.h"
40 #include "data_exchange_main.h"
40 #include "timer.h" 41 #include "timer.h"
41 #include "check_warning.h" 42 #include "check_warning.h"
42 #include "vpm.h" 43 #include "vpm.h"
43 #include "buehlmann.h" 44 #include "buehlmann.h"
44 #include "logbook_miniLive.h" 45 #include "logbook_miniLive.h"
202 pDiveState->lifeData.max_depth_meter = 0; 203 pDiveState->lifeData.max_depth_meter = 0;
203 pDiveState->lifeData.boolResetAverageDepth = 1; 204 pDiveState->lifeData.boolResetAverageDepth = 1;
204 } 205 }
205 } 206 }
206 207
207 /* average depth 208 setAvgDepth(pDiveState);
208 */
209 float *AvgDepthValue = &pDiveState->lifeData.average_depth_meter;
210 float DepthNow = pDiveState->lifeData.depth_meter;
211 uint32_t *AvgDepthCount = &pDiveState->lifeData.internal.average_depth_meter_Count;
212 uint32_t *AvgDepthTimer = &pDiveState->lifeData.internal.average_depth_last_update_dive_time_seconds_without_surface_time;
213 uint32_t AvgSecondsSinceLast;
214 uint32_t DiveTime = pDiveState->lifeData.dive_time_seconds_without_surface_time;
215
216 if(pDiveState->lifeData.boolResetAverageDepth)
217 {
218 *AvgDepthValue = DepthNow;
219 *AvgDepthCount = 1;
220 *AvgDepthTimer = DiveTime;
221 pDiveState->lifeData.boolResetAverageDepth = 0;
222 }
223 else if (DiveTime > *AvgDepthTimer)
224 {
225 AvgSecondsSinceLast = DiveTime - *AvgDepthTimer;
226 for(int i=0;i<AvgSecondsSinceLast;i++)
227 {
228 *AvgDepthValue = (*AvgDepthValue * *AvgDepthCount + DepthNow) / (*AvgDepthCount + 1);
229 *AvgDepthCount += 1;
230 }
231 *AvgDepthTimer = DiveTime;
232 }
233 if(*AvgDepthCount == 0)
234 *AvgDepthValue = 0;
235 209
236 /* Exposure Tissues 210 /* Exposure Tissues
237 */ 211 */
238 decom_tissues_exposure(1, &pDiveState->lifeData); 212 decom_tissues_exposure(1, &pDiveState->lifeData);
239 decom_oxygen_calculate_cns_exposure(1, &pDiveState->lifeData.actualGas, pDiveState->lifeData.pressure_ambient_bar, &pDiveState->lifeData.cns); 213 decom_oxygen_calculate_cns_exposure(1, &pDiveState->lifeData.actualGas, pDiveState->lifeData.pressure_ambient_bar, &pDiveState->lifeData.cns);