Mercurial > public > ostc4
annotate Discovery/Src/data_central.c @ 310:95928ef3986f cleanup-4
Make dive mode detection more advanced
In commit a09b1855d656, a RTE function was factored out, that was
used to detect dive mode vs. surface mode.
In the hunt for the time difference bug between stopwatch and dive time
some progress was made, but its still not totally right. Add some old
logic back, that seems reasonable.
A pool test after this commit shows some improvement, but still there
is some random difference between stopwatch and dive time. Things like
perfectly in sync for 20 min. surface shortly, and descend again in
the same dive. Now, the was a 1 or 2 second difference.
Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
author | Jan Mulder <jlmulder@xs4all.nl> |
---|---|
date | Sun, 26 May 2019 10:09:22 +0200 |
parents | eba8d1eb5bef |
children | d784f281833a |
rev | line source |
---|---|
38 | 1 /** |
2 ****************************************************************************** | |
3 * @copyright heinrichs weikamp | |
4 * @file data_central.c | |
5 * @author heinrichs weikamp gmbh | |
6 * @date 10-November-2014 | |
7 * @version V1.0.2 | |
8 * @since 10-Nov-2014 | |
9 * @brief All the data EXCEPT | |
10 * - settings (settings.c) | |
11 * feste Werte, die nur an der Oberfl�che ge�ndert werden | |
12 * - dataIn and dataOut (data_exchange.h and data_exchange_main.c) | |
13 * Austausch mit Small CPU | |
14 * @bug | |
15 * @warning | |
16 @verbatim | |
17 ============================================================================== | |
18 ##### SDiveState Real and Sim ##### | |
19 ============================================================================== | |
20 [..] SDiveSettings | |
21 copy of parts of Settings that are necessary during the dive | |
22 and could be modified during the dive without post dive changes. | |
23 | |
24 [..] SLifeData | |
25 written in DataEX_copy_to_LifeData(); | |
26 block 1 "lifedata" set by SmallCPU in stateReal | |
27 block 2 "actualGas" set by main CPU from user input and send to Small CPU | |
28 block 3 "calculated data" set by main CPU based on "lifedata" | |
29 | |
30 [..] SVpm | |
31 | |
32 [..] SEvents | |
33 | |
34 [..] SDecoinfo | |
35 | |
36 [..] mode | |
37 set by SmallCPU in stateReal, can be surface, dive, ... | |
38 | |
39 [..] data_old__lost_connection_to_slave | |
40 set by DataEX_copy_to_LifeData(); | |
41 | |
42 ============================================================================== | |
43 ##### SDiveState Deco ##### | |
44 ============================================================================== | |
45 [..] kjbkldafj�lasdfjasdf | |
46 | |
47 ============================================================================== | |
48 ##### decoLock ##### | |
49 ============================================================================== | |
50 [..] The handler that synchronizes the data between IRQ copy and main deco loop | |
51 | |
52 | |
53 @endverbatim | |
54 ****************************************************************************** | |
55 * @attention | |
56 * | |
57 * <h2><center>© COPYRIGHT(c) 2015 heinrichs weikamp</center></h2> | |
58 * | |
59 ****************************************************************************** | |
60 */ | |
61 | |
62 /* Includes ------------------------------------------------------------------*/ | |
63 #include <string.h> | |
64 #include "data_central.h" | |
65 #include "calc_crush.h" | |
66 #include "decom.h" | |
67 #include "stm32f4xx_hal.h" | |
68 #include "settings.h" | |
69 #include "data_exchange_main.h" | |
70 #include "ostc.h" // for button adjust on hw testboard 1 | |
71 #include "tCCR.h" | |
72 #include "crcmodel.h" | |
73 | |
225
2bb1db22b5f5
cleanup: random set of cleanups
Jan Mulder <jlmulder@xs4all.nl>
parents:
149
diff
changeset
|
74 static SDiveState stateReal = { 0 }; |
38 | 75 SDiveState stateSim = { 0 }; |
76 SDiveState stateDeco = { 0 }; | |
77 | |
300
5ca177d2df5d
cleanup: remove commented/unused code, make static
Jan Mulder <jlmulder@xs4all.nl>
parents:
281
diff
changeset
|
78 static SDevice stateDevice = |
38 | 79 { |
80 /* max is 0x7FFFFFFF, min is 0x80000000 but also defined in stdint.h :-) */ | |
81 | |
82 /* count, use 0 */ | |
83 .batteryChargeCompleteCycles.value_int32 = 0, | |
84 .batteryChargeCycles.value_int32 = 0, | |
85 .diveCycles.value_int32 = 0, | |
86 .hoursOfOperation.value_int32 = 0, | |
87 | |
88 /* max values, use min. */ | |
89 .temperatureMaximum.value_int32 = INT32_MIN, | |
90 .depthMaximum.value_int32 = INT32_MIN, | |
91 | |
92 /* min values, use max. */ | |
93 .temperatureMinimum.value_int32 = INT32_MAX, | |
94 .voltageMinimum.value_int32 = INT32_MAX, | |
95 }; | |
96 | |
300
5ca177d2df5d
cleanup: remove commented/unused code, make static
Jan Mulder <jlmulder@xs4all.nl>
parents:
281
diff
changeset
|
97 static SVpmRepetitiveData stateVPM = |
38 | 98 { |
99 .repetitive_variables_not_valid = 1, | |
100 .is_data_from_RTE_CPU = 0, | |
101 }; | |
102 | |
271
1303747b5ba2
cleanup: also write gas and setpoint changes in simulator mode
Jan Mulder <jlmulder@xs4all.nl>
parents:
270
diff
changeset
|
103 const SDiveState *stateUsed = &stateReal; |
1303747b5ba2
cleanup: also write gas and setpoint changes in simulator mode
Jan Mulder <jlmulder@xs4all.nl>
parents:
270
diff
changeset
|
104 SDiveState *stateUsedWrite = &stateReal; |
38 | 105 |
106 void set_stateUsedToReal(void) | |
107 { | |
271
1303747b5ba2
cleanup: also write gas and setpoint changes in simulator mode
Jan Mulder <jlmulder@xs4all.nl>
parents:
270
diff
changeset
|
108 stateUsed = stateUsedWrite = &stateReal; |
38 | 109 } |
110 | |
111 void set_stateUsedToSim(void) | |
112 { | |
271
1303747b5ba2
cleanup: also write gas and setpoint changes in simulator mode
Jan Mulder <jlmulder@xs4all.nl>
parents:
270
diff
changeset
|
113 stateUsed = stateUsedWrite = &stateSim; |
38 | 114 } |
115 | |
116 _Bool is_stateUsedSetToSim(void) | |
117 { | |
225
2bb1db22b5f5
cleanup: random set of cleanups
Jan Mulder <jlmulder@xs4all.nl>
parents:
149
diff
changeset
|
118 return stateUsed == &stateSim; |
38 | 119 } |
120 | |
121 const SDiveState * stateRealGetPointer(void) | |
122 { | |
123 return &stateReal; | |
124 } | |
125 | |
126 SDiveState * stateRealGetPointerWrite(void) | |
127 { | |
128 return &stateReal; | |
129 } | |
130 | |
131 | |
132 const SDiveState * stateSimGetPointer(void) | |
133 { | |
134 return &stateSim; | |
135 } | |
136 | |
137 | |
138 SDiveState * stateSimGetPointerWrite(void) | |
139 { | |
140 return &stateSim; | |
141 } | |
142 | |
143 | |
144 const SDevice * stateDeviceGetPointer(void) | |
145 { | |
146 return &stateDevice; | |
147 } | |
148 | |
149 | |
150 SDevice * stateDeviceGetPointerWrite(void) | |
151 { | |
152 return &stateDevice; | |
153 } | |
154 | |
155 | |
156 const SVpmRepetitiveData * stateVpmRepetitiveDataGetPointer(void) | |
157 { | |
158 return &stateVPM; | |
159 } | |
160 | |
161 | |
162 SVpmRepetitiveData * stateVpmRepetitiveDataGetPointerWrite(void) | |
163 { | |
164 return &stateVPM; | |
165 } | |
166 | |
167 | |
168 uint32_t time_elapsed_ms(uint32_t ticksstart,uint32_t ticksnow) | |
169 { | |
170 if(ticksstart <= ticksnow) | |
171 return ticksnow - ticksstart; | |
172 else | |
173 return 0xFFFFFFFF - ticksstart + ticksnow; | |
174 } | |
175 | |
176 | |
177 uint8_t decoLock = DECO_CALC_undefined; | |
300
5ca177d2df5d
cleanup: remove commented/unused code, make static
Jan Mulder <jlmulder@xs4all.nl>
parents:
281
diff
changeset
|
178 |
5ca177d2df5d
cleanup: remove commented/unused code, make static
Jan Mulder <jlmulder@xs4all.nl>
parents:
281
diff
changeset
|
179 static int descent_rate_meter_per_min = 20; |
5ca177d2df5d
cleanup: remove commented/unused code, make static
Jan Mulder <jlmulder@xs4all.nl>
parents:
281
diff
changeset
|
180 static int max_depth = 70; |
5ca177d2df5d
cleanup: remove commented/unused code, make static
Jan Mulder <jlmulder@xs4all.nl>
parents:
281
diff
changeset
|
181 static int bottom_time = 10; |
38 | 182 |
183 _Bool vpm_crush(SDiveState* pDiveState); | |
184 void setSimulationValues(int _ascent_rate_meter_per_min, int _descent_rate_meter_per_min, int _max_depth, int _bottom_time ) | |
185 { | |
186 descent_rate_meter_per_min = _descent_rate_meter_per_min; | |
187 max_depth = _max_depth; | |
188 bottom_time = _bottom_time; | |
189 } | |
190 | |
191 int current_second(void) { | |
192 | |
193 return HAL_GetTick() / 1000; | |
194 } | |
195 | |
196 #define OXY_ONE_SIXTIETH_PART 0.0166667f | |
197 | |
198 uint8_t calc_MOD(uint8_t gasId) | |
199 { | |
200 int16_t oxygen, maxppO2, result; | |
201 SSettings *pSettings; | |
202 | |
203 pSettings = settingsGetPointer(); | |
204 | |
205 oxygen = (int16_t)(pSettings->gas[gasId].oxygen_percentage); | |
206 | |
207 if(pSettings->gas[gasId].note.ub.deco > 0) | |
208 maxppO2 =(int16_t)(pSettings->ppO2_max_deco); | |
209 else | |
210 maxppO2 =(int16_t)(pSettings->ppO2_max_std); | |
211 | |
212 result = 10 * maxppO2; | |
213 result /= oxygen; | |
214 result -= 10; | |
215 | |
216 if(result < 0) | |
217 return 0; | |
218 | |
219 if(result > 255) | |
220 return 255; | |
221 | |
222 return result; | |
223 } | |
224 | |
225 float get_ambiant_pressure_simulation(long dive_time_seconds, float surface_pressure_bar ) | |
226 { | |
227 static | |
228 long descent_time; | |
229 float depth_meter; | |
230 | |
231 descent_time = 60 * max_depth / descent_rate_meter_per_min; | |
232 | |
233 if(dive_time_seconds <= descent_time) | |
234 { | |
235 depth_meter = ((float)(dive_time_seconds * descent_rate_meter_per_min)) / 60; | |
236 return surface_pressure_bar + depth_meter / 10; | |
237 } | |
238 //else if(dive_time_seconds <= (descent_time + bottom_time * 60)) | |
239 return surface_pressure_bar + max_depth / 10; | |
240 | |
241 | |
242 | |
243 } | |
244 | |
245 void UpdateLifeDataTest(SDiveState * pDiveState) | |
246 { | |
247 static int last_second = -1; | |
248 int now = current_second(); | |
249 if(last_second == now) | |
250 return; | |
251 last_second = now; | |
252 | |
253 pDiveState->lifeData.dive_time_seconds += 1; | |
254 pDiveState->lifeData.pressure_ambient_bar = get_ambiant_pressure_simulation(pDiveState->lifeData.dive_time_seconds,pDiveState->lifeData.pressure_surface_bar); | |
255 | |
256 pDiveState->lifeData.depth_meter = (pDiveState->lifeData.pressure_ambient_bar - pDiveState->lifeData.pressure_surface_bar) * 10.0f; | |
257 if(pDiveState->lifeData.max_depth_meter < pDiveState->lifeData.depth_meter) | |
258 pDiveState->lifeData.max_depth_meter = pDiveState->lifeData.depth_meter; | |
259 decom_tissues_exposure(1, &pDiveState->lifeData); | |
260 pDiveState->lifeData.ppO2 = decom_calc_ppO2( pDiveState->lifeData.pressure_ambient_bar, &pDiveState->lifeData.actualGas); | |
261 decom_oxygen_calculate_cns(& pDiveState->lifeData.cns, pDiveState->lifeData.ppO2); | |
262 | |
263 vpm_crush(pDiveState); | |
264 } | |
265 | |
266 | |
267 _Bool vpm_crush(SDiveState* pDiveState) | |
268 { | |
269 int i = 0; | |
270 static float starting_ambient_pressure = 0; | |
271 static float ending_ambient_pressure = 0; | |
272 static float time_calc_begin = -1; | |
273 static float initial_helium_pressure[16]; | |
274 static float initial_nitrogen_pressure[16]; | |
275 ending_ambient_pressure = pDiveState->lifeData.pressure_ambient_bar * 10; | |
276 | |
277 if((pDiveState->lifeData.dive_time_seconds <= 4) || (starting_ambient_pressure >= ending_ambient_pressure)) | |
278 { | |
279 time_calc_begin = pDiveState->lifeData.dive_time_seconds; | |
280 starting_ambient_pressure = pDiveState->lifeData.pressure_ambient_bar * 10; | |
281 for( i = 0; i < 16; i++) | |
282 { | |
283 initial_helium_pressure[i] = pDiveState->lifeData.tissue_helium_bar[i] * 10; | |
284 initial_nitrogen_pressure[i] = pDiveState->lifeData.tissue_nitrogen_bar[i] * 10; | |
285 } | |
286 return false; | |
287 } | |
288 if(pDiveState->lifeData.dive_time_seconds - time_calc_begin >= 4) | |
289 { | |
290 if(ending_ambient_pressure > starting_ambient_pressure + 0.5f) | |
291 { | |
292 float rate = (ending_ambient_pressure - starting_ambient_pressure) * 60 / 4; | |
293 calc_crushing_pressure(&pDiveState->lifeData, &pDiveState->vpm, initial_helium_pressure, initial_nitrogen_pressure, starting_ambient_pressure, rate); | |
294 | |
295 time_calc_begin = pDiveState->lifeData.dive_time_seconds; | |
296 starting_ambient_pressure = pDiveState->lifeData.pressure_ambient_bar * 10; | |
297 for( i = 0; i < 16; i++) | |
298 { | |
299 initial_helium_pressure[i] = pDiveState->lifeData.tissue_helium_bar[i] * 10; | |
300 initial_nitrogen_pressure[i] = pDiveState->lifeData.tissue_nitrogen_bar[i] * 10; | |
301 } | |
302 | |
303 return true; | |
304 } | |
305 | |
306 } | |
307 return false; | |
308 }; | |
309 | |
310 | |
311 void createDiveSettings(void) | |
312 { | |
313 SSettings* pSettings = settingsGetPointer(); | |
314 | |
315 setActualGasFirst(&stateReal.lifeData); | |
316 | |
317 stateReal.diveSettings.compassHeading = pSettings->compassBearing; | |
318 stateReal.diveSettings.ascentRate_meterperminute = 10; | |
319 | |
320 stateReal.diveSettings.diveMode = pSettings->dive_mode; | |
321 stateReal.diveSettings.CCR_Mode = pSettings->CCR_Mode; | |
322 if(stateReal.diveSettings.diveMode == DIVEMODE_CCR) | |
323 stateReal.diveSettings.ccrOption = 1; | |
324 else | |
325 stateReal.diveSettings.ccrOption = 0; | |
326 memcpy(stateReal.diveSettings.gas, pSettings->gas,sizeof(pSettings->gas)); | |
327 memcpy(stateReal.diveSettings.setpoint, pSettings->setpoint,sizeof(pSettings->setpoint)); | |
328 stateReal.diveSettings.gf_high = pSettings->GF_high; | |
329 stateReal.diveSettings.gf_low = pSettings->GF_low; | |
330 stateReal.diveSettings.input_next_stop_increment_depth_bar = ((float)pSettings->stop_increment_depth_meter) / 10.0f; | |
331 stateReal.diveSettings.last_stop_depth_bar = ((float)pSettings->last_stop_depth_meter) / 10.0f; | |
332 stateReal.diveSettings.vpm_conservatism = pSettings->VPM_conservatism.ub.standard; | |
333 stateReal.diveSettings.deco_type.uw = pSettings->deco_type.uw; | |
334 stateReal.diveSettings.fallbackOption = pSettings->fallbackToFixedSetpoint; | |
335 stateReal.diveSettings.ppo2sensors_deactivated = pSettings->ppo2sensors_deactivated; | |
336 stateReal.diveSettings.future_TTS_minutes = pSettings->future_TTS; | |
337 | |
338 decom_CreateGasChangeList(&stateReal.diveSettings, &stateReal.lifeData); // decogaslist | |
339 stateReal.diveSettings.internal__pressure_first_stop_ambient_bar_as_upper_limit_for_gf_low_otherwise_zero = 0; | |
340 | |
341 /* for safety */ | |
342 stateReal.diveSettings.input_second_to_last_stop_depth_bar = stateReal.diveSettings.last_stop_depth_bar + stateReal.diveSettings.input_next_stop_increment_depth_bar; | |
343 /* and the proper calc */ | |
344 for(int i = 1; i <10; i++) | |
345 { | |
346 if(stateReal.diveSettings.input_next_stop_increment_depth_bar * i > stateReal.diveSettings.last_stop_depth_bar) | |
347 { | |
348 stateReal.diveSettings.input_second_to_last_stop_depth_bar = stateReal.diveSettings.input_next_stop_increment_depth_bar * i; | |
349 break; | |
350 } | |
351 } | |
352 } | |
353 | |
354 | |
355 void copyDiveSettingsToSim(void) | |
356 { | |
357 memcpy(&stateSim, &stateReal, sizeof(stateReal)); | |
358 } | |
359 | |
360 | |
361 void copyVpmRepetetiveDataToSim(void) | |
362 { | |
363 SDiveState * pSimData = stateSimGetPointerWrite(); | |
364 const SVpmRepetitiveData * pVpmData = stateVpmRepetitiveDataGetPointer(); | |
365 | |
366 if(pVpmData->is_data_from_RTE_CPU) | |
367 { | |
368 for(int i=0; i<16;i++) | |
369 { | |
370 pSimData->vpm.adjusted_critical_radius_he[i] = pVpmData->adjusted_critical_radius_he[i]; | |
371 pSimData->vpm.adjusted_critical_radius_n2[i] = pVpmData->adjusted_critical_radius_n2[i]; | |
372 | |
373 pSimData->vpm.adjusted_crushing_pressure_he[i] = pVpmData->adjusted_crushing_pressure_he[i]; | |
374 pSimData->vpm.adjusted_crushing_pressure_n2[i] = pVpmData->adjusted_crushing_pressure_n2[i]; | |
375 | |
376 pSimData->vpm.initial_allowable_gradient_he[i] = pVpmData->initial_allowable_gradient_he[i]; | |
377 pSimData->vpm.initial_allowable_gradient_n2[i] = pVpmData->initial_allowable_gradient_n2[i]; | |
378 | |
379 pSimData->vpm.max_actual_gradient[i] = pVpmData->max_actual_gradient[i]; | |
380 } | |
381 pSimData->vpm.repetitive_variables_not_valid = pVpmData->repetitive_variables_not_valid; | |
382 } | |
383 } | |
384 | |
385 | |
386 void updateSetpointStateUsed(void) | |
387 { | |
388 if(stateUsed->diveSettings.diveMode != DIVEMODE_CCR) | |
389 { | |
271
1303747b5ba2
cleanup: also write gas and setpoint changes in simulator mode
Jan Mulder <jlmulder@xs4all.nl>
parents:
270
diff
changeset
|
390 stateUsedWrite->lifeData.actualGas.setPoint_cbar = 0; |
1303747b5ba2
cleanup: also write gas and setpoint changes in simulator mode
Jan Mulder <jlmulder@xs4all.nl>
parents:
270
diff
changeset
|
391 stateUsedWrite->lifeData.ppO2 = decom_calc_ppO2(stateUsed->lifeData.pressure_ambient_bar, &stateUsed->lifeData.actualGas); |
38 | 392 } |
393 else | |
394 { | |
395 if(stateUsed->diveSettings.CCR_Mode == CCRMODE_Sensors) | |
396 { | |
271
1303747b5ba2
cleanup: also write gas and setpoint changes in simulator mode
Jan Mulder <jlmulder@xs4all.nl>
parents:
270
diff
changeset
|
397 stateUsedWrite->lifeData.actualGas.setPoint_cbar = get_ppO2SensorWeightedResult_cbar(); |
38 | 398 } |
399 | |
400 if((stateUsed->lifeData.pressure_ambient_bar * 100) < stateUsed->lifeData.actualGas.setPoint_cbar) | |
271
1303747b5ba2
cleanup: also write gas and setpoint changes in simulator mode
Jan Mulder <jlmulder@xs4all.nl>
parents:
270
diff
changeset
|
401 stateUsedWrite->lifeData.ppO2 = stateUsed->lifeData.pressure_ambient_bar; |
38 | 402 else |
271
1303747b5ba2
cleanup: also write gas and setpoint changes in simulator mode
Jan Mulder <jlmulder@xs4all.nl>
parents:
270
diff
changeset
|
403 stateUsedWrite->lifeData.ppO2 = ((float)stateUsed->lifeData.actualGas.setPoint_cbar) / 100; |
38 | 404 } |
405 } | |
406 | |
407 void setActualGasFirst(SLifeData *lifeData) | |
408 { | |
409 SSettings* pSettings = settingsGetPointer(); | |
410 uint8_t start = 0; | |
411 uint8_t gasId = 0; | |
412 uint8_t setpoint_cbar = 0; | |
413 | |
414 if(pSettings->dive_mode == DIVEMODE_CCR) | |
415 { | |
416 setpoint_cbar = pSettings->setpoint[1].setpoint_cbar; | |
417 start = NUM_OFFSET_DILUENT+1; | |
418 } | |
419 else | |
420 { | |
421 setpoint_cbar = 0; | |
422 start = 1; | |
423 } | |
424 | |
425 gasId = start; | |
426 for(int i=start;i<=NUM_GASES+start;i++) | |
427 { | |
428 if(pSettings->gas[i].note.ub.first) | |
429 { | |
430 gasId = i; | |
431 break; | |
432 } | |
433 } | |
434 setActualGas(lifeData, gasId, setpoint_cbar); | |
435 } | |
436 | |
437 void setActualGasAir(SLifeData *lifeData) | |
438 { | |
439 uint8_t nitrogen; | |
440 nitrogen = 79; | |
441 lifeData->actualGas.GasIdInSettings = 0; | |
442 lifeData->actualGas.nitrogen_percentage = nitrogen; | |
443 lifeData->actualGas.helium_percentage =0; | |
444 lifeData->actualGas.setPoint_cbar = 0; | |
445 lifeData->actualGas.change_during_ascent_depth_meter_otherwise_zero = 0; | |
446 } | |
447 | |
448 | |
449 void setActualGas(SLifeData *lifeData, uint8_t gasId, uint8_t setpoint_cbar) | |
450 { | |
451 SSettings* pSettings = settingsGetPointer(); | |
452 uint8_t nitrogen; | |
453 | |
454 nitrogen = 100; | |
455 nitrogen -= pSettings->gas[gasId].oxygen_percentage; | |
456 nitrogen -= pSettings->gas[gasId].helium_percentage; | |
457 | |
458 lifeData->actualGas.GasIdInSettings = gasId; | |
459 lifeData->actualGas.nitrogen_percentage = nitrogen; | |
460 lifeData->actualGas.helium_percentage = pSettings->gas[gasId].helium_percentage; | |
461 lifeData->actualGas.setPoint_cbar = setpoint_cbar; | |
462 lifeData->actualGas.change_during_ascent_depth_meter_otherwise_zero = 0; | |
463 | |
464 if((pSettings->dive_mode == DIVEMODE_CCR) && (gasId > NUM_OFFSET_DILUENT)) | |
465 lifeData->lastDiluent_GasIdInSettings = gasId; | |
466 } | |
467 | |
468 | |
469 void setActualGas_DM(SLifeData *lifeData, uint8_t gasId, uint8_t setpoint_cbar) | |
470 { | |
471 if(stateUsed->diveSettings.ccrOption && gasId < 6) | |
472 { | |
473 if(lifeData->actualGas.GasIdInSettings != gasId) | |
474 { | |
475 SSettings* pSettings = settingsGetPointer(); | |
271
1303747b5ba2
cleanup: also write gas and setpoint changes in simulator mode
Jan Mulder <jlmulder@xs4all.nl>
parents:
270
diff
changeset
|
476 stateUsedWrite->events.bailout = 1; |
1303747b5ba2
cleanup: also write gas and setpoint changes in simulator mode
Jan Mulder <jlmulder@xs4all.nl>
parents:
270
diff
changeset
|
477 stateUsedWrite->events.info_bailoutO2 = pSettings->gas[gasId].oxygen_percentage; |
1303747b5ba2
cleanup: also write gas and setpoint changes in simulator mode
Jan Mulder <jlmulder@xs4all.nl>
parents:
270
diff
changeset
|
478 stateUsedWrite->events.info_bailoutHe = pSettings->gas[gasId].helium_percentage; |
38 | 479 } |
480 } | |
481 else | |
482 { | |
483 if(lifeData->actualGas.GasIdInSettings != gasId) | |
484 { | |
271
1303747b5ba2
cleanup: also write gas and setpoint changes in simulator mode
Jan Mulder <jlmulder@xs4all.nl>
parents:
270
diff
changeset
|
485 stateUsedWrite->events.gasChange = 1; |
1303747b5ba2
cleanup: also write gas and setpoint changes in simulator mode
Jan Mulder <jlmulder@xs4all.nl>
parents:
270
diff
changeset
|
486 stateUsedWrite->events.info_GasChange = gasId; |
38 | 487 } |
488 if( lifeData->actualGas.setPoint_cbar != setpoint_cbar) | |
489 { | |
490 // setPoint_cbar = 255 -> change to sensor mode | |
271
1303747b5ba2
cleanup: also write gas and setpoint changes in simulator mode
Jan Mulder <jlmulder@xs4all.nl>
parents:
270
diff
changeset
|
491 stateUsedWrite->events.setpointChange = 1; |
1303747b5ba2
cleanup: also write gas and setpoint changes in simulator mode
Jan Mulder <jlmulder@xs4all.nl>
parents:
270
diff
changeset
|
492 stateUsedWrite->events.info_SetpointChange = setpoint_cbar; |
38 | 493 } |
494 } | |
495 setActualGas(lifeData, gasId, setpoint_cbar); | |
496 } | |
497 | |
498 void setActualGas_ExtraGas(SLifeData *lifeData, uint8_t oxygen, uint8_t helium, uint8_t setpoint_cbar) | |
499 { | |
500 uint8_t nitrogen; | |
501 | |
502 nitrogen = 100; | |
503 nitrogen -= oxygen; | |
504 nitrogen -= helium; | |
505 | |
506 if((lifeData->actualGas.nitrogen_percentage != nitrogen) || (lifeData->actualGas.helium_percentage != helium)) | |
507 { | |
281 | 508 stateUsedWrite->events.manualGasSet = 1; |
509 stateUsedWrite->events.info_manualGasSetHe = helium; | |
510 stateUsedWrite->events.info_manualGasSetO2 = oxygen; | |
38 | 511 } |
512 if( lifeData->actualGas.setPoint_cbar != setpoint_cbar) | |
513 { | |
271
1303747b5ba2
cleanup: also write gas and setpoint changes in simulator mode
Jan Mulder <jlmulder@xs4all.nl>
parents:
270
diff
changeset
|
514 stateUsedWrite->events.setpointChange = 1; |
1303747b5ba2
cleanup: also write gas and setpoint changes in simulator mode
Jan Mulder <jlmulder@xs4all.nl>
parents:
270
diff
changeset
|
515 stateUsedWrite->events.info_SetpointChange = setpoint_cbar; |
38 | 516 } |
517 lifeData->actualGas.GasIdInSettings = 0; | |
518 lifeData->actualGas.nitrogen_percentage = nitrogen; | |
519 lifeData->actualGas.helium_percentage = helium; | |
520 lifeData->actualGas.setPoint_cbar = setpoint_cbar; | |
521 lifeData->actualGas.change_during_ascent_depth_meter_otherwise_zero = 0; | |
522 | |
523 } | |
524 | |
525 void setButtonResponsiveness(uint8_t *ButtonSensitivyList) | |
526 { | |
527 SDataReceiveFromMaster *pDataOut = dataOutGetPointer(); | |
528 | |
529 for(int i=0; i<4; i++) | |
530 { | |
531 pDataOut->data.buttonResponsiveness[i] = settingsHelperButtonSens_translate_percentage_to_hwOS_values(ButtonSensitivyList[i]); | |
532 } | |
533 pDataOut->setButtonSensitivityNow = 1; | |
534 } | |
535 | |
536 | |
537 void setDate(RTC_DateTypeDef Sdate) | |
538 { | |
539 SDataReceiveFromMaster *pDataOut = dataOutGetPointer(); | |
540 | |
541 pDataOut->data.newDate = Sdate; | |
542 pDataOut->setDateNow = 1; | |
543 } | |
544 | |
545 | |
546 void setTime(RTC_TimeTypeDef Stime) | |
547 { | |
548 SDataReceiveFromMaster *pDataOut = dataOutGetPointer(); | |
549 | |
550 pDataOut->data.newTime = Stime; | |
551 pDataOut->setTimeNow = 1; | |
552 } | |
553 | |
554 | |
555 void setBatteryPercentage(uint8_t newChargePercentage) | |
556 { | |
557 SDataReceiveFromMaster *pDataOut = dataOutGetPointer(); | |
558 | |
559 pDataOut->data.newBatteryGaugePercentageFloat = settingsGetPointer()->lastKnownBatteryPercentage; | |
560 pDataOut->setBatteryGaugeNow = 1; | |
561 } | |
562 | |
563 | |
564 void calibrateCompass(void) | |
565 { | |
566 SDataReceiveFromMaster *pDataOut = dataOutGetPointer(); | |
567 pDataOut->calibrateCompassNow = 1; | |
568 } | |
569 | |
570 | |
571 void clearDeco(void) | |
572 { | |
573 SDataReceiveFromMaster *pDataOut = dataOutGetPointer(); | |
574 pDataOut->clearDecoNow = 1; | |
575 | |
576 stateRealGetPointerWrite()->cnsHigh_at_the_end_of_dive = 0; | |
577 stateRealGetPointerWrite()->decoMissed_at_the_end_of_dive = 0; | |
578 } | |
579 | |
580 | |
300
5ca177d2df5d
cleanup: remove commented/unused code, make static
Jan Mulder <jlmulder@xs4all.nl>
parents:
281
diff
changeset
|
581 static int32_t helper_days_from_civil(int32_t y, uint32_t m, uint32_t d) |
38 | 582 { |
583 y += 2000; | |
584 y -= m <= 2; | |
585 int32_t era = (y >= 0 ? y : y-399) / 400; | |
586 uint32_t yoe = (uint32_t)(y - era * 400); // [0, 399] | |
587 uint32_t doy = (153*(m + (m > 2 ? -3 : 9)) + 2)/5 + d-1; // [0, 365] | |
588 uint32_t doe = yoe * 365 + yoe/4 - yoe/100 + doy; // [0, 146096] | |
589 return era * 146097 + (int32_t)(doe) - 719468; | |
590 } | |
591 | |
592 | |
300
5ca177d2df5d
cleanup: remove commented/unused code, make static
Jan Mulder <jlmulder@xs4all.nl>
parents:
281
diff
changeset
|
593 static uint8_t helper_weekday_from_days(int32_t z) |
38 | 594 { |
595 return (uint8_t)(z >= -4 ? (z+4) % 7 : (z+5) % 7 + 6); | |
596 } | |
597 | |
598 | |
599 void setWeekday(RTC_DateTypeDef *sDate) | |
600 { | |
601 uint8_t day; | |
602 // [0, 6] -> [Sun, Sat] | |
603 day = helper_weekday_from_days(helper_days_from_civil(sDate->Year, sDate->Month, sDate->Date)); | |
604 // [1, 7] -> [Mon, Sun] | |
605 if(day == 0) | |
606 day = 7; | |
607 sDate->WeekDay = day; | |
608 } | |
609 | |
610 | |
611 void translateDate(uint32_t datetmpreg, RTC_DateTypeDef *sDate) | |
612 { | |
613 datetmpreg = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK); | |
614 | |
615 /* Fill the structure fields with the read parameters */ | |
616 sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> 16); | |
617 sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> 8); | |
618 sDate->Date = (uint8_t)(datetmpreg & (RTC_DR_DT | RTC_DR_DU)); | |
619 sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> 13); | |
620 | |
621 /* Convert the date structure parameters to Binary format */ | |
622 sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year); | |
623 sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month); | |
624 sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date); | |
625 } | |
626 | |
627 void translateTime(uint32_t tmpreg, RTC_TimeTypeDef *sTime) | |
628 { | |
629 tmpreg = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK); | |
630 | |
631 /* Fill the structure fields with the read parameters */ | |
632 sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16); | |
633 sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >>8); | |
634 sTime->Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU)); | |
635 sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16); | |
636 | |
637 /* Convert the time structure parameters to Binary format */ | |
638 sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours); | |
639 sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes); | |
640 sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds); | |
641 sTime->SubSeconds = 0; | |
642 } | |
643 | |
270
2e58a4094770
feature, debug: make simulator write a logbook entry
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
644 void resetEvents(const SDiveState *pStateUsed) |
38 | 645 { |
270
2e58a4094770
feature, debug: make simulator write a logbook entry
Jan Mulder <jlmulder@xs4all.nl>
parents:
225
diff
changeset
|
646 memset((void *)&pStateUsed->events, 0, sizeof(SEvents)); |
38 | 647 } |
648 | |
649 | |
650 uint32_t CRC_CalcBlockCRC_moreThan768000(uint32_t *buffer1, uint32_t *buffer2, uint32_t words) | |
651 { | |
652 cm_t crc_model; | |
653 uint32_t word_to_do; | |
654 uint8_t byte_to_do; | |
655 int i; | |
656 | |
657 // Values for the STM32F generator. | |
658 | |
659 crc_model.cm_width = 32; // 32-bit CRC | |
660 crc_model.cm_poly = 0x04C11DB7; // CRC-32 polynomial | |
661 crc_model.cm_init = 0xFFFFFFFF; // CRC initialized to 1's | |
662 crc_model.cm_refin = FALSE; // CRC calculated MSB first | |
663 crc_model.cm_refot = FALSE; // Final result is not bit-reversed | |
664 crc_model.cm_xorot = 0x00000000; // Final result XOR'ed with this | |
665 | |
666 cm_ini(&crc_model); | |
667 | |
668 while (words--) | |
669 { | |
670 // The STM32F10x hardware does 32-bit words at a time!!! | |
671 if(words > (768000/4)) | |
672 word_to_do = *buffer2++; | |
673 else | |
674 word_to_do = *buffer1++; | |
675 | |
676 // Do all bytes in the 32-bit word. | |
677 | |
678 for (i = 0; i < sizeof(word_to_do); i++) | |
679 { | |
680 // We calculate a *byte* at a time. If the CRC is MSB first we | |
681 // do the next MS byte and vica-versa. | |
682 | |
683 if (crc_model.cm_refin == FALSE) | |
684 { | |
685 // MSB first. Do the next MS byte. | |
686 | |
687 byte_to_do = (uint8_t) ((word_to_do & 0xFF000000) >> 24); | |
688 word_to_do <<= 8; | |
689 } | |
690 else | |
691 { | |
692 // LSB first. Do the next LS byte. | |
693 | |
694 byte_to_do = (uint8_t) (word_to_do & 0x000000FF); | |
695 word_to_do >>= 8; | |
696 } | |
697 | |
698 cm_nxt(&crc_model, byte_to_do); | |
699 } | |
700 } | |
701 | |
702 // Return the final result. | |
703 | |
704 return (cm_crc(&crc_model)); | |
705 } | |
706 | |
707 | |
708 uint32_t CRC_CalcBlockCRC(uint32_t *buffer, uint32_t words) | |
709 { | |
710 cm_t crc_model; | |
711 uint32_t word_to_do; | |
712 uint8_t byte_to_do; | |
713 int i; | |
714 | |
715 // Values for the STM32F generator. | |
716 | |
717 crc_model.cm_width = 32; // 32-bit CRC | |
718 crc_model.cm_poly = 0x04C11DB7; // CRC-32 polynomial | |
719 crc_model.cm_init = 0xFFFFFFFF; // CRC initialized to 1's | |
720 crc_model.cm_refin = FALSE; // CRC calculated MSB first | |
721 crc_model.cm_refot = FALSE; // Final result is not bit-reversed | |
722 crc_model.cm_xorot = 0x00000000; // Final result XOR'ed with this | |
723 | |
724 cm_ini(&crc_model); | |
725 | |
726 while (words--) | |
727 { | |
728 // The STM32F10x hardware does 32-bit words at a time!!! | |
729 | |
730 word_to_do = *buffer++; | |
731 | |
732 // Do all bytes in the 32-bit word. | |
733 | |
734 for (i = 0; i < sizeof(word_to_do); i++) | |
735 { | |
736 // We calculate a *byte* at a time. If the CRC is MSB first we | |
737 // do the next MS byte and vica-versa. | |
738 | |
739 if (crc_model.cm_refin == FALSE) | |
740 { | |
741 // MSB first. Do the next MS byte. | |
742 | |
743 byte_to_do = (uint8_t) ((word_to_do & 0xFF000000) >> 24); | |
744 word_to_do <<= 8; | |
745 } | |
746 else | |
747 { | |
748 // LSB first. Do the next LS byte. | |
749 | |
750 byte_to_do = (uint8_t) (word_to_do & 0x000000FF); | |
751 word_to_do >>= 8; | |
752 } | |
753 | |
754 cm_nxt(&crc_model, byte_to_do); | |
755 } | |
756 } | |
757 | |
758 // Return the final result. | |
759 | |
760 return (cm_crc(&crc_model)); | |
761 } | |
762 | |
302
eba8d1eb5bef
bugfix, cleanup: keep both is_ambient_pressure_close_to_surface in sync
Jan Mulder <jlmulder@xs4all.nl>
parents:
300
diff
changeset
|
763 // This code is also in RTE. Keep it in sync when editing |
38 | 764 _Bool is_ambient_pressure_close_to_surface(SLifeData *lifeData) |
765 { | |
310
95928ef3986f
Make dive mode detection more advanced
Jan Mulder <jlmulder@xs4all.nl>
parents:
302
diff
changeset
|
766 if (lifeData->pressure_ambient_bar > 1.16) |
95928ef3986f
Make dive mode detection more advanced
Jan Mulder <jlmulder@xs4all.nl>
parents:
302
diff
changeset
|
767 return false; |
95928ef3986f
Make dive mode detection more advanced
Jan Mulder <jlmulder@xs4all.nl>
parents:
302
diff
changeset
|
768 else if(lifeData->pressure_ambient_bar < (lifeData->pressure_surface_bar + 0.1f)) |
38 | 769 return true; |
770 else | |
771 return false; | |
772 } |