avr_isp_app.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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_set_event_callback_context(app->view_dispatcher, app);
  28. view_dispatcher_set_custom_event_callback(
  29. app->view_dispatcher, avr_isp_app_custom_event_callback);
  30. view_dispatcher_set_navigation_event_callback(
  31. app->view_dispatcher, avr_isp_app_back_event_callback);
  32. view_dispatcher_set_tick_event_callback(
  33. app->view_dispatcher, avr_isp_app_tick_event_callback, 100);
  34. view_dispatcher_attach_to_gui(app->view_dispatcher, app->gui, ViewDispatcherTypeFullscreen);
  35. // Open Notification record
  36. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  37. // SubMenu
  38. app->submenu = submenu_alloc();
  39. view_dispatcher_add_view(
  40. app->view_dispatcher, AvrIspViewSubmenu, submenu_get_view(app->submenu));
  41. // Widget
  42. app->widget = widget_alloc();
  43. view_dispatcher_add_view(app->view_dispatcher, AvrIspViewWidget, widget_get_view(app->widget));
  44. // Text Input
  45. app->text_input = text_input_alloc();
  46. view_dispatcher_add_view(
  47. app->view_dispatcher, AvrIspViewTextInput, text_input_get_view(app->text_input));
  48. // Popup
  49. app->popup = popup_alloc();
  50. view_dispatcher_add_view(app->view_dispatcher, AvrIspViewPopup, popup_get_view(app->popup));
  51. //Dialog
  52. app->dialogs = furi_record_open(RECORD_DIALOGS);
  53. // Programmer view
  54. app->avr_isp_programmer_view = avr_isp_programmer_view_alloc();
  55. view_dispatcher_add_view(
  56. app->view_dispatcher,
  57. AvrIspViewProgrammer,
  58. avr_isp_programmer_view_get_view(app->avr_isp_programmer_view));
  59. // Reader view
  60. app->avr_isp_reader_view = avr_isp_reader_view_alloc();
  61. view_dispatcher_add_view(
  62. app->view_dispatcher,
  63. AvrIspViewReader,
  64. avr_isp_reader_view_get_view(app->avr_isp_reader_view));
  65. // Writer view
  66. app->avr_isp_writer_view = avr_isp_writer_view_alloc();
  67. view_dispatcher_add_view(
  68. app->view_dispatcher,
  69. AvrIspViewWriter,
  70. avr_isp_writer_view_get_view(app->avr_isp_writer_view));
  71. // Chip detect view
  72. app->avr_isp_chip_detect_view = avr_isp_chip_detect_view_alloc();
  73. view_dispatcher_add_view(
  74. app->view_dispatcher,
  75. AvrIspViewChipDetect,
  76. avr_isp_chip_detect_view_get_view(app->avr_isp_chip_detect_view));
  77. // Enable 5v power, multiple attempts to avoid issues with power chip protection false triggering
  78. uint8_t attempts = 0;
  79. while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
  80. furi_hal_power_enable_otg();
  81. furi_delay_ms(10);
  82. }
  83. scene_manager_next_scene(app->scene_manager, AvrIspSceneStart);
  84. return app;
  85. } //-V773
  86. void avr_isp_app_free(AvrIspApp* app) {
  87. furi_assert(app);
  88. // Disable 5v power
  89. if(furi_hal_power_is_otg_enabled()) {
  90. furi_hal_power_disable_otg();
  91. }
  92. // Submenu
  93. view_dispatcher_remove_view(app->view_dispatcher, AvrIspViewSubmenu);
  94. submenu_free(app->submenu);
  95. // Widget
  96. view_dispatcher_remove_view(app->view_dispatcher, AvrIspViewWidget);
  97. widget_free(app->widget);
  98. // TextInput
  99. view_dispatcher_remove_view(app->view_dispatcher, AvrIspViewTextInput);
  100. text_input_free(app->text_input);
  101. // Popup
  102. view_dispatcher_remove_view(app->view_dispatcher, AvrIspViewPopup);
  103. popup_free(app->popup);
  104. //Dialog
  105. furi_record_close(RECORD_DIALOGS);
  106. // Programmer view
  107. view_dispatcher_remove_view(app->view_dispatcher, AvrIspViewProgrammer);
  108. avr_isp_programmer_view_free(app->avr_isp_programmer_view);
  109. // Reader view
  110. view_dispatcher_remove_view(app->view_dispatcher, AvrIspViewReader);
  111. avr_isp_reader_view_free(app->avr_isp_reader_view);
  112. // Writer view
  113. view_dispatcher_remove_view(app->view_dispatcher, AvrIspViewWriter);
  114. avr_isp_writer_view_free(app->avr_isp_writer_view);
  115. // Chip detect view
  116. view_dispatcher_remove_view(app->view_dispatcher, AvrIspViewChipDetect);
  117. avr_isp_chip_detect_view_free(app->avr_isp_chip_detect_view);
  118. // View dispatcher
  119. view_dispatcher_free(app->view_dispatcher);
  120. scene_manager_free(app->scene_manager);
  121. // Notifications
  122. furi_record_close(RECORD_NOTIFICATION);
  123. app->notifications = NULL;
  124. // Close records
  125. furi_record_close(RECORD_GUI);
  126. // Path strings
  127. furi_string_free(app->file_path);
  128. free(app);
  129. }
  130. int32_t avr_isp_app(void* p) {
  131. UNUSED(p);
  132. furi_hal_usb_unlock();
  133. if(furi_hal_usb_is_locked()) {
  134. DialogsApp* dialogs = furi_record_open(RECORD_DIALOGS);
  135. DialogMessage* message = dialog_message_alloc();
  136. dialog_message_set_header(message, "Connection\nis active!", 3, 2, AlignLeft, AlignTop);
  137. dialog_message_set_text(
  138. message,
  139. "Disconnect\ncompanion app to\nuse this function.",
  140. 3,
  141. 30,
  142. AlignLeft,
  143. AlignTop);
  144. dialog_message_set_icon(message, &I_ActiveConnection_50x64, 78, 0);
  145. dialog_message_show(dialogs, message);
  146. dialog_message_free(message);
  147. furi_record_close(RECORD_DIALOGS);
  148. return -1;
  149. }
  150. AvrIspApp* avr_isp_app = avr_isp_app_alloc();
  151. view_dispatcher_run(avr_isp_app->view_dispatcher);
  152. avr_isp_app_free(avr_isp_app);
  153. return 0;
  154. }