esp_flasher_app.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. #include "esp_flasher_app_i.h"
  2. #include <furi.h>
  3. #include <furi_hal.h>
  4. static bool esp_flasher_app_custom_event_callback(void* context, uint32_t event) {
  5. furi_assert(context);
  6. EspFlasherApp* app = context;
  7. return scene_manager_handle_custom_event(app->scene_manager, event);
  8. }
  9. static bool esp_flasher_app_back_event_callback(void* context) {
  10. furi_assert(context);
  11. EspFlasherApp* app = context;
  12. return scene_manager_handle_back_event(app->scene_manager);
  13. }
  14. static void esp_flasher_app_tick_event_callback(void* context) {
  15. furi_assert(context);
  16. EspFlasherApp* app = context;
  17. scene_manager_handle_tick_event(app->scene_manager);
  18. }
  19. EspFlasherApp* esp_flasher_app_alloc() {
  20. EspFlasherApp* app = malloc(sizeof(EspFlasherApp));
  21. app->gui = furi_record_open(RECORD_GUI);
  22. app->dialogs = furi_record_open(RECORD_DIALOGS);
  23. app->storage = furi_record_open(RECORD_STORAGE);
  24. app->notification = furi_record_open(RECORD_NOTIFICATION);
  25. app->view_dispatcher = view_dispatcher_alloc();
  26. app->scene_manager = scene_manager_alloc(&esp_flasher_scene_handlers, app);
  27. view_dispatcher_enable_queue(app->view_dispatcher);
  28. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  29. view_dispatcher_set_custom_event_callback(
  30. app->view_dispatcher, esp_flasher_app_custom_event_callback);
  31. view_dispatcher_set_navigation_event_callback(
  32. app->view_dispatcher, esp_flasher_app_back_event_callback);
  33. view_dispatcher_set_tick_event_callback(
  34. app->view_dispatcher, esp_flasher_app_tick_event_callback, 100);
  35. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  36. app->var_item_list = variable_item_list_alloc();
  37. view_dispatcher_add_view(
  38. app->view_dispatcher,
  39. EspFlasherAppViewVarItemList,
  40. variable_item_list_get_view(app->var_item_list));
  41. app->text_box = text_box_alloc();
  42. view_dispatcher_add_view(
  43. app->view_dispatcher, EspFlasherAppViewConsoleOutput, text_box_get_view(app->text_box));
  44. app->text_box_store = furi_string_alloc();
  45. furi_string_reserve(app->text_box_store, ESP_FLASHER_TEXT_BOX_STORE_SIZE);
  46. app->widget = widget_alloc();
  47. view_dispatcher_add_view(
  48. app->view_dispatcher, EspFlasherAppViewWidget, widget_get_view(app->widget));
  49. // Submenu
  50. app->submenu = submenu_alloc();
  51. view_dispatcher_add_view(
  52. app->view_dispatcher, EspFlasherAppViewSubmenu, submenu_get_view(app->submenu));
  53. app->flash_worker_busy = false;
  54. app->reset = false;
  55. app->boot = false;
  56. app->quickflash = false;
  57. app->turbospeed = false;
  58. scene_manager_next_scene(app->scene_manager, EspFlasherSceneStart);
  59. return app;
  60. }
  61. void esp_flasher_make_app_folder(EspFlasherApp* app) {
  62. furi_assert(app);
  63. if(!storage_simply_mkdir(app->storage, ESP_APP_FOLDER)) {
  64. dialog_message_show_storage_error(app->dialogs, "Cannot create\napp folder");
  65. }
  66. }
  67. void esp_flasher_app_free(EspFlasherApp* app) {
  68. furi_assert(app);
  69. // Views
  70. view_dispatcher_remove_view(app->view_dispatcher, EspFlasherAppViewVarItemList);
  71. view_dispatcher_remove_view(app->view_dispatcher, EspFlasherAppViewConsoleOutput);
  72. view_dispatcher_remove_view(app->view_dispatcher, EspFlasherAppViewWidget);
  73. view_dispatcher_remove_view(app->view_dispatcher, EspFlasherAppViewSubmenu);
  74. widget_free(app->widget);
  75. text_box_free(app->text_box);
  76. furi_string_free(app->text_box_store);
  77. submenu_free(app->submenu);
  78. variable_item_list_free(app->var_item_list);
  79. // View dispatcher
  80. view_dispatcher_free(app->view_dispatcher);
  81. scene_manager_free(app->scene_manager);
  82. esp_flasher_uart_free(app->uart);
  83. // Close records
  84. furi_record_close(RECORD_GUI);
  85. furi_record_close(RECORD_STORAGE);
  86. furi_record_close(RECORD_DIALOGS);
  87. furi_record_close(RECORD_NOTIFICATION);
  88. free(app);
  89. }
  90. int32_t esp_flasher_app(void* p) {
  91. UNUSED(p);
  92. uint8_t attempts = 0;
  93. bool otg_was_enabled = furi_hal_power_is_otg_enabled();
  94. while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  95. furi_hal_power_enable_otg();
  96. furi_delay_ms(10);
  97. }
  98. furi_delay_ms(200);
  99. EspFlasherApp* esp_flasher_app = esp_flasher_app_alloc();
  100. esp_flasher_make_app_folder(esp_flasher_app);
  101. esp_flasher_app->uart = esp_flasher_usart_init(esp_flasher_app);
  102. view_dispatcher_run(esp_flasher_app->view_dispatcher);
  103. esp_flasher_app_free(esp_flasher_app);
  104. if(furi_hal_power_is_otg_enabled() && !otg_was_enabled) {
  105. furi_hal_power_disable_otg();
  106. }
  107. return 0;
  108. }