main/task_ina219.c

changeset 5
b1f38105ca7e
parent 3
e5d91caa6ab4
child 6
bad3414f7bc4
--- a/main/task_ina219.c	Wed Mar 29 21:39:07 2023 +0200
+++ b/main/task_ina219.c	Thu Mar 30 17:05:05 2023 +0200
@@ -15,6 +15,11 @@
 
 extern ina219_t			ina219_b_dev;
 extern ina219_t			ina219_s_dev;
+extern float			s_Volts[I_MAX_LOOPS + 1];
+extern float			s_Current[I_MAX_LOOPS + 1];
+extern float			b_Volts[I_MAX_LOOPS + 1];
+extern float			b_Current[I_MAX_LOOPS + 1];
+extern uint8_t			loopno;
 
 const int TASK_INA219_REQUEST_DONE = BIT0;			///< All requests are done.
 const int TASK_INA219_REQUEST_POWER = BIT1;			///< Request power readings
@@ -43,7 +48,6 @@
  */
 void task_ina219(void *pvParameter)
 {
-    int		error = 0;
     float	bus_voltage, shunt_voltage, current, power;
 
     ESP_LOGI(TAG, "Starting task INA219 sda=%d scl=%d", CONFIG_I2C_MASTER_SDA, CONFIG_I2C_MASTER_SCL);
@@ -104,6 +108,25 @@
 		ESP_LOGI(TAG, "Battery VBUS: %.04f V, VSHUNT: %.04f mV, IBUS: %.04f mA, PBUS: %.04f mW",
                 		bus_voltage, shunt_voltage * 1000, current * 1000, power * 1000);
 	    }
+	    if (xSemaphoreTake(xSemaphoreINA219, 25) == pdTRUE) {
+		if (ina219_state->Battery.fake) {
+		    ina219_state->Battery.volts = 13.21;
+		    if (ready_WiFi()) {
+			ina219_state->Battery.shunt = 0.00785;
+			ina219_state->Battery.current = 78.5;
+		    } else {
+			ina219_state->Battery.shunt = 0.00182;
+			ina219_state->Battery.current = 18.2;
+		    }
+		} else {
+		    ina219_state->Battery.volts = bus_voltage;
+		    ina219_state->Battery.shunt = shunt_voltage;
+		    ina219_state->Battery.current = current;
+		}
+		ina219_state->Battery.valid = true;
+		xSemaphoreGive(xSemaphoreINA219);
+	    }
+
 	    if (! ina219_state->Solar.fake) {
                 ESP_ERROR_CHECK(ina219_get_bus_voltage(&ina219_s_dev, &bus_voltage));
                 ESP_ERROR_CHECK(ina219_get_shunt_voltage(&ina219_s_dev, &shunt_voltage));
@@ -112,42 +135,26 @@
                 ESP_LOGI(TAG, "  Solar VBUS: %.04f V, VSHUNT: %.04f mV, IBUS: %.04f mA, PBUS: %.04f mW",
                                 bus_voltage, shunt_voltage * 1000, current * 1000, power * 1000);
 	    }
-
-	    /*
-		error = ina219_read_float(&ina219_dev, &temperature, &pressure, &humidity);
-		if (xSemaphoreTake(xSemaphoreINA219, 25) == pdTRUE) {
-		    if (error == ESP_OK) {
-			ina219_state->error = INA219_ERR_NONE;
-			ina219_state->valid = true;
-			ina219_state->temperature = temperature;
-			ina219_state->pressure = pressure;
-			ina219_state->humidity = humidity;
-		    } else {
-			ina219_state->error = INA219_ERR_READ;
-			ina219_state->valid = false;
-			ina219_state->temperature = 0;
-			ina219_state->pressure = 0;
-			ina219_state->humidity = 0;
-		    }
-		    xSemaphoreGive(xSemaphoreINA219);
+	    if (xSemaphoreTake(xSemaphoreINA219, 25) == pdTRUE) {
+		if (! ina219_state->Solar.fake && ! ina219_state->Battery.fake) {
+		    ina219_state->Solar.volts = bus_voltage;
+                    ina219_state->Solar.shunt = shunt_voltage;
+                    ina219_state->Solar.current = current;
+		} else if (ina219_state->Solar.fake && ! ina219_state->Battery.fake) {
+		    ina219_state->Solar.volts = ina219_state->Battery.volts + 0.78;
+		    ina219_state->Solar.shunt = 0.02341;
+		    ina219_state->Solar.current = 234.1;
+		} else {
+		    ina219_state->Solar.volts = 13.98;
+		    ina219_state->Solar.shunt = 0.02341;
+		    ina219_state->Solar.current = 234.1;
 		}
-	    } else {
-		if (xSemaphoreTake(xSemaphoreINA219, 25) == pdTRUE) {
-		    ina219_state->error = INA219_ERR_NONE;
-                    ina219_state->valid = true;
-		    ina219_state->temperature = 21.23;
-                    ina219_state->pressure = 101360;
-                    ina219_state->humidity = 0;
-		    xSemaphoreGive(xSemaphoreINA219);
-		}
+		ina219_state->Solar.valid = true;
+		xSemaphoreGive(xSemaphoreINA219);
 	    }
 
-	    */
 	    xEventGroupClearBits(xEventGroupINA219, TASK_INA219_REQUEST_POWER);
 	    xEventGroupSetBits(xEventGroupINA219, TASK_INA219_REQUEST_DONE);
-#if 0
-	    ESP_LOGI(TAG, "  TB: %.3f C, %.1f hPa, error: %d", ina219_state->temperature, ina219_state->pressure / 100, ina219_state->error);
-#endif
 	}
     }
 }

mercurial