changeset 262:f4c16ea0354a

Merged in janlmulder/ostc4/bm-3 (pull request #19) Buelhmann cleanup , 2 small functional changes, and 1 bugfix
author heinrichsweikamp <bitbucket@heinrichsweikamp.com>
date Tue, 23 Apr 2019 14:21:32 +0000
parents 1b9be6040d82 (current diff) cc2406b835ff (diff)
children cf6ad20380fb
files
diffstat 4 files changed, 122 insertions(+), 219 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/.hgtags	Tue Apr 23 14:21:32 2019 +0000
@@ -0,0 +1,1 @@
+1b9be6040d8279117c7fc99d528790611638f10b V1.4.7 release
--- a/Discovery/Src/buehlmann.c	Sat Apr 13 14:36:33 2019 +0200
+++ b/Discovery/Src/buehlmann.c	Tue Apr 23 14:21:32 2019 +0000
@@ -9,30 +9,16 @@
 */
 
 #include <string.h>
-//#include "arm_math.h"
 #include <math.h>
 #include <stdbool.h>
 #include "buehlmann.h"
 #include "decom.h"
 
-
 extern const float buehlmann_N2_a[];
 extern const float buehlmann_N2_b[];
 extern const float buehlmann_He_a[];
 extern const float buehlmann_He_b[];
 
-
-/*
-typedef struct
-{
-	float *pointer_array_tissue_nitrogen_bar;
-	float *pointer_array_tissue_helium_bar;
-	char gf_value;
-
-	float output_ceiling_ambient_bar_or_input;
-	_Bool output_ceiling_tolerated_if_ceiling_used_as_input;
-}		tissue_test_tolerance_struct;
-*/
 typedef struct
 {
 	float depth;
@@ -47,74 +33,45 @@
 # define PRESSURE_THREE_METER 0.333334f
 # define PRESSURE_150_CM 0.15f
 # define PRESSURE_HALF_METER 0.05f
-/*
-# define PRESSURE_150_CM_MBAR 150
-# define PRESSURE_TWO_M_MBAR 200
-# define PRESSURE_FIVE_M_MBAR 500
-# define PRESSURE_TEN_M_MBAR 1000
-# define PRESSURE_120_METER 12.0
-*/
-/*
-_____________________________________________________________
-*/
-
-
-void buehlmann_backup_and_restore(_Bool backup_restore_otherwise);
-float tissue_tolerance(void);
-void ambient_bar_to_deco_stop_depth_bar(float ceiling);
-int ascend_with_all_gaschanges(float pressure_decrease);
-float next_stop_depth_input_is_actual_stop_id(int actual_id);
-float get_gf_at_pressure(float pressure);
-void buehlmann_calc_ndl(void);
-_Bool dive1_check_deco(void);
 
-/*
-_____________________________________________________________
-*/
+static void buehlmann_backup_and_restore(_Bool backup_restore_otherwise);
+static float tissue_tolerance(void);
+static void ambient_bar_to_deco_stop_depth_bar(SDiveSettings *pDiveSettings, float ceiling);
+static int ascend_with_all_gaschanges(SDiveSettings *pDiveSettings, float pressure_decrease);
+static float next_stop_depth_input_is_actual_stop_id(SDiveSettings *pDiveSettings, int actual_id);
+static float get_gf_at_pressure(SDiveSettings *pDiveSettings, float pressure);
+static int buehlmann_calc_ndl(SDiveSettings *pDiveSettings);
+static _Bool dive1_check_deco(SDiveSettings *pDiveSettings);
 
-SDecoinfo gDecotable;
-float gSurface_pressure_bar;
-float gPressure;
-int gGas_id;
-float gTTS;
-float gTissue_nitrogen_bar[16];
-float gTissue_helium_bar[16];
-float gGF_value;
-float gCNS;
-//float gMax_ceiling_bar = 0;
-int gNDL;
+static float gSurface_pressure_bar;
+static float gPressure;
+static int gGas_id;
+static float gTissue_nitrogen_bar[16];
+static float gTissue_helium_bar[16];
+static float gGF_value;
+static float gCNS;
 
-
-//SLifeData *pLifeData;
-SDiveSettings *pBuDiveSettings;
-SDecoinfo* pDecolistBuehlmann;
-//signed char gGaschange_decreasing_depth_gas_id[BUEHLMANN_STRUCT_MAX_GASES];
 float gGF_low_depth_bar;
 SStop gStop;
 
 void buehlmann_init(void)
 {
-  //gMax_ceiling_bar = 0;
 }
 
-void buehlmann_backup_and_restore(_Bool backup_restore_otherwise)
+static void buehlmann_backup_and_restore(_Bool backup_restore_otherwise)
 {
 	static float pressure;
 	static float gas_id;
-	static float tts;
 	static float tissue_nitrogen_bar[16];
 	static float tissue_helium_bar[16];
 	static float gf_value;
-	static int ndl;
 	static float cns;
 
 	if(backup_restore_otherwise)
 	{
 		pressure = gPressure;
 		gas_id = gGas_id;
-		tts = gTTS;
 		gf_value = gGF_value;
-		ndl = gNDL;
 		cns = gCNS;
 		memcpy(tissue_nitrogen_bar, gTissue_nitrogen_bar, (4*16));
 		memcpy(tissue_helium_bar, gTissue_helium_bar, (4*16));
@@ -123,32 +80,13 @@
 	{
 		gPressure = pressure;
 		gGas_id = gas_id;
-		gTTS = tts;
 		gGF_value = gf_value;
-		gNDL = ndl;
 		gCNS = cns;
 		memcpy(gTissue_nitrogen_bar, tissue_nitrogen_bar, (4*16));
 		memcpy(gTissue_helium_bar, tissue_helium_bar, (4*16));
 	}
 
 }
-/*void buehlmann__test__saturate_tissues(SBuehlmann *pInput,  int seconds)
-{
-	pBuehlmann = pInput;
-	pInput->dive_time_seconds += seconds;
-	// internal copying
-	gSurface_pressure_bar = pBuehlmann->pressure_surface_bar;
-
-	gPressure = pBuehlmann->pressure_ambient_bar;
-	gGas_id = pBuehlmann->actual_gas_id;
-	memcpy(gTissue_nitrogen_bar, pBuehlmann->tissue_nitrogen_bar, (4*16));
-	memcpy(gTissue_helium_bar, pBuehlmann->tissue_helium_bar, (4*16));
-
-	tissues_exposure_at_gPressure_seconds(seconds);
-
-	memcpy(pBuehlmann->tissue_nitrogen_bar, gTissue_nitrogen_bar, (4*16));
-	memcpy(pBuehlmann->tissue_helium_bar, gTissue_helium_bar, (4*16));
-}*/
 
 float buehlmann_get_gCNS(void)
 {
@@ -163,23 +101,17 @@
 	float pressure_delta;
 	float next_depth;
 	_Bool deco_reached = false;
-//	tissue_test_tolerance_struct tolerance_data;
 	unsigned short *stoplist;
 	int i;
-	
-   // decom_CreateGasChangeList(pDiveSettings, pLifeData);
 
 	gCNS = 0;
-  pDecoInfo->output_time_to_surface_seconds = 0;
+	pDecoInfo->output_time_to_surface_seconds = 0;
 	pDecoInfo->output_ndl_seconds = 0;
 	for(int i=0;i<DECOINFO_STRUCT_MAX_STOPS;i++)
 	{
 		pDecoInfo->output_stop_length_seconds[i] = 0;
 	}
-	/* make input available global*/
-	pBuDiveSettings = pDiveSettings;
 
-  pDecolistBuehlmann = pDecoInfo;
 	/* internal copying */
 	gSurface_pressure_bar = pLifeData->pressure_surface_bar;
 
@@ -187,59 +119,42 @@
 	gGas_id = 0;
 	memcpy(gTissue_nitrogen_bar, pLifeData->tissue_nitrogen_bar, (4*16));
 	memcpy(gTissue_helium_bar, pLifeData->tissue_helium_bar, (4*16));
-	gGF_value = ((float)pBuDiveSettings->gf_low) / 100.0f;
+	gGF_value = ((float)pDiveSettings->gf_low) / 100.0f;
 	
-	//
-	memcpy(&gDecotable, pDecolistBuehlmann, sizeof(SDecoinfo));
-	stoplist = gDecotable.output_stop_length_seconds;
+	stoplist = pDecoInfo->output_stop_length_seconds;
 
-
-  if(pLifeData->dive_time_seconds < 60)
-    return;
-	/* coupling */
-
-	/* functions */
+	if(pLifeData->dive_time_seconds < 60)
+		return;
 
 	// clean stop list
 	for(i = 0; i < DECOINFO_STRUCT_MAX_STOPS; i++)
 		stoplist[i] = 0;
-	gTTS = 0;
-	gNDL = 0;
 
-  if(pDiveSettings->internal__pressure_first_stop_ambient_bar_as_upper_limit_for_gf_low_otherwise_zero >= (gPressure - PRESSURE_150_CM))
-  {
-    deco_reached = true;
-  }
-
+	if(pDiveSettings->internal__pressure_first_stop_ambient_bar_as_upper_limit_for_gf_low_otherwise_zero >= (gPressure - PRESSURE_150_CM))
+	{
+		deco_reached = true;
+	}
 
-//ascend_with_all_gaschanges(gPressure - gSurface_pressure_bar);
-	gGF_value = ((float)pBuDiveSettings->gf_high) / 100.0f;
-	//iling = tissue_tolerance();
-	// includes backup for gGF_value
-	// NDL
-	buehlmann_backup_and_restore(true); // includes backup for gGF_value
-	if(!dive1_check_deco() )
+	gGF_value = ((float)pDiveSettings->gf_high) / 100.0f;
+	buehlmann_backup_and_restore(true);
+	if(!dive1_check_deco(pDiveSettings) )
 	{
-	  buehlmann_backup_and_restore(false);
+		buehlmann_backup_and_restore(false);
 		// no deco
-		pDecolistBuehlmann->output_time_to_surface_seconds = 0;
+	  	pDecoInfo->output_time_to_surface_seconds = 0;
 		for(i = 0; i < DECOINFO_STRUCT_MAX_STOPS; i++)
-			pDecolistBuehlmann->output_stop_length_seconds[i] = 0;
+			pDecoInfo->output_stop_length_seconds[i] = 0;
 		// calc NDL
-		buehlmann_calc_ndl();
-		pDecolistBuehlmann->output_ndl_seconds = gNDL;
+		pDecoInfo->output_ndl_seconds = buehlmann_calc_ndl(pDiveSettings);;
 		return;
 	}
 	buehlmann_backup_and_restore(false);
-	pDecolistBuehlmann->output_ndl_seconds = 0;
+	pDecoInfo->output_ndl_seconds = 0;
 
-  gGF_value = get_gf_at_pressure(gPressure);
+	gGF_value = get_gf_at_pressure(pDiveSettings, gPressure);
 	//current ceiling at actual position
 	ceiling = tissue_tolerance();
-	//if(ceiling < pDiveSettings->internal__pressure_first_stop_ambient_bar_as_upper_limit_for_gf_low_otherwise_zero)
-		//ambient_bar_to_deco_stop_depth_bar(pDiveSettings->internal__pressure_first_stop_ambient_bar_as_upper_limit_for_gf_low_otherwise_zero);
-	//else
-  ambient_bar_to_deco_stop_depth_bar(ceiling);
+	ambient_bar_to_deco_stop_depth_bar(pDiveSettings, ceiling);
 
 	// set the base for all upcoming parameters
 	ceiling = gStop.depth + gSurface_pressure_bar;
@@ -251,29 +166,27 @@
 		// ascend within 10 mtr to GF_low // speed 12 mtr/min -> 50 sec / 10 mtr;  15 sec / 3 mtr.
 		if(ceiling < (gPressure - PRESSURE_TEN_METER) )
 		{ do {
-				ascend_time = ascend_with_all_gaschanges(PRESSURE_TEN_METER);
+				ascend_time = ascend_with_all_gaschanges(pDiveSettings, PRESSURE_TEN_METER);
 				tts_seconds += ascend_time;
 				ceiling = tissue_tolerance();
 				if(tts_seconds > DECO_STOPS_MAX_TTS_CALCULATON_IN_SECONDS)
 			{
-				/* pInput == pBuehlmann */
-				pDecolistBuehlmann->output_time_to_surface_seconds = NINETY_NINE_MINUTES_IN_SECONDS;
+				pDecoInfo->output_time_to_surface_seconds = NINETY_NINE_MINUTES_IN_SECONDS;
 				return;// NINETY_NINE_MINUTES_IN_SECONDS;
 			}
 			} while ((ascend_time > 0 ) && ((gPressure - PRESSURE_TEN_METER ) > gSurface_pressure_bar) && (ceiling < (gPressure - PRESSURE_TEN_METER)));
 		}
 		do {
 			buehlmann_backup_and_restore(true);
-			ascend_time = ascend_with_all_gaschanges(PRESSURE_THREE_METER);
+			ascend_time = ascend_with_all_gaschanges(pDiveSettings, PRESSURE_THREE_METER);
 			tts_seconds += ascend_time;
 			ceiling = tissue_tolerance();
 			if(tts_seconds > DECO_STOPS_MAX_TTS_CALCULATON_IN_SECONDS)
 			{
-				/* pInput == pBuehlmann */
-				pDecolistBuehlmann->output_time_to_surface_seconds = NINETY_NINE_MINUTES_IN_SECONDS;
+				pDecoInfo->output_time_to_surface_seconds = NINETY_NINE_MINUTES_IN_SECONDS;
 				return;// NINETY_NINE_MINUTES_IN_SECONDS;
 			}
-			ambient_bar_to_deco_stop_depth_bar(ceiling);
+			ambient_bar_to_deco_stop_depth_bar(pDiveSettings, ceiling);
 		} while ((ascend_time > 0 ) &&  ((gStop.depth + gSurface_pressure_bar) < gPressure));
 
 		if(gStop.depth + gSurface_pressure_bar > gPressure)
@@ -290,28 +203,23 @@
 		// initial values, upper code might not be executed (is within 150 cm)
 	}
 
-
-
-
-  if(ceiling > gSurface_pressure_bar)
-  {
-
-    ceiling = gStop.depth + gSurface_pressure_bar;
-    // ascend the last meters to first stop (especially consider any gas changes around)
-    pressure_delta = gPressure - ceiling;
-    ascend_time = (int)ceil(pressure_delta * 50.0f);
-    tts_seconds += ascend_with_all_gaschanges(pressure_delta);
-  }
+	if (ceiling > gSurface_pressure_bar)
+	{
+		ceiling = gStop.depth + gSurface_pressure_bar;
+		// ascend the last meters to first stop (especially consider any gas changes around)
+		pressure_delta = gPressure - ceiling;
+		ascend_time = (int) ceil(pressure_delta * 50.0f);
+		tts_seconds += ascend_with_all_gaschanges(pDiveSettings, pressure_delta);
+	}
 	// NDL check
 	if(ceiling <= gSurface_pressure_bar)
 	{
-		/* pInput == pBuehlmann  same pointer*/
 		// NDL with GF_low
-		pDecolistBuehlmann->output_time_to_surface_seconds = 0;
+		pDecoInfo->output_time_to_surface_seconds = 0;
 		return;
 	}
-  if(ceiling >pDiveSettings->internal__pressure_first_stop_ambient_bar_as_upper_limit_for_gf_low_otherwise_zero)
-    pDiveSettings->internal__pressure_first_stop_ambient_bar_as_upper_limit_for_gf_low_otherwise_zero = ceiling;
+	if (ceiling > pDiveSettings->internal__pressure_first_stop_ambient_bar_as_upper_limit_for_gf_low_otherwise_zero)
+		pDiveSettings->internal__pressure_first_stop_ambient_bar_as_upper_limit_for_gf_low_otherwise_zero = ceiling;
 
 	// calc gf loop
 	if(deco_reached)
@@ -323,16 +231,16 @@
 	{
 		do
 		{
-			next_depth = next_stop_depth_input_is_actual_stop_id(gStop.id);
-      gGF_value = get_gf_at_pressure(next_depth + gSurface_pressure_bar);
+			next_depth = next_stop_depth_input_is_actual_stop_id(pDiveSettings, gStop.id);
+			gGF_value = get_gf_at_pressure(pDiveSettings, next_depth + gSurface_pressure_bar);
 			buehlmann_backup_and_restore(true);
-			ascend_time = ascend_with_all_gaschanges(gStop.depth - next_depth);
+			ascend_time = ascend_with_all_gaschanges(pDiveSettings, gStop.depth - next_depth);
 			ceiling = tissue_tolerance();
 			/* pre check actual limit */
-			if(gDecotable.output_stop_length_seconds[gStop.id] >= 999*60)
+			if(pDecoInfo->output_stop_length_seconds[gStop.id] >= 999*60)
 			{
-				tts_seconds -= 999*60 - gDecotable.output_stop_length_seconds[gStop.id];
-				gDecotable.output_stop_length_seconds[gStop.id] = 999*60;
+				tts_seconds -= 999*60 - pDecoInfo->output_stop_length_seconds[gStop.id];
+				pDecoInfo->output_stop_length_seconds[gStop.id] = 999*60;
 			}
 			else
 			/* more deco on the actual depth */
@@ -340,19 +248,19 @@
 			{
 				next_depth = -1;
 				buehlmann_backup_and_restore(false);
-				decom_tissues_exposure2(10, &pBuDiveSettings->decogaslist[gGas_id], gPressure,gTissue_nitrogen_bar,gTissue_helium_bar); // some seconds at least at each stop
-				decom_oxygen_calculate_cns_exposure(10, &pBuDiveSettings->decogaslist[gGas_id], gPressure, &gCNS);
-        gDecotable.output_stop_length_seconds[gStop.id] += 10;
+				decom_tissues_exposure2(10, &pDiveSettings->decogaslist[gGas_id], gPressure,gTissue_nitrogen_bar,gTissue_helium_bar); // some seconds at least at each stop
+				decom_oxygen_calculate_cns_exposure(10, &pDiveSettings->decogaslist[gGas_id], gPressure, &gCNS);
+				pDecoInfo->output_stop_length_seconds[gStop.id] += 10;
         tts_seconds += 10;
 			}
 		} while(next_depth == -1);
-    tts_seconds += ascend_time;
+		tts_seconds += ascend_time;
 		gStop.depth = next_depth;
     for(i = gGas_id + 1; i < BUEHLMANN_STRUCT_MAX_GASES; i++)
     {
-        if(pBuDiveSettings->decogaslist[i].change_during_ascent_depth_meter_otherwise_zero == 0)
+        if(pDiveSettings->decogaslist[i].change_during_ascent_depth_meter_otherwise_zero == 0)
             break;
-        float pressureChange =  ((float)pBuDiveSettings->decogaslist[i].change_during_ascent_depth_meter_otherwise_zero) / 10;
+        float pressureChange =  ((float)pDiveSettings->decogaslist[i].change_during_ascent_depth_meter_otherwise_zero) / 10;
         if(gStop.depth <= pressureChange + 0.00001f)
         {
             gGas_id = i;
@@ -365,12 +273,11 @@
 		gStop.id--;
 	}
 
-  gDecotable.output_time_to_surface_seconds = tts_seconds;
-	memcpy(pDecolistBuehlmann, &gDecotable, sizeof(SDecoinfo));
+	pDecoInfo->output_time_to_surface_seconds = tts_seconds;
 }
 
 
-float tissue_tolerance(void)
+static float tissue_tolerance(void)
 {
 	float tissue_inertgas_saturation;
 	float inertgas_a;
@@ -445,7 +352,7 @@
 }
 
 
-float buehlmann_tissue_test_tolerance(float depth_in_bar_absolute)
+static float buehlmann_tissue_test_tolerance(float depth_in_bar_absolute)
 {
 	float tissue_inertgas_saturation;
 	float inertgas_a;
@@ -477,7 +384,7 @@
 }
 
 
-void ambient_bar_to_deco_stop_depth_bar(float ceiling)
+static void ambient_bar_to_deco_stop_depth_bar(SDiveSettings *pDiveSettings, float ceiling)
 {
 	int i;
 
@@ -485,59 +392,56 @@
 
 	if(ceiling <= 0)
 	{
-		gStop.depth = pBuDiveSettings->last_stop_depth_bar;
+		gStop.depth = pDiveSettings->last_stop_depth_bar;
 		gStop.id = 0;
 		return;
 	}
 
-
-    //for(int i = 1; i < 10; i++)
-
-	if((ceiling -  pBuDiveSettings->last_stop_depth_bar) <= 0)
+	if((ceiling -  pDiveSettings->last_stop_depth_bar) <= 0)
 	{
-		gStop.depth =  pBuDiveSettings->last_stop_depth_bar;
+		gStop.depth =  pDiveSettings->last_stop_depth_bar;
 		gStop.id = 0;
 		return;
 	}
 
-	gStop.depth = pBuDiveSettings->input_second_to_last_stop_depth_bar;
+	gStop.depth = pDiveSettings->input_second_to_last_stop_depth_bar;
 	gStop.id = 1;
-	ceiling -= pBuDiveSettings->input_second_to_last_stop_depth_bar;
+	ceiling -= pDiveSettings->input_second_to_last_stop_depth_bar;
 
 	if(ceiling <= 0)
 		return;
 
 	for(i = 1; i < (DECOINFO_STRUCT_MAX_STOPS - 2); i++)
 	{
-		ceiling -= pBuDiveSettings->input_next_stop_increment_depth_bar;
+		ceiling -= pDiveSettings->input_next_stop_increment_depth_bar;
 		if(ceiling <= 0)
 			break;
 	}
-	gStop.depth += i * pBuDiveSettings->input_next_stop_increment_depth_bar;
+	gStop.depth += i * pDiveSettings->input_next_stop_increment_depth_bar;
 	gStop.id += i;
 	return;
 }
 
-float next_stop_depth_input_is_actual_stop_id(int actual_id)
+static float next_stop_depth_input_is_actual_stop_id(SDiveSettings *pDiveSettings, int actual_id)
 {
 	if(actual_id == 0)
 		return 0;
 
 	if(actual_id == 1)
-		return pBuDiveSettings->last_stop_depth_bar;
+		return pDiveSettings->last_stop_depth_bar;
 
 	actual_id -= 2;
-	return pBuDiveSettings->input_second_to_last_stop_depth_bar + (actual_id * pBuDiveSettings->input_next_stop_increment_depth_bar);
+	return pDiveSettings->input_second_to_last_stop_depth_bar + (actual_id * pDiveSettings->input_next_stop_increment_depth_bar);
 }
 
-int ascend_with_all_gaschanges(float pressure_decrease)
+static int ascend_with_all_gaschanges(SDiveSettings *pDiveSettings, float pressure_decrease)
 {
 	float pressureTop, pressureTop_tmp, pressureBottom, pressureChange, ascendrate_in_seconds_for_one_bar, pressure_difference;
 	int time_for_ascend = 0;
 	int seconds;
 	int i;
 
-	ascendrate_in_seconds_for_one_bar = 60 * 10 / pBuDiveSettings->ascentRate_meterperminute;
+	ascendrate_in_seconds_for_one_bar = 60 * 10 / pDiveSettings->ascentRate_meterperminute;
 
 	if(fabsf(gPressure - gSurface_pressure_bar) < PRESSURE_HALF_METER)
 	{
@@ -554,9 +458,9 @@
         pressureTop_tmp = pressureTop;
         for(i = gGas_id + 1; i < BUEHLMANN_STRUCT_MAX_GASES; i++)
         {
-            if(pBuDiveSettings->decogaslist[i].change_during_ascent_depth_meter_otherwise_zero == 0)
+            if(pDiveSettings->decogaslist[i].change_during_ascent_depth_meter_otherwise_zero == 0)
                 break;
-            pressureChange = gSurface_pressure_bar + ((float)pBuDiveSettings->decogaslist[i].change_during_ascent_depth_meter_otherwise_zero) / 10;
+            pressureChange = gSurface_pressure_bar + ((float)pDiveSettings->decogaslist[i].change_during_ascent_depth_meter_otherwise_zero) / 10;
             if(pressureBottom <= pressureChange)
             {
                 gGas_id = i;
@@ -569,9 +473,9 @@
         }
         for(i = gGas_id + 1; i < BUEHLMANN_STRUCT_MAX_GASES; i++)
         {
-            if(pBuDiveSettings->decogaslist[i].change_during_ascent_depth_meter_otherwise_zero == 0)
+            if(pDiveSettings->decogaslist[i].change_during_ascent_depth_meter_otherwise_zero == 0)
                 break;
-            pressureChange = gSurface_pressure_bar + ((float)pBuDiveSettings->decogaslist[i].change_during_ascent_depth_meter_otherwise_zero)/ 10;
+            pressureChange = gSurface_pressure_bar + ((float)pDiveSettings->decogaslist[i].change_during_ascent_depth_meter_otherwise_zero)/ 10;
             if((pressureChange < pressureBottom) && (pressureChange > pressureTop))
             {
                 pressureTop_tmp = pressureChange;
@@ -581,9 +485,9 @@
         if(pressure_difference > 0.0001f)
         {
           time_for_ascend = (int)ceilf(pressure_difference * ascendrate_in_seconds_for_one_bar);
-          decom_tissues_exposure_stage_schreiner(time_for_ascend, &pBuDiveSettings->decogaslist[gGas_id],
+          decom_tissues_exposure_stage_schreiner(time_for_ascend, &pDiveSettings->decogaslist[gGas_id],
                                               pressureBottom, pressureTop_tmp, gTissue_nitrogen_bar, gTissue_helium_bar);
-					decom_oxygen_calculate_cns_stage_SchreinerStyle(time_for_ascend,&pBuDiveSettings->decogaslist[gGas_id],
+					decom_oxygen_calculate_cns_stage_SchreinerStyle(time_for_ascend,&pDiveSettings->decogaslist[gGas_id],
                                               pressureBottom, pressureTop_tmp, &gCNS);
         }
         pressureBottom = pressureTop_tmp;
@@ -594,47 +498,46 @@
 }
 
 
-float get_gf_at_pressure(float pressure)
+static float get_gf_at_pressure(SDiveSettings *pDiveSettings, float pressure)
 {
 	float gfSteigung = 0.0f;
 
 	if(gGF_low_depth_bar < 0)
 			gGF_low_depth_bar = PRESSURE_THREE_METER; // just to prevent erratic behaviour if variable is not set
 
-    gfSteigung = ((float)(pBuDiveSettings->gf_high - pBuDiveSettings->gf_low))/ gGF_low_depth_bar;
+    gfSteigung = ((float)(pDiveSettings->gf_high - pDiveSettings->gf_low))/ gGF_low_depth_bar;
 
 
 	if((pressure - gSurface_pressure_bar) <= PRESSURE_HALF_METER)
-		return ((float)pBuDiveSettings->gf_high) / 100.0f;
+		return ((float)pDiveSettings->gf_high) / 100.0f;
 
 	if(pressure >= gSurface_pressure_bar + gGF_low_depth_bar)
-		return ((float)pBuDiveSettings->gf_low) / 100.0f;
+		return ((float)pDiveSettings->gf_low) / 100.0f;
 
-	return (pBuDiveSettings->gf_high - gfSteigung * (pressure - gSurface_pressure_bar) )/ 100.0f;
+	return (pDiveSettings->gf_high - gfSteigung * (pressure - gSurface_pressure_bar) )/ 100.0f;
 }
 
 
-void buehlmann_calc_ndl(void)
+static int buehlmann_calc_ndl(SDiveSettings *pDiveSettings)
 {
 	float local_tissue_nitrogen_bar[16];
 	float local_tissue_helium_bar[16];
 	int i;
+	int ndl = 0;
 
-	gNDL = 0;
 	//Check ndl always use gHigh
-  gGF_value = ((float)pBuDiveSettings->gf_high) / 100.0f;
+	gGF_value = ((float)pDiveSettings->gf_high) / 100.0f;
 	//10 minutes steps
-	while(gNDL < (300 * 60))
+	while(ndl < (300 * 60))
 	{
 		memcpy(local_tissue_nitrogen_bar, gTissue_nitrogen_bar, (4*16));
 		memcpy(local_tissue_helium_bar, gTissue_helium_bar, (4*16));
 		//
-		gNDL += 600;
-    decom_tissues_exposure2(600, &pBuDiveSettings->decogaslist[gGas_id], gPressure,gTissue_nitrogen_bar,gTissue_helium_bar);
-		decom_oxygen_calculate_cns_exposure(600,&pBuDiveSettings->decogaslist[gGas_id],gPressure,&gCNS);
-		//tissues_exposure_at_gPressure_seconds(600);
+		ndl += 600;
+		decom_tissues_exposure2(600, &pDiveSettings->decogaslist[gGas_id], gPressure,gTissue_nitrogen_bar,gTissue_helium_bar);
+		decom_oxygen_calculate_cns_exposure(600,&pDiveSettings->decogaslist[gGas_id],gPressure,&gCNS);
 		buehlmann_backup_and_restore(true);
-		if(dive1_check_deco() == true)
+		if(dive1_check_deco(pDiveSettings))
 		{
 			buehlmann_backup_and_restore(false);
 			break;
@@ -642,30 +545,29 @@
 		buehlmann_backup_and_restore(false);
 	}
 
-	if(gNDL < (300 * 60))
-		gNDL -= 600;
+	if(ndl < (300 * 60))
+		ndl -= 600;
 
-	if(gNDL > (150 * 60))
-		return;
+	if(ndl > (150 * 60))
+		return ndl;
 
 	// refine
 	memcpy(gTissue_nitrogen_bar, local_tissue_nitrogen_bar, (4*16));
 	memcpy(gTissue_helium_bar, local_tissue_helium_bar, (4*16));
 
 	//One minutes step
-	for(i = 0; i < 20; i++)
+	for(i = 0; i < 10; i++)
 	{
-		gNDL += 60;
+		ndl += 60;
 		//tissues_exposure_at_gPressure_seconds(60);
-		decom_tissues_exposure2(60, &pBuDiveSettings->decogaslist[gGas_id], gPressure,gTissue_nitrogen_bar,gTissue_helium_bar);
-		decom_oxygen_calculate_cns_exposure(60,&pBuDiveSettings->decogaslist[gGas_id],gPressure,&gCNS);
+		decom_tissues_exposure2(60, &pDiveSettings->decogaslist[gGas_id], gPressure,gTissue_nitrogen_bar,gTissue_helium_bar);
+		decom_oxygen_calculate_cns_exposure(60,&pDiveSettings->decogaslist[gGas_id],gPressure,&gCNS);
 		buehlmann_backup_and_restore(true);
-		if(dive1_check_deco() == true)
+		if(dive1_check_deco(pDiveSettings))
 			break;
 		buehlmann_backup_and_restore(false);
 	}
-	//gNDL -= 60;
-	return;
+	return ndl;
 }
 
 
@@ -674,7 +576,7 @@
 /// @brief	for NDL calculations
 ///					160614 using ceilingOther and not ceiling
 //  ===============================================================================
-_Bool dive1_check_deco(void)
+static _Bool dive1_check_deco(SDiveSettings *pDiveSettings)
 {
 	// gGF_value is set in call routine;
 	// internes Backup!
@@ -684,7 +586,7 @@
 	float ceilingOther; // new hw 160614
 
 	ceiling = tissue_tolerance();
-	ambient_bar_to_deco_stop_depth_bar(ceiling); // this will set gStop.depth :-) (and gStop.id)
+	ambient_bar_to_deco_stop_depth_bar(pDiveSettings, ceiling); // this will set gStop.depth :-) (and gStop.id)
 
 	// set the base for all upcoming parameters
 	ceilingOther = gStop.depth + gSurface_pressure_bar;
@@ -695,19 +597,16 @@
 		// ascend within 10 mtr to GF_low // speed 12 mtr/min -> 50 sec / 10 mtr;  15 sec / 3 mtr.
 		while(((gPressure - PRESSURE_TEN_METER ) > gSurface_pressure_bar) && (ceiling < (gPressure - PRESSURE_TEN_METER)))
 		{
-			ascend_with_all_gaschanges(PRESSURE_TEN_METER);
+			ascend_with_all_gaschanges(pDiveSettings, PRESSURE_TEN_METER);
 			ceiling = tissue_tolerance();
 		}
 		while(((gPressure - PRESSURE_THREE_METER )> gSurface_pressure_bar) && (ceiling < gPressure))
 		{
-			ascend_with_all_gaschanges(PRESSURE_THREE_METER);
+			ascend_with_all_gaschanges(pDiveSettings, PRESSURE_THREE_METER);
 			ceiling = tissue_tolerance();
 		}
 	}
-	if(ceiling <= gSurface_pressure_bar)
-		return false;
-	else
-		return true;
+	return ceiling > gSurface_pressure_bar;
 }
 
 // compute ceiling recursively, with a resolution of 10cm. Notice
@@ -731,6 +630,9 @@
 {
 	float ceiling;
 
+	memcpy(gTissue_nitrogen_bar, pLifeData->tissue_nitrogen_bar, (4*16));
+	memcpy(gTissue_helium_bar, pLifeData->tissue_helium_bar, (4*16));
+
 	// this is just performance optimizing. The code below runs just fine
 	// without this. There is never a ceiling in NDL deco state
 	if (!pDecoInfo->output_time_to_surface_seconds) {
@@ -738,9 +640,6 @@
 		return;
 	}
 
-	memcpy(gTissue_nitrogen_bar, pLifeData->tissue_nitrogen_bar, (4*16));
-	memcpy(gTissue_helium_bar, pLifeData->tissue_helium_bar, (4*16));
-
 	ceiling = compute_ceiling(pLifeData->pressure_surface_bar, 1.0f + pLifeData->max_depth_meter/10.0f);
 	pDecoInfo->output_ceiling_meter = (ceiling - pLifeData->pressure_surface_bar) * 10.0f;
 }
--- a/Discovery/Src/settings.c	Sat Apr 13 14:36:33 2019 +0200
+++ b/Discovery/Src/settings.c	Tue Apr 23 14:21:32 2019 +0000
@@ -1155,7 +1155,7 @@
 
 /*	uint8_t tX_userselectedLeftLowerCornerPrimary;
  */
-    if(Settings.tX_userselectedLeftLowerCornerPrimary > 7)
+    if(Settings.tX_userselectedLeftLowerCornerPrimary > 8)
     {
         Settings.tX_userselectedLeftLowerCornerPrimary = 1;
         corrections++;
--- a/Discovery/Src/tMenuEditSystem.c	Sat Apr 13 14:36:33 2019 +0200
+++ b/Discovery/Src/tMenuEditSystem.c	Tue Apr 23 14:21:32 2019 +0000
@@ -862,6 +862,9 @@
     case 7:
         text[4] = TXT_CNS;
         break;
+    case 8:
+    	text[4] = TXT_ActualGradient;
+    	break;
     /* none */
     case 0:
         text[4] = '-';
@@ -1008,7 +1011,7 @@
 
     value += 1;
 
-    if(value > 7)
+    if(value > 8)
         value = 0;
 
     settingsGetPointer()->tX_userselectedLeftLowerCornerPrimary = value;