diff options
author | Sebastian <sebasjm@gmail.com> | 2022-07-24 11:47:59 -0300 |
---|---|---|
committer | Sebastian <sebasjm@gmail.com> | 2022-07-24 11:47:59 -0300 |
commit | 90627094f1437806967eff7fdf701b3c4247d5d9 (patch) | |
tree | 3b0ce4f167451372a71e3b432ca7baaf60c585cc | |
parent | f0b47883c9bab841a61d0641bb9797786e5d9fd7 (diff) | |
download | mch2022-90627094f1437806967eff7fdf701b3c4247d5d9.tar.gz mch2022-90627094f1437806967eff7fdf701b3c4247d5d9.tar.bz2 mch2022-90627094f1437806967eff7fdf701b3c4247d5d9.zip |
menu
-rw-r--r-- | main/CMakeLists.txt | 2 | ||||
-rw-r--r-- | main/component.mk | 2 | ||||
-rw-r--r-- | main/icon.png | bin | 0 -> 1755 bytes | |||
-rw-r--r-- | main/main.c | 230 |
4 files changed, 169 insertions, 65 deletions
diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index f35667a..451b43e 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -8,6 +8,6 @@ idf_component_register( "qrcodegen.c" "esp_qrcode_wrapper.c" "esp_qrcode_main.c" "main.c" INCLUDE_DIRS "." "include" - EMBED_TXTFILES inet_sec.pem + EMBED_TXTFILES inet_sec.pem icon.png ) diff --git a/main/component.mk b/main/component.mk index bc67960..8ee1d6d 100644 --- a/main/component.mk +++ b/main/component.mk @@ -5,4 +5,4 @@ # embed files from the "certs" directory as binary data symbols # in the app -COMPONENT_EMBED_TXTFILES := inet_sec.pem +COMPONENT_EMBED_TXTFILES := inet_sec.pem icon.png diff --git a/main/icon.png b/main/icon.png Binary files differnew file mode 100644 index 0000000..c5dbf31 --- /dev/null +++ b/main/icon.png diff --git a/main/main.c b/main/main.c index 7806193..494dc7f 100644 --- a/main/main.c +++ b/main/main.c @@ -21,6 +21,10 @@ xQueueHandle buttonQueue; #include <esp_log.h> #include <cJSON.h> #include "qrcode.h" +#include <pax_codecs.h> + +extern const char icon_png_start[] asm("_binary_icon_png_start"); +extern const char icon_png_end[] asm("_binary_icon_png_end"); // Updates the screen with the latest buffer. void disp_flush() @@ -61,9 +65,10 @@ void initialize_things(void) pax_background(&buf, col); } +const pax_font_t *font = pax_font_saira_regular; + void show_message(const char *message) { - const pax_font_t *font = pax_font_saira_condensed; int hue = esp_random() & 255; pax_col_t col = pax_col_hsv(hue, 255 /*saturation*/, 255 /*brighness*/); @@ -88,17 +93,23 @@ void show_message(const char *message) disp_flush(); } -void wait_any_key_or_exit() +int wait_any_key_or_exit() { - // Structure used to receive data. - rp2040_input_message_t message; - // Wait forever for a button press (because of portMAX_DELAY) - xQueueReceive(buttonQueue, &message, portMAX_DELAY); - // Which button is currently pressed? - if (message.input == RP2040_INPUT_BUTTON_START && message.state) + while (1) { - // If home is pressed, exit to launcher. - exit_to_launcher(); + rp2040_input_message_t buttonMessage = {0}; + if (xQueueReceive(buttonQueue, &buttonMessage, portMAX_DELAY) == pdTRUE) + { + if (buttonMessage.state) + { + if (buttonMessage.input == RP2040_INPUT_BUTTON_START) + { + exit_to_launcher(); + } + + return buttonMessage.input; + } + } } } @@ -113,20 +124,13 @@ bool create_new_order(float amount, char *token, char **paymentURI) char local_response_buffer[MAX_HTTP_OUTPUT_BUFFER] = {0}; int token_len = strlen(token); - const char post_data[128]; + char post_data[128]; - // return "2022.204-123123123123"; - // POST - // static const char *host = TALER_MERCHANT_HOST; - // static const char *path = TALER_CREATE_ORDER_URL; - ESP_LOGI(TAG, "creating config"); esp_http_client_config_t config = { .port = 443, - .host = "merchant-backend.taler.ar", - .path = "/instances/mch2022/private/orders", - // .host = host, - // .path = path, - // .keep_alive_enable = true, + .host = TALER_MERCHANT_HOST, + .path = TALER_CREATE_ORDER_URL, + .keep_alive_enable = true, .method = HTTP_METHOD_POST, .transport_type = HTTP_TRANSPORT_OVER_SSL, .cert_pem = inet_sec_root_cert_pem_start, @@ -135,7 +139,6 @@ bool create_new_order(float amount, char *token, char **paymentURI) }; esp_http_client_handle_t client = esp_http_client_init(&config); esp_http_client_set_header(client, "Content-Type", "application/json"); - { char *auth_buffer = (char *)malloc(sizeof("Bearer secret-token:") + token_len + 1); sprintf(auth_buffer, "Bearer secret-token:%s", token); @@ -143,43 +146,118 @@ bool create_new_order(float amount, char *token, char **paymentURI) free(auth_buffer); } - sprintf(post_data, "{\"order\":{\"summary\":\"sold in mch2022\",\"amount\":\"ARS:%f\"},\"create_token\":false}", amount); + snprintf(post_data, sizeof(post_data), "{\"order\":{\"summary\":\"sold in mch2022\",\"amount\":\"ARS:%3.2f\"},\"create_token\":false}", amount); esp_http_client_set_post_field(client, post_data, strlen(post_data)); - ESP_LOGI(TAG, "about to perform the http query"); + ESP_LOGI(TAG, "querying..."); esp_err_t err = esp_http_client_perform(client); if (err == ESP_OK) { - ESP_LOGI(TAG, "buffer has %s", local_response_buffer); + ESP_LOGI(TAG, "query ok. buffer has %s bytes", local_response_buffer); cJSON *response_json = cJSON_Parse(local_response_buffer); cJSON *orderId = cJSON_GetObjectItemCaseSensitive(response_json, "order_id"); show_message(orderId->valuestring); int order_len = strlen(orderId->valuestring); - // static const char *uri = TALER_PAYMENT_URI; - // static const char *template = TALER_PAYMENT_TEMPLATE; - // *paymentURI = (char *)malloc(sizeof(uri) + order_len + /*last slash*/ 1 + /*0*/ 1); - // sprintf(*paymentURI, template, orderId->valuestring); - *paymentURI = (char *)malloc(sizeof("taler://pay/merchant-backend.taler.ar/instances/mch2022/") + order_len + /*last slash*/ 1 + /*0*/ 1); - sprintf(*paymentURI, "taler://pay/merchant-backend.taler.ar/instances/mch2022/%s/", orderId->valuestring); + *paymentURI = (char *)malloc(sizeof(TALER_PAYMENT_URI) + order_len + /*last slash*/ 1 + /*0*/ 1); + sprintf(*paymentURI, TALER_PAYMENT_TEMPLATE, orderId->valuestring); ESP_LOGI(TAG, "payment uri %s", *paymentURI); cJSON_Delete(response_json); } else { - const char buffer[200]; - sprintf(buffer, "failed = %d, %s", - esp_http_client_get_status_code(client), - esp_err_to_name(err)); + char buffer[200]; + snprintf(buffer, sizeof(buffer), "failed = %d, %s", + esp_http_client_get_status_code(client), + esp_err_to_name(err)); show_message(buffer); } - ESP_LOGI(TAG, "almos done"); esp_http_client_cleanup(client); - ESP_LOGI(TAG, "done"); return err == ESP_OK; } +#define SQUARE_SIZE 5 + +void esp_qrcode_print_screen(esp_qrcode_handle_t qrcode) +{ + int size = esp_qrcode_get_size(qrcode); + int border = 2; + + float ypos = 40; + + pax_background(&buf, 0xb0FFFFFF); // black background + + const char *message = "Use the GNU Taler Wallet to pay\n or any key to cancel."; + pax_vec1_t dims = pax_text_size(font, font->default_size / 2, message); + + // 240y * 320x + + // Draw the centered text. + pax_draw_text( + &buf, // Buffer to draw to. + 0xff000000, // color + font, font->default_size / 2, // Font and size to use. + // Position (top left corner) of the app. + (buf.width - dims.x) / 2.0, + 5, + // The text to be rendered. + message); + + for (int y = -border; y < size + border; y += 2) + { + + float xpos = 60; + for (int x = -border; x < size + border; x += 2) + { + if (esp_qrcode_get_module(qrcode, x, y)) + { + pax_draw_rect(&buf, 0xb0000000, xpos, ypos, SQUARE_SIZE, SQUARE_SIZE); + } + if ((x < size + border) && esp_qrcode_get_module(qrcode, x + 1, y)) + { + pax_draw_rect(&buf, 0xb0000000, xpos + SQUARE_SIZE, ypos, SQUARE_SIZE, SQUARE_SIZE); + } + if ((y < size + border) && esp_qrcode_get_module(qrcode, x, y + 1)) + { + pax_draw_rect(&buf, 0xb0000000, xpos, ypos + SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE); + } + if ((x < size + border) && (y < size + border) && esp_qrcode_get_module(qrcode, x + 1, y + 1)) + { + pax_draw_rect(&buf, 0xb0000000, xpos + SQUARE_SIZE, ypos + SQUARE_SIZE, SQUARE_SIZE, SQUARE_SIZE); + } + + xpos += SQUARE_SIZE + SQUARE_SIZE; + } + ypos += SQUARE_SIZE + SQUARE_SIZE; + } + + disp_flush(); +} + +char *LAST_STATUS = "ok"; +char *WIFI_STATUS = "connected"; +float NEXT_ORDER_PRICE = 3.2; +#define MENU_TEMPLATE "Merchant name: %s\nWifi status: %s\nLast order: %s\nNext order price: %3.2f\n" + +void show_menu() +{ + pax_background(&buf, 0xb0FFFFFF); // black background + // pax_draw_image(&buf, &icon_png_start, 10, 10); + pax_insert_png_buf(&buf, icon_png_start, icon_png_end - icon_png_start, 10, 10, PAX_BUF_32_8888ARGB); + + pax_draw_text(&buf, 0xFF000000, font, 18, 50, 20, "GNU Taler Merchant client\n#MCH2022"); + + char *status_buffer = NULL; + int status_buffer_size = sizeof(MENU_TEMPLATE) + strlen(TALER_MERCHANT_NAME) + strlen(LAST_STATUS) + strlen(WIFI_STATUS); + status_buffer = (char *)malloc(status_buffer_size); + 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, 60, status_buffer); + + pax_draw_text(&buf, 0xFF000000, font, 18, 20, 240 - 18, "🅰 create order 🅱 exit"); + disp_flush(); +} + void app_main() { @@ -187,41 +265,67 @@ void app_main() initialize_things(); show_message("Connecting to MCH2022"); - // wifi_connect_to_stored(); ESP_LOGI(TAG, "Initializing wifi..."); - wifi_init(); - bool connected = false; - while (!connected) - { - connected = wifi_connect_ent("MCH2022", "badge", "badge", "badge", ESP_EAP_TTLS_PHASE2_PAP, 3); - if (!connected) - { - ESP_LOGI(TAG, "could not connect, trying again..."); - show_message("go near an MCH2022 access point"); - } - } + // wifi_init(); + + // bool connected = false; + // while (!connected) + // { + // connected = wifi_connect_ent("MCH2022", "badge", "badge", "badge", ESP_EAP_TTLS_PHASE2_PAP, 3); + // if (!connected) + // { + // ESP_LOGI(TAG, "could not connect, trying again..."); + // show_message("go near an MCH2022 access point"); + // } + // } ESP_LOGI(TAG, "Connected."); show_message("connected!"); while (1) { - const char *orderId = NULL; - bool ok = create_new_order(3.2, "305a16ce7cc2e2793060e39b9210a700", &orderId); + show_menu(); + ESP_LOGI(TAG, "order generated!"); + const int key = wait_any_key_or_exit(); - if (ok) + if (key == RP2040_INPUT_BUTTON_ACCEPT) { - esp_qrcode_config_t cfg = { - .max_qrcode_version = 10, - .display_func = esp_qrcode_print_console, - .qrcode_ecc_level = ESP_QRCODE_ECC_LOW, - }; - - esp_qrcode_generate(&cfg, orderId); - free(orderId); + char *orderId = NULL; + + bool ok = create_new_order(NEXT_ORDER_PRICE, "305a16ce7cc2e2793060e39b9210a700", &orderId); + + if (ok) + { + LAST_STATUS = "generated, not payed"; + esp_qrcode_config_t cfg = { + .max_qrcode_version = 10, + .display_func = esp_qrcode_print_screen, + .qrcode_ecc_level = ESP_QRCODE_ECC_LOW, + }; + + esp_qrcode_generate(&cfg, orderId); + } + else + { + LAST_STATUS = "error creating the order"; + } + // free(orderId); + } + if (key == RP2040_INPUT_JOYSTICK_UP) + { + NEXT_ORDER_PRICE += 0.1; + } + if (key == RP2040_INPUT_JOYSTICK_DOWN) + { + NEXT_ORDER_PRICE -= 0.1; + } + if (key == RP2040_INPUT_JOYSTICK_LEFT) + { + NEXT_ORDER_PRICE += 1; + } + if (key == RP2040_INPUT_JOYSTICK_RIGHT) + { + NEXT_ORDER_PRICE -= 1; } - - wait_any_key_or_exit(); - show_message("again..."); } } |