Просмотр исходного кода

Merge nfc_playlist from https://github.com/acegoal07/FlipperZero_NFC_Playlist

Willy-JL 1 год назад
Родитель
Сommit
4915916f6f

+ 6 - 6
nfc_playlist/README.md

@@ -3,14 +3,16 @@ The idea behind this app is to allow for you to test multiple copies of NFC's at
 ## How it works:
 When starting the app you are greeted by a select file option where you choose the playlist you wanna run.
 
-All the playlists should be placed in ext/apps_data/nfc_playlist and an example of how the data in the file should look can be found below.
+All the playlists should be placed in ext/apps_data/nfc_playlist but can be placed in other file locations and an example of how the data in the file should look can be found below along with a example file in the repository.
 ```txt
 /ext/nfc/link.nfc
 /ext/nfc/link2.nfc
 ```
 An example file can be found in the repository
-## How to build
-This app was design, built and tested using the <a href="https://github.com/Next-Flip/Momentum-Firmware">Momentum</a> so keep that in mind when building the FAP for yourself
+## Feedback:
+Any feedback is welcome and would be very much appreciated as this is my first C project
+- <a href="https://github.com/acegoal07/FlipperZero_NFC_Playlist/issues/new?assignees=acegoal07&labels=enhancement&projects=&template=feature_request.md&title=%5BFEATURE%7D">Feature request</a>
+- <a href="https://github.com/acegoal07/FlipperZero_NFC_Playlist/issues/new?assignees=acegoal07&labels=bug&projects=&template=bug_report.md&title=%5BBUG%5D">Bug report</a>
 ## Supported Firmwares
 As i know these firmwares are supported and working if you know any more please let me know
 - <a href="https://github.com/Next-Flip/Momentum-Firmware">Momentum</a>
@@ -31,6 +33,4 @@ As i know these firmwares are supported and working if you know any more please
 - View playlist content (Allows you to view the contents of the playlist)
 ## Feature ideas:
 - A function to allow you to add multiple nfc items to a playlist at once
-- A view playlist function which only shows the name of the playlist items excluding the file path
-
-Any feedback is welcome and would be very much appreciated
+- A view playlist function which only shows the name of the playlist items excluding the file path

+ 1 - 1
nfc_playlist/application.fam

@@ -8,7 +8,7 @@ App(
     fap_category="NFC",
     fap_author="@acegoal07",
     fap_weburl="https://github.com/acegoal07/FlipperZero_NFC_Playlist/tree/main",
-    fap_version="2.5",
+    fap_version="2.6",
     fap_icon_assets="assets",
     fap_icon="assets/Playlist_10px.png",
     fap_private_libs=[

+ 27 - 16
nfc_playlist/lib/emulation_worker/nfc_playlist_emulation_worker.c

@@ -1,8 +1,13 @@
 #include "nfc_playlist_emulation_worker.h"
 
 NfcPlaylistEmulationWorker* nfc_playlist_emulation_worker_alloc() {
-   NfcPlaylistEmulationWorker* nfc_playlist_emulation_worker = malloc(sizeof(NfcPlaylistEmulationWorker));
-   nfc_playlist_emulation_worker->thread = furi_thread_alloc_ex("NfcPlaylistEmulationWorker", 4096, nfc_playlist_emulation_worker_task, nfc_playlist_emulation_worker);
+   NfcPlaylistEmulationWorker* nfc_playlist_emulation_worker =
+      malloc(sizeof(NfcPlaylistEmulationWorker));
+   nfc_playlist_emulation_worker->thread = furi_thread_alloc_ex(
+      "NfcPlaylistEmulationWorker",
+      4096,
+      nfc_playlist_emulation_worker_task,
+      nfc_playlist_emulation_worker);
    nfc_playlist_emulation_worker->state = NfcPlaylistEmulationWorkerState_Stopped;
    nfc_playlist_emulation_worker->nfc = nfc_alloc();
    nfc_playlist_emulation_worker->nfc_device = nfc_device_alloc();
@@ -19,13 +24,14 @@ void nfc_playlist_emulation_worker_free(NfcPlaylistEmulationWorker* nfc_playlist
 
 void nfc_playlist_emulation_worker_stop(NfcPlaylistEmulationWorker* nfc_playlist_emulation_worker) {
    furi_assert(nfc_playlist_emulation_worker);
-   if (nfc_playlist_emulation_worker->state != NfcPlaylistEmulationWorkerState_Stopped) {
+   if(nfc_playlist_emulation_worker->state != NfcPlaylistEmulationWorkerState_Stopped) {
       nfc_playlist_emulation_worker->state = NfcPlaylistEmulationWorkerState_Stopped;
       furi_thread_join(nfc_playlist_emulation_worker->thread);
    }
 }
 
-void nfc_playlist_emulation_worker_start(NfcPlaylistEmulationWorker* nfc_playlist_emulation_worker) {
+void nfc_playlist_emulation_worker_start(
+   NfcPlaylistEmulationWorker* nfc_playlist_emulation_worker) {
    furi_assert(nfc_playlist_emulation_worker);
    nfc_playlist_emulation_worker->state = NfcPlaylistEmulationWorkerState_Emulating;
    furi_thread_start(nfc_playlist_emulation_worker->thread);
@@ -34,13 +40,13 @@ void nfc_playlist_emulation_worker_start(NfcPlaylistEmulationWorker* nfc_playlis
 int32_t nfc_playlist_emulation_worker_task(void* context) {
    NfcPlaylistEmulationWorker* nfc_playlist_emulation_worker = context;
 
-   if (nfc_playlist_emulation_worker->state == NfcPlaylistEmulationWorkerState_Emulating) {
-
-      nfc_playlist_emulation_worker->nfc_listener =
-         nfc_listener_alloc(nfc_playlist_emulation_worker->nfc,
-            nfc_playlist_emulation_worker->nfc_protocol,
-            nfc_device_get_data(nfc_playlist_emulation_worker->nfc_device, nfc_playlist_emulation_worker->nfc_protocol)
-         );
+   if(nfc_playlist_emulation_worker->state == NfcPlaylistEmulationWorkerState_Emulating) {
+      nfc_playlist_emulation_worker->nfc_listener = nfc_listener_alloc(
+         nfc_playlist_emulation_worker->nfc,
+         nfc_playlist_emulation_worker->nfc_protocol,
+         nfc_device_get_data(
+            nfc_playlist_emulation_worker->nfc_device,
+            nfc_playlist_emulation_worker->nfc_protocol));
       nfc_listener_start(nfc_playlist_emulation_worker->nfc_listener, NULL, NULL);
 
       while(nfc_playlist_emulation_worker->state == NfcPlaylistEmulationWorkerState_Emulating) {
@@ -56,18 +62,23 @@ int32_t nfc_playlist_emulation_worker_task(void* context) {
    return 0;
 }
 
-bool nfc_playlist_emulation_worker_is_emulating(NfcPlaylistEmulationWorker* nfc_playlist_emulation_worker) {
+bool nfc_playlist_emulation_worker_is_emulating(
+   NfcPlaylistEmulationWorker* nfc_playlist_emulation_worker) {
    furi_assert(nfc_playlist_emulation_worker);
    return nfc_playlist_emulation_worker->state == NfcPlaylistEmulationWorkerState_Emulating;
 }
 
-void nfc_playlist_emulation_worker_set_nfc_data(NfcPlaylistEmulationWorker* nfc_playlist_emulation_worker, char* file_path) {
+void nfc_playlist_emulation_worker_set_nfc_data(
+   NfcPlaylistEmulationWorker* nfc_playlist_emulation_worker,
+   char* file_path) {
    furi_assert(nfc_playlist_emulation_worker);
    nfc_device_load(nfc_playlist_emulation_worker->nfc_device, file_path);
-   nfc_playlist_emulation_worker->nfc_protocol = nfc_device_get_protocol(nfc_playlist_emulation_worker->nfc_device);
+   nfc_playlist_emulation_worker->nfc_protocol =
+      nfc_device_get_protocol(nfc_playlist_emulation_worker->nfc_device);
 }
 
-void nfc_playlist_emulation_worker_clear_nfc_data(NfcPlaylistEmulationWorker* nfc_playlist_emulation_worker) {
+void nfc_playlist_emulation_worker_clear_nfc_data(
+   NfcPlaylistEmulationWorker* nfc_playlist_emulation_worker) {
    furi_assert(nfc_playlist_emulation_worker);
    nfc_device_clear(nfc_playlist_emulation_worker->nfc_device);
-}
+}

+ 7 - 3
nfc_playlist/lib/emulation_worker/nfc_playlist_emulation_worker.h

@@ -26,6 +26,10 @@ void nfc_playlist_emulation_worker_start(NfcPlaylistEmulationWorker* nfc_playlis
 
 int32_t nfc_playlist_emulation_worker_task(void* context);
 
-bool nfc_playlist_emulation_worker_is_emulating(NfcPlaylistEmulationWorker* nfc_playlist_emulation_worker);
-void nfc_playlist_emulation_worker_set_nfc_data(NfcPlaylistEmulationWorker* nfc_playlist_emulation_worker, char* file_path);
-void nfc_playlist_emulation_worker_clear_nfc_data(NfcPlaylistEmulationWorker* nfc_playlist_emulation_worker);
+bool nfc_playlist_emulation_worker_is_emulating(
+   NfcPlaylistEmulationWorker* nfc_playlist_emulation_worker);
+void nfc_playlist_emulation_worker_set_nfc_data(
+   NfcPlaylistEmulationWorker* nfc_playlist_emulation_worker,
+   char* file_path);
+void nfc_playlist_emulation_worker_clear_nfc_data(
+   NfcPlaylistEmulationWorker* nfc_playlist_emulation_worker);

+ 1 - 0
nfc_playlist/scenes/nfc_playlist_scene_config.h

@@ -1,5 +1,6 @@
 ADD_SCENE(nfc_playlist, confirm_delete, ConfirmDelete)
 ADD_SCENE(nfc_playlist, emulation, Emulation)
+ADD_SCENE(nfc_playlist, error_playlist_already_exists, ErrorPlaylistAlreadyExists)
 ADD_SCENE(nfc_playlist, main_menu, MainMenu)
 ADD_SCENE(nfc_playlist, name_new_playlist, NameNewPlaylist)
 ADD_SCENE(nfc_playlist, nfc_add, NfcAdd)

+ 1 - 1
nfc_playlist/scenes/nfc_playlist_scene_emulation.c

@@ -6,7 +6,7 @@ typedef enum NfcPlaylistEmulationState {
     NfcPlaylistEmulationState_Canceled
 } NfcPlaylistEmulationState;
 
-NfcPlaylistEmulationState EmulationState = NfcPlaylistEmulationState_Stopped;
+static NfcPlaylistEmulationState EmulationState = NfcPlaylistEmulationState_Stopped;
 
 int32_t nfc_playlist_emulation_task(void* context) {
     NfcPlaylist* nfc_playlist = context;

+ 67 - 0
nfc_playlist/scenes/nfc_playlist_scene_error_playlist_already_exists.c

@@ -0,0 +1,67 @@
+#include "../nfc_playlist.h"
+
+void nfc_playlist_error_playlist_already_exists_menu_callback(
+    GuiButtonType result,
+    InputType type,
+    void* context) {
+    NfcPlaylist* nfc_playlist = context;
+    if(type == InputTypeShort) {
+        view_dispatcher_send_custom_event(nfc_playlist->view_dispatcher, result);
+    }
+}
+
+void nfc_playlist_error_playlist_already_exists_scene_on_enter(void* context) {
+    NfcPlaylist* nfc_playlist = context;
+
+    widget_add_text_box_element(
+        nfc_playlist->widget,
+        0,
+        0,
+        128,
+        23,
+        AlignCenter,
+        AlignCenter,
+        "\e#A playlist with that name already exists\e#",
+        false);
+    widget_add_button_element(
+        nfc_playlist->widget,
+        GuiButtonTypeLeft,
+        "Try Again",
+        nfc_playlist_error_playlist_already_exists_menu_callback,
+        nfc_playlist);
+    widget_add_button_element(
+        nfc_playlist->widget,
+        GuiButtonTypeRight,
+        "Main Menu",
+        nfc_playlist_error_playlist_already_exists_menu_callback,
+        nfc_playlist);
+
+    view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Widget);
+}
+
+bool nfc_playlist_error_playlist_already_exists_scene_on_event(
+    void* context,
+    SceneManagerEvent event) {
+    NfcPlaylist* nfc_playlist = context;
+    bool consumed = false;
+    if(event.type == SceneManagerEventTypeCustom) {
+        switch(event.event) {
+        case GuiButtonTypeRight:
+            scene_manager_search_and_switch_to_previous_scene(
+                nfc_playlist->scene_manager, NfcPlaylistScene_MainMenu);
+            break;
+        case GuiButtonTypeLeft:
+            scene_manager_previous_scene(nfc_playlist->scene_manager);
+            consumed = true;
+            break;
+        default:
+            break;
+        }
+    }
+    return consumed;
+}
+
+void nfc_playlist_error_playlist_already_exists_scene_on_exit(void* context) {
+    NfcPlaylist* nfc_playlist = context;
+    widget_reset(nfc_playlist->widget);
+}

+ 14 - 2
nfc_playlist/scenes/nfc_playlist_scene_name_new_playlist.c

@@ -1,5 +1,7 @@
 #include "../nfc_playlist.h"
 
+static bool playlist_exist_already = false;
+
 int32_t nfc_playlist_name_new_playlist_thread_task(void* context) {
     NfcPlaylist* nfc_playlist = context;
 
@@ -16,6 +18,8 @@ int32_t nfc_playlist_name_new_playlist_thread_task(void* context) {
             furi_string_swap(nfc_playlist->settings.playlist_path, file_name);
             nfc_playlist->settings.playlist_length = 0;
         }
+    } else {
+        playlist_exist_already = true;
     }
 
     furi_string_free(file_name);
@@ -30,13 +34,19 @@ void nfc_playlist_name_new_playlist_thread_state_callback(FuriThreadState state,
     if(state == FuriThreadStateStopped) {
         furi_thread_yield();
         nfc_playlist->thread = NULL;
-        scene_manager_search_and_switch_to_previous_scene(
-            nfc_playlist->scene_manager, NfcPlaylistScene_MainMenu);
+        if(playlist_exist_already) {
+            scene_manager_next_scene(
+                nfc_playlist->scene_manager, NfcPlaylistScene_ErrorPlaylistAlreadyExists);
+        } else {
+            scene_manager_search_and_switch_to_previous_scene(
+                nfc_playlist->scene_manager, NfcPlaylistScene_MainMenu);
+        }
     }
 }
 
 void nfc_playlist_name_new_playlist_menu_callback(void* context) {
     NfcPlaylist* nfc_playlist = context;
+
     nfc_playlist->thread = furi_thread_alloc_ex(
         "NfcPlaylistCreator", 1024, nfc_playlist_name_new_playlist_thread_task, nfc_playlist);
     furi_thread_set_state_context(nfc_playlist->thread, nfc_playlist);
@@ -48,6 +58,8 @@ void nfc_playlist_name_new_playlist_menu_callback(void* context) {
 void nfc_playlist_name_new_playlist_scene_on_enter(void* context) {
     NfcPlaylist* nfc_playlist = context;
 
+    playlist_exist_already = false;
+
     nfc_playlist->text_input_output = malloc(MAX_PLAYLIST_NAME_LEN + 1);
     text_input_set_header_text(nfc_playlist->text_input, "Enter file name");
     text_input_set_minimum_length(nfc_playlist->text_input, 1);

+ 16 - 2
nfc_playlist/scenes/nfc_playlist_scene_playlist_rename.c

@@ -1,5 +1,7 @@
 #include "../nfc_playlist.h"
 
+static bool playlist_exist_already = false;
+
 int32_t nfc_playlist_playlist_rename_thread_task(void* context) {
     NfcPlaylist* nfc_playlist = context;
 
@@ -21,6 +23,10 @@ int32_t nfc_playlist_playlist_rename_thread_task(void* context) {
                furi_string_get_cstr(new_file_path)) == FSE_OK) {
             furi_string_swap(nfc_playlist->settings.playlist_path, new_file_path);
         }
+    } else {
+        if(furi_string_cmp(nfc_playlist->settings.playlist_path, new_file_path) != 0) {
+            playlist_exist_already = true;
+        }
     }
 
     furi_string_free(new_file_path);
@@ -34,13 +40,19 @@ void nfc_playlist_playlist_rename_thread_state_callback(FuriThreadState state, v
     if(state == FuriThreadStateStopped) {
         furi_thread_yield();
         nfc_playlist->thread = NULL;
-        scene_manager_search_and_switch_to_previous_scene(
-            nfc_playlist->scene_manager, NfcPlaylistScene_MainMenu);
+        if(playlist_exist_already) {
+            scene_manager_next_scene(
+                nfc_playlist->scene_manager, NfcPlaylistScene_ErrorPlaylistAlreadyExists);
+        } else {
+            scene_manager_search_and_switch_to_previous_scene(
+                nfc_playlist->scene_manager, NfcPlaylistScene_MainMenu);
+        }
     }
 }
 
 void nfc_playlist_playlist_rename_menu_callback(void* context) {
     NfcPlaylist* nfc_playlist = context;
+
     nfc_playlist->thread = furi_thread_alloc_ex(
         "NfcPlaylistRenamer", 1024, nfc_playlist_playlist_rename_thread_task, nfc_playlist);
     furi_thread_set_state_context(nfc_playlist->thread, nfc_playlist);
@@ -52,6 +64,8 @@ void nfc_playlist_playlist_rename_menu_callback(void* context) {
 void nfc_playlist_playlist_rename_scene_on_enter(void* context) {
     NfcPlaylist* nfc_playlist = context;
 
+    playlist_exist_already = false;
+
     FuriString* tmp_file_name = furi_string_alloc();
     path_extract_filename_no_ext(
         furi_string_get_cstr(nfc_playlist->settings.playlist_path), tmp_file_name);

+ 18 - 7
nfc_playlist/scenes/nfc_playlist_scene_view_playlist_content.c

@@ -6,16 +6,27 @@ void nfc_playlist_view_playlist_content_scene_on_enter(void* context) {
     Storage* storage = furi_record_open(RECORD_STORAGE);
     Stream* stream = file_stream_alloc(storage);
 
-    if(file_stream_open(
-           stream,
-           furi_string_get_cstr(nfc_playlist->settings.playlist_path),
-           FSAM_READ,
-           FSOM_OPEN_EXISTING)) {
+    if(nfc_playlist->settings.playlist_length == 0) {
+        widget_add_text_box_element(
+            nfc_playlist->widget,
+            4,
+            4,
+            128,
+            64,
+            AlignCenter,
+            AlignCenter,
+            "\ePlaylist is empty\n\n\n\nPress back\e",
+            false);
+    } else if(file_stream_open(
+                  stream,
+                  furi_string_get_cstr(nfc_playlist->settings.playlist_path),
+                  FSAM_READ,
+                  FSOM_OPEN_EXISTING)) {
         FuriString* line = furi_string_alloc();
         FuriString* tmp_str = furi_string_alloc();
 
         while(stream_read_line(stream, line)) {
-            furi_string_cat_printf(tmp_str, "%s", furi_string_get_cstr(line));
+            furi_string_cat(tmp_str, furi_string_get_cstr(line));
         }
 
         furi_string_free(line);
@@ -35,7 +46,7 @@ void nfc_playlist_view_playlist_content_scene_on_enter(void* context) {
             64,
             AlignCenter,
             AlignCenter,
-            "\eFailed to open playlist\n\nPress back\e",
+            "\eFailed to open playlist\n\n\n\nPress back\e",
             false);
     }