More menus development.

Thu, 31 Oct 2019 22:22:22 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 31 Oct 2019 22:22:22 +0100
changeset 17
f9eca4a55911
parent 16
e38ffa806e84
child 18
d969e0fe05dc

More menus development.

main/co2meter.c file | annotate | diff | comparison | revisions
main/config.h file | annotate | diff | comparison | revisions
--- a/main/co2meter.c	Wed Oct 30 23:21:46 2019 +0100
+++ b/main/co2meter.c	Thu Oct 31 22:22:22 2019 +0100
@@ -1,5 +1,6 @@
-/*
- * co2meter project.
+/**
+ * @file co2meter.c
+ * @brief co2meter project.
  */
 
 #include "config.h"
@@ -13,8 +14,6 @@
 #define ROT_ENC_SW_GPIO		(CONFIG_ROT_ENC_SW_GPIO)
 #define INACTIVITY		480					///< Time in 250 mSec units.
 
-#define RESET_AT		0		///< Set to a positive non-zero number to reset the position if this value is exceeded
-
 
 int					Main_Loop1 = ML1_INIT;		///< Loop 1 init
 int					Main_Loop2 = -1;		///< Loop 2 invalid
@@ -45,23 +44,67 @@
 
 
 
+/**
+ * @brief Write a menu line on the display.
+ * @param bright Display the line with a bold or normal font.
+ * @param x The horizontal start position of the line.
+ * @param y The vertical bottom of the line position.
+ * @param format The formatted data to display.
+ */
+void menu_line(int bright, int x, int y, const char *format, ...)
+{
+    char        buf[65];
+    va_list     va_ptr;
+
+    if (bright)
+        u8g2_SetFont(&u8g2, u8g2_font_t0_12b_tr);
+    else
+        u8g2_SetFont(&u8g2, u8g2_font_t0_12_tr);
+
+    va_start(va_ptr, format);
+    vsnprintf(buf, 65, format, va_ptr);
+    va_end(va_ptr);
+
+    u8g2_DrawStr(&u8g2, x, y, buf);
+}
+
+
+
+/**
+ * @brief Clear the display and prepare the top of the display.
+ * @format The formatted data to display at the top.
+ */
+void screen_top(const char *format, ...)
+{
+    char        buf[65];
+    va_list     va_ptr;
+
+    va_start(va_ptr, format);
+    vsnprintf(buf, 65, format, va_ptr);
+    va_end(va_ptr);
+
+    u8g2_ClearBuffer(&u8g2);
+    u8g2_DrawHLine(&u8g2, 0, 14, 128);
+
+    u8g2_SetFont(&u8g2, u8g2_font_t0_15_tr);
+    u8g2_uint_t w = u8g2_GetStrWidth(&u8g2, buf);
+    u8g2_DrawStr(&u8g2, (128 - w) / 2,12, buf);
+}
+
+
+
 void screen_main()
 {
     char	buf[65];
     int		i;
 
-    u8g2_ClearBuffer(&u8g2);
-    u8g2_DrawHLine(&u8g2, 0, 14, 128);
-    u8g2_SetFont(&u8g2, u8g2_font_t0_15_tr);
-    sprintf(buf, "CO2 meter %s", app_desc->version);
-    u8g2_uint_t w = u8g2_GetStrWidth(&u8g2, buf);
-    u8g2_DrawStr(&u8g2, (128 - w) / 2,12, buf);
+    screen_top("CO2 meter %s", app_desc->version);
 
     if (xSemaphoreTake(xSemaphoreUnits, 25) == pdTRUE) {
 
     	u8g2_SetFont(&u8g2, u8g2_font_t0_22b_tf);
     	sprintf(buf, "%.1f °C", units[0].temperature / 1000.0);
-    	w = u8g2_GetUTF8Width(&u8g2, buf);
+    	u8g2_uint_t w = u8g2_GetUTF8Width(&u8g2, buf);
     	u8g2_DrawUTF8(&u8g2, (128 - w) / 2,40, buf);
     	u8g2_SetFont(&u8g2, u8g2_font_t0_18b_tf);
 
@@ -82,20 +125,14 @@
 {
     char        buf[65];
 
-    u8g2_ClearBuffer(&u8g2);
-    u8g2_DrawHLine(&u8g2, 0, 14, 128);
-    u8g2_SetFont(&u8g2, u8g2_font_t0_15_tr);
-    sprintf(buf, "Unit %d", no + 1);
-    u8g2_uint_t w = u8g2_GetStrWidth(&u8g2, buf);
-    u8g2_DrawStr(&u8g2, (128 - w) / 2,12, buf);
+    if (xSemaphoreTake(xSemaphoreUnits, 25) == pdTRUE) {
 
-    if (xSemaphoreTake(xSemaphoreUnits, 25) == pdTRUE) {
+	screen_top("Unit %d %s", no + 1, units[no].mode ? "On":"Off");
 
     	u8g2_SetFont(&u8g2, u8g2_font_t0_22b_tf);
     	sprintf(buf, "%.1f °C", units[no].temperature / 1000.0);
-    	w = u8g2_GetUTF8Width(&u8g2, buf);
+    	u8g2_uint_t w = u8g2_GetUTF8Width(&u8g2, buf);
     	u8g2_DrawUTF8(&u8g2, (128 - w) / 2,40, buf);
-//    	u8g2_SetFont(&u8g2, u8g2_font_t0_18b_tf);
 
     	sprintf(buf, "%.2f bar", units[no].pressure / 1000.0);
     	w = u8g2_GetUTF8Width(&u8g2, buf);
@@ -111,42 +148,11 @@
 
 void screen_unit_setup(int no, int sub)
 {
-    char        buf[65];
-
-    u8g2_ClearBuffer(&u8g2);
-    u8g2_DrawHLine(&u8g2, 0, 14, 128);
-    u8g2_SetFont(&u8g2, u8g2_font_t0_15_tr);
-    sprintf(buf, "Unit %d setup", no + 1);
-    u8g2_uint_t w = u8g2_GetStrWidth(&u8g2, buf);
-    u8g2_DrawStr(&u8g2, (128 - w) / 2,12, buf);
-
-    if (sub == 0)
-	u8g2_SetFont(&u8g2, u8g2_font_t0_15b_tr);
-    else
-	u8g2_SetFont(&u8g2, u8g2_font_t0_15_tr);
-    sprintf(buf, "Mode      %s", units[no].mode ? "ON":"OFF");
-    u8g2_DrawStr(&u8g2,2,28, buf);
-
-    if (sub == 1)
-        u8g2_SetFont(&u8g2, u8g2_font_t0_15b_tr);
-    else
-        u8g2_SetFont(&u8g2, u8g2_font_t0_15_tr);
-    sprintf(buf, "Calibrate");
-    u8g2_DrawStr(&u8g2,2,40, buf);
-
-    if (sub == 2)
-        u8g2_SetFont(&u8g2, u8g2_font_t0_15b_tr);
-    else
-        u8g2_SetFont(&u8g2, u8g2_font_t0_15_tr);
-    sprintf(buf, "T.sensor  ");
-    u8g2_DrawStr(&u8g2,2,52, buf);
-
-    if (sub == 3)
-        u8g2_SetFont(&u8g2, u8g2_font_t0_15b_tr);
-    else
-        u8g2_SetFont(&u8g2, u8g2_font_t0_15_tr);
-    sprintf(buf, "Return");
-    u8g2_DrawStr(&u8g2,2,64, buf);
+    screen_top("Unit %d setup", no + 1);
+    menu_line(sub == 0, 2, 25, "Mode    %s", units[no].mode ? "ON":"OFF");
+    menu_line(sub == 1, 2, 37, "Zero mV %d", units[no].pressure_zero);
+    menu_line(sub == 2, 2, 49, "DS18B20 %s", units[no].temperature_rom_code);
+    menu_line(sub == 3, 2, 61, "Return");
 
     u8g2_SendBuffer(&u8g2);
     u8g2_SetPowerSave(&u8g2, 0);
@@ -524,8 +530,6 @@
 		switch (Main_Loop2) {
 		    case ML2_INIT:
 			ESP_LOGI(TAG, "Loop user: Init");
-//			u8g2_SetPowerSave(&u8g2, 0); // wake up display
-//			u8g2_ClearBuffer(&u8g2);
 			New_Loop2 = ML2_USER;
 			usertimer = INACTIVITY;
 			break;
@@ -581,16 +585,10 @@
 	     * Main user processing. Handle the rotary encoder and pushbutton.
 	     */
 	    if (Main_Loop2 < ML2_INACTIVE) {
-	    	// If wakeup from GPIO -- state machine 2
-		// Init OLED
 		// If not configured, start configure
 		// If configured select first unit
-		// New rotate position, set screen, reset waittimer
 		// Handle screen (first is show measured values)
-		// Count inactivity
-		// flag if inactive and OLED lowpower.
 
-		// Generic display all units at once. Press is xxx? Bold/Italic is selected.
 		// Display per unit. Temp + Pressure + state. Press is setup this sensor.
 		// Setup menu: 	Sensors
 		//		WiFi
@@ -619,14 +617,18 @@
 			case ML2_SETUP_UNIT3:	if (event.state.position > 0) {
 						    if (sub < 3) {
 							sub++;
-							screen_unit_setup(Main_Loop2 - ML2_SETUP_UNIT1, sub);
+						    } else {
+							sub = 0;
 						    }
+						    screen_unit_setup(Main_Loop2 - ML2_SETUP_UNIT1, sub);
 						    ESP_ERROR_CHECK(rotary_encoder_reset(&rinfo));
 						} else if (event.state.position < 0) {
 						    if (sub > 0) {
 							sub--;
-							screen_unit_setup(Main_Loop2 - ML2_SETUP_UNIT1, sub);
+						    } else {
+							sub = 3;
 						    }
+						    screen_unit_setup(Main_Loop2 - ML2_SETUP_UNIT1, sub);
 						    ESP_ERROR_CHECK(rotary_encoder_reset(&rinfo));
 						}
 						break;
@@ -636,21 +638,20 @@
 		    }
                 } else {
                     // Poll current position and direction
-                    rotary_encoder_state_t state = { 0 };
-                    ESP_ERROR_CHECK(rotary_encoder_get_state(&rinfo, &state));
+//                    rotary_encoder_state_t state = { 0 };
+//                    ESP_ERROR_CHECK(rotary_encoder_get_state(&rinfo, &state));
 
 //                    ESP_LOGI(TAG, "Poll: position %d, direction %s timer %d", state.position,
 //                    state.direction ? (state.direction == ROTARY_ENCODER_DIRECTION_CLOCKWISE ? "CW" : "CCW") : "NOT_SET", usertimer);
-		    if (usertimer)
+		    if (usertimer) {
 			usertimer--;
-		    else
+			if ((usertimer % 240) == 0) { // Each minute
+			    ESP_LOGI(TAG, "usertimer %d", usertimer);
+			    if (Main_Loop1 == ML1_DONE)
+				Main_Loop1 = ML1_INIT;
+			}
+		    } else
 			New_Loop2 = ML2_INACTIVE;
-
-                            // Reset the device
-       //                     if (RESET_AT && (state.position >= RESET_AT || state.position <= -RESET_AT)) {
-       //                         ESP_LOGI(TAG, "Reset");
-       //                         ESP_ERROR_CHECK(rotary_encoder_reset(&rinfo));
-       //                     }
                 }
 	    }
 
@@ -699,14 +700,7 @@
 	    vTaskDelay(10 / portTICK_PERIOD_MS);
 	}
 
-//	u8g2_ClearBuffer(&u8g2);
-//	u8g2_SendBuffer(&u8g2);
-//	u8g2_SetPowerSave(&u8g2, 1);
-
-//	printf("Simulate deep sleep\n");
-// 	vTaskDelay(1000 * wakeup_time_sec / portTICK_PERIOD_MS);
-  
-	printf("Entering deep sleep\n");
+	ESP_LOGI(TAG, "Entering deep sleep");
     	gettimeofday(&sleep_enter_time, NULL);
 	esp_deep_sleep_start();
 
--- a/main/config.h	Wed Oct 30 23:21:46 2019 +0100
+++ b/main/config.h	Thu Oct 31 22:22:22 2019 +0100
@@ -67,12 +67,12 @@
 typedef enum
 {
     ML1_INIT = 0,		///< Init fase
-    ML1_CONNECT,			///< Connect WiFi
+    ML1_CONNECT,		///< Connect WiFi
     ML1_MQTT_CONNECT,		///< Connect MQTT if WiFi
-    ML1_WAITCON,			///< Wait for connection and measurements
+    ML1_WAITCON,		///< Wait for connection and measurements
     ML1_SEND,			///< Send MQTT node and units messages
-    ML1_WAITACK,			///< MQTT messages received
-    ML1_MQTT_DISCONNECT,		///< MQTT disconnect
+    ML1_WAITACK,		///< MQTT messages received
+    ML1_MQTT_DISCONNECT,	///< MQTT disconnect
     ML1_DISCONNECT,		///< Disconnect WiFi
     ML1_WIFI_OFF,		///< WiFi power off
     ML1_DONE			///< All done
@@ -111,7 +111,7 @@
     char                ntp_server[32];                 ///< Preffered NTP server.
     char                hostname[32];                   ///< Our hostname.
     char                uuid[37];                       ///< Sort of uuid code.
-    char                xlastSSID[32];                  ///< Last connected station.
+    char                xlastSSID[32];                  ///< Not used anymore.
     char		mqtt_server[32];		///< MQTT server.
     uint16_t		mqtt_port;			///< MQTT TCP port.
     char		mqtt_user[32];			///< MQTT user name if needed.
@@ -124,8 +124,9 @@
  */
 void write_config(void);
 
-/** @brief Read configuration file. If it doesn't exist create and
- *         write a new configuration file with sane defaults.
+/**
+ * @brief Read configuration file. If it doesn't exist create and
+ *        write a new configuration file with sane defaults.
  */
 void read_config(void);
 

mercurial