Finished the editor using the rotary switch. Added editing of the MQTT server parameters.

Thu, 07 Nov 2019 22:58:48 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 07 Nov 2019 22:58:48 +0100
changeset 24
64078aa15512
parent 23
58a328e91881
child 25
cc7c423f03fb

Finished the editor using the rotary switch. Added editing of the MQTT server parameters.

main/co2meter.c file | annotate | diff | comparison | revisions
main/task_mqtt.c file | annotate | diff | comparison | revisions
main/task_user.c file | annotate | diff | comparison | revisions
main/task_wifi.c file | annotate | diff | comparison | revisions
--- a/main/co2meter.c	Thu Nov 07 15:09:51 2019 +0100
+++ b/main/co2meter.c	Thu Nov 07 22:58:48 2019 +0100
@@ -155,7 +155,7 @@
     xSemaphoreADC = xSemaphoreCreateMutex();
     xSemaphoreUnits = xSemaphoreCreateMutex();
 
-    xTaskCreate(&task_user,    "task_user",     2048, NULL,10, &xTaskUser);
+    xTaskCreate(&task_user,    "task_user",     3072, NULL,10, &xTaskUser);
     xTaskCreate(&task_ds18b20, "task_ds18b20",  2560, NULL, 8, &xTaskDS18B20);
     xTaskCreate(&task_adc,     "task_adc",      2560, NULL, 8, &xTaskADC);
     esp_log_level_set("wifi", ESP_LOG_ERROR);
@@ -211,14 +211,15 @@
 			}
 
 			/* Copy measured data and calculate results */
-			if (xSemaphoreTake(xSemaphoreUnits, 25) == pdTRUE) {
-			    for (int i = 0; i < 3; i++) {
+			for (int i = 0; i < 3; i++) {
+			    if (xSemaphoreTake(xSemaphoreUnits, 25) == pdTRUE) {
 			    	units[i].temperature = temp;
 			    	units[i].temperature_state = state;
 				units[i].alarm = 0;
 				if (state)
 				    units[i].alarm |= ALARM_SYS_TEMPERATURE & ALARM_UNIT_TEMPERATURE;
 			    	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;
@@ -236,15 +237,20 @@
 // Setup tijd max 2 mS
 				    xSemaphoreGive(xSemaphoreADC);
 			    	} else {
-				    ESP_LOGE(TAG, "ML1_MQTT_CONNECT ADC lock error");
+				    ESP_LOGE(TAG, "ML1_MQTT_CONNECT ADC[%d] lock error", i);
 				}
-			    }
+				xSemaphoreGive(xSemaphoreUnits);
+			    } else {
+                            	ESP_LOGE(TAG, "ML1_MQTT_CONNECT units[%d] lock error", i);
+                            }
+			}
+			if (xSemaphoreTake(xSemaphoreUnits, 25) == pdTRUE) {
 			    write_units();
 			    xSemaphoreGive(xSemaphoreUnits);
-			    user_refresh();
 			} else {
-			    ESP_LOGE(TAG, "ML1_MQTT_CONNECT units lock error");
+			    ESP_LOGE(TAG, "ML1_MQTT_CONNECT write_units lock error");
 			}
+			user_refresh();
 		    }
 		    break;
 
--- a/main/task_mqtt.c	Thu Nov 07 15:09:51 2019 +0100
+++ b/main/task_mqtt.c	Thu Nov 07 22:58:48 2019 +0100
@@ -209,7 +209,7 @@
 	ESP_LOGE(TAG, "publishNode() lock DS18B20 error");
     }
 
-    if (xSemaphoreTake(xSemaphoreWiFi, 10) == pdTRUE) {
+    if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
 	payload = xstrcat(payload, (char *)",\"net\":{\"address\":\"");
 	payload = xstrcat(payload, wifi_state->STA_ip);
         payload = xstrcat(payload, (char *)"\",\"ifname\":\"sta\",\"rssi\":");
--- a/main/task_user.c	Thu Nov 07 15:09:51 2019 +0100
+++ b/main/task_user.c	Thu Nov 07 22:58:48 2019 +0100
@@ -19,6 +19,7 @@
 rotary_encoder_info_t			rinfo = { 0 };			///< Rotary encoder record
 rotary_encoder_event_t			event = { 0 };
 QueueHandle_t				event_queue;
+static xQueueHandle                     gpio_evt_queue = NULL;          ///< Rotary pushbutton queue
 static int				PushDuration = 0;		///< Duration of the pushed button
 
 extern const esp_app_desc_t		*app_desc;
@@ -29,12 +30,11 @@
 extern ADC_State                	*adc_state;             	///< ADC state
 extern SemaphoreHandle_t        	xSemaphoreADC;          	///< ADC lock semaphore
 extern WIFI_State			*wifi_state;			///< WiFi state
+extern SemaphoreHandle_t		xSemaphoreWiFi;			///< WiFi lock semaphore
 extern int				count_pub;			///< Published MQTT messages in transit
-static xQueueHandle			gpio_evt_queue = NULL;		///< Rotary pushbutton queue
 extern int				Main_Loop1;			///< Main measure loop
 
 
-
 const int TASK_USER_COLD = BIT0;					///< System cold start
 const int TASK_USER_WAKEUP = BIT1;					///< System wakeup from deepsleep
 const int TASK_USER_BUSY = BIT2;					///< User interface is busy doing something.
@@ -97,7 +97,7 @@
 
 /**
  * @brief Get a keyboard character from the rotary encoder.
- * @param curkey The referenced value if the key being edited. NOTE, start at 0 for a new char??
+ * @param curkey The referenced value if the key being edited.
  * @param type The edittype, all values, integer or float.
  * @param x The x position on the screen.
  * @param y The y position on the screen.
@@ -105,50 +105,87 @@
  */
 int getkey(int *curkey, int type, int x, int y)
 {
-    int	key = *curkey;
-    int	rc = 0;
+    int		key = *curkey;
+    int		rc = 0;
     int8_t	ascent = u8g2_GetAscent(&u8g2);
+    int8_t	descent = u8g2_GetDescent(&u8g2);
     int8_t	charheight = u8g2_GetMaxCharHeight(&u8g2);
     int8_t	charwidth = u8g2_GetMaxCharWidth(&u8g2);
 
+    u8g2_DrawGlyph(&u8g2, x, y, key);
     u8g2_DrawHLine(&u8g2, x, y+3, 12);
-    u8g2_SendBuffer(&u8g2);
+    u8g2_UpdateDisplay(&u8g2);
 
-    ESP_LOGI(TAG, "getkey(%c, %d, %d, %d) a %d  h %d  w %d", key, type, x, y, ascent, charheight, charwidth);
+    ESP_LOGI(TAG, "getkey(%c, %d, %d, %d) a %d  d %d  h %d  w %d", key, type, x, y, ascent, descent, charheight, charwidth);
 
     for (;;) {
     	if (xQueueReceive(event_queue, &event, 100 / portTICK_PERIOD_MS) == pdTRUE) {
 	    UserTimer = INACTIVITY;
 	    if (event.state.position != 0) {
 
-//		u8g2_SetDrawColor(&u8g2, 0);
-//		u8g2_DrawGlyph(&u8g2, x, y, key);
-//		u8g2_SetDrawColor(&u8g2, 1);
+		u8g2_SetDrawColor(&u8g2, 0);
+		u8g2_DrawBox(&u8g2, x, y - 12, charwidth, charheight);
+		u8g2_SetDrawColor(&u8g2, 1);
+		u8g2_DrawHLine(&u8g2, x, y+3, charwidth);
 
-		u8g2_DrawBox(&u8g2, x, y - ascent, 16, 16);
-//		u8g2_UpdateDisplayArea(&u8g2, x, y, 16, 16);
-		u8g2_SendBuffer(&u8g2);
+		if (event.state.position > 0) {
+		    /*
+		     * If turned fast, the encoder registers multiple steps.
+		     * So follow these steps.
+		     */
+		    for (int i = 0; i < event.state.position; i++) {
+			if (type == EDIT_TYPE_CAPS) {
+			    if (key < 127)
+                            	key++;
+                            if (key > 96 && key < 123)
+                            	key = 123;
+			} else if (type == EDIT_TYPE_INT) {
+			    if (key < 127)
+                            	key++;
+                            if (key < 45)
+                            	key = 45;
+                            if (key == 46 || key == 47)
+                            	key = 48;
+                            if (key > 57)
+                            	key = 127;
+			} else if (type == EDIT_TYPE_FLOAT) {
 
-	    	if (event.state.position > 0) {
-//		    if (key == 126)
-//		    	key = 171;
-//		    else
-		    if (key < 127)
-		    	key++;
-	    	} else if (event.state.position < 0) {
-//		    if (key == 171)
-//		    	key = 126;
-//		    else 
-		    if (key > 32)
-		    	key--;
+			} else { // EDIT_TYPE_TEXT
+			    if (key < 127)
+                        	key++;
+			}
+		    }
+		} else if (event.state.position < 0) {
+		    for (int i = 0; i > event.state.position; i--) {
+			if (type == EDIT_TYPE_CAPS) {
+			    if (key > 32)
+                            	key--;
+                            if (key > 96 && key < 123)
+                            	key = 96;
+			} else if (type == EDIT_TYPE_INT) {
+			    if (key > 45)
+                                key--;
+                            if (key > 57)
+                            	key = 57;
+                            if (key == 46 || key == 47)
+                          	key = 45;
+			} else if (type == EDIT_TYPE_FLOAT) {
+
+			} else { // EDIT_TYPE_TEXT
+			    if (key > 32)
+				key--;
+			}
+		    }
 		}
 
 		ESP_ERROR_CHECK(rotary_encoder_reset(&rinfo));
 	    	u8g2_DrawGlyph(&u8g2, x, y, key);
 	    	u8g2_SendBuffer(&u8g2);
+		u8g2_UpdateDisplay(&u8g2);
 	    }
     	} else {
 	    if (PushDuration) {
+		UserTimer = INACTIVITY;
 		if (PushDuration > 500)
 		    rc = 2;
 		else
@@ -159,9 +196,11 @@
     	}
     }
     u8g2_SetDrawColor(&u8g2, 0);
-    u8g2_DrawHLine(&u8g2, x, y+3, 12);
+    if (key == 127)
+	u8g2_DrawBox(&u8g2, x, y - 12, charwidth, charheight); // Erase DEL character
+    u8g2_DrawHLine(&u8g2, x, y+3, charwidth);
     u8g2_SetDrawColor(&u8g2, 1);
-    u8g2_SendBuffer(&u8g2);
+    u8g2_UpdateDisplay(&u8g2);
 
     *curkey = key;
     return rc;
@@ -194,29 +233,40 @@
 	u8g2_DrawStr(&u8g2, 0, 61, errmsg);
     }
     u8g2_SetFont(&u8g2, u8g2_font_unifont_t_symbols);
-//    u8g2_SetFont(&u8g2, u8g2_font_t0_12_tf);
     y = 36;
     u8g2_DrawStr(&u8g2, 0, y, txt);
     u8g2_SendBuffer(&u8g2);
     ESP_LOGI(TAG, "rotary_editer(%s, %s, %s, %d, %d)", label, txt, errmsg, len, type);
 
+    /*
+     * Choose initial edit key
+     */
+    if (type == EDIT_TYPE_CAPS)
+	key = 'A';
+    else if (type == EDIT_TYPE_INT || type == EDIT_TYPE_FLOAT)
+	key = '0';
+    else
+	key = 'a';
+
     for (;;) {
 	x = u8g2_GetUTF8Width(&u8g2, txt);
-	key = 'a';
 	rc = getkey(&key, type, x, y);
 	if (rc == 1) {
 	    if (key >= 32 && key <= 126 && strlen(txt) < len) {
 		txt[strlen(txt) + 1] = '\0';
                 txt[strlen(txt)] = key;
-	    } else if (key == 171 && strlen(txt)) {
+	    } else if (key == 127 && strlen(txt)) {
 		// delete key
 		txt[strlen(txt) - 1] = '\0';
+		if (strlen(txt))
+		    key = txt[strlen(txt) - 1];
 	    }
-printf("strlen %d  x %d  key %d\n", strlen(txt), x, key);
+	    ESP_LOGI(TAG, " strlen %d  x %d  key %d  `%s`", strlen(txt), x, key, txt);
 	} else if (rc == 2) {
 	    break;
 	}
     }
+    ESP_LOGI(TAG, "rotary_editer() `%s`", txt);
 }
 
 
@@ -328,7 +378,7 @@
 {
     char        buf[65];
 
-    if (xSemaphoreTake(xSemaphoreUnits, 25) == pdTRUE) {
+    if (xSemaphoreTake(xSemaphoreUnits, 35) == pdTRUE) {
 
 	screen_top("Unit %d %s", no + 1, units[no].mode ? "On":"Off");
 
@@ -340,9 +390,9 @@
     	sprintf(buf, "%.2f bar", units[no].pressure / 1000.0);
     	w = u8g2_GetUTF8Width(&u8g2, buf);
     	u8g2_DrawUTF8(&u8g2, (128 - w) / 2,63, buf);
-	u8g2_SendBuffer(&u8g2);
 
 	xSemaphoreGive(xSemaphoreUnits);
+	u8g2_SendBuffer(&u8g2);
     } else {
 	ESP_LOGE(TAG, "screen_unit(%d) lock error", no);
     }
@@ -387,14 +437,19 @@
 {
     char	buf[65];
 
-    screen_top("WiFi Status");
-    snprintf(buf, 65, "SSID %s", wifi_state->STA_ssid);
-    u8g2_DrawStr(&u8g2, 1, 28, buf);
-    snprintf(buf, 65, "RSSI %d", wifi_state->STA_rssi);
-    u8g2_DrawStr(&u8g2, 1, 43, buf);
-    snprintf(buf, 65, "Online %s", wifi_state->STA_online ? "Yes":"No");
-    u8g2_DrawStr(&u8g2, 1, 59, buf);
-    u8g2_SendBuffer(&u8g2);
+    if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
+    	screen_top("WiFi Status");
+    	snprintf(buf, 65, "SSID %s", wifi_state->STA_ssid);
+    	u8g2_DrawStr(&u8g2, 1, 28, buf);
+    	snprintf(buf, 65, "RSSI %d", wifi_state->STA_rssi);
+    	u8g2_DrawStr(&u8g2, 1, 43, buf);
+    	snprintf(buf, 65, "Online %s", wifi_state->STA_online ? "Yes":"No");
+    	u8g2_DrawStr(&u8g2, 1, 59, buf);
+	xSemaphoreGive(xSemaphoreWiFi);
+	u8g2_SendBuffer(&u8g2);
+    } else {
+	ESP_LOGE(TAG, "screen_wifi() lock error");
+    }
 }
 
 
@@ -434,6 +489,18 @@
 
 
 
+void screen_mqtt_setup(int sub)
+{
+    screen_top("MQTT Setup");
+    menu_line(sub == 0, 2, 25, "serv %s", config.mqtt_server);
+    menu_line(sub == 1, 2, 37, "port %d", config.mqtt_port);
+    menu_line(sub == 2, 2, 49, "user %s", config.mqtt_user);
+    menu_line(sub == 3, 2, 61, "Return");
+    u8g2_SendBuffer(&u8g2);
+}
+
+
+
 void screen_update()
 {
     screen_top("Update firmware");
@@ -536,8 +603,6 @@
 
 void menu_change(void)
 {
-    char	txt[65];
-
     if (New_Loop2 != Main_Loop2) {
 
 	Main_Loop2 = New_Loop2;
@@ -577,10 +642,8 @@
 
               case ML2_SETUP_MQTT:
                       ESP_LOGI(TAG, "Loop user: MQTT setup");
-                      sprintf(txt, "EDtXt");
-                      rotary_editer("MQTT demo", txt, "", 16, EDIT_TYPE_TEXT);
-                      New_Loop2 = ML2_MQTT;
-                      break;
+		      screen_mqtt_setup(SubMenu);
+		      break;
 
               case ML2_UPDATE:
                       ESP_LOGI(TAG, "Loop user: Update");
@@ -638,6 +701,9 @@
 	case ML2_ZERO_UNIT3:    if (rotate_to_sub(event.state.position, 0, 1, &SubMenu))
 				    screen_unit_zero(Main_Loop2 - ML2_ZERO_UNIT1, SubMenu);
 				break;
+	case ML2_SETUP_MQTT:	if (rotate_to_sub(event.state.position, 0, 3, &SubMenu))
+				    screen_mqtt_setup(SubMenu);
+				break;
 	default:
 				ESP_LOGI(TAG, "Event: position %d, direction %s", event.state.position,
 				    event.state.direction ? (event.state.direction == ROTARY_ENCODER_DIRECTION_CLOCKWISE ? "CW":"CCW"):"NOT_SET");
@@ -664,7 +730,8 @@
 
 void menu_loop(void)
 {
-    int idx = 0;
+    int 	idx = 0;
+    char	txt[32];
 
     switch (Main_Loop2) {
 	case ML2_UNIT1:
@@ -685,6 +752,8 @@
 			    units[idx].mode = 1;
 			write_units();
 			xSemaphoreGive(xSemaphoreUnits);
+		    } else {
+			ESP_LOGE(TAG, "menu_loop() ML2_SETUP_UNIT%d units lock", idx + 1);
 		    }
 		    screen_unit_setup(idx, SubMenu);
 		    if (Main_Loop1 == ML1_DONE)
@@ -727,6 +796,30 @@
 		New_Loop2 = ML2_SETUP_MQTT;
 		break;
 
+	case ML2_SETUP_MQTT:
+		if (SubMenu == 0) {
+		    rotary_editer("MQTT server", config.mqtt_server, "", 16, EDIT_TYPE_TEXT);
+		    screen_mqtt_setup(SubMenu);
+		}
+		if (SubMenu == 1) {
+		    sprintf(txt, "%d", config.mqtt_port);
+		    rotary_editer("MQTT server poort", txt, "", 6, EDIT_TYPE_INT);
+		    config.mqtt_port = atoi(txt);
+		    screen_mqtt_setup(SubMenu);
+		}
+		if (SubMenu == 2) {
+		    rotary_editer("MQTT user", config.mqtt_user, "", 16, EDIT_TYPE_TEXT);
+                    rotary_editer("MQTT password", config.mqtt_pwd, "", 16, EDIT_TYPE_TEXT);
+		    screen_mqtt_setup(SubMenu);
+		}
+		if (SubMenu == 3) {
+		    ESP_LOGI(TAG, "mqtt_server %s:%d", config.mqtt_server, config.mqtt_port);
+		    ESP_LOGI(TAG, "mqtt_user/pass `%s/%s`", config.mqtt_user, config.mqtt_pwd);
+		    write_config();
+                    New_Loop2 = ML2_MQTT;
+		}
+		break;
+
 	default:
 		break;
     }
--- a/main/task_wifi.c	Thu Nov 07 15:09:51 2019 +0100
+++ b/main/task_wifi.c	Thu Nov 07 22:58:48 2019 +0100
@@ -149,7 +149,7 @@
 
                 ESP_LOGI(TAG, "Event STA disconnected, ssid:%s, ssid_len:%d, bssid:" MACSTR ", reason:%d",
                        disconnected->ssid, disconnected->ssid_len, MAC2STR(disconnected->bssid), disconnected->reason);
-                if (xSemaphoreTake(xSemaphoreWiFi, 10) == pdTRUE) {
+                if (xSemaphoreTake(xSemaphoreWiFi, 25) == pdTRUE) {
                     wifi_state->STA_connected = false;
                     wifi_state->STA_online = false;
                     wifi_state->STA_rssi = 0;

mercurial