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
|
|
74 SDiveState stateReal = { 0 };
|
|
75 SDiveState stateSim = { 0 };
|
|
76 SDiveState stateDeco = { 0 };
|
|
77
|
|
78 SLifeData2 secondaryInformation = { 0 };
|
|
79
|
|
80 SDevice stateDevice =
|
|
81 {
|
|
82 /* max is 0x7FFFFFFF, min is 0x80000000 but also defined in stdint.h :-) */
|
|
83
|
|
84 /* count, use 0 */
|
|
85 .batteryChargeCompleteCycles.value_int32 = 0,
|
|
86 .batteryChargeCycles.value_int32 = 0,
|
|
87 .diveCycles.value_int32 = 0,
|
|
88 .hoursOfOperation.value_int32 = 0,
|
|
89
|
|
90 /* max values, use min. */
|
|
91 .temperatureMaximum.value_int32 = INT32_MIN,
|
|
92 .depthMaximum.value_int32 = INT32_MIN,
|
|
93
|
|
94 /* min values, use max. */
|
|
95 .temperatureMinimum.value_int32 = INT32_MAX,
|
|
96 .voltageMinimum.value_int32 = INT32_MAX,
|
|
97 };
|
|
98
|
|
99 SVpmRepetitiveData stateVPM =
|
|
100 {
|
|
101 .repetitive_variables_not_valid = 1,
|
|
102 .is_data_from_RTE_CPU = 0,
|
|
103 };
|
|
104
|
|
105 const SDiveState * stateUsed = &stateReal;
|
|
106
|
|
107
|
|
108 void set_stateUsedToReal(void)
|
|
109 {
|
|
110 stateUsed = &stateReal;
|
|
111 }
|
|
112
|
|
113 void set_stateUsedToSim(void)
|
|
114 {
|
|
115 stateUsed = &stateSim;
|
|
116 }
|
|
117
|
|
118 _Bool is_stateUsedSetToSim(void)
|
|
119 {
|
|
120 if(stateUsed == &stateSim)
|
|
121 return 1;
|
|
122 else
|
|
123 return 0;
|
|
124
|
|
125 }
|
|
126
|
|
127 const SDiveState * stateRealGetPointer(void)
|
|
128 {
|
|
129 return &stateReal;
|
|
130 }
|
|
131
|
|
132 SDiveState * stateRealGetPointerWrite(void)
|
|
133 {
|
|
134 return &stateReal;
|
|
135 }
|
|
136
|
|
137
|
|
138 const SDiveState * stateSimGetPointer(void)
|
|
139 {
|
|
140 return &stateSim;
|
|
141 }
|
|
142
|
|
143
|
|
144 SDiveState * stateSimGetPointerWrite(void)
|
|
145 {
|
|
146 return &stateSim;
|
|
147 }
|
|
148
|
|
149
|
|
150 const SDevice * stateDeviceGetPointer(void)
|
|
151 {
|
|
152 return &stateDevice;
|
|
153 }
|
|
154
|
|
155
|
|
156 SDevice * stateDeviceGetPointerWrite(void)
|
|
157 {
|
|
158 return &stateDevice;
|
|
159 }
|
|
160
|
|
161
|
|
162 const SVpmRepetitiveData * stateVpmRepetitiveDataGetPointer(void)
|
|
163 {
|
|
164 return &stateVPM;
|
|
165 }
|
|
166
|
|
167
|
|
168 SVpmRepetitiveData * stateVpmRepetitiveDataGetPointerWrite(void)
|
|
169 {
|
|
170 return &stateVPM;
|
|
171 }
|
|
172
|
|
173
|
|
174 uint32_t time_elapsed_ms(uint32_t ticksstart,uint32_t ticksnow)
|
|
175 {
|
|
176 if(ticksstart <= ticksnow)
|
|
177 return ticksnow - ticksstart;
|
|
178 else
|
|
179 return 0xFFFFFFFF - ticksstart + ticksnow;
|
|
180 }
|
|
181
|
|
182
|
|
183 uint8_t decoLock = DECO_CALC_undefined;
|
|
184 int ascent_rate_meter_per_min = 12;
|
|
185 int descent_rate_meter_per_min = 20;
|
|
186 int max_depth = 70;
|
|
187 int bottom_time = 10;
|
|
188
|
|
189 _Bool vpm_crush(SDiveState* pDiveState);
|
|
190 void setSimulationValues(int _ascent_rate_meter_per_min, int _descent_rate_meter_per_min, int _max_depth, int _bottom_time )
|
|
191 {
|
|
192 ascent_rate_meter_per_min = _ascent_rate_meter_per_min;
|
|
193 descent_rate_meter_per_min = _descent_rate_meter_per_min;
|
|
194 max_depth = _max_depth;
|
|
195 bottom_time = _bottom_time;
|
|
196 }
|
|
197
|
|
198
|
|
199
|
|
200 int current_second(void) {
|
|
201
|
|
202 return HAL_GetTick() / 1000;
|
|
203 // printf("milliseconds: %lld\n", milliseconds);
|
|
204 //return milliseconds;
|
|
205 }
|
|
206
|
|
207
|
|
208
|
|
209 #define OXY_ONE_SIXTIETH_PART 0.0166667f
|
|
210
|
|
211 /*void oxygen_calculate_cns(float* oxygen_cns, float pressure_oxygen_real)
|
|
212 {
|
|
213 int cns_no_range = 0;
|
|
214 _Bool not_found = 1;
|
|
215 //for the cns calculation
|
|
216 const float cns_ppo2_ranges[60][2] = { {0.50, 0.00}, {0.60, 0.14}, {0.64, 0.15}, {0.66, 0.16}, {0.68, 0.17}, {0.70, 0.18},
|
|
217 {0.74, 0.19}, {0.76, 0.20}, {0.78, 0.21}, {0.80, 0.22}, {0.82, 0.23}, {0.84, 0.24},
|
|
218 {0.86, 0.25}, {0.88, 0.26}, {0.90, 0.28}, {0.92, 0.29}, {0.94, 0.30}, {0.96, 0.31},
|
|
219 {0.98, 0.32}, {1.00, 0.33}, {1.02, 0.35}, {1.04, 0.36}, {1.06, 0.38}, {1.08, 0.40},
|
|
220 {1.10, 0.42}, {1.12, 0.43}, {1.14, 0.43}, {1.16, 0.44}, {1.18, 0.46}, {1.20, 0.47},
|
|
221 {1.22, 0.48}, {1.24, 0.51}, {1.26, 0.52}, {1.28, 0.54}, {1.30, 0.56}, {1.32, 0.57},
|
|
222 {1.34, 0.60}, {1.36, 0.62}, {1.38, 0.63}, {1.40, 0.65}, {1.42, 0.68}, {1.44, 0.71},
|
|
223 {1.46, 0.74}, {1.48, 0.78}, {1.50, 0.83}, {1.52, 0.93}, {1.54, 1.04}, {1.56, 1.19},
|
|
224 {1.58, 1.47}, {1.60, 2.22}, {1.62, 5.00}, {1.65, 6.25}, {1.67, 7.69}, {1.70, 10.0},
|
|
225 {1.72,12.50}, {1.74,20.00}, {1.77,25.00}, {1.79,31.25}, {1.80,50.00}, {1.82,100.0}};
|
|
226 //find the correct cns range for the corresponding ppo2
|
|
227 cns_no_range = 58;
|
|
228 while (cns_no_range && not_found)
|
|
229 {
|
|
230 if (pressure_oxygen_real > cns_ppo2_ranges[cns_no_range][0])
|
|
231 {
|
|
232 cns_no_range++;
|
|
233 not_found = 0;
|
|
234 }
|
|
235 else
|
|
236 cns_no_range--;
|
|
237 }
|
|
238
|
|
239 //calculate cns for the actual ppo2 for 1 second
|
|
240 *oxygen_cns += OXY_ONE_SIXTIETH_PART * cns_ppo2_ranges[cns_no_range][1];
|
|
241 }*/
|
|
242
|
|
243 uint8_t calc_MOD(uint8_t gasId)
|
|
244 {
|
|
245 int16_t oxygen, maxppO2, result;
|
|
246 SSettings *pSettings;
|
|
247
|
|
248 pSettings = settingsGetPointer();
|
|
249
|
|
250 oxygen = (int16_t)(pSettings->gas[gasId].oxygen_percentage);
|
|
251
|
|
252 if(pSettings->gas[gasId].note.ub.deco > 0)
|
|
253 maxppO2 =(int16_t)(pSettings->ppO2_max_deco);
|
|
254 else
|
|
255 maxppO2 =(int16_t)(pSettings->ppO2_max_std);
|
|
256
|
|
257 result = 10 * maxppO2;
|
|
258 result /= oxygen;
|
|
259 result -= 10;
|
|
260
|
|
261 if(result < 0)
|
|
262 return 0;
|
|
263
|
|
264 if(result > 255)
|
|
265 return 255;
|
|
266
|
|
267 return result;
|
|
268 }
|
|
269
|
|
270 uint8_t calc_MinOD(uint8_t gasId)
|
|
271 {
|
|
272 int16_t oxygen, minppO2, result;
|
|
273 SSettings *pSettings;
|
|
274
|
|
275 pSettings = settingsGetPointer();
|
|
276
|
|
277 oxygen = (int16_t)(pSettings->gas[gasId].oxygen_percentage);
|
|
278 minppO2 =(int16_t)(pSettings->ppO2_min);
|
|
279 result = 10 * minppO2;
|
|
280 result += 9;
|
|
281 result /= oxygen;
|
|
282 result -= 10;
|
|
283
|
|
284 if(result < 0)
|
|
285 return 0;
|
|
286
|
|
287 if(result > 255)
|
|
288 return 255;
|
|
289
|
|
290 return result;
|
|
291 }
|
|
292 /*
|
|
293 float calc_ppO2(float input_ambient_pressure_bar, SGas* pGas)
|
|
294 {
|
|
295 float percent_N2 = 0;
|
|
296 float percent_He = 0;
|
|
297 float percent_O2 = 0;
|
|
298 decom_get_inert_gases(input_ambient_pressure_bar, pGas, &percent_N2, &percent_He);
|
|
299 percent_O2 = 1 - percent_N2 - percent_He;
|
|
300
|
|
301 return (input_ambient_pressure_bar - WATER_VAPOUR_PRESSURE) * percent_O2;
|
|
302 }*/
|
|
303
|
|
304 float get_ambiant_pressure_simulation(long dive_time_seconds, float surface_pressure_bar )
|
|
305 {
|
|
306 static
|
|
307 long descent_time;
|
|
308 float depth_meter;
|
|
309
|
|
310 descent_time = 60 * max_depth / descent_rate_meter_per_min;
|
|
311
|
|
312 if(dive_time_seconds <= descent_time)
|
|
313 {
|
|
314 depth_meter = ((float)(dive_time_seconds * descent_rate_meter_per_min)) / 60;
|
|
315 return surface_pressure_bar + depth_meter / 10;
|
|
316 }
|
|
317 //else if(dive_time_seconds <= (descent_time + bottom_time * 60))
|
|
318 return surface_pressure_bar + max_depth / 10;
|
|
319
|
|
320
|
|
321
|
|
322 }
|
|
323
|
|
324 void UpdateLifeDataTest(SDiveState * pDiveState)
|
|
325 {
|
|
326 static int last_second = -1;
|
|
327 int now = current_second();
|
|
328 if(last_second == now)
|
|
329 return;
|
|
330 last_second = now;
|
|
331
|
|
332 pDiveState->lifeData.dive_time_seconds += 1;
|
|
333 pDiveState->lifeData.pressure_ambient_bar = get_ambiant_pressure_simulation(pDiveState->lifeData.dive_time_seconds,pDiveState->lifeData.pressure_surface_bar);
|
|
334
|
|
335 pDiveState->lifeData.depth_meter = (pDiveState->lifeData.pressure_ambient_bar - pDiveState->lifeData.pressure_surface_bar) * 10.0f;
|
|
336 if(pDiveState->lifeData.max_depth_meter < pDiveState->lifeData.depth_meter)
|
|
337 pDiveState->lifeData.max_depth_meter = pDiveState->lifeData.depth_meter;
|
|
338 decom_tissues_exposure(1, &pDiveState->lifeData);
|
|
339 pDiveState->lifeData.ppO2 = decom_calc_ppO2( pDiveState->lifeData.pressure_ambient_bar, &pDiveState->lifeData.actualGas);
|
|
340 decom_oxygen_calculate_cns(& pDiveState->lifeData.cns, pDiveState->lifeData.ppO2);
|
|
341
|
|
342 vpm_crush(pDiveState);
|
|
343 }
|
|
344
|
|
345
|
|
346 _Bool vpm_crush(SDiveState* pDiveState)
|
|
347 {
|
|
348 int i = 0;
|
|
349 static float starting_ambient_pressure = 0;
|
|
350 static float ending_ambient_pressure = 0;
|
|
351 static float time_calc_begin = -1;
|
|
352 static float initial_helium_pressure[16];
|
|
353 static float initial_nitrogen_pressure[16];
|
|
354 ending_ambient_pressure = pDiveState->lifeData.pressure_ambient_bar * 10;
|
|
355
|
|
356 if((pDiveState->lifeData.dive_time_seconds <= 4) || (starting_ambient_pressure >= ending_ambient_pressure))
|
|
357 {
|
|
358 time_calc_begin = pDiveState->lifeData.dive_time_seconds;
|
|
359 starting_ambient_pressure = pDiveState->lifeData.pressure_ambient_bar * 10;
|
|
360 for( i = 0; i < 16; i++)
|
|
361 {
|
|
362 initial_helium_pressure[i] = pDiveState->lifeData.tissue_helium_bar[i] * 10;
|
|
363 initial_nitrogen_pressure[i] = pDiveState->lifeData.tissue_nitrogen_bar[i] * 10;
|
|
364 }
|
|
365 return false;
|
|
366 }
|
|
367 if(pDiveState->lifeData.dive_time_seconds - time_calc_begin >= 4)
|
|
368 {
|
|
369 if(ending_ambient_pressure > starting_ambient_pressure + 0.5f)
|
|
370 {
|
|
371 float rate = (ending_ambient_pressure - starting_ambient_pressure) * 60 / 4;
|
|
372 calc_crushing_pressure(&pDiveState->lifeData, &pDiveState->vpm, initial_helium_pressure, initial_nitrogen_pressure, starting_ambient_pressure, rate);
|
|
373
|
|
374 time_calc_begin = pDiveState->lifeData.dive_time_seconds;
|
|
375 starting_ambient_pressure = pDiveState->lifeData.pressure_ambient_bar * 10;
|
|
376 for( i = 0; i < 16; i++)
|
|
377 {
|
|
378 initial_helium_pressure[i] = pDiveState->lifeData.tissue_helium_bar[i] * 10;
|
|
379 initial_nitrogen_pressure[i] = pDiveState->lifeData.tissue_nitrogen_bar[i] * 10;
|
|
380 }
|
|
381
|
|
382 return true;
|
|
383 }
|
|
384
|
|
385 }
|
|
386 return false;
|
|
387 };
|
|
388
|
|
389
|
|
390 void createDiveSettings(void)
|
|
391 {
|
|
392 SSettings* pSettings = settingsGetPointer();
|
|
393
|
|
394 setActualGasFirst(&stateReal.lifeData);
|
|
395
|
|
396 stateReal.diveSettings.compassHeading = pSettings->compassBearing;
|
|
397 stateReal.diveSettings.ascentRate_meterperminute = 10;
|
|
398
|
|
399 stateReal.diveSettings.diveMode = pSettings->dive_mode;
|
|
400 stateReal.diveSettings.CCR_Mode = pSettings->CCR_Mode;
|
|
401 if(stateReal.diveSettings.diveMode == DIVEMODE_CCR)
|
|
402 stateReal.diveSettings.ccrOption = 1;
|
|
403 else
|
|
404 stateReal.diveSettings.ccrOption = 0;
|
|
405 memcpy(stateReal.diveSettings.gas, pSettings->gas,sizeof(pSettings->gas));
|
|
406 memcpy(stateReal.diveSettings.setpoint, pSettings->setpoint,sizeof(pSettings->setpoint));
|
|
407 stateReal.diveSettings.gf_high = pSettings->GF_high;
|
|
408 stateReal.diveSettings.gf_low = pSettings->GF_low;
|
|
409 stateReal.diveSettings.input_next_stop_increment_depth_bar = ((float)pSettings->stop_increment_depth_meter) / 10.0f;
|
|
410 stateReal.diveSettings.last_stop_depth_bar = ((float)pSettings->last_stop_depth_meter) / 10.0f;
|
|
411 stateReal.diveSettings.vpm_conservatism = pSettings->VPM_conservatism.ub.standard;
|
|
412 stateReal.diveSettings.deco_type.uw = pSettings->deco_type.uw;
|
|
413 stateReal.diveSettings.fallbackOption = pSettings->fallbackToFixedSetpoint;
|
|
414 stateReal.diveSettings.ppo2sensors_deactivated = pSettings->ppo2sensors_deactivated;
|
|
415 stateReal.diveSettings.future_TTS_minutes = pSettings->future_TTS;
|
|
416
|
|
417 decom_CreateGasChangeList(&stateReal.diveSettings, &stateReal.lifeData); // decogaslist
|
|
418 stateReal.diveSettings.internal__pressure_first_stop_ambient_bar_as_upper_limit_for_gf_low_otherwise_zero = 0;
|
|
419
|
|
420 /* for safety */
|
|
421 stateReal.diveSettings.input_second_to_last_stop_depth_bar = stateReal.diveSettings.last_stop_depth_bar + stateReal.diveSettings.input_next_stop_increment_depth_bar;
|
|
422 /* and the proper calc */
|
|
423 for(int i = 1; i <10; i++)
|
|
424 {
|
|
425 if(stateReal.diveSettings.input_next_stop_increment_depth_bar * i > stateReal.diveSettings.last_stop_depth_bar)
|
|
426 {
|
|
427 stateReal.diveSettings.input_second_to_last_stop_depth_bar = stateReal.diveSettings.input_next_stop_increment_depth_bar * i;
|
|
428 break;
|
|
429 }
|
|
430 }
|
|
431 }
|
|
432
|
|
433
|
|
434 void copyDiveSettingsToSim(void)
|
|
435 {
|
|
436 memcpy(&stateSim, &stateReal, sizeof(stateReal));
|
|
437 }
|
|
438
|
|
439
|
|
440 void copyVpmRepetetiveDataToSim(void)
|
|
441 {
|
|
442 SDiveState * pSimData = stateSimGetPointerWrite();
|
|
443 const SVpmRepetitiveData * pVpmData = stateVpmRepetitiveDataGetPointer();
|
|
444
|
|
445 if(pVpmData->is_data_from_RTE_CPU)
|
|
446 {
|
|
447 for(int i=0; i<16;i++)
|
|
448 {
|
|
449 pSimData->vpm.adjusted_critical_radius_he[i] = pVpmData->adjusted_critical_radius_he[i];
|
|
450 pSimData->vpm.adjusted_critical_radius_n2[i] = pVpmData->adjusted_critical_radius_n2[i];
|
|
451
|
|
452 pSimData->vpm.adjusted_crushing_pressure_he[i] = pVpmData->adjusted_crushing_pressure_he[i];
|
|
453 pSimData->vpm.adjusted_crushing_pressure_n2[i] = pVpmData->adjusted_crushing_pressure_n2[i];
|
|
454
|
|
455 pSimData->vpm.initial_allowable_gradient_he[i] = pVpmData->initial_allowable_gradient_he[i];
|
|
456 pSimData->vpm.initial_allowable_gradient_n2[i] = pVpmData->initial_allowable_gradient_n2[i];
|
|
457
|
|
458 pSimData->vpm.max_actual_gradient[i] = pVpmData->max_actual_gradient[i];
|
|
459 }
|
|
460 pSimData->vpm.repetitive_variables_not_valid = pVpmData->repetitive_variables_not_valid;
|
|
461 }
|
|
462 }
|
|
463
|
|
464
|
|
465 void updateSetpointStateUsed(void)
|
|
466 {
|
|
467 SLifeData *pLifeDataWrite;
|
|
468
|
|
469 if(is_stateUsedSetToSim())
|
|
470 pLifeDataWrite = &stateSimGetPointerWrite()->lifeData;
|
|
471 else
|
|
472 pLifeDataWrite = &stateRealGetPointerWrite()->lifeData;
|
|
473
|
|
474 if(stateUsed->diveSettings.diveMode != DIVEMODE_CCR)
|
|
475 {
|
|
476 pLifeDataWrite->actualGas.setPoint_cbar = 0;
|
|
477 pLifeDataWrite->ppO2 = decom_calc_ppO2(stateUsed->lifeData.pressure_ambient_bar, &stateUsed->lifeData.actualGas);
|
|
478 }
|
|
479 else
|
|
480 {
|
|
481 if(stateUsed->diveSettings.CCR_Mode == CCRMODE_Sensors)
|
|
482 {
|
|
483 pLifeDataWrite->actualGas.setPoint_cbar = get_ppO2SensorWeightedResult_cbar();
|
|
484 }
|
|
485
|
|
486 if((stateUsed->lifeData.pressure_ambient_bar * 100) < stateUsed->lifeData.actualGas.setPoint_cbar)
|
|
487 pLifeDataWrite->ppO2 = stateUsed->lifeData.pressure_ambient_bar;
|
|
488 else
|
|
489 pLifeDataWrite->ppO2 = ((float)stateUsed->lifeData.actualGas.setPoint_cbar) / 100;
|
|
490 }
|
|
491 }
|
|
492
|
|
493 /*
|
|
494 void fallbackToFixedSetpoints(SLifeData *lifeData)
|
|
495 {
|
|
496
|
|
497 }
|
|
498 */
|
|
499
|
|
500 void setActualGasFirst(SLifeData *lifeData)
|
|
501 {
|
|
502 SSettings* pSettings = settingsGetPointer();
|
|
503 uint8_t start = 0;
|
|
504 uint8_t gasId = 0;
|
|
505 uint8_t setpoint_cbar = 0;
|
|
506
|
|
507 if(pSettings->dive_mode == DIVEMODE_CCR)
|
|
508 {
|
|
509 setpoint_cbar = pSettings->setpoint[1].setpoint_cbar;
|
|
510 start = NUM_OFFSET_DILUENT+1;
|
|
511 }
|
|
512 else
|
|
513 {
|
|
514 setpoint_cbar = 0;
|
|
515 start = 1;
|
|
516 }
|
|
517
|
|
518 gasId = start;
|
|
519 for(int i=start;i<=NUM_GASES+start;i++)
|
|
520 {
|
|
521 if(pSettings->gas[i].note.ub.first)
|
|
522 {
|
|
523 gasId = i;
|
|
524 break;
|
|
525 }
|
|
526 }
|
|
527 setActualGas(lifeData, gasId, setpoint_cbar);
|
|
528 }
|
|
529
|
|
530 void setActualGasAir(SLifeData *lifeData)
|
|
531 {
|
|
532 uint8_t nitrogen;
|
|
533 nitrogen = 79;
|
|
534 lifeData->actualGas.GasIdInSettings = 0;
|
|
535 lifeData->actualGas.nitrogen_percentage = nitrogen;
|
|
536 lifeData->actualGas.helium_percentage =0;
|
|
537 lifeData->actualGas.setPoint_cbar = 0;
|
|
538 lifeData->actualGas.change_during_ascent_depth_meter_otherwise_zero = 0;
|
|
539 }
|
|
540
|
|
541
|
|
542 void setActualGas(SLifeData *lifeData, uint8_t gasId, uint8_t setpoint_cbar)
|
|
543 {
|
|
544 SSettings* pSettings = settingsGetPointer();
|
|
545 uint8_t nitrogen;
|
|
546
|
|
547 nitrogen = 100;
|
|
548 nitrogen -= pSettings->gas[gasId].oxygen_percentage;
|
|
549 nitrogen -= pSettings->gas[gasId].helium_percentage;
|
|
550
|
|
551 lifeData->actualGas.GasIdInSettings = gasId;
|
|
552 lifeData->actualGas.nitrogen_percentage = nitrogen;
|
|
553 lifeData->actualGas.helium_percentage = pSettings->gas[gasId].helium_percentage;
|
|
554 lifeData->actualGas.setPoint_cbar = setpoint_cbar;
|
|
555 lifeData->actualGas.change_during_ascent_depth_meter_otherwise_zero = 0;
|
|
556
|
|
557 if((pSettings->dive_mode == DIVEMODE_CCR) && (gasId > NUM_OFFSET_DILUENT))
|
|
558 lifeData->lastDiluent_GasIdInSettings = gasId;
|
|
559 }
|
|
560
|
|
561
|
|
562 void setActualGas_DM(SLifeData *lifeData, uint8_t gasId, uint8_t setpoint_cbar)
|
|
563 {
|
|
564 //Real dive => Set events for logbook
|
|
565 if(stateUsed == stateRealGetPointer())
|
|
566 {
|
|
567 SDiveState * pStateUsed;
|
|
568 pStateUsed = stateRealGetPointerWrite();
|
|
569
|
|
570 if(stateUsed->diveSettings.ccrOption && gasId < 6)
|
|
571 {
|
|
572 if(lifeData->actualGas.GasIdInSettings != gasId)
|
|
573 {
|
|
574 SSettings* pSettings = settingsGetPointer();
|
|
575 pStateUsed->events.bailout = 1;
|
|
576 pStateUsed->events.info_bailoutO2 = pSettings->gas[gasId].oxygen_percentage;
|
|
577 pStateUsed->events.info_bailoutHe = pSettings->gas[gasId].helium_percentage;
|
|
578 }
|
|
579 }
|
|
580 else
|
|
581 {
|
|
582 if(lifeData->actualGas.GasIdInSettings != gasId)
|
|
583 {
|
|
584 pStateUsed->events.gasChange = 1;
|
|
585 pStateUsed->events.info_GasChange = gasId;
|
|
586 }
|
|
587 if( lifeData->actualGas.setPoint_cbar != setpoint_cbar)
|
|
588 {
|
|
589 // setPoint_cbar = 255 -> change to sensor mode
|
|
590 pStateUsed->events.setpointChange = 1;
|
|
591 pStateUsed->events.info_SetpointChange = setpoint_cbar;
|
|
592 }
|
|
593 }
|
|
594 }
|
|
595 setActualGas(lifeData, gasId, setpoint_cbar);
|
|
596 }
|
|
597
|
|
598 void setActualGas_ExtraGas(SLifeData *lifeData, uint8_t oxygen, uint8_t helium, uint8_t setpoint_cbar)
|
|
599 {
|
|
600 uint8_t nitrogen;
|
|
601
|
|
602 nitrogen = 100;
|
|
603 nitrogen -= oxygen;
|
|
604 nitrogen -= helium;
|
|
605
|
|
606 //Real dive => Set events for logbook
|
|
607 if(stateUsed == stateRealGetPointer())
|
|
608 {
|
|
609 SDiveState * pStateUsed;
|
|
610 pStateUsed = stateRealGetPointerWrite();
|
|
611 if((lifeData->actualGas.nitrogen_percentage != nitrogen) || (lifeData->actualGas.helium_percentage != helium))
|
|
612 {
|
|
613 pStateUsed->events.manuelGasSet = 1;
|
|
614 pStateUsed->events.info_manuelGasSetHe = helium;
|
|
615 pStateUsed->events.info_manuelGasSetO2 = oxygen;
|
|
616 }
|
|
617 if( lifeData->actualGas.setPoint_cbar != setpoint_cbar)
|
|
618 {
|
|
619 pStateUsed->events.setpointChange = 1;
|
|
620 pStateUsed->events.info_SetpointChange = setpoint_cbar;
|
|
621 }
|
|
622 }
|
|
623 lifeData->actualGas.GasIdInSettings = 0;
|
|
624 lifeData->actualGas.nitrogen_percentage = nitrogen;
|
|
625 lifeData->actualGas.helium_percentage = helium;
|
|
626 lifeData->actualGas.setPoint_cbar = setpoint_cbar;
|
|
627 lifeData->actualGas.change_during_ascent_depth_meter_otherwise_zero = 0;
|
|
628
|
|
629 }
|
|
630
|
|
631 void setButtonResponsiveness(uint8_t *ButtonSensitivyList)
|
|
632 {
|
|
633 SDataReceiveFromMaster *pDataOut = dataOutGetPointer();
|
|
634
|
|
635 for(int i=0; i<4; i++)
|
|
636 {
|
|
637 pDataOut->data.buttonResponsiveness[i] = settingsHelperButtonSens_translate_percentage_to_hwOS_values(ButtonSensitivyList[i]);
|
|
638 }
|
|
639 pDataOut->setButtonSensitivityNow = 1;
|
|
640 }
|
|
641
|
|
642
|
|
643 void setDate(RTC_DateTypeDef Sdate)
|
|
644 {
|
|
645 SDataReceiveFromMaster *pDataOut = dataOutGetPointer();
|
|
646
|
|
647 pDataOut->data.newDate = Sdate;
|
|
648 pDataOut->setDateNow = 1;
|
|
649 }
|
|
650
|
|
651
|
|
652 void setTime(RTC_TimeTypeDef Stime)
|
|
653 {
|
|
654 SDataReceiveFromMaster *pDataOut = dataOutGetPointer();
|
|
655
|
|
656 pDataOut->data.newTime = Stime;
|
|
657 pDataOut->setTimeNow = 1;
|
|
658 }
|
|
659
|
|
660
|
|
661 void setBatteryPercentage(uint8_t newChargePercentage)
|
|
662 {
|
|
663 SDataReceiveFromMaster *pDataOut = dataOutGetPointer();
|
|
664
|
|
665 pDataOut->data.newBatteryGaugePercentageFloat = settingsGetPointer()->lastKnownBatteryPercentage;
|
|
666 pDataOut->setBatteryGaugeNow = 1;
|
|
667 }
|
|
668
|
|
669
|
|
670 void calibrateCompass(void)
|
|
671 {
|
|
672 SDataReceiveFromMaster *pDataOut = dataOutGetPointer();
|
|
673 pDataOut->calibrateCompassNow = 1;
|
|
674 }
|
|
675
|
|
676
|
|
677 void clearDeco(void)
|
|
678 {
|
|
679 SDataReceiveFromMaster *pDataOut = dataOutGetPointer();
|
|
680 pDataOut->clearDecoNow = 1;
|
|
681
|
|
682 stateRealGetPointerWrite()->cnsHigh_at_the_end_of_dive = 0;
|
|
683 stateRealGetPointerWrite()->decoMissed_at_the_end_of_dive = 0;
|
|
684 }
|
|
685
|
|
686
|
|
687 int32_t helper_days_from_civil(int32_t y, uint32_t m, uint32_t d)
|
|
688 {
|
|
689 y += 2000;
|
|
690 y -= m <= 2;
|
|
691 int32_t era = (y >= 0 ? y : y-399) / 400;
|
|
692 uint32_t yoe = (uint32_t)(y - era * 400); // [0, 399]
|
|
693 uint32_t doy = (153*(m + (m > 2 ? -3 : 9)) + 2)/5 + d-1; // [0, 365]
|
|
694 uint32_t doe = yoe * 365 + yoe/4 - yoe/100 + doy; // [0, 146096]
|
|
695 return era * 146097 + (int32_t)(doe) - 719468;
|
|
696 }
|
|
697
|
|
698
|
|
699 uint8_t helper_weekday_from_days(int32_t z)
|
|
700 {
|
|
701 return (uint8_t)(z >= -4 ? (z+4) % 7 : (z+5) % 7 + 6);
|
|
702 }
|
|
703
|
|
704
|
|
705 void setWeekday(RTC_DateTypeDef *sDate)
|
|
706 {
|
|
707 uint8_t day;
|
|
708 // [0, 6] -> [Sun, Sat]
|
|
709 day = helper_weekday_from_days(helper_days_from_civil(sDate->Year, sDate->Month, sDate->Date));
|
|
710 // [1, 7] -> [Mon, Sun]
|
|
711 if(day == 0)
|
|
712 day = 7;
|
|
713 sDate->WeekDay = day;
|
|
714 }
|
|
715
|
|
716
|
|
717 void translateDate(uint32_t datetmpreg, RTC_DateTypeDef *sDate)
|
|
718 {
|
|
719 datetmpreg = (uint32_t)(datetmpreg & RTC_DR_RESERVED_MASK);
|
|
720
|
|
721 /* Fill the structure fields with the read parameters */
|
|
722 sDate->Year = (uint8_t)((datetmpreg & (RTC_DR_YT | RTC_DR_YU)) >> 16);
|
|
723 sDate->Month = (uint8_t)((datetmpreg & (RTC_DR_MT | RTC_DR_MU)) >> 8);
|
|
724 sDate->Date = (uint8_t)(datetmpreg & (RTC_DR_DT | RTC_DR_DU));
|
|
725 sDate->WeekDay = (uint8_t)((datetmpreg & (RTC_DR_WDU)) >> 13);
|
|
726
|
|
727 /* Convert the date structure parameters to Binary format */
|
|
728 sDate->Year = (uint8_t)RTC_Bcd2ToByte(sDate->Year);
|
|
729 sDate->Month = (uint8_t)RTC_Bcd2ToByte(sDate->Month);
|
|
730 sDate->Date = (uint8_t)RTC_Bcd2ToByte(sDate->Date);
|
|
731 }
|
|
732
|
|
733 void translateTime(uint32_t tmpreg, RTC_TimeTypeDef *sTime)
|
|
734 {
|
|
735 tmpreg = (uint32_t)(tmpreg & RTC_TR_RESERVED_MASK);
|
|
736
|
|
737 /* Fill the structure fields with the read parameters */
|
|
738 sTime->Hours = (uint8_t)((tmpreg & (RTC_TR_HT | RTC_TR_HU)) >> 16);
|
|
739 sTime->Minutes = (uint8_t)((tmpreg & (RTC_TR_MNT | RTC_TR_MNU)) >>8);
|
|
740 sTime->Seconds = (uint8_t)(tmpreg & (RTC_TR_ST | RTC_TR_SU));
|
|
741 sTime->TimeFormat = (uint8_t)((tmpreg & (RTC_TR_PM)) >> 16);
|
|
742
|
|
743 /* Convert the time structure parameters to Binary format */
|
|
744 sTime->Hours = (uint8_t)RTC_Bcd2ToByte(sTime->Hours);
|
|
745 sTime->Minutes = (uint8_t)RTC_Bcd2ToByte(sTime->Minutes);
|
|
746 sTime->Seconds = (uint8_t)RTC_Bcd2ToByte(sTime->Seconds);
|
|
747 sTime->SubSeconds = 0;
|
|
748 }
|
|
749
|
|
750
|
|
751 /*
|
|
752 void initDiveState(SDiveSettings * pDiveSettings, SVpm * pVpm)
|
|
753 {
|
|
754 SSettings* pSettings = settingsGetPointer();
|
|
755 for(int i = 0; i< NUM_GASES; i++)
|
|
756 {
|
|
757 pDiveSettings->gas[i] = pSettings->gas[i];
|
|
758 pDiveSettings->gas[NUM_OFFSET_DILUENT + i] = pSettings->gas[NUM_OFFSET_DILUENT + i];
|
|
759 pDiveSettings->setpoint[i] = pSettings->setpoint[i];
|
|
760 }
|
|
761 pDiveSettings->diveMode = pSettings->dive_mode;
|
|
762
|
|
763 pDiveSettings->gf_high = pSettings->GF_high;
|
|
764 pDiveSettings->gf_low = pSettings->GF_low;
|
|
765 pDiveSettings->last_stop_depth_bar = ((float)pSettings->last_stop_depth_meter) / 10.0;
|
|
766 pDiveSettings->ascentRate_meterperminute = 10;
|
|
767 pDiveSettings->vpm_conservatism = 1;
|
|
768
|
|
769 pDiveSettings->input_next_stop_increment_depth_bar = ((float)pSettings->stop_increment_depth_meter) / 10.0f;
|
|
770
|
|
771 vpm_init(pVpm, pDiveSettings->vpm_conservatism, 0, 0);
|
|
772 }
|
|
773 */
|
|
774 _Bool deco_zone_reached(void)
|
|
775 {
|
|
776 if(stateUsed->diveSettings.deco_type.ub.standard == GF_MODE)
|
|
777 return stateUsed->lifeData.pressure_ambient_bar <= stateUsed->diveSettings.internal__pressure_first_stop_ambient_bar_as_upper_limit_for_gf_low_otherwise_zero;
|
|
778 else
|
|
779 return stateUsed->vpm.deco_zone_reached;
|
|
780
|
|
781 }
|
|
782
|
|
783
|
|
784 void resetEvents(void)
|
|
785 {
|
149
|
786 SDiveState * pStateUsed;
|
38
|
787 if(stateUsed == stateRealGetPointer())
|
149
|
788 {
|
38
|
789 pStateUsed = stateRealGetPointerWrite();
|
149
|
790 }
|
38
|
791 else
|
149
|
792 {
|
38
|
793 pStateUsed = stateSimGetPointerWrite();
|
149
|
794 }
|
|
795 memset(&pStateUsed->events,0, sizeof(SEvents));
|
38
|
796 }
|
|
797
|
|
798
|
|
799 /* This is derived from crc32b but does table lookup. First the table
|
|
800 itself is calculated, if it has not yet been set up.
|
|
801 Not counting the table setup (which would probably be a separate
|
|
802 function), when compiled to Cyclops with GCC, this function executes in
|
|
803 7 + 13n instructions, where n is the number of bytes in the input
|
|
804 message. It should be doable in 4 + 9n instructions. In any case, two
|
|
805 of the 13 or 9 instrucions are load byte.
|
|
806 This is Figure 14-7 in the text. */
|
|
807
|
|
808 /* http://www.hackersdelight.org/ i guess ;-) *hw */
|
|
809
|
|
810 uint32_t crc32c_checksum(uint8_t* message, uint16_t length, uint8_t* message2, uint16_t length2) {
|
|
811 int i, j;
|
|
812 uint32_t byte, crc, mask;
|
|
813 static unsigned int table[256] = {0};
|
|
814
|
|
815 /* Set up the table, if necessary. */
|
|
816 if (table[1] == 0) {
|
|
817 for (byte = 0; byte <= 255; byte++) {
|
|
818 crc = byte;
|
|
819 for (j = 7; j >= 0; j--) { // Do eight times.
|
|
820 mask = -(crc & 1);
|
|
821 crc = (crc >> 1) ^ (0xEDB88320 & mask);
|
|
822 }
|
|
823 table[byte] = crc;
|
|
824 }
|
|
825 }
|
|
826
|
|
827 /* Through with table setup, now calculate the CRC. */
|
|
828 i = 0;
|
|
829 crc = 0xFFFFFFFF;
|
|
830 while (length--) {
|
|
831 byte = message[i];
|
|
832 crc = (crc >> 8) ^ table[(crc ^ byte) & 0xFF];
|
|
833 i = i + 1;
|
|
834 }
|
|
835 if(length2)
|
|
836 {
|
|
837 i = 0;
|
|
838 while (length2--) {
|
|
839 byte = message2[i];
|
|
840 crc = (crc >> 8) ^ table[(crc ^ byte) & 0xFF];
|
|
841 i = i + 1;
|
|
842 }
|
|
843 }
|
|
844 return ~crc;
|
|
845 }
|
|
846
|
|
847
|
|
848 uint32_t CRC_CalcBlockCRC_moreThan768000(uint32_t *buffer1, uint32_t *buffer2, uint32_t words)
|
|
849 {
|
|
850 cm_t crc_model;
|
|
851 uint32_t word_to_do;
|
|
852 uint8_t byte_to_do;
|
|
853 int i;
|
|
854
|
|
855 // Values for the STM32F generator.
|
|
856
|
|
857 crc_model.cm_width = 32; // 32-bit CRC
|
|
858 crc_model.cm_poly = 0x04C11DB7; // CRC-32 polynomial
|
|
859 crc_model.cm_init = 0xFFFFFFFF; // CRC initialized to 1's
|
|
860 crc_model.cm_refin = FALSE; // CRC calculated MSB first
|
|
861 crc_model.cm_refot = FALSE; // Final result is not bit-reversed
|
|
862 crc_model.cm_xorot = 0x00000000; // Final result XOR'ed with this
|
|
863
|
|
864 cm_ini(&crc_model);
|
|
865
|
|
866 while (words--)
|
|
867 {
|
|
868 // The STM32F10x hardware does 32-bit words at a time!!!
|
|
869 if(words > (768000/4))
|
|
870 word_to_do = *buffer2++;
|
|
871 else
|
|
872 word_to_do = *buffer1++;
|
|
873
|
|
874 // Do all bytes in the 32-bit word.
|
|
875
|
|
876 for (i = 0; i < sizeof(word_to_do); i++)
|
|
877 {
|
|
878 // We calculate a *byte* at a time. If the CRC is MSB first we
|
|
879 // do the next MS byte and vica-versa.
|
|
880
|
|
881 if (crc_model.cm_refin == FALSE)
|
|
882 {
|
|
883 // MSB first. Do the next MS byte.
|
|
884
|
|
885 byte_to_do = (uint8_t) ((word_to_do & 0xFF000000) >> 24);
|
|
886 word_to_do <<= 8;
|
|
887 }
|
|
888 else
|
|
889 {
|
|
890 // LSB first. Do the next LS byte.
|
|
891
|
|
892 byte_to_do = (uint8_t) (word_to_do & 0x000000FF);
|
|
893 word_to_do >>= 8;
|
|
894 }
|
|
895
|
|
896 cm_nxt(&crc_model, byte_to_do);
|
|
897 }
|
|
898 }
|
|
899
|
|
900 // Return the final result.
|
|
901
|
|
902 return (cm_crc(&crc_model));
|
|
903 }
|
|
904
|
|
905
|
|
906 uint32_t CRC_CalcBlockCRC(uint32_t *buffer, uint32_t words)
|
|
907 {
|
|
908 cm_t crc_model;
|
|
909 uint32_t word_to_do;
|
|
910 uint8_t byte_to_do;
|
|
911 int i;
|
|
912
|
|
913 // Values for the STM32F generator.
|
|
914
|
|
915 crc_model.cm_width = 32; // 32-bit CRC
|
|
916 crc_model.cm_poly = 0x04C11DB7; // CRC-32 polynomial
|
|
917 crc_model.cm_init = 0xFFFFFFFF; // CRC initialized to 1's
|
|
918 crc_model.cm_refin = FALSE; // CRC calculated MSB first
|
|
919 crc_model.cm_refot = FALSE; // Final result is not bit-reversed
|
|
920 crc_model.cm_xorot = 0x00000000; // Final result XOR'ed with this
|
|
921
|
|
922 cm_ini(&crc_model);
|
|
923
|
|
924 while (words--)
|
|
925 {
|
|
926 // The STM32F10x hardware does 32-bit words at a time!!!
|
|
927
|
|
928 word_to_do = *buffer++;
|
|
929
|
|
930 // Do all bytes in the 32-bit word.
|
|
931
|
|
932 for (i = 0; i < sizeof(word_to_do); i++)
|
|
933 {
|
|
934 // We calculate a *byte* at a time. If the CRC is MSB first we
|
|
935 // do the next MS byte and vica-versa.
|
|
936
|
|
937 if (crc_model.cm_refin == FALSE)
|
|
938 {
|
|
939 // MSB first. Do the next MS byte.
|
|
940
|
|
941 byte_to_do = (uint8_t) ((word_to_do & 0xFF000000) >> 24);
|
|
942 word_to_do <<= 8;
|
|
943 }
|
|
944 else
|
|
945 {
|
|
946 // LSB first. Do the next LS byte.
|
|
947
|
|
948 byte_to_do = (uint8_t) (word_to_do & 0x000000FF);
|
|
949 word_to_do >>= 8;
|
|
950 }
|
|
951
|
|
952 cm_nxt(&crc_model, byte_to_do);
|
|
953 }
|
|
954 }
|
|
955
|
|
956 // Return the final result.
|
|
957
|
|
958 return (cm_crc(&crc_model));
|
|
959 }
|
|
960
|
|
961
|
|
962 _Bool is_ambient_pressure_close_to_surface(SLifeData *lifeData)
|
|
963 {
|
|
964 if(lifeData->pressure_ambient_bar < (lifeData->pressure_surface_bar + 0.04f))
|
|
965 return true;
|
|
966 else
|
|
967 return false;
|
|
968 }
|