esp_flasher_app.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. scene_manager_next_scene(app->scene_manager, EspFlasherSceneStart);
  58. return app;
  59. }
  60. void esp_flasher_make_app_folder(EspFlasherApp* app) {
  61. furi_assert(app);
  62. if(!storage_simply_mkdir(app->storage, ESP_APP_FOLDER)) {
  63. dialog_message_show_storage_error(app->dialogs, "Cannot create\napp folder");
  64. }
  65. }
  66. void esp_flasher_app_free(EspFlasherApp* app) {
  67. furi_assert(app);
  68. // Views
  69. view_dispatcher_remove_view(app->view_dispatcher, EspFlasherAppViewVarItemList);
  70. view_dispatcher_remove_view(app->view_dispatcher, EspFlasherAppViewConsoleOutput);
  71. view_dispatcher_remove_view(app->view_dispatcher, EspFlasherAppViewWidget);
  72. view_dispatcher_remove_view(app->view_dispatcher, EspFlasherAppViewSubmenu);
  73. widget_free(app->widget);
  74. text_box_free(app->text_box);
  75. furi_string_free(app->text_box_store);
  76. submenu_free(app->submenu);
  77. variable_item_list_free(app->var_item_list);
  78. // View dispatcher
  79. view_dispatcher_free(app->view_dispatcher);
  80. scene_manager_free(app->scene_manager);
  81. esp_flasher_uart_free(app->uart);
  82. // Close records
  83. furi_record_close(RECORD_GUI);
  84. furi_record_close(RECORD_STORAGE);
  85. furi_record_close(RECORD_DIALOGS);
  86. furi_record_close(RECORD_NOTIFICATION);
  87. free(app);
  88. }
  89. int32_t esp_flasher_app(void* p) {
  90. UNUSED(p);
  91. uint8_t attempts = 0;
  92. bool otg_was_enabled = furi_hal_power_is_otg_enabled();
  93. while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  94. furi_hal_power_enable_otg();
  95. furi_delay_ms(10);
  96. }
  97. furi_delay_ms(200);
  98. EspFlasherApp* esp_flasher_app = esp_flasher_app_alloc();
  99. esp_flasher_make_app_folder(esp_flasher_app);
  100. esp_flasher_app->uart = esp_flasher_usart_init(esp_flasher_app);
  101. view_dispatcher_run(esp_flasher_app->view_dispatcher);
  102. esp_flasher_app_free(esp_flasher_app);
  103. if(furi_hal_power_is_otg_enabled() && !otg_was_enabled) {
  104. furi_hal_power_disable_otg();
  105. }
  106. return 0;
  107. }