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