diff Discovery/Src/logbook.c @ 273:5fe136480a47

Merged in janlmulder/ostc4/write-from-sim (pull request #20) Cleanup and debug capabilty to write logbook from simulator
author heinrichsweikamp <bitbucket@heinrichsweikamp.com>
date Sun, 28 Apr 2019 07:05:23 +0000
parents 2e58a4094770
children 54d14bc2083c
line wrap: on
line diff
--- a/Discovery/Src/logbook.c	Fri Apr 26 10:36:11 2019 +0000
+++ b/Discovery/Src/logbook.c	Sun Apr 28 07:05:23 2019 +0000
@@ -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;
@@ -385,7 +385,7 @@
     int i = 0;
     for(i = 0; i <256 ;i++)
             sample[i] = 0;
-    addU16(sample, (uint16_t)(state.lifeData.depth_meter * 100));
+    addU16(sample, (uint16_t)(state->lifeData.depth_meter * 100));
     length += 2;
     sample[2] = 0;
     length++;
@@ -396,50 +396,50 @@
 
     //BuildEevntyte 1
 		// sub old 0-3 only one at a time
-    if(state.events.manualMarker)
+    if(state->events.manualMarker)
     {
         eventByte1.uw = 6;
     }
 		else
-    if(state.warnings.decoMissed)
+    if(state->warnings.decoMissed)
     {
         eventByte1.uw = 2;
     }
 		else
-    if(state.warnings.ppO2Low)
+    if(state->warnings.ppO2Low)
     {
         eventByte1.uw = 4;
     }
 		else
-    if(state.warnings.ppO2High)
+    if(state->warnings.ppO2High)
     {
         eventByte1.uw = 5;
     }
 		else
-    if(state.warnings.lowBattery)
+    if(state->warnings.lowBattery)
     {
         eventByte1.uw = 7;
     }
 		else
-    if(state.warnings.slowWarning)
+    if(state->warnings.slowWarning)
     {
         eventByte1.uw = 1;
     }
 		// sub bit 4 to 7
-    if(state.events.manuelGasSet)
+    if(state->events.manuelGasSet)
     {
         eventByte1.ub.bit4 = 1;
     }
-    if(state.events.gasChange)
+    if(state->events.gasChange)
     {
         eventByte1.ub.bit5 = 1;
     }
-    if(state.events.setpointChange)
+    if(state->events.setpointChange)
     {
         eventByte1.ub.bit6 = 1;
     }
 		// sub bit 7 + eventbyte2
-    if(state.events.bailout)
+    if(state->events.bailout)
     {
         eventByte1.ub.bit7 = 1;
         eventByte2.ub.bit0 = 1;
@@ -456,32 +456,32 @@
         length++;
     }
     //Add EventInfos
-    if(state.events.manuelGasSet)
+    if(state->events.manuelGasSet)
     {
         //manual gas in %O2 & %He
-        sample[length] = state.events.info_manuelGasSetO2;
+        sample[length] = state->events.info_manuelGasSetO2;
         length += 1;
-        sample[length] = state.events.info_manuelGasSetHe;
+        sample[length] = state->events.info_manuelGasSetHe;
         length += 1;
     }
-    if(state.events.gasChange)
+    if(state->events.gasChange)
     {
         //Current gas (gasid)
-        sample[length] = state.events.info_GasChange;
+        sample[length] = state->events.info_GasChange;
         length += 1;
     }
-    if(state.events.setpointChange)
+    if(state->events.setpointChange)
     {
         //New setpoint in cbar
-        sample[length] = state.events.info_SetpointChange;
+        sample[length] = state->events.info_SetpointChange;
         length += 1;
     }
-    if(state.events.bailout)
+    if(state->events.bailout)
     {
       //bailout gas in % O2 & %He
-        sample[length] = state.events.info_bailoutO2;
+        sample[length] = state->events.info_bailoutO2;
         length += 1;
-        sample[length] = state.events.info_bailoutHe;
+        sample[length] = state->events.info_bailoutHe;
         length += 1;
     }
 
@@ -489,7 +489,7 @@
     if(divisor.temperature == 0)
     {
 			divisor.temperature = smallHeader.tempDivisor - 1;
-			addS16(&sample[length], (int16_t)((state.lifeData.temperature_celsius * 10.0f) + 0.5f));
+			addS16(&sample[length], (int16_t)((state->lifeData.temperature_celsius * 10.0f) + 0.5f));
 			length += 2;
     }
     else
@@ -556,9 +556,9 @@
 
         for(int i = 0; i <3; i++)
         {
-          sample[length] = (uint8_t)(state.lifeData.ppO2Sensor_bar[i] * 100.0f + 0.5f);
+          sample[length] = (uint8_t)(state->lifeData.ppO2Sensor_bar[i] * 100.0f + 0.5f);
           length += 1;
-          addU16(&sample[length], (uint16_t)(state.lifeData.sensorVoltage_mV[i] * 10.0f + 0.5f));
+          addU16(&sample[length], (uint16_t)(state->lifeData.sensorVoltage_mV[i] * 10.0f + 0.5f));
           length += 2;
         }
       }
@@ -574,19 +574,19 @@
       if(divisor.decoplan == 0)
       {
           divisor.decoplan  = smallHeader.decoplanDivisor - 1;
-          if(state.diveSettings.deco_type.ub.standard == VPM_MODE)
+          if(state->diveSettings.deco_type.ub.standard == VPM_MODE)
           {
             for(int i = 0; i <15; i++)
             {
-              sample[length] = state.decolistVPM.output_stop_length_seconds[i] / 60;
+              sample[length] = state->decolistVPM.output_stop_length_seconds[i] / 60;
               length += 1;
             }
           }
-          else if(state.diveSettings.deco_type.ub.standard == GF_MODE)
+          else if(state->diveSettings.deco_type.ub.standard == GF_MODE)
           {
             for(int i = 0; i <15; i++)
             {
-              sample[length] = state.decolistBuehlmann.output_stop_length_seconds[i] / 60;
+              sample[length] = state->decolistBuehlmann.output_stop_length_seconds[i] / 60;
               length += 1;
             }
           }
@@ -609,7 +609,7 @@
     if(divisor.cns == 0)
     {
         divisor.cns = smallHeader.cnsDivisor - 1;
-        addU16(&sample[length], (uint16_t)state.lifeData.cns);
+        addU16(&sample[length], (uint16_t)state->lifeData.cns);
         length += 2;
     }
     else
@@ -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))
@@ -1152,8 +1150,8 @@
 			logbook_initNewdiveProfile(pStateReal,settingsGetPointer());
 			min_temperature_float_celsius = pStateReal->lifeData.temperature_celsius;
 			//Write logbook sample
-			logbook_writeSample(*pStateReal);
-			resetEvents();
+			logbook_writeSample(pStateReal);
+			resetEvents(pStateReal);
 			tickstart = HAL_GetTick();
 			bDiveMode = 1;
 		}
@@ -1166,8 +1164,8 @@
 		if(ticksdiff >= 2000)
 		{
 			//Write logbook sample
-			logbook_writeSample(*pStateReal);
-			resetEvents();
+			logbook_writeSample(pStateReal);
+			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);