hex_viewer.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include "hex_viewer.h"
  2. // typedef struct {
  3. // uint8_t file_bytes[HEX_VIEWER_LINES_ON_SCREEN][HEX_VIEWER_BYTES_PER_LINE];
  4. // uint32_t file_offset;
  5. // uint32_t file_read_bytes;
  6. // uint32_t file_size;
  7. // bool mode; // Print address or content
  8. // Stream* stream;
  9. // } HexViewerModel;
  10. // // TODO Clean
  11. // typedef struct {
  12. // HexViewerModel* model;
  13. // FuriMutex** mutex; // TODO Don't need?
  14. // Gui* gui;
  15. // Storage* storage;
  16. // NotificationApp* notification;
  17. // ViewDispatcher* view_dispatcher;
  18. // Submenu* submenu;
  19. // SceneManager* scene_manager;
  20. // VariableItemList* variable_item_list;
  21. // HexViewerStartscreen* hex_viewer_startscreen;
  22. // HexViewerScene1* hex_viewer_scene_1;
  23. // HexViewerScene2* hex_viewer_scene_2;
  24. // DialogsApp* dialogs; // File Browser
  25. // FuriString* file_path; // File Browser
  26. // uint32_t haptic;
  27. // uint32_t speaker;
  28. // uint32_t led;
  29. // uint32_t save_settings;
  30. // ButtonMenu* button_menu; // Button Menu
  31. // } HexViewer;
  32. bool hex_viewer_custom_event_callback(void* context, uint32_t event) {
  33. furi_assert(context);
  34. HexViewer* app = context;
  35. return scene_manager_handle_custom_event(app->scene_manager, event);
  36. }
  37. void hex_viewer_tick_event_callback(void* context) {
  38. furi_assert(context);
  39. HexViewer* app = context;
  40. scene_manager_handle_tick_event(app->scene_manager);
  41. }
  42. //leave app if back button pressed
  43. bool hex_viewer_navigation_event_callback(void* context) {
  44. furi_assert(context);
  45. HexViewer* app = context;
  46. return scene_manager_handle_back_event(app->scene_manager);
  47. }
  48. HexViewer* hex_viewer_app_alloc() {
  49. HexViewer* app = malloc(sizeof(HexViewer));
  50. app->model = malloc(sizeof(HexViewerModel));
  51. memset(app->model, 0, sizeof(HexViewerModel));
  52. app->gui = furi_record_open(RECORD_GUI);
  53. app->storage = furi_record_open(RECORD_STORAGE);
  54. app->notification = furi_record_open(RECORD_NOTIFICATION);
  55. //Turn backlight on, believe me this makes testing your app easier
  56. notification_message(app->notification, &sequence_display_backlight_on);
  57. //Scene additions
  58. app->view_dispatcher = view_dispatcher_alloc();
  59. view_dispatcher_enable_queue(app->view_dispatcher);
  60. app->scene_manager = scene_manager_alloc(&hex_viewer_scene_handlers, app);
  61. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  62. view_dispatcher_set_navigation_event_callback(app->view_dispatcher, hex_viewer_navigation_event_callback);
  63. view_dispatcher_set_tick_event_callback(app->view_dispatcher, hex_viewer_tick_event_callback, 100);
  64. view_dispatcher_set_custom_event_callback(app->view_dispatcher, hex_viewer_custom_event_callback);
  65. app->submenu = submenu_alloc();
  66. // Set defaults, in case no config loaded
  67. app->haptic = 1;
  68. app->speaker = 1;
  69. app->led = 1;
  70. app->save_settings = 1;
  71. // Used for File Browser
  72. app->dialogs = furi_record_open(RECORD_DIALOGS);
  73. app->file_path = furi_string_alloc();
  74. // Load configs
  75. hex_viewer_read_settings(app);
  76. view_dispatcher_add_view(app->view_dispatcher, HexViewerViewIdMenu, submenu_get_view(app->submenu));
  77. app->hex_viewer_startscreen = hex_viewer_startscreen_alloc();
  78. view_dispatcher_add_view(app->view_dispatcher, HexViewerViewIdStartscreen, hex_viewer_startscreen_get_view(app->hex_viewer_startscreen));
  79. app->hex_viewer_scene_1 = hex_viewer_scene_1_alloc();
  80. view_dispatcher_add_view(app->view_dispatcher, HexViewerViewIdScene1, hex_viewer_scene_1_get_view(app->hex_viewer_scene_1));
  81. app->hex_viewer_scene_2 = hex_viewer_scene_2_alloc();
  82. view_dispatcher_add_view(app->view_dispatcher, HexViewerViewIdScene2, hex_viewer_scene_2_get_view(app->hex_viewer_scene_2));
  83. app->button_menu = button_menu_alloc();
  84. view_dispatcher_add_view(app->view_dispatcher, HexViewerViewIdScene3, button_menu_get_view(app->button_menu));
  85. app->variable_item_list = variable_item_list_alloc();
  86. view_dispatcher_add_view(app->view_dispatcher, HexViewerViewIdSettings, variable_item_list_get_view(app->variable_item_list));
  87. //End Scene Additions
  88. return app;
  89. }
  90. void hex_viewer_app_free(HexViewer* app) {
  91. furi_assert(app);
  92. if(app->model->stream) buffered_file_stream_close(app->model->stream);
  93. // Scene manager
  94. scene_manager_free(app->scene_manager);
  95. // View Dispatcher
  96. view_dispatcher_remove_view(app->view_dispatcher, HexViewerViewIdMenu);
  97. view_dispatcher_remove_view(app->view_dispatcher, HexViewerViewIdScene1);
  98. view_dispatcher_remove_view(app->view_dispatcher, HexViewerViewIdScene2);
  99. view_dispatcher_remove_view(app->view_dispatcher, HexViewerViewIdSettings);
  100. submenu_free(app->submenu);
  101. view_dispatcher_free(app->view_dispatcher);
  102. furi_record_close(RECORD_STORAGE);
  103. furi_record_close(RECORD_GUI);
  104. app->storage = NULL;
  105. app->gui = NULL;
  106. app->notification = NULL;
  107. // Close File Browser
  108. furi_record_close(RECORD_DIALOGS);
  109. furi_string_free(app->file_path);
  110. free(app->model);
  111. //Remove whatever is left
  112. free(app);
  113. }
  114. int32_t hex_viewer_app(void* p) {
  115. UNUSED(p);
  116. HexViewer* app = hex_viewer_app_alloc();
  117. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  118. scene_manager_next_scene(app->scene_manager, HexViewerSceneStartscreen); //Start with start screen
  119. //scene_manager_next_scene(app->scene_manager, HexViewerSceneMenu); //if you want to directly start with Menu
  120. furi_hal_power_suppress_charge_enter();
  121. view_dispatcher_run(app->view_dispatcher);
  122. hex_viewer_save_settings(app);
  123. furi_hal_power_suppress_charge_exit();
  124. hex_viewer_app_free(app);
  125. return 0;
  126. }