changeset 270:2e58a4094770 write-from-sim

feature, debug: make simulator write a logbook entry When compiling the code with -DSIM_WRITES_LOGBOOK, the simulator writes to the logbook. This is for debug purpose only. This commit does *not* define this SIM_WRITES_LOGBOOK, so when compiled, things are functionally unchanged. Caveat 1: a simulator generated log cannot be advanced with +5 min. It needs to run in real time. Caveat 2: The generated log is currently not "complete". For example, CCR setpoint switches are not logged. There are likely more small events not logged. This means that a sim generated log is not a full replacement for real dive testing. Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
author Jan Mulder <jlmulder@xs4all.nl>
date Wed, 24 Apr 2019 17:10:51 +0200
parents 6e78137952af
children 1303747b5ba2
files Common/Inc/data_central.h Discovery/Inc/logbook.h Discovery/Src/base.c Discovery/Src/data_central.c Discovery/Src/data_exchange_main.c Discovery/Src/logbook.c
diffstat 6 files changed, 28 insertions(+), 32 deletions(-) [+]
line wrap: on
line diff
--- a/Common/Inc/data_central.h	Tue Apr 23 21:03:24 2019 +0200
+++ b/Common/Inc/data_central.h	Wed Apr 24 17:10:51 2019 +0200
@@ -454,7 +454,7 @@
 int current_second(void);
 _Bool vpm_crush(SDiveState* pDiveState);
 _Bool deco_zone_reached(void);
-void resetEvents(void);
+void resetEvents(const SDiveState *pStateUsed);
 
 uint32_t crc32c_checksum(uint8_t* message, uint16_t length, uint8_t* message2, uint16_t length2);
 uint32_t	CRC_CalcBlockCRC(uint32_t *buffer, uint32_t words);
--- a/Discovery/Inc/logbook.h	Tue Apr 23 21:03:24 2019 +0200
+++ b/Discovery/Inc/logbook.h	Wed Apr 24 17:10:51 2019 +0200
@@ -199,7 +199,7 @@
     int8_t percentageHe;
 } SManualGas;
 
-void logbook_writeSample(SDiveState *state);
+void logbook_writeSample(const SDiveState *state);
 void logbook_initNewdiveProfile(const SDiveState* pInfo, SSettings* pSettings);
 void logbook_EndDive(void);
 
@@ -211,7 +211,7 @@
 uint8_t logbook_getHeader(uint8_t StepBackwards,SLogbookHeader* pLogbookHeader);
 uint16_t logbook_readSampleData(uint8_t StepBackwards, uint16_t length,uint16_t* depth, uint8_t*  gasid, int16_t* temperature, uint16_t* ppo2, uint16_t* setpoint, uint16_t* sensor1, uint16_t* sensor2, uint16_t* sensor3, uint16_t* cns, uint8_t* bailout, uint16_t* decostopDepth);
 void logbook_test(void);
-void logbook_InitAndWrite(void);
+void logbook_InitAndWrite(const SDiveState* pStateReal);
 void logbook_recover_brokenlog(uint8_t headerId);
 
 uint16_t logbook_lastDive_diveNumber(void);
--- a/Discovery/Src/base.c	Tue Apr 23 21:03:24 2019 +0200
+++ b/Discovery/Src/base.c	Wed Apr 24 17:10:51 2019 +0200
@@ -508,8 +508,18 @@
 	        DoDisplayRefresh = 0;
         	RefreshDisplay();
 
+// Enable this to make the simulator write a logbook entry
+// #define SIM_WRITES_LOGBOOK 1
+
+        if(stateUsed == stateSimGetPointer())
+        {
+#ifdef SIM_WRITES_LOGBOOK
+            logbook_InitAndWrite(stateUsed);
+#endif
+        }
+
         	if(stateUsed == stateRealGetPointer())	/* Handle log entries while in dive mode*/
-                logbook_InitAndWrite();
+                logbook_InitAndWrite(stateUsed);
         }
 
 #ifdef DEBUG_RUNTIME
--- a/Discovery/Src/data_central.c	Tue Apr 23 21:03:24 2019 +0200
+++ b/Discovery/Src/data_central.c	Wed Apr 24 17:10:51 2019 +0200
@@ -775,18 +775,9 @@
 }
 
 
-void resetEvents(void)
+void resetEvents(const SDiveState *pStateUsed)
 {
-    SDiveState * pStateUsed;
-	if(stateUsed == stateRealGetPointer())
-	{
-		pStateUsed = stateRealGetPointerWrite();
-	}
-	else
-	{
-		pStateUsed = stateSimGetPointerWrite();
-	}
-	memset(&pStateUsed->events,0, sizeof(SEvents));
+	memset((void *)&pStateUsed->events, 0, sizeof(SEvents));
 }
 
 
--- a/Discovery/Src/data_exchange_main.c	Tue Apr 23 21:03:24 2019 +0200
+++ b/Discovery/Src/data_exchange_main.c	Wed Apr 24 17:10:51 2019 +0200
@@ -436,7 +436,7 @@
 	    vpm_init(&pStateUsed->vpm,  pStateUsed->diveSettings.vpm_conservatism, 0, 0);
 	    buehlmann_init();
 	    timer_init();
-	    resetEvents();
+	    resetEvents(pStateUsed);
       pStateUsed->diveSettings.internal__pressure_first_stop_ambient_bar_as_upper_limit_for_gf_low_otherwise_zero = 0;
 			/*
 			 * ToDo by Peter
--- a/Discovery/Src/logbook.c	Tue Apr 23 21:03:24 2019 +0200
+++ b/Discovery/Src/logbook.c	Wed Apr 24 17:10:51 2019 +0200
@@ -90,10 +90,10 @@
 static void logbook_SetAverageDepth(float average_depth_meter);
 static void logbook_SetMinTemperature(float min_temperature_celsius);
 static void logbook_SetMaxCNS(float max_cns_percentage);
-static void logbook_SetCompartmentDesaturation(void);
+static void logbook_SetCompartmentDesaturation(const SDiveState * pStateReal);
 static void logbook_SetLastStop(float last_stop_depth_bar);
 static void logbook_writedata(void * data, int length_byte);
-static void logbook_UpdateHeader(void);
+static void logbook_UpdateHeader(const SDiveState * pStateReal);
 
 /* Exported functions --------------------------------------------------------*/
 
@@ -267,7 +267,7 @@
 	memcpy(header.n2Compartments, pInfo->lifeData.tissue_nitrogen_bar, 64);
 	memcpy(header.heCompartments, pInfo->lifeData.tissue_helium_bar, 64);
 
-	logbook_SetCompartmentDesaturation();
+	logbook_SetCompartmentDesaturation(pInfo);
 
 	ext_flash_start_new_dive_log_and_set_actualPointerSample((uint8_t*)&header);
 
@@ -371,7 +371,7 @@
   * @param  SDiveState state:
   */
 
-void logbook_writeSample(SDiveState *state)
+void logbook_writeSample(const SDiveState *state)
 {
     uint8_t sample[256];
   //  int position = 0;
@@ -1132,7 +1132,7 @@
  *          and finishes logbook after end of dive
 *********************************************************************************/
 
-void logbook_InitAndWrite(void)
+void logbook_InitAndWrite(const SDiveState *pStateReal)
 {
 	SSettings *pSettings = settingsGetPointer();
 	static uint8_t bDiveMode = 0;
@@ -1141,8 +1141,6 @@
 	uint32_t lasttick = 0;
 	static float min_temperature_float_celsius = 0;
 
-	const SDiveState * pStateReal = stateRealGetPointer();
-
 	if(!bDiveMode)
 	{
 		if((pStateReal->mode == MODE_DIVE) && (pStateReal->diveSettings.diveMode != DIVEMODE_Apnea) && (pStateReal->lifeData.dive_time_seconds >= 5))
@@ -1153,7 +1151,7 @@
 			min_temperature_float_celsius = pStateReal->lifeData.temperature_celsius;
 			//Write logbook sample
 			logbook_writeSample(pStateReal);
-			resetEvents();
+			resetEvents(pStateReal);
 			tickstart = HAL_GetTick();
 			bDiveMode = 1;
 		}
@@ -1167,7 +1165,7 @@
 		{
 			//Write logbook sample
 			logbook_writeSample(pStateReal);
-			resetEvents();
+			resetEvents(pStateReal);
 			if(min_temperature_float_celsius > pStateReal->lifeData.temperature_celsius)
 				min_temperature_float_celsius = pStateReal->lifeData.temperature_celsius;
 			tickstart = lasttick;
@@ -1187,7 +1185,7 @@
 				bDiveMode = 3;
 			}
 			if(bDiveMode == 3)
-				logbook_UpdateHeader();
+				logbook_UpdateHeader(pStateReal);
 		}
 	}
 	else if(bDiveMode == 3)
@@ -1196,7 +1194,7 @@
 		logbook_SetAverageDepth(pStateReal->lifeData.average_depth_meter);
 		logbook_SetMinTemperature(min_temperature_float_celsius);
 		logbook_SetMaxCNS(pStateReal->lifeData.cns);
-		logbook_SetCompartmentDesaturation();
+		logbook_SetCompartmentDesaturation(pStateReal);
 		logbook_SetLastStop(pStateReal->diveSettings.last_stop_depth_bar);
 		logbook_EndDive();
 		bDiveMode = 0;
@@ -1218,10 +1216,8 @@
   * @version V0.0.1
   * @date    27-Nov-2014
 *********************************************************************************/
-static void logbook_UpdateHeader(void)
+static void logbook_UpdateHeader(const SDiveState *pStateReal)
 {
-	const SDiveState * pStateReal = stateRealGetPointer();
-
 //	uint16_t secondsAtShallow = 0;
 	RTC_DateTypeDef Sdate;
 	RTC_TimeTypeDef Stime;
@@ -1339,9 +1335,8 @@
 }
 
 
-static void logbook_SetCompartmentDesaturation(void)
+static void logbook_SetCompartmentDesaturation(const SDiveState * pStateReal)
 {
-	const SDiveState * pStateReal = stateRealGetPointer();
 	SLifeData2 secondaryInformation  = { 0 };
 
 	decom_tissues_desaturation_time(&pStateReal->lifeData, &secondaryInformation);