From e16d1ec982908e1950d967b5a192c22b0a48d75b Mon Sep 17 00:00:00 2001 From: Sebastian Date: Sun, 24 Jul 2022 15:09:16 -0300 Subject: wifi status --- main/main.c | 71 +++++++++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 53 insertions(+), 18 deletions(-) diff --git a/main/main.c b/main/main.c index aa6773b..f191e60 100644 --- a/main/main.c +++ b/main/main.c @@ -19,6 +19,7 @@ static pax_buf_t buf; xQueueHandle buttonQueue; #include +#include #include #include "qrcode.h" #include @@ -325,7 +326,9 @@ float NEXT_ORDER_PRICE = 3.2; #define SECOND_OPTION_RETRY 1 #define SECOND_OPTION_REFUND 2 -void show_menu(int second_option) +int LAST_SECOND_OPTION = SECOND_OPTION_EXIT; + +void show_menu() { pax_background(&buf, 0xb0FFFFFF); // black background // pax_draw_image(&buf, &icon_png_start, 10, 10); @@ -339,21 +342,49 @@ void show_menu(int second_option) snprintf(status_buffer, status_buffer_size, MENU_TEMPLATE, TALER_MERCHANT_NAME, WIFI_STATUS, LAST_STATUS, NEXT_ORDER_PRICE); pax_draw_text(&buf, 0xFF000000, font, 18, 20, 80, status_buffer); - if (second_option == SECOND_OPTION_EXIT) + if (LAST_SECOND_OPTION == SECOND_OPTION_EXIT) { - pax_draw_text(&buf, 0xFF000000, font, 18, 20, 240 - 18, "🅰 create order 🅱 exit"); + pax_draw_text(&buf, 0xFF000000, font, 18, 20, 240 - 18, "🅱 exit 🅰 create e-ticket"); } - if (second_option == SECOND_OPTION_RETRY) + if (LAST_SECOND_OPTION == SECOND_OPTION_RETRY) { - pax_draw_text(&buf, 0xFF000000, font, 18, 20, 240 - 18, "🅰 create order 🅱 retry"); + pax_draw_text(&buf, 0xFF000000, font, 18, 20, 240 - 18, "🅱 retry 🅰 create e-ticket"); } - if (second_option == SECOND_OPTION_REFUND) + if (LAST_SECOND_OPTION == SECOND_OPTION_REFUND) { - pax_draw_text(&buf, 0xFF000000, font, 18, 20, 240 - 18, "🅰 create order 🅱 refund"); + pax_draw_text(&buf, 0xFF000000, font, 18, 20, 240 - 18, "🅱 refund 🅰 create e-ticket"); } disp_flush(); } +// Handles WiFi events required to stay connected. +static void _wifi_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) +{ + if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) + { + ESP_LOGI(TAG, "WiFi station start."); + strcpy(WIFI_STATUS, "connecting..."); + show_menu(); + } + else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_STOP) + { + ESP_LOGI(TAG, "WiFi station stop."); + strcpy(WIFI_STATUS, "stopped"); + show_menu(); + } + else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) + { + strcpy(WIFI_STATUS, "disconnected..."); + show_menu(); + ESP_LOGI(TAG, "Retrying connection"); + } + else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) + { + strcpy(WIFI_STATUS, "connected"); + show_menu(); + } +} + void setup_wifi() { show_message("Connecting to MCH2022 wifi"); @@ -370,6 +401,11 @@ void setup_wifi() show_message("go near an MCH2022 access point"); } } + esp_event_handler_instance_t instance_any_id; + esp_event_handler_instance_t instance_got_ip; + ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &_wifi_event_handler, NULL, &instance_any_id)); + ESP_ERROR_CHECK(esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &_wifi_event_handler, NULL, &instance_got_ip)); + ESP_LOGI(TAG, "Connected."); } @@ -378,7 +414,7 @@ bool create_new_order() char *orderId = NULL; char creating_order_message[200]; - snprintf(creating_order_message, 200, "Creating order for ARS:%3.2f ...", NEXT_ORDER_PRICE); + snprintf(creating_order_message, 200, "Creating e-ticket for ARS:%3.2f ...", NEXT_ORDER_PRICE); show_message(creating_order_message); bool order_created = post_new_order(NEXT_ORDER_PRICE, "305a16ce7cc2e2793060e39b9210a700", &orderId); @@ -476,10 +512,9 @@ void app_main() initialize_things(); setup_wifi(); - int second_option = SECOND_OPTION_EXIT; while (1) { - show_menu(second_option); + show_menu(); const int key = wait_any_key_or_exit(); @@ -491,38 +526,38 @@ void app_main() bool ok = create_new_order(); if (!ok) { - second_option = SECOND_OPTION_EXIT; + LAST_SECOND_OPTION = SECOND_OPTION_EXIT; continue; } ESP_LOGI(TAG, "user is seeing qr code now"); ok = query_order_until_paid(); if (!ok) { - second_option = SECOND_OPTION_RETRY; + LAST_SECOND_OPTION = SECOND_OPTION_RETRY; } else { - second_option = SECOND_OPTION_REFUND; + LAST_SECOND_OPTION = SECOND_OPTION_REFUND; } } - else if (key == RP2040_INPUT_BUTTON_BACK && second_option == SECOND_OPTION_EXIT) + else if (key == RP2040_INPUT_BUTTON_BACK && LAST_SECOND_OPTION == SECOND_OPTION_EXIT) { exit_to_launcher(); } - else if (key == RP2040_INPUT_BUTTON_BACK && second_option == SECOND_OPTION_RETRY) + else if (key == RP2040_INPUT_BUTTON_BACK && LAST_SECOND_OPTION == SECOND_OPTION_RETRY) { ESP_LOGI(TAG, "retrying order %s", LAST_ORDER); bool ok = query_order_until_paid(); if (!ok) { - second_option = SECOND_OPTION_RETRY; + LAST_SECOND_OPTION = SECOND_OPTION_RETRY; } else { - second_option = SECOND_OPTION_REFUND; + LAST_SECOND_OPTION = SECOND_OPTION_REFUND; } } - else if (key == RP2040_INPUT_BUTTON_BACK && second_option == SECOND_OPTION_REFUND) + else if (key == RP2040_INPUT_BUTTON_BACK && LAST_SECOND_OPTION == SECOND_OPTION_REFUND) { show_message("not yet implemented!"); wait_any_key_or_exit(); -- cgit v1.2.3