]> granicus.if.org Git - esp-idf/commitdiff
examples/system: use esp_event library to handle events
authorIvan Grokhotkov <ivan@espressif.com>
Tue, 20 Nov 2018 10:25:25 +0000 (18:25 +0800)
committerIvan Grokhotkov <ivan@espressif.com>
Thu, 11 Apr 2019 04:32:03 +0000 (12:32 +0800)
examples/system/console/main/cmd_wifi.c

index 039c6b94bf48318142dedeaa7bb05c84370618e0..3cca7c3db950292c37ea70285de3e30f16a1bb52 100644 (file)
@@ -17,7 +17,7 @@
 #include "freertos/event_groups.h"
 #include "esp_wifi.h"
 #include "tcpip_adapter.h"
-#include "esp_event_loop.h"
+#include "esp_event.h"
 #include "cmd_wifi.h"
 
 #define JOIN_TIMEOUT_MS (10000)
 static EventGroupHandle_t wifi_event_group;
 const int CONNECTED_BIT = BIT0;
 
-static esp_err_t event_handler(void *ctx, system_event_t *event)
+
+static void event_handler(void* arg, esp_event_base_t event_base, 
+                                int32_t event_id, void* event_data)
 {
-    switch (event->event_id) {
-    case SYSTEM_EVENT_STA_GOT_IP:
-        xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
-        break;
-    case SYSTEM_EVENT_STA_DISCONNECTED:
+    if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
         esp_wifi_connect();
         xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
-        break;
-    default:
-        break;
+    } else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
+        xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
     }
-    return ESP_OK;
 }
 
 static void initialise_wifi(void)
@@ -50,9 +46,11 @@ static void initialise_wifi(void)
     }
     tcpip_adapter_init();
     wifi_event_group = xEventGroupCreate();
-    ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
+    ESP_ERROR_CHECK(esp_event_loop_create_default());
     wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
     ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
+    ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &event_handler, NULL) );
+    ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL) );
     ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
     ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_NULL) );
     ESP_ERROR_CHECK( esp_wifi_start() );