main/co2meter.c

changeset 4
2a57c466bf45
parent 2
c0184362d48c
child 6
06a5028dbcdf
--- a/main/co2meter.c	Tue Oct 08 16:51:30 2019 +0200
+++ b/main/co2meter.c	Tue Oct 08 21:09:36 2019 +0200
@@ -30,9 +30,10 @@
 static TaskHandle_t			xTaskWifi = NULL;
 static TaskHandle_t			xTaskMQTT = NULL;
 const esp_app_desc_t			*app_desc = NULL;
-unit_t					units[3];			///< Pressure test units
 
 
+extern unit_t				units[3];			///< Pressure test units
+extern SemaphoreHandle_t		xSemaphoreUnits;		///< Units lock semaphore
 extern DS18B20_State            	*ds18b20_state;         	///< DS18B20 state
 extern SemaphoreHandle_t        	xSemaphoreDS18B20;      	///< DS18B20 lock semaphore
 extern ADC_State                	*adc_state;             	///< ADC state
@@ -203,6 +204,7 @@
 
     xSemaphoreDS18B20 = xSemaphoreCreateMutex();
     xSemaphoreADC = xSemaphoreCreateMutex();
+    xSemaphoreUnits = xSemaphoreCreateMutex();
 
     xTaskCreate(&task_ds18b20, "task_ds18b20",  2560, NULL, 8, &xTaskDS18B20);
     xTaskCreate(&task_adc,     "task_adc",      2560, NULL, 8, &xTaskADC);
@@ -291,28 +293,31 @@
     			}
 
 			/* Copy measured data and calculate results */
-			for (int i = 0; i < 3; i++) {
-			    units[i].temperature = temp;
-			    units[i].temperature_state = state;
-			    strncpy(units[i].temperature_rom_code, rom_code, strlen(rom_code));
-			    if (xSemaphoreTake(xSemaphoreADC, 10) == pdTRUE) {
-				units[i].pressure_state = adc_state->Pressure[i].error;
-				units[i].pressure_channel = adc_state->Pressure[i].channel;
-				units[i].pressure_voltage = adc_state->Pressure[i].voltage;
-				units[i].pressure_zero = 110;
-				int P = (units[i].pressure_voltage / (adc_state->Batt_voltage / 1000) - units[i].pressure_zero) * 14; // in bar
-				if (P < 0)
-				    P = 0;
-				units[i].pressure = P;
+			if (xSemaphoreTake(xSemaphoreUnits, 25) == pdTRUE) {
+			    for (int i = 0; i < 3; i++) {
+			    	units[i].temperature = temp;
+			    	units[i].temperature_state = state;
+			    	strncpy(units[i].temperature_rom_code, rom_code, 17);
+			    	if (xSemaphoreTake(xSemaphoreADC, 10) == pdTRUE) {
+				    units[i].pressure_state = adc_state->Pressure[i].error;
+				    units[i].pressure_channel = adc_state->Pressure[i].channel;
+				    units[i].pressure_voltage = adc_state->Pressure[i].voltage;
+				    units[i].pressure_zero = 110;
+				    int P = (units[i].pressure_voltage / (adc_state->Batt_voltage / 1000) - units[i].pressure_zero) * 14; // in bar
+				    if (P < 0)
+				    	P = 0;
+				    units[i].pressure = P;
 printf("%d volt: %d batt: %d scale: %d  bar: %d\n", i, units[i].pressure_voltage, adc_state->Batt_voltage, 
 		units[i].pressure_voltage / (adc_state->Batt_voltage / 1000) - units[i].pressure_zero, P);
 // Moet die echt op 5 volt?
 // Verbruik 10 mA
 // Setup tijd max 2 mS
-				xSemaphoreGive(xSemaphoreADC);
+				    xSemaphoreGive(xSemaphoreADC);
+			    	}
 			    }
+			    write_units();
+			    xSemaphoreGive(xSemaphoreUnits);
 			}
-			write_units();
 		    }
 		    break;
 

mercurial