diff --git a/components/meteofrance/meteofrance.c b/components/meteofrance/meteofrance.c index cf30e09..def69d0 100644 --- a/components/meteofrance/meteofrance.c +++ b/components/meteofrance/meteofrance.c @@ -330,7 +330,7 @@ static void http_request_task(void* domotic_event_group) // Waiting until either the connection is established (WIFI_CONNECTED_BIT). EventBits_t bits = xEventGroupWaitBits(domotic_event_group, BIT0, - pdTRUE, + pdFALSE, pdFALSE, portMAX_DELAY); if (bits & BIT0){ diff --git a/main/communication.c b/main/communication.c index 02fe390..efa5c71 100644 --- a/main/communication.c +++ b/main/communication.c @@ -126,7 +126,7 @@ static void mqtt_event_handler(void *handler_args, esp_event_base_t base, int32_ break; } } -void mqtt_app_start(mqtt_callback callback) +void mqtt_app_start(mqtt_callback callback, EventGroupHandle_t domotic_event_group) { @@ -169,7 +169,17 @@ void mqtt_app_start(mqtt_callback callback) client = esp_mqtt_client_init(&mqtt_cfg); /* The last argument may be used to pass data to the event handler, in this example mqtt_event_handler */ esp_mqtt_client_register_event(client, ESP_EVENT_ANY_ID, mqtt_event_handler, NULL); - esp_mqtt_client_start(client); + // Waiting until either the connection is established (WIFI_CONNECTED_BIT) or connection failed for the maximum + // number of re-tries (WIFI_FAIL_BIT). The bits are set by event_handler() (see above) + EventBits_t bits = xEventGroupWaitBits(domotic_event_group, + BIT0, + pdFALSE, + pdFALSE, + portMAX_DELAY); + if (bits & BIT0) + { + esp_mqtt_client_start(client); + } } diff --git a/main/ihm.c b/main/ihm.c index e495bf2..50576bf 100644 --- a/main/ihm.c +++ b/main/ihm.c @@ -37,7 +37,85 @@ char dateHeureStr[30]; static lv_style_t no_padding; static const char *TAG = "IHM"; +static lv_subject_t wifiStatus; +LV_IMAGE_DECLARE(wifi_ok); +LV_IMAGE_DECLARE(wifi_ko); + + +static void wifiStatus_obs_cb(lv_observer_t * observer, lv_subject_t * subject) +{ + ESP_LOGE(TAG, "On passe dans le callback de chgt de statut; %li", lv_subject_get_int(subject)); + bsp_display_lock(0); + if(lv_layer_top()!=NULL && lv_obj_get_child_cnt(lv_layer_top())){ + lv_obj_t * wifiSt = lv_obj_get_child(lv_obj_get_child(lv_layer_top(), 0),2); + if(lv_obj_check_type(wifiSt, &lv_image_class)){ + switch (lv_subject_get_int(subject)) + { + case 0: + lv_image_set_src(wifiSt,&wifi_ko); + break; + case 1: + lv_image_set_src(wifiSt,&wifi_ok); + break; + default: + break; + } + }else{ + ESP_LOGE(TAG, "L'objet recuip en semble pas etre du bon type"); + } + }else{ + ESP_LOGI(TAG,"Pour le moment l'icone de statut n'existe pas"); + } + + bsp_display_unlock(); + //int32_t prev_v = lv_subject_get_previous_int(subject); + //int32_t cur_v = lv_subject_get_int(subject); + + //lv_obj_t * btn = lv_observer_get_target(observer); +} + +void drawIhm(void *xIHMEventQueue){ + + init_display(); + + lv_subject_init_int(&wifiStatus,0); + lv_subject_add_observer_obj(&wifiStatus, wifiStatus_obs_cb, NULL, NULL); + + display_lock("app_main"); + app_main_display(); + display_unlock("app_main"); + // Show LVGL objects + if(display_lock("draw_ihm")){ + draw_ihm(); + display_unlock("draw_ihm"); + }else{ + ESP_LOGE(TAG,"Impossible d'obtenir le mutex pour draw_ihm"); + } + + while (1) + { + xIPStackEvent_t xReceivedEvent; + // On cree une queue qui va permettre de recevoir les informations a afficher + xQueueReceive(xIHMEventQueue,&xReceivedEvent, portMAX_DELAY ); + switch (xReceivedEvent.eEventType) + { + case IHM_EVT_WIFI_STATUS: + ESP_LOGE(TAG, "On a recu un evt wifi"); + lv_subject_set_int(&wifiStatus, (int)xReceivedEvent.pvData); + break; + case IHM_EVT_TIME_SETTED: + ESP_LOGE(TAG, "On a recu un evt timesetted"); + draw_time(xReceivedEvent.pvData); + break; + default: + ESP_LOGE(TAG, "Evt inconnu"); + break; + }; + vTaskDelay(5/portTICK_PERIOD_MS); + } + +} void draw_time(char* dateHeure){ diff --git a/main/ihm.h b/main/ihm.h index d1bf8d7..67dc6ae 100644 --- a/main/ihm.h +++ b/main/ihm.h @@ -30,3 +30,17 @@ void tabChgEvt(lv_event_t *event); void draw_tabCuve(lv_obj_t * parent); void draw_tabHome(lv_obj_t * parent); void draw_tabSettings(lv_obj_t * parent); + +typedef enum eIHMEvent_t{ + IHM_EVT_WIFI_STATUS, + IHM_EVT_TIME_SETTED +} eIHMEvent_t; + +typedef struct IP_TASK_COMMANDS +{ + eIHMEvent_t eEventType; /* Tells the receiving task what the event is. */ + void *pvData; /* Holds or points to any data associated with the event. */ +} xIPStackEvent_t; + + +void drawIhm(void *pvParameter); diff --git a/main/include/communication.h b/main/include/communication.h index b438fd4..77004f2 100644 --- a/main/include/communication.h +++ b/main/include/communication.h @@ -15,7 +15,7 @@ typedef enum mqtt_evt{ typedef void (*wifi_callback)(wifi_evt evt); typedef void (*mqtt_callback)(mqtt_evt evt, esp_mqtt_event_handle_t evt_data); void wifi_init_sta(wifi_callback cb); -void mqtt_app_start(mqtt_callback cb); +void mqtt_app_start(mqtt_callback cb, EventGroupHandle_t evtGroup); #define topicTempExt "house/temp/282A802600008059" diff --git a/main/main.c b/main/main.c index 1eaf035..89baa5a 100644 --- a/main/main.c +++ b/main/main.c @@ -46,47 +46,18 @@ static const char *TAG = "domoTic"; extern esp_mqtt_client_handle_t client; -typedef enum domo_events{ - EVT_WIFI_CONNECTED, - EVT_TIME_SETTED -} domo_events; -static EventGroupHandle_t hevt; + #define WIFI_RDY 0b0001 -static lv_subject_t wifiStatus; -LV_IMAGE_DECLARE(wifi_ok); -LV_IMAGE_DECLARE(wifi_ko); + +EventGroupHandle_t domotic_event_group; +QueueHandle_t ihm_queue; static void wifiStatus_obs_cb(lv_observer_t * observer, lv_subject_t * subject); -static void wifiStatus_obs_cb(lv_observer_t * observer, lv_subject_t * subject) -{ - ESP_LOGE(TAG, "On passe dans le callback de chgt de statut; %li", lv_subject_get_int(subject)); - bsp_display_lock(0); - lv_obj_t * wifiSt = lv_obj_get_child(lv_obj_get_child(lv_layer_top(), 0),2); - if(lv_obj_check_type(wifiSt, &lv_image_class)){ - switch (lv_subject_get_int(subject)) - { - case 0: - lv_image_set_src(wifiSt,&wifi_ko); - break; - case 1: - lv_image_set_src(wifiSt,&wifi_ok); - break; - default: - break; - } - }else{ - ESP_LOGE(TAG, "L'objet recuip en semble pas etre du bon type"); - } - bsp_display_unlock(); - //int32_t prev_v = lv_subject_get_previous_int(subject); - //int32_t cur_v = lv_subject_get_int(subject); - //lv_obj_t * btn = lv_observer_get_target(observer); -} lv_subject_t mqttStatus; extern lv_subject_t tempIntSubj; @@ -186,22 +157,27 @@ void mqtt_cb(mqtt_evt evt, esp_mqtt_event_handle_t event){ } } -EventGroupHandle_t domotic_event_group; -void send_event(domo_events evt){ +void send_event(domo_events evt, void* pDatas){ ESP_LOGE(TAG,"On est dans l'event handler %i", evt); switch(evt){ case EVT_WIFI_CONNECTED: xEventGroupSetBits(domotic_event_group, WIFI_CONNECTED_BIT); ESP_LOGI(TAG, "connected to ap SSID"); - if(lvgl_port_lock(0)){ - ESP_LOGE(TAG,"Statut Wifi 1"); - lv_subject_set_int(&wifiStatus,1); - lvgl_port_unlock(); - } - + xIPStackEvent_t evt = { + .eEventType = IHM_EVT_WIFI_STATUS, + .pvData = (void *)true + }; + if(xQueueSendToFront( ihm_queue, ( void * ) &evt, ( TickType_t ) 10 ) != pdPASS){ + ESP_LOGE(TAG, "La queue est pleine"); + }; break; case EVT_TIME_SETTED: + xIPStackEvent_t m = { + .eEventType = IHM_EVT_TIME_SETTED, + .pvData = pDatas + }; + xQueueSendToFront(ihm_queue,&m,5); break; } } @@ -402,35 +378,19 @@ void wifi_cb(wifi_evt evt){ switch(evt){ case WIFI_CONNECTED: mainState.wifi_init=true; - send_event(WIFI_CONNECTED); + send_event(WIFI_CONNECTED,NULL); break; case WIFI_DISCONNECTED: - if(lvgl_port_lock(0)){ - ESP_LOGE(TAG,"Statut Wifi 0"); - lv_subject_set_int(&wifiStatus,0); - lvgl_port_unlock(); - } + mainState.wifi_init=true; + send_event(WIFI_DISCONNECTED,NULL); break; case WIFI_GOT_IP: - if(lvgl_port_lock(0)){ - ESP_LOGE(TAG,"Statut Wifi 1"); - lv_subject_set_int(&wifiStatus,1); - lvgl_port_unlock(); - } + mainState.wifi_init=true; + send_event(WIFI_CONNECTED,NULL); break; case WIFI_CONNECT_FAIL: - if(lvgl_port_lock(0)){ - ESP_LOGE(TAG,"Statut Wifi 0"); - lv_subject_set_int(&wifiStatus,0); - lvgl_port_unlock(); - } break; default: - if(lvgl_port_lock(0)){ - ESP_LOGE(TAG,"Statut Wifi 0"); - lv_subject_set_int(&wifiStatus,0); - lvgl_port_unlock(); - } break; } @@ -541,37 +501,14 @@ void app_main(void) { domotic_event_group = xEventGroupCreate(); - printf("Minimum free heap size: %" PRIu32 " bytes\n", esp_get_minimum_free_heap_size()); - printf("Free heap size: %" PRIu32 " bytes\n", esp_get_free_heap_size()); - heap_caps_print_heap_info(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); - heap_caps_register_failed_alloc_callback(alloc_fail); - - printf("1- Free heap after buffers allocation: %d\n", xPortGetFreeHeapSize()); + ihm_queue = xQueueCreate(10,sizeof(xIPStackEvent_t)); esp_log_level_set("wifi", ESP_LOG_ERROR); esp_log_level_set(TAG, ESP_LOG_VERBOSE); - printf("2- Free heap after buffers allocation: %d\n", xPortGetFreeHeapSize()); - //mount_sd_card(); bsp_sdcard_mount(); - - - //lv_log_register_print_cb(log_cb); - // LCD HW initialization - //ESP_ERROR_CHECK(app_lcd_init()); - - printf("4 - Free heap after buffers allocation: %d\n", xPortGetFreeHeapSize()); - // Touch initialization - //ESP_ERROR_CHECK(app_touch_init()); - - printf("5 - Free heap after buffers allocation: %d\n", xPortGetFreeHeapSize()); - // LVGL initialization - //ESP_ERROR_CHECK(app_lvgl_init()); - - printf("6 - Free heap after buffers allocation: %d\n", xPortGetFreeHeapSize()); - ESP_LOGI(TAG, "Initializing LittleFS"); esp_vfs_littlefs_conf_t conflfs = { @@ -608,12 +545,8 @@ void app_main(void) // On affiche au plus tot l'ecran de démarrage // ESP_ERROR_CHECK(esp_lcd_panel_mirror(lcd_panel,true,true)); - init_display(); - display_lock("app_main"); - app_main_display(); - display_unlock("app_main"); + xTaskCreatePinnedToCore(&drawIhm,"ihm_task",10000,ihm_queue,10,NULL,0); - printf("7 - Free heap after buffers allocation: %d\n", xPortGetFreeHeapSize()); // Initialize NVS esp_err_t ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) @@ -623,11 +556,8 @@ void app_main(void) } ESP_ERROR_CHECK(ret); - printf("8 - Free heap after buffers allocation: %d\n", xPortGetFreeHeapSize()); ESP_LOGI(TAG, "ESP_WIFI_MODE_STA"); - lv_subject_init_int(&wifiStatus,0); - lv_subject_add_observer_obj(&wifiStatus, wifiStatus_obs_cb, NULL, NULL); wifi_init_sta(wifi_cb); on_weather_data_retrieval(weather_data_retreived); @@ -650,38 +580,14 @@ void app_main(void) } /* Tache updateTime - FIN*/ - mqtt_app_start(mqtt_cb); + mqtt_app_start(mqtt_cb, domotic_event_group); start_wifi_logger(); wifi_log_e("test", "%s %d %f", "hello world wifi logger", 43, 45.341223242); // write log over wifi with log level -> ERROR } - - //esp_log_level_set("tcp_handler", ESP_LOG_NONE); - printf("8b - Free heap after buffers allocation: %d\n", xPortGetFreeHeapSize()); - - printf("9 - Free heap after buffers allocation: %d\n", xPortGetFreeHeapSize()); - - - printf("10. Free heap after buffers allocation: %d\n", xPortGetFreeHeapSize()); - heap_caps_print_heap_info(MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT); - - - printf("11. Free heap after buffers allocation: %d\n", xPortGetFreeHeapSize()); - - printf("12. Free heap after buffers allocation: %d\n", xPortGetFreeHeapSize()); - // Show LVGL objects - if(display_lock("draw_ihm")){ - draw_ihm(); - display_unlock("draw_ihm"); - }else{ - ESP_LOGE(TAG,"Impossible d'obtenir le mutex pour draw_ihm"); - } - lv_subject_init_int(&mqttStatus,-1); lv_subject_add_observer_obj(&mqttStatus, mqttStatus_obs_cb, NULL, NULL); - - // Configuration de la sonde Temp/Humid. am2302_config_t am2302_config = { .gpio_num = AM2302_GPIO, @@ -690,8 +596,6 @@ void app_main(void) .clk_src = RMT_CLK_SRC_DEFAULT, }; ESP_ERROR_CHECK(am2302_new_sensor_rmt(&am2302_config, &rmt_config, &sensor)); - - xTaskCreate(&readTempHumid, "read_temp_task", 8192, NULL, 5, NULL); //xTaskCreate(&simple_ota_example_task, "ota_example_task", 8192, NULL, 5, NULL); diff --git a/main/main.h b/main/main.h index 0cf725f..fdd80bd 100644 --- a/main/main.h +++ b/main/main.h @@ -1,2 +1,7 @@ #pragma once #define WIFI_CONNECTED_BIT BIT0 +typedef enum domo_events{ + EVT_WIFI_CONNECTED, + EVT_TIME_SETTED +} domo_events; +void send_event(domo_events evt, void* pDatas); diff --git a/main/obtain_time.c b/main/obtain_time.c index 34955e6..a237bcc 100644 --- a/main/obtain_time.c +++ b/main/obtain_time.c @@ -25,7 +25,7 @@ void time_sync_notification_cb(struct timeval *tv) //strftime(strftime_buf, sizeof(strftime_buf), "%c", &timeinfo); sprintf(strftime_buf, "%s %d %s %02d:%02d", days[timeinfo.tm_wday], timeinfo.tm_mday, months[timeinfo.tm_mon], timeinfo.tm_hour, timeinfo.tm_min); localtime_r(&now, &timeinfo); - draw_time(strftime_buf); + send_event(EVT_TIME_SETTED,&strftime_buf); } void obtain_time() @@ -63,12 +63,7 @@ void updateTime(void *pvParameter) struct tm timeinfo = {0}; localtime_r(&now, &timeinfo); sprintf(strftime_buf, "%s %d %s %02d:%02d", days[timeinfo.tm_wday], timeinfo.tm_mday, months[timeinfo.tm_mon], timeinfo.tm_hour, timeinfo.tm_min); - draw_time(strftime_buf); - /* - lv_obj_refr_size(lv_obj_get_child(lv_obj_get_child(lv_layer_top(),0),0)); - lv_obj_refr_size(lv_obj_get_child(lv_layer_top(),0)); - lvgl_port_unlock(); - }*/ + send_event(EVT_TIME_SETTED,&strftime_buf); vTaskDelay(60000 / portTICK_PERIOD_MS); } }