Procházet zdrojové kódy

Merge pull request #1 from acegoal07/Timeout-Improvements

Timeout improvements
acegoal07 před 2 roky
rodič
revize
f69b9eade2
3 změnil soubory, kde provedl 94 přidání a 86 odebrání
  1. 1 3
      README.md
  2. 80 70
      nfc_playlist.c
  3. 13 13
      nfc_playlist_worker.c

+ 1 - 3
README.md

@@ -1,5 +1,4 @@
 # FlipperZero_NFC_Playlist
-
 The idea behind this app is to allow for you to test multiple copies of NFC's at once as a bulk test
 
 At the moment this project is very basic and just reads the cards from the txt file and emulates the NFC's but this is very early days of development and i wanted to make a basic version of my idea first
@@ -8,8 +7,7 @@ At the moment this project is very basic and just reads the cards from the txt f
 At the moment it reads the files and in what order from a txt file in the apps_data folder called playlist.txt an example of the file can be found in the repository
 
 ## TODO:
-
 - [x] Add basic UI
 - [ ] Add the ability to adjust time between cards
 - [ ] Add the ability to adjust card queue from within the app
-- [ ] Make it so you can pause, stop and continue the running of the cards
+- [ ] Make it so you can pause, stop and continue the running of the cards

+ 80 - 70
nfc_playlist.c

@@ -1,4 +1,5 @@
 #include <furi.h>
+#include <string.h>
 
 #include <storage/storage.h>
 #include <toolbox/stream/stream.h>
@@ -9,83 +10,83 @@
 #include <gui/gui.h>
 #include <gui/view_dispatcher.h>
 #include <gui/scene_manager.h>
-#include <gui/modules/menu.h>
 #include <gui/modules/submenu.h>
 #include <gui/modules/popup.h>
 
 // Define log tag
 #define TAG "NfcPlaylist"
 
-/** ids for all scenes used by the app */
+// IDs for all scenes used by the app
 typedef enum {
    NfcPlaylistScene_MainMenu,
-   NfcPlaylistScene_FirstPopup,
+   NfcPlaylistScene_EmulatingPopup,
    NfcPlaylistScene_count
 } NfcPlaylistScene;
 
-/** ids for the 2 types of view used by the app */
+// IDs for the view used by the app
 typedef enum { NfcPlaylistView_Menu, NfcPlaylistView_Popup } NfcPlaylistView;
 
-/** the app context struct */
+// The app context struct
 typedef struct {
    SceneManager* scene_manager;
    ViewDispatcher* view_dispatcher;
-   Submenu* menu;
+   Submenu* submenu;
    Popup* popup;
+   NfcPlaylistWorker* nfc_worker;
    int emulate_timeout;
 } NfcPlaylist;
 
-/** all custom events */
-typedef enum { NfcPlaylistEvent_ShowPopupOne } NfcPlaylistEvent;
+// All custom events
+typedef enum { NfcPlaylistEvent_ShowEmulatingPopup } NfcPlaylistEvent;
 
 /* main menu scene */
 
-/** indices for menu items */
-typedef enum { NfcPlaylistMenuSelection_One } NfcPlaylistMenuSelection;
+// Indices for menu items
+typedef enum { NfcPlaylistMenuSelection_Start, NfcPlaylistMenuSelection_Settings } NfcPlaylistMenuSelection;
 
-/** main menu callback - sends a custom event to the scene manager based on the menu selection */
+// Main menu callback - sends a custom event to the scene manager based on the menu selection
 void nfc_playlist_menu_callback_main_menu(void* context, uint32_t index) {
    FURI_LOG_T(TAG, "nfc_playlist_menu_callback_main_menu");
    NfcPlaylist* app = context;
    switch(index) {
-   case NfcPlaylistMenuSelection_One:
-      scene_manager_handle_custom_event(app->scene_manager, NfcPlaylistEvent_ShowPopupOne);
-      break;
+      case NfcPlaylistMenuSelection_Start:
+         scene_manager_handle_custom_event(app->scene_manager, NfcPlaylistEvent_ShowEmulatingPopup);
+         break;
    }
 }
 
-/** resets the menu, gives it content, callbacks and selection enums */
+// Resets the menu, gives it content, callbacks and selection enums
 void nfc_playlist_scene_on_enter_main_menu(void* context) {
    FURI_LOG_T(TAG, "nfc_playlist_scene_on_enter_main_menu");
    NfcPlaylist* app = context;
-   submenu_reset(app->menu);
-   submenu_set_header(app->menu, "NFC Playlist");
+   submenu_reset(app->submenu);
+   submenu_set_header(app->submenu, "NFC Playlist");
    submenu_add_item(
-      app->menu,
+      app->submenu,
       "Start",
-      NfcPlaylistMenuSelection_One,
+      NfcPlaylistMenuSelection_Start,
       nfc_playlist_menu_callback_main_menu,
       app);
    view_dispatcher_switch_to_view(app->view_dispatcher, NfcPlaylistView_Menu);
 }
 
-/** main menu event handler - switches scene based on the event */
+// Main menu event handler - switches scene based on the event
 bool nfc_playlist_scene_on_event_main_menu(void* context, SceneManagerEvent event) {
    FURI_LOG_T(TAG, "nfc_playlist_scene_on_event_main_menu");
    NfcPlaylist* app = context;
    bool consumed = false;
    switch(event.type) {
-   case SceneManagerEventTypeCustom:
-      switch(event.event) {
-      case NfcPlaylistEvent_ShowPopupOne:
-         scene_manager_next_scene(app->scene_manager, NfcPlaylistScene_FirstPopup);
-         consumed = true;
+      case SceneManagerEventTypeCustom:
+         switch(event.event) {
+            case NfcPlaylistEvent_ShowEmulatingPopup:
+               scene_manager_next_scene(app->scene_manager, NfcPlaylistScene_EmulatingPopup);
+               consumed = true;
+               break;
+         }
+         break;
+      default: // eg. SceneManagerEventTypeBack, SceneManagerEventTypeTick
+         consumed = false;
          break;
-      }
-      break;
-   default: // eg. SceneManagerEventTypeBack, SceneManagerEventTypeTick
-      consumed = false;
-      break;
    }
    return consumed;
 }
@@ -93,11 +94,10 @@ bool nfc_playlist_scene_on_event_main_menu(void* context, SceneManagerEvent even
 void nfc_playlist_scene_on_exit_main_menu(void* context) {
    FURI_LOG_T(TAG, "nfc_playlist_scene_on_exit_main_menu");
    NfcPlaylist* app = context;
-   submenu_reset(app->menu);
+   submenu_reset(app->submenu);
 }
 
-/* popup 1 scene */
-
+// Emulating scene
 void nfc_playlist_scene_on_enter_popup_emulating(void* context) {
    FURI_LOG_T(TAG, "nfc_playlist_scene_on_enter_popup_emulating");
    NfcPlaylist* app = context;
@@ -106,28 +106,39 @@ void nfc_playlist_scene_on_enter_popup_emulating(void* context) {
    Storage* storage = furi_record_open(RECORD_STORAGE);
    Stream* stream = file_stream_alloc(storage);
    FuriString* line = furi_string_alloc();
-   NfcPlaylistWorker* nfc_worker = nfc_playlist_worker_alloc();
+   app->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);
       // read the file line by line and print the text
       while(stream_read_line(stream, line)) {
+         char* file_path = (char*)furi_string_get_cstr(line);
+         char* file_name = &strrchr(file_path, '/')[1];
+         int time_counter_ms = app->emulate_timeout;
+
+         nfc_playlist_worker_set_nfc_data(app->nfc_worker, file_path);
+         nfc_playlist_worker_start(app->nfc_worker);
+
+         do {
+            int size = (strlen(file_name) + 4);
+            char display_text[size];
+            snprintf(display_text, size, "%s\n%ds", file_name, (time_counter_ms/1000));
+            popup_set_text(app->popup, display_text, 64, 25, AlignCenter, AlignTop);
+            furi_delay_ms(500);
+            time_counter_ms -= 500;
+            if (time_counter_ms <= 0) {
+               break;
+            }
+         } while(nfc_playlist_worker_is_emulating(app->nfc_worker));
+
+         if (nfc_playlist_worker_is_emulating(app->nfc_worker)) {
+            nfc_playlist_worker_stop(app->nfc_worker);
+         }
 
-         char* str = (char*)furi_string_get_cstr(line);
-         
-         popup_set_context(app->popup, app);
-         popup_set_header(app->popup, "Emulating", 64, 10, AlignCenter, AlignTop);
-         popup_set_text(app->popup, str, 64, 30, AlignCenter, AlignTop);
-         view_dispatcher_switch_to_view(app->view_dispatcher, NfcPlaylistView_Popup);
-
-         nfc_playlist_worker_set_nfc_data(nfc_worker, str);
-         nfc_playlist_worker_start(nfc_worker);
-
-         furi_delay_ms(app->emulate_timeout);
-
-         nfc_playlist_worker_stop(nfc_worker);
          furi_string_reset(line);
-
       }
    } else {
       FURI_LOG_E(TAG, "Failed to open file");
@@ -136,7 +147,8 @@ void nfc_playlist_scene_on_enter_popup_emulating(void* context) {
    furi_string_free(line);
    file_stream_close(stream);
    stream_free(stream);
-   nfc_playlist_worker_free(nfc_worker);
+   nfc_playlist_worker_free(app->nfc_worker);
+   app->nfc_worker = NULL;
    // Close storage
    furi_record_close(RECORD_STORAGE);
 
@@ -157,29 +169,29 @@ void nfc_playlist_scene_on_exit_popup_emulating(void* context) {
    popup_reset(app->popup);
 }
 
-/** collection of all scene on_enter handlers - in the same order as their enum */
+// Collection of all scene on_enter handlers - in the same order as their enum
 void (*const nfc_playlist_scene_on_enter_handlers[])(void*) = {
    nfc_playlist_scene_on_enter_main_menu,
    nfc_playlist_scene_on_enter_popup_emulating};
 
-/** collection of all scene on event handlers - in the same order as their enum */
+// Collection of all scene on event handlers - in the same order as their enum
 bool (*const nfc_playlist_scene_on_event_handlers[])(void*, SceneManagerEvent) = {
    nfc_playlist_scene_on_event_main_menu,
    nfc_playlist_scene_on_event_popup_emulating};
 
-/** collection of all scene on exit handlers - in the same order as their enum */
+// Collection of all scene on exit handlers - in the same order as their enum
 void (*const nfc_playlist_scene_on_exit_handlers[])(void*) = {
    nfc_playlist_scene_on_exit_main_menu,
    nfc_playlist_scene_on_exit_popup_emulating};
 
-/** collection of all on_enter, on_event, on_exit handlers */
+// Collection of all on_enter, on_event, on_exit handlers */
 const SceneManagerHandlers nfc_playlist_scene_event_handlers = {
    .on_enter_handlers = nfc_playlist_scene_on_enter_handlers,
    .on_event_handlers = nfc_playlist_scene_on_event_handlers,
    .on_exit_handlers = nfc_playlist_scene_on_exit_handlers,
    .scene_num = NfcPlaylistScene_count};
 
-/** custom event handler - passes the event to the scene manager */
+// Custom event handler - passes the event to the scene manager
 bool nfc_playlist_scene_manager_custom_event_callback(void* context, uint32_t custom_event) {
    FURI_LOG_T(TAG, "nfc_playlist_scene_manager_custom_event_callback");
    furi_assert(context);
@@ -187,7 +199,7 @@ bool nfc_playlist_scene_manager_custom_event_callback(void* context, uint32_t cu
    return scene_manager_handle_custom_event(app->scene_manager, custom_event);
 }
 
-/** navigation event handler - passes the event to the scene manager */
+// Navigation event handler - passes the event to the scene manager
 bool nfc_playlist_scene_manager_navigation_event_callback(void* context) {
    FURI_LOG_T(TAG, "nfc_playlist_scene_manager_navigation_event_callback");
    furi_assert(context);
@@ -195,13 +207,13 @@ bool nfc_playlist_scene_manager_navigation_event_callback(void* context) {
    return scene_manager_handle_back_event(app->scene_manager);
 }
 
-/** initialise the scene manager with all handlers */
+// Initialise the scene manager with all handlers
 void nfc_playlist_scene_manager_init(NfcPlaylist* app) {
    FURI_LOG_T(TAG, "nfc_playlist_scene_manager_init");
    app->scene_manager = scene_manager_alloc(&nfc_playlist_scene_event_handlers, app);
 }
 
-/** initialise the views, and initialise the view dispatcher with all views */
+// Initialise the views, and initialise the view dispatcher with all views
 void nfc_playlist_view_dispatcher_init(NfcPlaylist* app) {
    FURI_LOG_T(TAG, "nfc_playlist_view_dispatcher_init");
    app->view_dispatcher = view_dispatcher_alloc();
@@ -209,27 +221,25 @@ void nfc_playlist_view_dispatcher_init(NfcPlaylist* app) {
 
    // allocate each view
    FURI_LOG_D(TAG, "nfc_playlist_view_dispatcher_init allocating views");
-   app->menu = submenu_alloc();
+   app->submenu = submenu_alloc();
    app->popup = popup_alloc();
-   app->emulate_timeout = 2000;
+   app->emulate_timeout = 4000;
 
    // assign callback that pass events from views to the scene manager
    FURI_LOG_D(TAG, "nfc_playlist_view_dispatcher_init setting callbacks");
    view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
-   view_dispatcher_set_custom_event_callback(
-      app->view_dispatcher, nfc_playlist_scene_manager_custom_event_callback);
-   view_dispatcher_set_navigation_event_callback(
-      app->view_dispatcher, nfc_playlist_scene_manager_navigation_event_callback);
+   view_dispatcher_set_custom_event_callback( app->view_dispatcher, nfc_playlist_scene_manager_custom_event_callback);
+   view_dispatcher_set_navigation_event_callback(app->view_dispatcher, nfc_playlist_scene_manager_navigation_event_callback);
 
    // add views to the dispatcher, indexed by their enum value
    FURI_LOG_D(TAG, "nfc_playlist_view_dispatcher_init adding view menu");
-   view_dispatcher_add_view(app->view_dispatcher, NfcPlaylistView_Menu, submenu_get_view(app->menu));
+   view_dispatcher_add_view(app->view_dispatcher, NfcPlaylistView_Menu, submenu_get_view(app->submenu));
 
    FURI_LOG_D(TAG, "nfc_playlist_view_dispatcher_init adding view popup");
    view_dispatcher_add_view(app->view_dispatcher, NfcPlaylistView_Popup, popup_get_view(app->popup));
 }
 
-/** initialise app data, scene manager, and view dispatcher */
+// Initialise app data, scene manager, and view dispatcher
 NfcPlaylist* nfc_playlist_init() {
    FURI_LOG_T(TAG, "nfc_playlist_init");
    NfcPlaylist* app = malloc(sizeof(NfcPlaylist));
@@ -238,19 +248,19 @@ NfcPlaylist* nfc_playlist_init() {
    return app;
 }
 
-/** free all app data, scene manager, and view dispatcher */
+// Free all app data, scene manager, and view dispatcher
 void nfc_playlist_free(NfcPlaylist* app) {
    FURI_LOG_T(TAG, "nfc_playlist_free");
    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);
-   submenu_free(app->menu);
+   submenu_free(app->submenu);
    popup_free(app->popup);
    free(app);
 }
 
-/** go to trace log level in the dev environment */
+// Go to trace log level in the dev environment
 void nfc_playlist_set_log_level() {
 #ifdef FURI_DEBUG
    furi_log_set_level(FuriLogLevelTrace);
@@ -267,7 +277,7 @@ int32_t nfc_playlist_main(void* p) {
    nfc_playlist_set_log_level();
 
    // create the app context struct, scene manager, and view dispatcher
-   FURI_LOG_I(TAG, "Test app starting...");
+   FURI_LOG_I(TAG, "NFC PLaylist starting...");
    NfcPlaylist* app = nfc_playlist_init();
 
    // set the scene and launch the main loop
@@ -278,7 +288,7 @@ int32_t nfc_playlist_main(void* p) {
    view_dispatcher_run(app->view_dispatcher);
 
    // free all memory
-   FURI_LOG_I(TAG, "Test app finishing...");
+   FURI_LOG_I(TAG, "NFC PLaylist finishing...");
    furi_record_close(RECORD_GUI);
    nfc_playlist_free(app);
 

+ 13 - 13
nfc_playlist_worker.c

@@ -54,26 +54,26 @@ int32_t nfc_playlist_worker_task(void* context) {
     if(nfc_playlist_worker->state == NfcPlaylistWorkerState_Emulating) {
 
         nfc_playlist_worker->nfc_listener =
-            nfc_listener_alloc(nfc_playlist_worker->nfc, 
-                nfc_playlist_worker->nfc_protocol, 
+            nfc_listener_alloc(nfc_playlist_worker->nfc,
+                nfc_playlist_worker->nfc_protocol,
                 nfc_device_get_data(nfc_playlist_worker->nfc_device, nfc_playlist_worker->nfc_protocol)
             );
         nfc_listener_start(nfc_playlist_worker->nfc_listener, NULL, NULL);
 
 
-        int counter = 0;
-        while(true) {
-            furi_delay_ms(50);
-            counter++;
-            if (counter == 100) {
-                break;
-            }
-        }
-
-        // while(nfc_playlist_worker->state == NfcPlaylistWorkerState_Emulating) {
+        // int counter = 0;
+        // while(true) {
         //     furi_delay_ms(50);
+        //     counter++;
+        //     if (counter == 100) {
+        //         break;
+        //     }
         // }
 
+        while(nfc_playlist_worker->state == NfcPlaylistWorkerState_Emulating) {
+            furi_delay_ms(50);
+        }
+
         nfc_listener_stop(nfc_playlist_worker->nfc_listener);
         nfc_listener_free(nfc_playlist_worker->nfc_listener);
     }
@@ -99,7 +99,7 @@ bool nfc_playlist_worker_is_emulating(NfcPlaylistWorker* nfc_playlist_worker) {
 void nfc_playlist_worker_set_nfc_data(NfcPlaylistWorker* nfc_playlist_worker, char* file_path) {
 
     FURI_LOG_I("NfcPlaylistWorker", "nfc_playlist_worker_set_nfc_data: %s", file_path);
-    
+
     nfc_device_clear(nfc_playlist_worker->nfc_device);
     nfc_device_load(nfc_playlist_worker->nfc_device, file_path);