Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
- Moves nfc_playlist_worker into a lib folder
- Refactoring
  • Loading branch information
acegoal07 committed Dec 18, 2023
1 parent ee805ea commit f3fcc1d
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 92 deletions.
5 changes: 5 additions & 0 deletions application.fam
Original file line number Diff line number Diff line change
Expand Up @@ -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",
),
],
)
2 changes: 1 addition & 1 deletion build.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
fbt fap_nfc_playlist && fbt launch_app APPSRC=applications_user/nfc_playlist
fbt fap_nfc_playlist && fbt launch APPSRC=applications_user/nfc_playlist
File renamed without changes.
File renamed without changes.
69 changes: 33 additions & 36 deletions nfc_playlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
6 changes: 2 additions & 4 deletions nfc_playlist.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#pragma once

#include <furi.h>
#include <string.h>

#include <gui/gui.h>
#include <gui/view_dispatcher.h>
#include <gui/scene_manager.h>
Expand Down Expand Up @@ -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;
static const int default_emulate_delay = 0;
40 changes: 20 additions & 20 deletions scences/emulation.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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");
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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);
}
4 changes: 0 additions & 4 deletions scences/emulation.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#pragma once

#include <furi.h>
#include <string.h>
#include <storage/storage.h>
#include <toolbox/stream/stream.h>
#include <toolbox/stream/file_stream.h>
Expand All @@ -10,8 +8,6 @@
#include <gui/view_dispatcher.h>
#include <gui/scene_manager.h>
#include <gui/modules/popup.h>
#include <gui/modules/variable_item_list.h>
#include <nfc_playlist.h>

void nfc_playlist_emulation_scene_on_enter(void* context);
bool nfc_playlist_emulation_scene_on_event(void* context, SceneManagerEvent event);
Expand Down
49 changes: 26 additions & 23 deletions scences/main_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,31 @@ 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;
}
}

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);
Expand All @@ -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:
Expand All @@ -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);
}
4 changes: 0 additions & 4 deletions scences/main_menu.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
#pragma once

#include <furi.h>
#include <string.h>

#include <gui/gui.h>
#include <gui/view_dispatcher.h>
#include <gui/scene_manager.h>
#include <gui/modules/popup.h>
#include <gui/modules/variable_item_list.h>

void nfc_playlist_main_menu_scene_on_enter(void* context);
Expand Down

0 comments on commit f3fcc1d

Please sign in to comment.