diff --git a/application.fam b/application.fam index 2782caa9ebd..7a168714119 100644 --- a/application.fam +++ b/application.fam @@ -9,4 +9,9 @@ App( fap_author="@acegoal07", fap_weburl="https://github.com/acegoal07/FlipperZero_NFC_Playlist/tree/main", fap_version="1.0", + fap_private_libs=[ + Lib( + name="worker", + ), + ], ) diff --git a/build.txt b/build.txt index f71a4a94ec1..434b9a22701 100644 --- a/build.txt +++ b/build.txt @@ -1 +1 @@ -fbt fap_nfc_playlist && fbt launch_app APPSRC=applications_user/nfc_playlist \ No newline at end of file +fbt fap_nfc_playlist && fbt launch APPSRC=applications_user/nfc_playlist \ No newline at end of file diff --git a/nfc_playlist_worker.c b/lib/worker/nfc_playlist_worker.c similarity index 100% rename from nfc_playlist_worker.c rename to lib/worker/nfc_playlist_worker.c diff --git a/nfc_playlist_worker.h b/lib/worker/nfc_playlist_worker.h similarity index 100% rename from nfc_playlist_worker.h rename to lib/worker/nfc_playlist_worker.h diff --git a/nfc_playlist.c b/nfc_playlist.c index d38e6cf547b..f23737bf56e 100644 --- a/nfc_playlist.c +++ b/nfc_playlist.c @@ -25,66 +25,63 @@ static const SceneManagerHandlers nfc_playlist_scene_manager_handlers = { static bool nfc_playlist_custom_callback(void* context, uint32_t custom_event) { furi_assert(context); - NfcPlaylist* app = context; - return scene_manager_handle_custom_event(app->scene_manager, custom_event); + NfcPlaylist* nfc_playlist = context; + return scene_manager_handle_custom_event(nfc_playlist->scene_manager, custom_event); } static bool nfc_playlist_back_event_callback(void* context) { furi_assert(context); - NfcPlaylist* app = context; - return scene_manager_handle_back_event(app->scene_manager); + NfcPlaylist* nfc_playlist = context; + return scene_manager_handle_back_event(nfc_playlist->scene_manager); } static NfcPlaylist* nfc_playlist_alloc() { + NfcPlaylist* nfc_playlist = malloc(sizeof(NfcPlaylist)); + furi_assert(nfc_playlist); + nfc_playlist->scene_manager = scene_manager_alloc(&nfc_playlist_scene_manager_handlers, nfc_playlist); - NfcPlaylist* app = malloc(sizeof(NfcPlaylist)); - furi_assert(app); - app->scene_manager = scene_manager_alloc(&nfc_playlist_scene_manager_handlers, app); + nfc_playlist->view_dispatcher = view_dispatcher_alloc(); + view_dispatcher_enable_queue(nfc_playlist->view_dispatcher); - app->view_dispatcher = view_dispatcher_alloc(); - view_dispatcher_enable_queue(app->view_dispatcher); + nfc_playlist->variable_item_list = variable_item_list_alloc(); + nfc_playlist->popup = popup_alloc(); + nfc_playlist->emulate_timeout = default_emulate_timeout; + nfc_playlist->emulate_delay = default_emulate_delay; - app->variable_item_list = variable_item_list_alloc(); - app->popup = popup_alloc(); - app->emulate_timeout = default_emulate_timeout; - app->emulate_delay = default_emulate_delay; + view_dispatcher_set_event_callback_context(nfc_playlist->view_dispatcher, nfc_playlist); + view_dispatcher_set_custom_event_callback(nfc_playlist->view_dispatcher, nfc_playlist_custom_callback); + view_dispatcher_set_navigation_event_callback(nfc_playlist->view_dispatcher, nfc_playlist_back_event_callback); - view_dispatcher_set_event_callback_context(app->view_dispatcher, app); - view_dispatcher_set_custom_event_callback( app->view_dispatcher, nfc_playlist_custom_callback); - view_dispatcher_set_navigation_event_callback(app->view_dispatcher, nfc_playlist_back_event_callback); + view_dispatcher_add_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Menu, variable_item_list_get_view(nfc_playlist->variable_item_list)); - view_dispatcher_add_view(app->view_dispatcher, NfcPlaylistView_Menu, variable_item_list_get_view(app->variable_item_list)); + view_dispatcher_add_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Popup, popup_get_view(nfc_playlist->popup)); - view_dispatcher_add_view(app->view_dispatcher, NfcPlaylistView_Popup, popup_get_view(app->popup)); - - return app; + return nfc_playlist; } -static void nfc_playlist_free(NfcPlaylist* app) { - furi_assert(app); - scene_manager_free(app->scene_manager); - view_dispatcher_remove_view(app->view_dispatcher, NfcPlaylistView_Menu); - view_dispatcher_remove_view(app->view_dispatcher, NfcPlaylistView_Popup); - view_dispatcher_free(app->view_dispatcher); - variable_item_list_free(app->variable_item_list); - popup_free(app->popup); - free(app); +static void nfc_playlist_free(NfcPlaylist* nfc_playlist) { + furi_assert(nfc_playlist); + scene_manager_free(nfc_playlist->scene_manager); + view_dispatcher_remove_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Menu); + view_dispatcher_remove_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Popup); + view_dispatcher_free(nfc_playlist->view_dispatcher); + variable_item_list_free(nfc_playlist->variable_item_list); + popup_free(nfc_playlist->popup); + free(nfc_playlist); } int32_t nfc_playlist_main(void* p) { - // Mark argument as unused UNUSED(p); - NfcPlaylist* app = nfc_playlist_alloc(); + NfcPlaylist* nfc_playlist = nfc_playlist_alloc(); Gui* gui = furi_record_open(RECORD_GUI); - view_dispatcher_attach_to_gui(app->view_dispatcher, gui, ViewDispatcherTypeFullscreen); - scene_manager_next_scene(app->scene_manager, NfcPlaylistScene_MainMenu); - view_dispatcher_run(app->view_dispatcher); + view_dispatcher_attach_to_gui(nfc_playlist->view_dispatcher, gui, ViewDispatcherTypeFullscreen); + scene_manager_next_scene(nfc_playlist->scene_manager, NfcPlaylistScene_MainMenu); + view_dispatcher_run(nfc_playlist->view_dispatcher); - // free all memory furi_record_close(RECORD_GUI); - nfc_playlist_free(app); + nfc_playlist_free(nfc_playlist); return 0; } diff --git a/nfc_playlist.h b/nfc_playlist.h index 23772b2fd19..9f02c56435e 100644 --- a/nfc_playlist.h +++ b/nfc_playlist.h @@ -1,8 +1,6 @@ #pragma once - #include #include - #include #include #include @@ -30,6 +28,6 @@ typedef struct { } NfcPlaylist; static const int options_emulate_timeout[] = { 1000, 2000, 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000 }; -const int default_emulate_timeout = 4; +static const int default_emulate_timeout = 4; static const int options_emulate_delay[] = { 0, 1000, 2000, 3000, 4000, 5000 }; -const int default_emulate_delay = 0; \ No newline at end of file +static const int default_emulate_delay = 0; \ No newline at end of file diff --git a/scences/emulation.c b/scences/emulation.c index faf5c9c43fe..e986b2b1868 100644 --- a/scences/emulation.c +++ b/scences/emulation.c @@ -2,7 +2,7 @@ #include "scences/emulation.h" void nfc_playlist_emulation_scene_on_enter(void* context) { - NfcPlaylist* app = context; + NfcPlaylist* nfc_playlist = context; // open/alloc resources Storage* storage = furi_record_open(RECORD_STORAGE); @@ -11,21 +11,21 @@ void nfc_playlist_emulation_scene_on_enter(void* context) { NfcPlaylistWorker* nfc_worker = nfc_playlist_worker_alloc(); // Read file if(file_stream_open(stream, APP_DATA_PATH("playlist.txt"), FSAM_READ, FSOM_OPEN_EXISTING)) { - popup_reset(app->popup); - popup_set_context(app->popup, app); - popup_set_header(app->popup, "Emulating:", 64, 10, AlignCenter, AlignTop); - view_dispatcher_switch_to_view(app->view_dispatcher, NfcPlaylistView_Popup); + popup_reset(nfc_playlist->popup); + popup_set_context(nfc_playlist->popup, nfc_playlist); + popup_set_header(nfc_playlist->popup, "Emulating:", 64, 10, AlignCenter, AlignTop); + view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Popup); int file_position = 0; // read the file line by line and print the text while(stream_read_line(stream, line)) { - if (options_emulate_delay[app->emulate_delay] > 0) { + if (options_emulate_delay[nfc_playlist->emulate_delay] > 0) { if (file_position > 0) { - int time_counter_delay_ms = options_emulate_delay[app->emulate_delay]; + int time_counter_delay_ms = options_emulate_delay[nfc_playlist->emulate_delay]; do { char display_text[30]; snprintf(display_text, 30, "%s\n\n%ds", "Delaying...", (time_counter_delay_ms/1000)); - popup_set_text(app->popup, display_text, 64, 25, AlignCenter, AlignTop); + popup_set_text(nfc_playlist->popup, display_text, 64, 25, AlignCenter, AlignTop); furi_delay_ms(500); time_counter_delay_ms -= 500; } while(time_counter_delay_ms > 0); @@ -36,7 +36,7 @@ void nfc_playlist_emulation_scene_on_enter(void* context) { char* file_path = (char*)furi_string_get_cstr(line); char* file_name = &strrchr(file_path, '/')[1]; - int time_counter_ms = options_emulate_timeout[app->emulate_timeout]; + int time_counter_ms = options_emulate_timeout[nfc_playlist->emulate_timeout]; if (storage_file_exists(storage, file_path) == false) { char const* popup_text_unformatted = strcat(file_name, "\nnot found"); @@ -45,7 +45,7 @@ void nfc_playlist_emulation_scene_on_enter(void* context) { do { snprintf(popup_text, popup_text_size, "%s\n%ds", file_name, (time_counter_ms/1000)); - popup_set_text(app->popup, popup_text, 64, 25, AlignCenter, AlignTop); + popup_set_text(nfc_playlist->popup, popup_text, 64, 25, AlignCenter, AlignTop); furi_delay_ms(500); time_counter_ms -= 500; } while(time_counter_ms > 0); @@ -58,7 +58,7 @@ void nfc_playlist_emulation_scene_on_enter(void* context) { do { snprintf(popup_text, popup_text_size, "%s\n%ds", file_name, (time_counter_ms/1000)); - popup_set_text(app->popup, popup_text, 64, 25, AlignCenter, AlignTop); + popup_set_text(nfc_playlist->popup, popup_text, 64, 25, AlignCenter, AlignTop); furi_delay_ms(500); time_counter_ms -= 500; } while(nfc_playlist_worker_is_emulating(nfc_worker) && time_counter_ms > 0); @@ -68,14 +68,14 @@ void nfc_playlist_emulation_scene_on_enter(void* context) { } } } - popup_reset(app->popup); - scene_manager_previous_scene(app->scene_manager); + popup_reset(nfc_playlist->popup); + scene_manager_previous_scene(nfc_playlist->scene_manager); } else { - popup_reset(app->popup); - popup_set_context(app->popup, app); - popup_set_header(app->popup, "Error:", 64, 10, AlignCenter, AlignTop); - popup_set_text(app->popup, "Failed to open file\n/ext/apps_data/nfc_playlist/playlist.txt", 64, 25, AlignCenter, AlignTop); - view_dispatcher_switch_to_view(app->view_dispatcher, NfcPlaylistView_Popup); + popup_reset(nfc_playlist->popup); + popup_set_context(nfc_playlist->popup, nfc_playlist); + popup_set_header(nfc_playlist->popup, "Error:", 64, 10, AlignCenter, AlignTop); + popup_set_text(nfc_playlist->popup, "Failed to open file\n/ext/apps_data/nfc_playlist/playlist.txt", 64, 25, AlignCenter, AlignTop); + view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Popup); } // Free/close resources furi_string_free(line); @@ -93,6 +93,6 @@ bool nfc_playlist_emulation_scene_on_event(void* context, SceneManagerEvent even } void nfc_playlist_emulation_scene_on_exit(void* context) { - NfcPlaylist* app = context; - popup_reset(app->popup); + NfcPlaylist* nfc_playlist = context; + popup_reset(nfc_playlist->popup); } \ No newline at end of file diff --git a/scences/emulation.h b/scences/emulation.h index 1383d0c3ab5..7f31b7bbad4 100644 --- a/scences/emulation.h +++ b/scences/emulation.h @@ -1,7 +1,5 @@ #pragma once - #include -#include #include #include #include @@ -10,8 +8,6 @@ #include #include #include -#include -#include void nfc_playlist_emulation_scene_on_enter(void* context); bool nfc_playlist_emulation_scene_on_event(void* context, SceneManagerEvent event); diff --git a/scences/main_menu.c b/scences/main_menu.c index f227e9f917b..82f90df8930 100644 --- a/scences/main_menu.c +++ b/scences/main_menu.c @@ -12,10 +12,10 @@ typedef enum { } NfcPlaylistMenuSelection; static void nfc_playlist_menu_callback(void* context, uint32_t index) { - NfcPlaylist* app = context; + NfcPlaylist* nfc_playlist = context; switch(index) { case NfcPlaylistMenuSelection_Start: - scene_manager_handle_custom_event(app->scene_manager, NfcPlaylistEvent_ShowEmulatingPopup); + scene_manager_handle_custom_event(nfc_playlist->scene_manager, NfcPlaylistEvent_ShowEmulatingPopup); break; default: break; @@ -23,20 +23,20 @@ static void nfc_playlist_menu_callback(void* context, uint32_t index) { } static void nfc_playlist_settings_change_callback(VariableItem* item) { - NfcPlaylist* app = variable_item_get_context(item); + NfcPlaylist* nfc_playlist = variable_item_get_context(item); - uint8_t current_option = variable_item_list_get_selected_item_index(app->variable_item_list); + uint8_t current_option = variable_item_list_get_selected_item_index(nfc_playlist->variable_item_list); uint8_t option_value_index = variable_item_get_current_value_index(item); switch(current_option) { case NfcPlaylistSettings_Timeout: ; - app->emulate_timeout = option_value_index; + nfc_playlist->emulate_timeout = option_value_index; char emulate_timeout_text[10]; snprintf(emulate_timeout_text, 10, "%ds", (options_emulate_timeout[option_value_index]/1000)); variable_item_set_current_value_text(item, (char*)emulate_timeout_text); break; case NfcPlaylistSettings_Delay: ; - app->emulate_delay = option_value_index; + nfc_playlist->emulate_delay = option_value_index; char emulate_delay_text[10]; snprintf(emulate_delay_text, 10, "%ds", (options_emulate_delay[option_value_index]/1000)); variable_item_set_current_value_text(item, (char*)emulate_delay_text); @@ -47,41 +47,44 @@ static void nfc_playlist_settings_change_callback(VariableItem* item) { } void nfc_playlist_main_menu_scene_on_enter(void* context) { - NfcPlaylist* app = context; - variable_item_list_set_header(app->variable_item_list, "NFC Playlist"); + NfcPlaylist* nfc_playlist = context; + variable_item_list_set_header(nfc_playlist->variable_item_list, "NFC Playlist"); + VariableItem* emulation_timeout_settings = variable_item_list_add( - app->variable_item_list, + nfc_playlist->variable_item_list, "Timeout", 10, nfc_playlist_settings_change_callback, - app); - variable_item_set_current_value_index(emulation_timeout_settings, app->emulate_timeout); + nfc_playlist); + variable_item_set_current_value_index(emulation_timeout_settings, nfc_playlist->emulate_timeout); char emulation_timeout_settings_text[10]; - snprintf(emulation_timeout_settings_text, 10, "%ds", (options_emulate_timeout[app->emulate_timeout]/1000)); + snprintf(emulation_timeout_settings_text, 10, "%ds", (options_emulate_timeout[nfc_playlist->emulate_timeout]/1000)); variable_item_set_current_value_text(emulation_timeout_settings, (char*)emulation_timeout_settings_text); + VariableItem* emulation_delay_settings = variable_item_list_add( - app->variable_item_list, + nfc_playlist->variable_item_list, "Delay", 6, nfc_playlist_settings_change_callback, - app); - variable_item_set_current_value_index(emulation_delay_settings, app->emulate_delay); + nfc_playlist); + variable_item_set_current_value_index(emulation_delay_settings, nfc_playlist->emulate_delay); char emulation_delay_settings_text[10]; - snprintf(emulation_delay_settings_text, 10, "%ds", (options_emulate_delay[app->emulate_delay]/1000)); + snprintf(emulation_delay_settings_text, 10, "%ds", (options_emulate_delay[nfc_playlist->emulate_delay]/1000)); variable_item_set_current_value_text(emulation_delay_settings, (char*)emulation_delay_settings_text); - variable_item_list_add(app->variable_item_list, "Start", 0, NULL, NULL); - variable_item_list_set_enter_callback(app->variable_item_list, nfc_playlist_menu_callback, app); - view_dispatcher_switch_to_view(app->view_dispatcher, NfcPlaylistView_Menu); + + variable_item_list_add(nfc_playlist->variable_item_list, "Start", 0, NULL, NULL); + variable_item_list_set_enter_callback(nfc_playlist->variable_item_list, nfc_playlist_menu_callback, nfc_playlist); + view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Menu); } bool nfc_playlist_main_menu_scene_on_event(void* context, SceneManagerEvent event) { - NfcPlaylist* app = context; + NfcPlaylist* nfc_playlist = context; bool consumed = false; switch(event.type) { case SceneManagerEventTypeCustom: switch(event.event) { case NfcPlaylistEvent_ShowEmulatingPopup: - scene_manager_next_scene(app->scene_manager, NfcPlaylistScene_EmulatingPopup); + scene_manager_next_scene(nfc_playlist->scene_manager, NfcPlaylistScene_EmulatingPopup); consumed = true; break; default: @@ -96,6 +99,6 @@ bool nfc_playlist_main_menu_scene_on_event(void* context, SceneManagerEvent even } void nfc_playlist_main_menu_scene_on_exit(void* context) { - NfcPlaylist* app = context; - variable_item_list_reset(app->variable_item_list); + NfcPlaylist* nfc_playlist = context; + variable_item_list_reset(nfc_playlist->variable_item_list); } \ No newline at end of file diff --git a/scences/main_menu.h b/scences/main_menu.h index 944e19a8316..8f3270609af 100644 --- a/scences/main_menu.h +++ b/scences/main_menu.h @@ -1,12 +1,8 @@ #pragma once - #include -#include - #include #include #include -#include #include void nfc_playlist_main_menu_scene_on_enter(void* context);