main/task_user.c

changeset 34
495b7eafbf5a
parent 33
331e7f700971
child 36
e2a43e7c4035
equal deleted inserted replaced
33:331e7f700971 34:495b7eafbf5a
24 static int PushDuration = 0; ///< Duration of the pushed button 24 static int PushDuration = 0; ///< Duration of the pushed button
25 struct strStations APs[10]; ///< List of APs we know 25 struct strStations APs[10]; ///< List of APs we know
26 int edit_ssid = 0; ///< SSID being edited 26 int edit_ssid = 0; ///< SSID being edited
27 int num_ssids = 0; ///< Number of SSIDs we know 27 int num_ssids = 0; ///< Number of SSIDs we know
28 struct strStations editAP; ///< Data of station to edit 28 struct strStations editAP; ///< Data of station to edit
29 char sensors[DS18B20_MAX][17]; ///< Sensors to select
29 30
30 extern const esp_app_desc_t *app_desc; 31 extern const esp_app_desc_t *app_desc;
31 extern unit_t units[3]; ///< Pressure test units 32 extern unit_t units[3]; ///< Pressure test units
32 extern SemaphoreHandle_t xSemaphoreUnits; ///< Units lock semaphore 33 extern SemaphoreHandle_t xSemaphoreUnits; ///< Units lock semaphore
33 extern DS18B20_State *ds18b20_state; ///< DS18B20 state 34 extern DS18B20_State *ds18b20_state; ///< DS18B20 state
37 extern WIFI_State *wifi_state; ///< WiFi state 38 extern WIFI_State *wifi_state; ///< WiFi state
38 extern SemaphoreHandle_t xSemaphoreWiFi; ///< WiFi lock semaphore 39 extern SemaphoreHandle_t xSemaphoreWiFi; ///< WiFi lock semaphore
39 extern int count_pub; ///< Published MQTT messages in transit 40 extern int count_pub; ///< Published MQTT messages in transit
40 extern int Main_Loop1; ///< Main measure loop 41 extern int Main_Loop1; ///< Main measure loop
41 extern int update_running; ///< If update is running 42 extern int update_running; ///< If update is running
43 extern int num_sensors; ///< Detected DS18B20 sensors
42 44
43 const int TASK_USER_COLD = BIT0; ///< System cold start 45 const int TASK_USER_COLD = BIT0; ///< System cold start
44 const int TASK_USER_WAKEUP = BIT1; ///< System wakeup from deepsleep 46 const int TASK_USER_WAKEUP = BIT1; ///< System wakeup from deepsleep
45 const int TASK_USER_BUSY = BIT2; ///< User interface is busy doing something. 47 const int TASK_USER_BUSY = BIT2; ///< User interface is busy doing something.
46 const int TASK_USER_REFRESH = BIT3; ///< Refresh requested 48 const int TASK_USER_REFRESH = BIT3; ///< Refresh requested
328 /** 330 /**
329 * @brief The splash screen shown during cold boot or user wakeup. 331 * @brief The splash screen shown during cold boot or user wakeup.
330 */ 332 */
331 void screen_splash() 333 void screen_splash()
332 { 334 {
333 screen_top("CO2 meter %s", app_desc->version); 335 // screen_top("CO2 meter %s", app_desc->version);
334 336
335 u8g2_SetFont(&u8g2, u8g2_font_t0_22b_tf); 337 // u8g2_SetFont(&u8g2, u8g2_font_t0_22b_tf);
336 u8g2_uint_t w = u8g2_GetUTF8Width(&u8g2, "Welkom"); 338 // u8g2_uint_t w = u8g2_GetUTF8Width(&u8g2, "Welkom");
337 u8g2_DrawUTF8(&u8g2, (128 - w) / 2,50, "Welkom"); 339 // u8g2_DrawUTF8(&u8g2, (128 - w) / 2,50, "Welkom");
340
341 u8g2_SetDrawColor(&u8g2, 1);
342 u8g2_DrawBox(&u8g2, 0, 0, 128, 64);
338 343
339 u8g2_SendBuffer(&u8g2); 344 u8g2_SendBuffer(&u8g2);
340 u8g2_SetPowerSave(&u8g2, 0); // wake up display 345 u8g2_SetPowerSave(&u8g2, 0); // wake up display
341 } 346 }
342 347
423 { 428 {
424 screen_top("Meter %d nulpunt", no + 1); 429 screen_top("Meter %d nulpunt", no + 1);
425 menu_line( 0, 1, 25, "Huidig %d", units[no].pressure_zero); 430 menu_line( 0, 1, 25, "Huidig %d", units[no].pressure_zero);
426 menu_line(sub == 0, 1, 37, "Nieuw %d", units[no].pressure_voltage / (adc_state->Batt_voltage / 1000)); 431 menu_line(sub == 0, 1, 37, "Nieuw %d", units[no].pressure_voltage / (adc_state->Batt_voltage / 1000));
427 menu_line(sub == 1, 1, 49, "Terug"); 432 menu_line(sub == 1, 1, 49, "Terug");
433 u8g2_SendBuffer(&u8g2);
434 }
435
436
437
438 void screen_list_sensors(int no, int sub, int offset)
439 {
440 int i;
441
442 if (xSemaphoreTake(xSemaphoreDS18B20, 10) == pdTRUE) {
443 for (i = 0; i < ds18b20_state->num_sensors; i++) {
444 strncpy(sensors[i], ds18b20_state->sensor[i].rom_code, 17);
445 sensors[i][16] = '\0';
446 }
447 xSemaphoreGive(xSemaphoreDS18B20);
448 } else {
449 ESP_LOGE(TAG, "screen_list_sensors() DS18B20 lock error");
450 }
451
452 screen_top("DS18B20 lijst");
453 if (num_sensors == 0) {
454 menu_line(0, 1, 25, "Geen sensors");
455 } else {
456 for (i = 0; i < num_sensors; i++) {
457 menu_line(sub == i, 1, 25 + (i * 12), sensors[i + offset]);
458 if (i == 3)
459 break;
460 }
461 if ((i + offset) == num_sensors)
462 menu_line(sub == i, 1, 25 + (i * 12), "Terug");
463 }
428 u8g2_SendBuffer(&u8g2); 464 u8g2_SendBuffer(&u8g2);
429 } 465 }
430 466
431 467
432 468
812 ESP_LOGI(TAG, "Loop user: Zero Unit %d", Main_Loop2 - ML2_ZERO_UNIT1); 848 ESP_LOGI(TAG, "Loop user: Zero Unit %d", Main_Loop2 - ML2_ZERO_UNIT1);
813 SubMenu = SubOffset = 0; 849 SubMenu = SubOffset = 0;
814 screen_unit_zero(Main_Loop2 - ML2_ZERO_UNIT1, SubMenu); 850 screen_unit_zero(Main_Loop2 - ML2_ZERO_UNIT1, SubMenu);
815 break; 851 break;
816 852
853 case ML2_SEL_SENSOR1:
854 case ML2_SEL_SENSOR2:
855 case ML2_SEL_SENSOR3:
856 ESP_LOGI(TAG, "Loop user: Select sensor %d", Main_Loop2 - ML2_SEL_SENSOR1);
857 SubMenu = SubOffset = 0;
858 screen_list_sensors(Main_Loop2 - ML2_SEL_SENSOR1, SubMenu, SubOffset);
859 break;
860
817 case ML2_INACTIVE: 861 case ML2_INACTIVE:
818 ESP_LOGI(TAG, "Loop user: Inactive"); 862 ESP_LOGI(TAG, "Loop user: Inactive");
819 u8g2_SetPowerSave(&u8g2, 1); // powersave display 863 u8g2_SetPowerSave(&u8g2, 1); // powersave display
820 New_Loop2 = ML2_DONE; 864 New_Loop2 = ML2_DONE;
821 break; 865 break;
846 break; 890 break;
847 case ML2_ZERO_UNIT1: 891 case ML2_ZERO_UNIT1:
848 case ML2_ZERO_UNIT2: 892 case ML2_ZERO_UNIT2:
849 case ML2_ZERO_UNIT3: if (rotate_to_sub(event.state.position, 1, &SubMenu, &SubOffset)) 893 case ML2_ZERO_UNIT3: if (rotate_to_sub(event.state.position, 1, &SubMenu, &SubOffset))
850 screen_unit_zero(Main_Loop2 - ML2_ZERO_UNIT1, SubMenu); 894 screen_unit_zero(Main_Loop2 - ML2_ZERO_UNIT1, SubMenu);
895 break;
896 case ML2_SEL_SENSOR1:
897 case ML2_SEL_SENSOR2:
898 case ML2_SEL_SENSOR3: if (rotate_to_sub(event.state.position, num_sensors, &SubMenu, &SubOffset))
899 screen_list_sensors(Main_Loop2 - ML2_SEL_SENSOR1, SubMenu, SubOffset);
851 break; 900 break;
852 case ML2_SETUP_WIFI: if (rotate_to_sub(event.state.position, 2, &SubMenu, &SubOffset)) 901 case ML2_SETUP_WIFI: if (rotate_to_sub(event.state.position, 2, &SubMenu, &SubOffset))
853 screen_wifi_setup(SubMenu); 902 screen_wifi_setup(SubMenu);
854 break; 903 break;
855 case ML2_LIST_APS: if (rotate_to_sub(event.state.position, num_ssids, &SubMenu, &SubOffset)) 904 case ML2_LIST_APS: if (rotate_to_sub(event.state.position, num_ssids, &SubMenu, &SubOffset))
916 ESP_LOGE(TAG, "menu_loop() ML2_SETUP_UNIT%d units lock", idx + 1); 965 ESP_LOGE(TAG, "menu_loop() ML2_SETUP_UNIT%d units lock", idx + 1);
917 } 966 }
918 screen_unit_setup(idx, SubMenu); 967 screen_unit_setup(idx, SubMenu);
919 if (Main_Loop1 == ML1_DONE) 968 if (Main_Loop1 == ML1_DONE)
920 Main_Loop1 = ML1_INIT; 969 Main_Loop1 = ML1_INIT;
970 } else if (SubMenu == 1) {
971 New_Loop2 = ML2_ZERO_UNIT1 + idx;
972 } else if (SubMenu == 2) {
973 New_Loop2 = ML2_SEL_SENSOR1 + idx;
974 } else if (SubMenu == 3) {
975 New_Loop2 = ML2_UNIT1 + idx;
921 } 976 }
922 if (SubMenu == 1)
923 New_Loop2 = ML2_ZERO_UNIT1 + idx;
924 if (SubMenu == 3)
925 New_Loop2 = ML2_UNIT1 + idx;
926 break; 977 break;
927 978
928 case ML2_ZERO_UNIT1: 979 case ML2_ZERO_UNIT1:
929 case ML2_ZERO_UNIT2: 980 case ML2_ZERO_UNIT2:
930 case ML2_ZERO_UNIT3: 981 case ML2_ZERO_UNIT3:
941 ESP_LOGI(TAG, "Zero set unit %d, %d mV set %d", idx, adc_state->Pressure[idx].voltage, units[idx].pressure_zero); 992 ESP_LOGI(TAG, "Zero set unit %d, %d mV set %d", idx, adc_state->Pressure[idx].voltage, units[idx].pressure_zero);
942 screen_unit_zero(idx, SubMenu); 993 screen_unit_zero(idx, SubMenu);
943 if (Main_Loop1 == ML1_DONE) 994 if (Main_Loop1 == ML1_DONE)
944 Main_Loop1 = ML1_INIT; 995 Main_Loop1 = ML1_INIT;
945 } 996 }
946 } 997 } else if (SubMenu == 1) {
947 if (SubMenu == 1) {
948 New_Loop2 = ML2_SETUP_UNIT1 + idx; 998 New_Loop2 = ML2_SETUP_UNIT1 + idx;
949 SubMenu = 1; 999 SubMenu = 1;
950 } 1000 }
951 break; 1001 break;
952 1002
1003 case ML2_SEL_SENSOR1:
1004 case ML2_SEL_SENSOR2:
1005 case ML2_SEL_SENSOR3:
1006 idx = Main_Loop2 - ML2_SEL_SENSOR1;
1007 if ((SubMenu + SubOffset) < num_sensors) {
1008 ESP_LOGI(TAG, "Select sensor %d %s for unit %d", SubMenu + SubOffset, sensors[SubMenu + SubOffset], idx + 1);
1009
1010 } else {
1011 New_Loop2 = ML2_SETUP_UNIT1 + idx;
1012 SubMenu = 2;
1013 SubOffset = 0;
1014 }
1015 break;
1016
953 case ML2_WIFI: 1017 case ML2_WIFI:
954 New_Loop2 = ML2_SETUP_WIFI; 1018 New_Loop2 = ML2_SETUP_WIFI;
955 break; 1019 break;
956 1020
957 case ML2_SETUP_WIFI: 1021 case ML2_SETUP_WIFI:
958 if (SubMenu == 0) { 1022 if (SubMenu == 0) {
959 New_Loop2 = ML2_LIST_APS; 1023 New_Loop2 = ML2_LIST_APS;
960 } 1024 } else if (SubMenu == 1) {
961 if (SubMenu == 1) {
962 edit_ssid = -1; 1025 edit_ssid = -1;
963 editAP.SSID[0] = '\0'; 1026 editAP.SSID[0] = '\0';
964 editAP.Password[0] = '\0'; 1027 editAP.Password[0] = '\0';
965 SubMenu = SubOffset = 0; 1028 SubMenu = SubOffset = 0;
966 New_Loop2 = ML2_EDIT_AP; 1029 New_Loop2 = ML2_EDIT_AP;
967 } 1030 } else if (SubMenu == 2) {
968 if (SubMenu == 2) {
969 New_Loop2 = ML2_WIFI; 1031 New_Loop2 = ML2_WIFI;
970 } 1032 }
971 break; 1033 break;
972 1034
973 case ML2_LIST_APS: 1035 case ML2_LIST_APS:
985 1047
986 case ML2_EDIT_AP: 1048 case ML2_EDIT_AP:
987 if (SubMenu == 0) { 1049 if (SubMenu == 0) {
988 rotary_editer("SSID", editAP.SSID, "", 16, EDIT_TYPE_TEXT); 1050 rotary_editer("SSID", editAP.SSID, "", 16, EDIT_TYPE_TEXT);
989 screen_edit_ap(SubMenu); 1051 screen_edit_ap(SubMenu);
990 } 1052 } else if (SubMenu == 1) {
991 if (SubMenu == 1) {
992 rotary_editer("PSK", editAP.Password, "", 16, EDIT_TYPE_TEXT); 1053 rotary_editer("PSK", editAP.Password, "", 16, EDIT_TYPE_TEXT);
993 screen_edit_ap(SubMenu); 1054 screen_edit_ap(SubMenu);
994 } 1055 } else if (SubMenu == 2 || SubMenu == 3) {
995 if (SubMenu == 2 || SubMenu == 3) {
996 update_running = 1; // Block measurements 1056 update_running = 1; // Block measurements
997 int timeout = 600; 1057 int timeout = 600;
998 if (edit_ssid < 0) 1058 if (edit_ssid < 0)
999 New_Loop2 = ML2_SETUP_WIFI; 1059 New_Loop2 = ML2_SETUP_WIFI;
1000 else 1060 else
1030 1090
1031 case ML2_SETUP_NETWORK: 1091 case ML2_SETUP_NETWORK:
1032 if (SubMenu == 0) { 1092 if (SubMenu == 0) {
1033 rotary_editer("Hostnaam", config.hostname, "", 16, EDIT_TYPE_TEXT); 1093 rotary_editer("Hostnaam", config.hostname, "", 16, EDIT_TYPE_TEXT);
1034 screen_network_setup(SubMenu); 1094 screen_network_setup(SubMenu);
1035 } 1095 } else if (SubMenu == 1) {
1036 if (SubMenu == 1) {
1037 rotary_editer("NTP server", config.ntp_server, "", 16, EDIT_TYPE_TEXT); 1096 rotary_editer("NTP server", config.ntp_server, "", 16, EDIT_TYPE_TEXT);
1038 screen_network_setup(SubMenu); 1097 screen_network_setup(SubMenu);
1039 } 1098 } else if (SubMenu == 2) {
1040 if (SubMenu == 2) {
1041 ESP_LOGI(TAG, "Hostname `%s`", config.hostname); 1099 ESP_LOGI(TAG, "Hostname `%s`", config.hostname);
1042 ESP_LOGI(TAG, "NTP server `%s`", config.ntp_server); 1100 ESP_LOGI(TAG, "NTP server `%s`", config.ntp_server);
1043 write_config(); 1101 write_config();
1044 New_Loop2 = ML2_NETWORK; 1102 New_Loop2 = ML2_NETWORK;
1045 } 1103 }
1051 1109
1052 case ML2_SETUP_MQTT: 1110 case ML2_SETUP_MQTT:
1053 if (SubMenu == 0) { 1111 if (SubMenu == 0) {
1054 rotary_editer("MQTT server", config.mqtt_server, "", 16, EDIT_TYPE_TEXT); 1112 rotary_editer("MQTT server", config.mqtt_server, "", 16, EDIT_TYPE_TEXT);
1055 screen_mqtt_setup(SubMenu); 1113 screen_mqtt_setup(SubMenu);
1056 } 1114 } else if (SubMenu == 1) {
1057 if (SubMenu == 1) {
1058 sprintf(txt, "%d", config.mqtt_port); 1115 sprintf(txt, "%d", config.mqtt_port);
1059 rotary_editer("MQTT server poort", txt, "", 6, EDIT_TYPE_INT); 1116 rotary_editer("MQTT server poort", txt, "", 6, EDIT_TYPE_INT);
1060 config.mqtt_port = atoi(txt); 1117 config.mqtt_port = atoi(txt);
1061 screen_mqtt_setup(SubMenu); 1118 screen_mqtt_setup(SubMenu);
1062 } 1119 } else if (SubMenu == 2) {
1063 if (SubMenu == 2) {
1064 rotary_editer("MQTT user", config.mqtt_user, "", 16, EDIT_TYPE_TEXT); 1120 rotary_editer("MQTT user", config.mqtt_user, "", 16, EDIT_TYPE_TEXT);
1065 rotary_editer("MQTT password", config.mqtt_pwd, "", 16, EDIT_TYPE_TEXT); 1121 rotary_editer("MQTT password", config.mqtt_pwd, "", 16, EDIT_TYPE_TEXT);
1066 screen_mqtt_setup(SubMenu); 1122 screen_mqtt_setup(SubMenu);
1067 } 1123 } else if (SubMenu == 3) {
1068 if (SubMenu == 3) {
1069 ESP_LOGI(TAG, "mqtt_server %s:%d", config.mqtt_server, config.mqtt_port); 1124 ESP_LOGI(TAG, "mqtt_server %s:%d", config.mqtt_server, config.mqtt_port);
1070 ESP_LOGI(TAG, "mqtt_user/pass `%s/%s`", config.mqtt_user, config.mqtt_pwd); 1125 ESP_LOGI(TAG, "mqtt_user/pass `%s/%s`", config.mqtt_user, config.mqtt_pwd);
1071 write_config(); 1126 write_config();
1072 New_Loop2 = ML2_MQTT; 1127 New_Loop2 = ML2_MQTT;
1073 } 1128 }

mercurial