Skip to content

Commit

Permalink
Merge pull request #7 from acegoal07/file-select
Browse files Browse the repository at this point in the history
File select
  • Loading branch information
acegoal07 authored Jan 6, 2024
2 parents 0e9c7a5 + 8e94168 commit b861e0e
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 88 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ To set the playlist you need to create a file in ext/apps_data/nfc_playlist call
This app was design, built and tested using the <a href="https://github.com/Flipper-XFW/Xtreme-Firmware">Xtreme firmware</a> i don't see why it wont work with other firmwares but do keep this in mind when building it with FBT/uFBT

## Ideas
- [ ] Add the ability to change playlist
- [x] Add the ability to change playlist
- [ ] Make it so changed settings are saved (maybe make it so settings can be specified for each playlist changing the settings based on the playlist selected)
2 changes: 1 addition & 1 deletion application.fam
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ App(
fap_category="NFC",
fap_author="@acegoal07",
fap_weburl="https://github.com/acegoal07/FlipperZero_NFC_Playlist/tree/main",
fap_version="1.0",
fap_version="1.1",
fap_icon="icon.png",
fap_private_libs=[
Lib(
Expand Down
20 changes: 15 additions & 5 deletions nfc_playlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@
static void (*const nfc_playlist_scene_on_enter_handlers[])(void*) = {
nfc_playlist_main_menu_scene_on_enter,
nfc_playlist_settings_scene_on_enter,
nfc_playlist_emulation_scene_on_enter
nfc_playlist_emulation_scene_on_enter,
nfc_playlist_file_select_scene_on_enter
};

static bool (*const nfc_playlist_scene_on_event_handlers[])(void*, SceneManagerEvent) = {
nfc_playlist_main_menu_scene_on_event,
nfc_playlist_settings_scene_on_event,
nfc_playlist_emulation_scene_on_event
nfc_playlist_emulation_scene_on_event,
nfc_playlist_file_select_scene_on_event
};

static void (*const nfc_playlist_scene_on_exit_handlers[])(void*) = {
nfc_playlist_main_menu_scene_on_exit,
nfc_playlist_settings_scene_on_exit,
nfc_playlist_emulation_scene_on_exit
nfc_playlist_emulation_scene_on_exit,
nfc_playlist_file_select_scene_on_exit
};

static const SceneManagerHandlers nfc_playlist_scene_manager_handlers = {
Expand Down Expand Up @@ -48,6 +51,10 @@ static NfcPlaylist* nfc_playlist_alloc() {

nfc_playlist->variable_item_list = variable_item_list_alloc();
nfc_playlist->submenu = submenu_alloc();
nfc_playlist->base_file_path = furi_string_alloc_set_str("/ext/apps_data/nfc_playlist/");
nfc_playlist->file_path = nfc_playlist->base_file_path;
nfc_playlist->file_selected = false;
nfc_playlist->file_browser = file_browser_alloc(nfc_playlist->file_path);
nfc_playlist->popup = popup_alloc();
nfc_playlist->emulate_timeout = default_emulate_timeout;
nfc_playlist->emulate_delay = default_emulate_delay;
Expand All @@ -59,10 +66,9 @@ static NfcPlaylist* nfc_playlist_alloc() {
view_dispatcher_set_navigation_event_callback(nfc_playlist->view_dispatcher, nfc_playlist_back_event_callback);

view_dispatcher_add_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Menu, submenu_get_view(nfc_playlist->submenu));

view_dispatcher_add_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Settings, variable_item_list_get_view(nfc_playlist->variable_item_list));

view_dispatcher_add_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Popup, popup_get_view(nfc_playlist->popup));
view_dispatcher_add_view(nfc_playlist->view_dispatcher, NfcPlaylistView_FileSelect, file_browser_get_view(nfc_playlist->file_browser));

return nfc_playlist;
}
Expand All @@ -73,11 +79,15 @@ static void nfc_playlist_free(NfcPlaylist* nfc_playlist) {
view_dispatcher_remove_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Menu);
view_dispatcher_remove_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Settings);
view_dispatcher_remove_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Popup);
view_dispatcher_remove_view(nfc_playlist->view_dispatcher, NfcPlaylistView_FileSelect);
view_dispatcher_free(nfc_playlist->view_dispatcher);
variable_item_list_free(nfc_playlist->variable_item_list);
submenu_free(nfc_playlist->submenu);
file_browser_free(nfc_playlist->file_browser);
popup_free(nfc_playlist->popup);
furi_record_close(RECORD_NOTIFICATION);
furi_string_free(nfc_playlist->base_file_path);
furi_string_free(nfc_playlist->file_path);
free(nfc_playlist);
}

Expand Down
10 changes: 9 additions & 1 deletion nfc_playlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,31 @@
#include <gui/modules/popup.h>
#include <gui/modules/variable_item_list.h>
#include <gui/modules/submenu.h>
#include <gui/modules/file_browser.h>
#include <notification/notification_messages.h>
#include <nfc_playlist_worker.h>
#include <assets_icons.h>

typedef enum {
NfcPlaylistView_Menu,
NfcPlaylistView_Settings,
NfcPlaylistView_Popup
NfcPlaylistView_Popup,
NfcPlaylistView_FileSelect
} NfcPlayScenesView;

typedef enum {
NfcPlaylistScene_MainMenu,
NfcPlaylistScene_Settings,
NfcPlaylistScene_EmulatingPopup,
NfcPlaylistScene_FileSelect,
NfcPlaylistScene_count
} NfcPlaylistScene;

typedef struct {
SceneManager* scene_manager;
ViewDispatcher* view_dispatcher;
VariableItemList* variable_item_list;
FileBrowser* file_browser;
Submenu* submenu;
Popup* popup;
NotificationApp* notification;
Expand All @@ -36,6 +41,9 @@ typedef struct {
uint8_t emulate_timeout;
uint8_t emulate_delay;
bool emulate_led_indicator;
FuriString* base_file_path;
FuriString* file_path;
bool file_selected;
} NfcPlaylist;

static const int options_emulate_timeout[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
Expand Down
3 changes: 2 additions & 1 deletion nfc_playlist_i.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@

#include "scences/main_menu.h"
#include "scences/settings.h"
#include "scences/emulation.h"
#include "scences/emulation.h"
#include "scences/file_select.h"
147 changes: 73 additions & 74 deletions scences/emulation.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ bool nfc_playlist_emulation_scene_on_event(void* context, SceneManagerEvent even
default:
break;
}

return false;
}

Expand All @@ -36,8 +35,7 @@ void nfc_playlist_emulation_scene_on_exit(void* context) {
void nfc_playlist_emulation_setup(void* context) {
NfcPlaylist* nfc_playlist = context;

nfc_playlist->thread = furi_thread_alloc_ex(
"NfcPlaylistEmulationWorker", 8192, nfc_playlist_emulation_task, nfc_playlist);
nfc_playlist->thread = furi_thread_alloc_ex("NfcPlaylistEmulationWorker", 8192, nfc_playlist_emulation_task, nfc_playlist);
nfc_playlist->nfc_playlist_worker = nfc_playlist_worker_alloc();
}

Expand Down Expand Up @@ -71,95 +69,96 @@ int32_t nfc_playlist_emulation_task(void* context) {
view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Popup);

// Read file
if(file_stream_open(stream, APP_DATA_PATH("playlist.txt"), FSAM_READ, FSOM_OPEN_EXISTING)) {
if(file_stream_open(stream, furi_string_get_cstr(nfc_playlist->file_path), FSAM_READ, FSOM_OPEN_EXISTING)) {
EmulationState = NfcPlaylistEmulationState_Emulating;
int file_position = 0;
// read the file line by line and print the text
while(stream_read_line(stream, line) && EmulationState == NfcPlaylistEmulationState_Emulating) {
if (options_emulate_delay[nfc_playlist->emulate_delay] > 0) {
if (file_position > 0) {
popup_set_header(nfc_playlist->popup, "Delaying", 64, 10, AlignCenter, AlignTop);
start_error_blink(nfc_playlist);
int time_counter_delay_ms = (options_emulate_delay[nfc_playlist->emulate_delay] * 1000);
do {
char display_text[10];
snprintf(display_text, 10, "%ds", (time_counter_delay_ms/1000));
popup_set_text(nfc_playlist->popup, display_text, 64, 50, AlignCenter, AlignTop);
furi_delay_ms(500);
time_counter_delay_ms -= 500;
} while(time_counter_delay_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating);
} else {
file_position++;
if (strlen(furi_string_get_cstr(line)) > 1) {
if (options_emulate_delay[nfc_playlist->emulate_delay] > 0) {
if (file_position > 0) {
popup_set_header(nfc_playlist->popup, "Delaying", 64, 10, AlignCenter, AlignTop);
start_error_blink(nfc_playlist);
int time_counter_delay_ms = (options_emulate_delay[nfc_playlist->emulate_delay] * 1000);
do {
char display_text[10];
snprintf(display_text, 10, "%ds", (time_counter_delay_ms/1000));
popup_set_text(nfc_playlist->popup, display_text, 64, 50, AlignCenter, AlignTop);
furi_delay_ms(500);
time_counter_delay_ms -= 500;
} while(time_counter_delay_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating);
} else {
file_position++;
}
}
}

if (EmulationState != NfcPlaylistEmulationState_Emulating) {
break;
}
if (EmulationState != NfcPlaylistEmulationState_Emulating) {
break;
}

char* file_path = (char*)furi_string_get_cstr(line);
char* file_name;
if (strchr(file_path, '/') != NULL) {
file_name = &strrchr(file_path, '/')[1];
} else {
file_name = file_path;
}
char const* file_ext = &strrchr(file_path, '.')[1];
int time_counter_ms = (options_emulate_timeout[nfc_playlist->emulate_timeout] * 1000);

if (storage_file_exists(storage, file_path) == false) {
char popup_header_text[(18 + strlen(file_name))];
snprintf(popup_header_text, (18 + strlen(file_name)), "%s\n%s", "ERROR not found:", file_name);
popup_set_header(nfc_playlist->popup, popup_header_text, 64, 10, AlignCenter, AlignTop);
start_error_blink(nfc_playlist);
while(time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
char popup_text[9];
snprintf(popup_text, 9, "%ds", (time_counter_ms/1000));
popup_set_text(nfc_playlist->popup, popup_text, 64, 50, AlignCenter, AlignTop);
furi_delay_ms(500);
time_counter_ms -= 500;
char* file_path = (char*)furi_string_get_cstr(line);
char* file_name;
if (strchr(file_path, '/') != NULL) {
file_name = &strrchr(file_path, '/')[1];
} else {
file_name = file_path;
}
}
char const* file_ext = &strrchr(file_path, '.')[1];
int time_counter_ms = (options_emulate_timeout[nfc_playlist->emulate_timeout] * 1000);

else if (strcasestr(file_ext, "nfc") == NULL) {
char popup_header_text[(21 + strlen(file_name))];
snprintf(popup_header_text, (21 + strlen(file_name)), "%s\n%s", "ERROR invalid file:", file_name);
popup_set_header(nfc_playlist->popup, popup_header_text, 64, 10, AlignCenter, AlignTop);
start_error_blink(nfc_playlist);
while(time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
char popup_text[9];
snprintf(popup_text, 9, "%ds", (time_counter_ms/1000));
popup_set_text(nfc_playlist->popup, popup_text, 64, 50, AlignCenter, AlignTop);
furi_delay_ms(500);
time_counter_ms -= 500;
if (storage_file_exists(storage, file_path) == false) {
char popup_header_text[(18 + strlen(file_name))];
snprintf(popup_header_text, (18 + strlen(file_name)), "%s\n%s", "ERROR not found:", file_name);
popup_set_header(nfc_playlist->popup, popup_header_text, 64, 10, AlignCenter, AlignTop);
start_error_blink(nfc_playlist);
while(time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
char popup_text[9];
snprintf(popup_text, 9, "%ds", (time_counter_ms/1000));
popup_set_text(nfc_playlist->popup, popup_text, 64, 50, AlignCenter, AlignTop);
furi_delay_ms(500);
time_counter_ms -= 500;
}
}
}

else {
char popup_header_text[(12 + strlen(file_name))];
snprintf(popup_header_text, (12 + strlen(file_name)), "%s\n%s", "Emulating:", file_name);
popup_set_header(nfc_playlist->popup, popup_header_text, 64, 10, AlignCenter, AlignTop);
nfc_playlist_worker_set_nfc_data(nfc_playlist->nfc_playlist_worker, file_path);
nfc_playlist_worker_start(nfc_playlist->nfc_playlist_worker);
start_normal_blink(nfc_playlist);
while(nfc_playlist_worker_is_emulating(nfc_playlist->nfc_playlist_worker) && time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
char popup_text[9];
snprintf(popup_text, 9, "%ds", (time_counter_ms/1000));
popup_set_text(nfc_playlist->popup, popup_text, 64, 50, AlignCenter, AlignTop);
furi_delay_ms(500);
time_counter_ms -= 500;
else if (strcasestr(file_ext, "nfc") == NULL) {
char popup_header_text[(21 + strlen(file_name))];
snprintf(popup_header_text, (21 + strlen(file_name)), "%s\n%s", "ERROR invalid file:", file_name);
popup_set_header(nfc_playlist->popup, popup_header_text, 64, 10, AlignCenter, AlignTop);
start_error_blink(nfc_playlist);
while(time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
char popup_text[9];
snprintf(popup_text, 9, "%ds", (time_counter_ms/1000));
popup_set_text(nfc_playlist->popup, popup_text, 64, 50, AlignCenter, AlignTop);
furi_delay_ms(500);
time_counter_ms -= 500;
}
}

else {
char popup_header_text[(12 + strlen(file_name))];
snprintf(popup_header_text, (12 + strlen(file_name)), "%s\n%s", "Emulating:", file_name);
popup_set_header(nfc_playlist->popup, popup_header_text, 64, 10, AlignCenter, AlignTop);
nfc_playlist_worker_set_nfc_data(nfc_playlist->nfc_playlist_worker, file_path);
nfc_playlist_worker_start(nfc_playlist->nfc_playlist_worker);
start_normal_blink(nfc_playlist);
while(nfc_playlist_worker_is_emulating(nfc_playlist->nfc_playlist_worker) && time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
char popup_text[9];
snprintf(popup_text, 9, "%ds", (time_counter_ms/1000));
popup_set_text(nfc_playlist->popup, popup_text, 64, 50, AlignCenter, AlignTop);
furi_delay_ms(500);
time_counter_ms -= 500;
}
nfc_playlist_worker_stop(nfc_playlist->nfc_playlist_worker);
}
nfc_playlist_worker_stop(nfc_playlist->nfc_playlist_worker);
}
}
popup_reset(nfc_playlist->popup);
popup_set_header(nfc_playlist->popup, EmulationState == NfcPlaylistEmulationState_Canceled ? "Emulation stopped" : "Emulation finished", 64, 10, AlignCenter, AlignTop);
popup_set_text(nfc_playlist->popup, "Press back", 64, 50, AlignCenter, AlignTop);
stop_blink(nfc_playlist);
EmulationState = NfcPlaylistEmulationState_Stopped;
EmulationState = NfcPlaylistEmulationState_Stopped;
} else {
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);
popup_set_header(nfc_playlist->popup, "Failed to open playlist", 64, 10, AlignCenter, AlignTop);
popup_set_text(nfc_playlist->popup, "Press back", 64, 50, AlignCenter, AlignTop);
}
// Free/close resources
furi_string_free(line);
Expand Down
34 changes: 34 additions & 0 deletions scences/file_select.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "nfc_playlist.h"
#include "scences/file_select.h"

void nfc_playlist_file_select_menu_callback(void* context) {
NfcPlaylist* nfc_playlist = context;
scene_manager_previous_scene(nfc_playlist->scene_manager);
}

void nfc_playlist_file_select_scene_on_enter(void* context) {
NfcPlaylist* nfc_playlist = context;
file_browser_configure(
nfc_playlist->file_browser,
".txt",
furi_string_get_cstr(nfc_playlist->base_file_path),
true,
true,
&I_sub1_10px,
false);
view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_FileSelect);
file_browser_set_callback(nfc_playlist->file_browser, nfc_playlist_file_select_menu_callback, nfc_playlist);
file_browser_start(nfc_playlist->file_browser, nfc_playlist->base_file_path);
}

bool nfc_playlist_file_select_scene_on_event(void* context, SceneManagerEvent event) {
UNUSED(event);
UNUSED(context);
bool consumed = false;
return consumed;
}

void nfc_playlist_file_select_scene_on_exit(void* context) {
NfcPlaylist* nfc_playlist = context;
file_browser_stop(nfc_playlist->file_browser);
}
10 changes: 10 additions & 0 deletions scences/file_select.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once
#include <furi.h>
#include <gui/gui.h>
#include <gui/view_dispatcher.h>
#include <gui/scene_manager.h>
#include <gui/modules/file_browser.h>

void nfc_playlist_file_select_scene_on_enter(void* context);
bool nfc_playlist_file_select_scene_on_event(void* context, SceneManagerEvent event);
void nfc_playlist_file_select_scene_on_exit(void* context);
Loading

0 comments on commit b861e0e

Please sign in to comment.