diff Discovery/Src/data_exchange_main.c @ 177:458f16cda15c

Merged in janlmulder/ostc4/max-depth (pull request #3) Bugfix: make max depth move with current depth Approved-by: heinrichsweikamp <bitbucket@heinrichsweikamp.com>
author heinrichsweikamp <bitbucket@heinrichsweikamp.com>
date Tue, 12 Mar 2019 15:04:19 +0000
parents 05c770dc2911
children 8b8074080d7b
line wrap: on
line diff
--- a/Discovery/Src/data_exchange_main.c	Sun Mar 10 20:30:42 2019 +0100
+++ b/Discovery/Src/data_exchange_main.c	Tue Mar 12 15:04:19 2019 +0000
@@ -73,7 +73,7 @@
 #include "externLogbookFlash.h"
 
 
-/* Expoted variables --------------------------------------------------------*/
+/* Exported variables --------------------------------------------------------*/
 uint8_t	wasPowerOn = 0;
 confirmbit8_Type requestNecessary = { .uw = 0 };
 uint8_t wasUpdateNotPowerOn = 0;
@@ -749,10 +749,31 @@
 	}
 }
 
+#define AVERAGE_COUNT	4
+static float getSampleDepth(SDataExchangeSlaveToMaster *d, SDiveState *ds)
+{
+	static uint8_t c = 0;
+	static float ambient[AVERAGE_COUNT] = {0};
+	static float surface[AVERAGE_COUNT]= {0};
+	static float depth[AVERAGE_COUNT]= {0};
+
+	ambient[c] = d->data[d->boolPressureData].pressure_mbar / 1000.0f;
+	surface[c] = d->data[d->boolPressureData].surface_mbar / 1000.0f;
+	float density = ((float)( 100 + settingsGetPointer()->salinity)) / 100.0f;
+
+	ds->lifeData.pressure_ambient_bar = (ambient[0] + ambient[1] + ambient[2] + ambient[3])/4.0f;
+	ds->lifeData.pressure_surface_bar = (surface[0] + surface[1] + surface[2] + surface[3])/4.0f;
+	depth[c] = (ambient[c] - surface[c]) / (0.09807f * density);
+
+	c++;
+	if (c == AVERAGE_COUNT) c = 0;
+
+	return (depth[0] + depth[1] + depth[2] + depth[3])/4.0f;
+}
 
 void DataEX_copy_to_LifeData(_Bool *modeChangeFlag)
 {
-	SDiveState * pStateReal = stateRealGetPointerWrite();
+	SDiveState *pStateReal = stateRealGetPointerWrite();
 	static uint16_t getDeviceDataAfterStartOfMainCPU = 20;
 	
 	/* internal sensor: HUD data
@@ -780,12 +801,6 @@
 		}
 	}
 	
-/* Why? hw 8.6.2015
-	if(DataEX_check_header_and_footer_ok() && dataIn.power_on_reset)
-	{
-		return;
-	}
-*/
 	if(!DataEX_check_header_and_footer_ok())
 	{
 		if(DataEX_check_header_and_footer_devicedata())
@@ -838,22 +853,11 @@
 		{
 			setButtonResponsiveness(settingsGetPointer()->ButtonResponsiveness);
 		}
-/*
-	}
-		if((dataIn.confirmRequest.ub.clearDeco != 1) && (requestNecessary.ub.clearDeco == 1))
-		{
-			clearDeco(); // is dataOut.clearDecoNow = 1;
-		}
-*/		
 	}
 	requestNecessary.uw = 0; // clear all 
 	
-	float ambient, surface, density, meter;
+	float meter = 0;
 	SSettings *pSettings;
-	
-	ambient = 0;
-	surface = 0;
-	meter = 0;
 
 	/*	uint8_t IAmStolenPleaseKillMe;
 	 */
@@ -869,20 +873,11 @@
 
 	if(pStateReal->data_old__lost_connection_to_slave == 0)
 	{
-		ambient = dataIn.data[dataIn.boolPressureData].pressure_mbar / 1000.0f;
-		surface = dataIn.data[dataIn.boolPressureData].surface_mbar / 1000.0f;
-
-		density = ((float)( 100 + pSettings->salinity)) / 100.0f;
-		meter = (ambient - surface);
-		meter /= (0.09807f * density);
-
+		meter = getSampleDepth(&dataIn, pStateReal);
 
 		pStateReal->pressure_uTick_old = pStateReal->pressure_uTick_new;
 		pStateReal->pressure_uTick_new = dataIn.data[dataIn.boolPressureData].pressure_uTick;
 		pStateReal->pressure_uTick_local_new = HAL_GetTick();
-
-		/* what was the code behind this if statement ? */
-		/* if(ambient < (surface + 0.04f)) */
 	
 		pStateReal->lifeData.dateBinaryFormat = dataIn.data[dataIn.boolTimeData].localtime_rtc_dr;
 		pStateReal->lifeData.timeBinaryFormat = dataIn.data[dataIn.boolTimeData].localtime_rtc_tr;
@@ -933,8 +928,6 @@
 		pStateReal->mode = dataIn.mode;
 		pStateReal->chargeStatus = dataIn.chargeStatus;
 	
-		pStateReal->lifeData.pressure_ambient_bar = ambient;
-		pStateReal->lifeData.pressure_surface_bar = surface;
 		if(is_ambient_pressure_close_to_surface(&pStateReal->lifeData))
 		{
 			pStateReal->lifeData.depth_meter = 0;
@@ -972,7 +965,6 @@
 			if (pStateReal->lifeData.compass_heading < 0) pStateReal->lifeData.compass_heading +=360.0;
 		}
 
-
 		pStateReal->lifeData.compass_roll = dataIn.data[dataIn.boolCompassData].compass_roll;
 		pStateReal->lifeData.compass_pitch = dataIn.data[dataIn.boolCompassData].compass_pitch;
 
@@ -1056,7 +1048,7 @@
 			{
 				pStateReal->lifeData.apnea_last_max_depth_meter = pStateReal->lifeData.max_depth_meter;
 			}
-		// eset max_depth_meter, average_depth_meter and internal values
+		// reset max_depth_meter, average_depth_meter and internal values
 			pStateReal->lifeData.max_depth_meter = 0;
 			pStateReal->lifeData.boolResetAverageDepth = 1;
 			pStateReal->lifeData.boolResetStopwatch = 1;