comparison Discovery/Src/buehlmann.c @ 257:21387d7e786f bm-3

Buehlmann: factor out gNDL and gTTS Get rid of global gNDL and gTTS. Trivial cleanup. Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
author Jan Mulder <jlmulder@xs4all.nl>
date Fri, 12 Apr 2019 21:04:52 +0200
parents d10f53e39374
children 0087d6251f59
comparison
equal deleted inserted replaced
256:d10f53e39374 257:21387d7e786f
38 static float tissue_tolerance(void); 38 static float tissue_tolerance(void);
39 static void ambient_bar_to_deco_stop_depth_bar(SDiveSettings *pDiveSettings, float ceiling); 39 static void ambient_bar_to_deco_stop_depth_bar(SDiveSettings *pDiveSettings, float ceiling);
40 static int ascend_with_all_gaschanges(SDiveSettings *pDiveSettings, float pressure_decrease); 40 static int ascend_with_all_gaschanges(SDiveSettings *pDiveSettings, float pressure_decrease);
41 static float next_stop_depth_input_is_actual_stop_id(SDiveSettings *pDiveSettings, int actual_id); 41 static float next_stop_depth_input_is_actual_stop_id(SDiveSettings *pDiveSettings, int actual_id);
42 static float get_gf_at_pressure(SDiveSettings *pDiveSettings, float pressure); 42 static float get_gf_at_pressure(SDiveSettings *pDiveSettings, float pressure);
43 static void buehlmann_calc_ndl(SDiveSettings *pDiveSettings); 43 static int buehlmann_calc_ndl(SDiveSettings *pDiveSettings);
44 static _Bool dive1_check_deco(SDiveSettings *pDiveSettings); 44 static _Bool dive1_check_deco(SDiveSettings *pDiveSettings);
45 45
46 static SDecoinfo gDecotable; 46 static SDecoinfo gDecotable;
47 static float gSurface_pressure_bar; 47 static float gSurface_pressure_bar;
48 static float gPressure; 48 static float gPressure;
49 static int gGas_id; 49 static int gGas_id;
50 static float gTTS;
51 static float gTissue_nitrogen_bar[16]; 50 static float gTissue_nitrogen_bar[16];
52 static float gTissue_helium_bar[16]; 51 static float gTissue_helium_bar[16];
53 static float gGF_value; 52 static float gGF_value;
54 static float gCNS; 53 static float gCNS;
55 static int gNDL;
56 54
57 float gGF_low_depth_bar; 55 float gGF_low_depth_bar;
58 SStop gStop; 56 SStop gStop;
59 57
60 void buehlmann_init(void) 58 void buehlmann_init(void)
63 61
64 static void buehlmann_backup_and_restore(_Bool backup_restore_otherwise) 62 static void buehlmann_backup_and_restore(_Bool backup_restore_otherwise)
65 { 63 {
66 static float pressure; 64 static float pressure;
67 static float gas_id; 65 static float gas_id;
68 static float tts;
69 static float tissue_nitrogen_bar[16]; 66 static float tissue_nitrogen_bar[16];
70 static float tissue_helium_bar[16]; 67 static float tissue_helium_bar[16];
71 static float gf_value; 68 static float gf_value;
72 static int ndl;
73 static float cns; 69 static float cns;
74 70
75 if(backup_restore_otherwise) 71 if(backup_restore_otherwise)
76 { 72 {
77 pressure = gPressure; 73 pressure = gPressure;
78 gas_id = gGas_id; 74 gas_id = gGas_id;
79 tts = gTTS;
80 gf_value = gGF_value; 75 gf_value = gGF_value;
81 ndl = gNDL;
82 cns = gCNS; 76 cns = gCNS;
83 memcpy(tissue_nitrogen_bar, gTissue_nitrogen_bar, (4*16)); 77 memcpy(tissue_nitrogen_bar, gTissue_nitrogen_bar, (4*16));
84 memcpy(tissue_helium_bar, gTissue_helium_bar, (4*16)); 78 memcpy(tissue_helium_bar, gTissue_helium_bar, (4*16));
85 } 79 }
86 else 80 else
87 { 81 {
88 gPressure = pressure; 82 gPressure = pressure;
89 gGas_id = gas_id; 83 gGas_id = gas_id;
90 gTTS = tts;
91 gGF_value = gf_value; 84 gGF_value = gf_value;
92 gNDL = ndl;
93 gCNS = cns; 85 gCNS = cns;
94 memcpy(gTissue_nitrogen_bar, tissue_nitrogen_bar, (4*16)); 86 memcpy(gTissue_nitrogen_bar, tissue_nitrogen_bar, (4*16));
95 memcpy(gTissue_helium_bar, tissue_helium_bar, (4*16)); 87 memcpy(gTissue_helium_bar, tissue_helium_bar, (4*16));
96 } 88 }
97 89
138 return; 130 return;
139 131
140 // clean stop list 132 // clean stop list
141 for(i = 0; i < DECOINFO_STRUCT_MAX_STOPS; i++) 133 for(i = 0; i < DECOINFO_STRUCT_MAX_STOPS; i++)
142 stoplist[i] = 0; 134 stoplist[i] = 0;
143 gTTS = 0;
144 gNDL = 0;
145 135
146 if(pDiveSettings->internal__pressure_first_stop_ambient_bar_as_upper_limit_for_gf_low_otherwise_zero >= (gPressure - PRESSURE_150_CM)) 136 if(pDiveSettings->internal__pressure_first_stop_ambient_bar_as_upper_limit_for_gf_low_otherwise_zero >= (gPressure - PRESSURE_150_CM))
147 { 137 {
148 deco_reached = true; 138 deco_reached = true;
149 } 139 }
150 140
151 gGF_value = ((float)pDiveSettings->gf_high) / 100.0f; 141 gGF_value = ((float)pDiveSettings->gf_high) / 100.0f;
152 buehlmann_backup_and_restore(true); // includes backup for gGF_value 142 buehlmann_backup_and_restore(true);
153 if(!dive1_check_deco(pDiveSettings) ) 143 if(!dive1_check_deco(pDiveSettings) )
154 { 144 {
155 buehlmann_backup_and_restore(false); 145 buehlmann_backup_and_restore(false);
156 // no deco 146 // no deco
157 pDecoInfo->output_time_to_surface_seconds = 0; 147 pDecoInfo->output_time_to_surface_seconds = 0;
158 for(i = 0; i < DECOINFO_STRUCT_MAX_STOPS; i++) 148 for(i = 0; i < DECOINFO_STRUCT_MAX_STOPS; i++)
159 pDecoInfo->output_stop_length_seconds[i] = 0; 149 pDecoInfo->output_stop_length_seconds[i] = 0;
160 // calc NDL 150 // calc NDL
161 buehlmann_calc_ndl(pDiveSettings); 151 pDecoInfo->output_ndl_seconds = buehlmann_calc_ndl(pDiveSettings);;
162 pDecoInfo->output_ndl_seconds = gNDL;
163 return; 152 return;
164 } 153 }
165 buehlmann_backup_and_restore(false); 154 buehlmann_backup_and_restore(false);
166 pDecoInfo->output_ndl_seconds = 0; 155 pDecoInfo->output_ndl_seconds = 0;
167 156
531 520
532 return (pDiveSettings->gf_high - gfSteigung * (pressure - gSurface_pressure_bar) )/ 100.0f; 521 return (pDiveSettings->gf_high - gfSteigung * (pressure - gSurface_pressure_bar) )/ 100.0f;
533 } 522 }
534 523
535 524
536 static void buehlmann_calc_ndl(SDiveSettings *pDiveSettings) 525 static int buehlmann_calc_ndl(SDiveSettings *pDiveSettings)
537 { 526 {
538 float local_tissue_nitrogen_bar[16]; 527 float local_tissue_nitrogen_bar[16];
539 float local_tissue_helium_bar[16]; 528 float local_tissue_helium_bar[16];
540 int i; 529 int i;
541 530 int ndl = 0;
542 gNDL = 0; 531
543 //Check ndl always use gHigh 532 //Check ndl always use gHigh
544 gGF_value = ((float)pDiveSettings->gf_high) / 100.0f; 533 gGF_value = ((float)pDiveSettings->gf_high) / 100.0f;
545 //10 minutes steps 534 //10 minutes steps
546 while(gNDL < (300 * 60)) 535 while(ndl < (300 * 60))
547 { 536 {
548 memcpy(local_tissue_nitrogen_bar, gTissue_nitrogen_bar, (4*16)); 537 memcpy(local_tissue_nitrogen_bar, gTissue_nitrogen_bar, (4*16));
549 memcpy(local_tissue_helium_bar, gTissue_helium_bar, (4*16)); 538 memcpy(local_tissue_helium_bar, gTissue_helium_bar, (4*16));
550 // 539 //
551 gNDL += 600; 540 ndl += 600;
552 decom_tissues_exposure2(600, &pDiveSettings->decogaslist[gGas_id], gPressure,gTissue_nitrogen_bar,gTissue_helium_bar); 541 decom_tissues_exposure2(600, &pDiveSettings->decogaslist[gGas_id], gPressure,gTissue_nitrogen_bar,gTissue_helium_bar);
553 decom_oxygen_calculate_cns_exposure(600,&pDiveSettings->decogaslist[gGas_id],gPressure,&gCNS); 542 decom_oxygen_calculate_cns_exposure(600,&pDiveSettings->decogaslist[gGas_id],gPressure,&gCNS);
554 //tissues_exposure_at_gPressure_seconds(600);
555 buehlmann_backup_and_restore(true); 543 buehlmann_backup_and_restore(true);
556 if(dive1_check_deco(pDiveSettings)) 544 if(dive1_check_deco(pDiveSettings))
557 { 545 {
558 buehlmann_backup_and_restore(false); 546 buehlmann_backup_and_restore(false);
559 break; 547 break;
560 } 548 }
561 buehlmann_backup_and_restore(false); 549 buehlmann_backup_and_restore(false);
562 } 550 }
563 551
564 if(gNDL < (300 * 60)) 552 if(ndl < (300 * 60))
565 gNDL -= 600; 553 ndl -= 600;
566 554
567 if(gNDL > (150 * 60)) 555 if(ndl > (150 * 60))
568 return; 556 return ndl;
569 557
570 // refine 558 // refine
571 memcpy(gTissue_nitrogen_bar, local_tissue_nitrogen_bar, (4*16)); 559 memcpy(gTissue_nitrogen_bar, local_tissue_nitrogen_bar, (4*16));
572 memcpy(gTissue_helium_bar, local_tissue_helium_bar, (4*16)); 560 memcpy(gTissue_helium_bar, local_tissue_helium_bar, (4*16));
573 561
574 //One minutes step 562 //One minutes step
575 for(i = 0; i < 10; i++) 563 for(i = 0; i < 10; i++)
576 { 564 {
577 gNDL += 60; 565 ndl += 60;
578 //tissues_exposure_at_gPressure_seconds(60); 566 //tissues_exposure_at_gPressure_seconds(60);
579 decom_tissues_exposure2(60, &pDiveSettings->decogaslist[gGas_id], gPressure,gTissue_nitrogen_bar,gTissue_helium_bar); 567 decom_tissues_exposure2(60, &pDiveSettings->decogaslist[gGas_id], gPressure,gTissue_nitrogen_bar,gTissue_helium_bar);
580 decom_oxygen_calculate_cns_exposure(60,&pDiveSettings->decogaslist[gGas_id],gPressure,&gCNS); 568 decom_oxygen_calculate_cns_exposure(60,&pDiveSettings->decogaslist[gGas_id],gPressure,&gCNS);
581 buehlmann_backup_and_restore(true); 569 buehlmann_backup_and_restore(true);
582 if(dive1_check_deco(pDiveSettings)) 570 if(dive1_check_deco(pDiveSettings))
583 break; 571 break;
584 buehlmann_backup_and_restore(false); 572 buehlmann_backup_and_restore(false);
585 } 573 }
574 return ndl;
586 } 575 }
587 576
588 577
589 // =============================================================================== 578 // ===============================================================================
590 // dive1_check_deco 579 // dive1_check_deco