avr_isp_app.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #include "avr_isp_app_i.h"
  2. static bool avr_isp_app_custom_event_callback(void* context, uint32_t event) {
  3. furi_assert(context);
  4. AvrIspApp* app = context;
  5. return scene_manager_handle_custom_event(app->scene_manager, event);
  6. }
  7. static bool avr_isp_app_back_event_callback(void* context) {
  8. furi_assert(context);
  9. AvrIspApp* app = context;
  10. return scene_manager_handle_back_event(app->scene_manager);
  11. }
  12. static void avr_isp_app_tick_event_callback(void* context) {
  13. furi_assert(context);
  14. AvrIspApp* app = context;
  15. scene_manager_handle_tick_event(app->scene_manager);
  16. }
  17. AvrIspApp* avr_isp_app_alloc() {
  18. AvrIspApp* app = malloc(sizeof(AvrIspApp));
  19. app->file_path = furi_string_alloc();
  20. furi_string_set(app->file_path, STORAGE_APP_DATA_PATH_PREFIX);
  21. app->error = AvrIspErrorNoError;
  22. // GUI
  23. app->gui = furi_record_open(RECORD_GUI);
  24. // View Dispatcher
  25. app->view_dispatcher = view_dispatcher_alloc();
  26. app->scene_manager = scene_manager_alloc(&avr_isp_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, avr_isp_app_custom_event_callback);
  31. view_dispatcher_set_navigation_event_callback(
  32. app->view_dispatcher, avr_isp_app_back_event_callback);
  33. view_dispatcher_set_tick_event_callback(
  34. app->view_dispatcher, avr_isp_app_tick_event_callback, 100);
  35. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  36. // Open Notification record
  37. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  38. // SubMenu
  39. app->submenu = submenu_alloc();
  40. view_dispatcher_add_view(
  41. app->view_dispatcher, AvrIspViewSubmenu, submenu_get_view(app->submenu));
  42. // Widget
  43. app->widget = widget_alloc();
  44. view_dispatcher_add_view(app->view_dispatcher, AvrIspViewWidget, widget_get_view(app->widget));
  45. // Text Input
  46. app->text_input = text_input_alloc();
  47. view_dispatcher_add_view(
  48. app->view_dispatcher, AvrIspViewTextInput, text_input_get_view(app->text_input));
  49. // Popup
  50. app->popup = popup_alloc();
  51. view_dispatcher_add_view(app->view_dispatcher, AvrIspViewPopup, popup_get_view(app->popup));
  52. //Dialog
  53. app->dialogs = furi_record_open(RECORD_DIALOGS);
  54. // Programmer view
  55. app->avr_isp_programmer_view = avr_isp_programmer_view_alloc();
  56. view_dispatcher_add_view(
  57. app->view_dispatcher,
  58. AvrIspViewProgrammer,
  59. avr_isp_programmer_view_get_view(app->avr_isp_programmer_view));
  60. // Reader view
  61. app->avr_isp_reader_view = avr_isp_reader_view_alloc();
  62. view_dispatcher_add_view(
  63. app->view_dispatcher,
  64. AvrIspViewReader,
  65. avr_isp_reader_view_get_view(app->avr_isp_reader_view));
  66. // Writer view
  67. app->avr_isp_writer_view = avr_isp_writer_view_alloc();
  68. view_dispatcher_add_view(
  69. app->view_dispatcher,
  70. AvrIspViewWriter,
  71. avr_isp_writer_view_get_view(app->avr_isp_writer_view));
  72. // Chip detect view
  73. app->avr_isp_chip_detect_view = avr_isp_chip_detect_view_alloc();
  74. view_dispatcher_add_view(
  75. app->view_dispatcher,
  76. AvrIspViewChipDetect,
  77. avr_isp_chip_detect_view_get_view(app->avr_isp_chip_detect_view));
  78. // Enable 5v power, multiple attempts to avoid issues with power chip protection false triggering
  79. uint8_t attempts = 0;
  80. while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  81. furi_hal_power_enable_otg();
  82. furi_delay_ms(10);
  83. }
  84. scene_manager_next_scene(app->scene_manager, AvrIspSceneStart);
  85. return app;
  86. } //-V773
  87. void avr_isp_app_free(AvrIspApp* app) {
  88. furi_assert(app);
  89. // Disable 5v power
  90. if(furi_hal_power_is_otg_enabled()) {
  91. furi_hal_power_disable_otg();
  92. }
  93. // Submenu
  94. view_dispatcher_remove_view(app->view_dispatcher, AvrIspViewSubmenu);
  95. submenu_free(app->submenu);
  96. // Widget
  97. view_dispatcher_remove_view(app->view_dispatcher, AvrIspViewWidget);
  98. widget_free(app->widget);
  99. // TextInput
  100. view_dispatcher_remove_view(app->view_dispatcher, AvrIspViewTextInput);
  101. text_input_free(app->text_input);
  102. // Popup
  103. view_dispatcher_remove_view(app->view_dispatcher, AvrIspViewPopup);
  104. popup_free(app->popup);
  105. //Dialog
  106. furi_record_close(RECORD_DIALOGS);
  107. // Programmer view
  108. view_dispatcher_remove_view(app->view_dispatcher, AvrIspViewProgrammer);
  109. avr_isp_programmer_view_free(app->avr_isp_programmer_view);
  110. // Reader view
  111. view_dispatcher_remove_view(app->view_dispatcher, AvrIspViewReader);
  112. avr_isp_reader_view_free(app->avr_isp_reader_view);
  113. // Writer view
  114. view_dispatcher_remove_view(app->view_dispatcher, AvrIspViewWriter);
  115. avr_isp_writer_view_free(app->avr_isp_writer_view);
  116. // Chip detect view
  117. view_dispatcher_remove_view(app->view_dispatcher, AvrIspViewChipDetect);
  118. avr_isp_chip_detect_view_free(app->avr_isp_chip_detect_view);
  119. // View dispatcher
  120. view_dispatcher_free(app->view_dispatcher);
  121. scene_manager_free(app->scene_manager);
  122. // Notifications
  123. furi_record_close(RECORD_NOTIFICATION);
  124. app->notifications = NULL;
  125. // Close records
  126. furi_record_close(RECORD_GUI);
  127. // Path strings
  128. furi_string_free(app->file_path);
  129. free(app);
  130. }
  131. int32_t avr_isp_app(void* p) {
  132. UNUSED(p);
  133. AvrIspApp* avr_isp_app = avr_isp_app_alloc();
  134. view_dispatcher_run(avr_isp_app->view_dispatcher);
  135. avr_isp_app_free(avr_isp_app);
  136. return 0;
  137. }