Skip to content

Commit 1035a10

Browse files
committed
Optimisation
- Improves response time on cancel making it instant instead of there being a small delay - Moves some variables to make them accessible in other places so there's not multiple calls to get the same variable
1 parent 99c9556 commit 1035a10

File tree

3 files changed

+66
-71
lines changed

3 files changed

+66
-71
lines changed

scences/emulation.c

+64-70
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ void nfc_playlist_emulation_stop(NfcPlaylist* nfc_playlist) {
5353

5454
int32_t nfc_playlist_emulation_task(void* context) {
5555
NfcPlaylist* nfc_playlist = context;
56-
// open/alloc resources
56+
5757
Storage* storage = furi_record_open(RECORD_STORAGE);
5858
Stream* stream = file_stream_alloc(storage);
5959
FuriString* line = furi_string_alloc();
@@ -62,87 +62,81 @@ int32_t nfc_playlist_emulation_task(void* context) {
6262
popup_set_context(nfc_playlist->popup, nfc_playlist);
6363
view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Popup);
6464

65-
// Read file
6665
if(file_stream_open(stream, furi_string_get_cstr(nfc_playlist->file_path), FSAM_READ, FSOM_OPEN_EXISTING)) {
6766
EmulationState = NfcPlaylistEmulationState_Emulating;
6867
int file_position = 0;
6968
while(stream_read_line(stream, line) && EmulationState == NfcPlaylistEmulationState_Emulating) {
70-
if (strlen(furi_string_get_cstr(line)) > 1) {
71-
if (options_emulate_delay[nfc_playlist->emulate_delay] > 0) {
72-
if (file_position > 0) {
73-
popup_set_header(nfc_playlist->popup, "Delaying", 64, 10, AlignCenter, AlignTop);
74-
start_blink(nfc_playlist, NfcPlaylistLedState_Error);
75-
int time_counter_delay_ms = (options_emulate_delay[nfc_playlist->emulate_delay] * 1000);
76-
do {
77-
char display_text[10];
78-
snprintf(display_text, 10, "%ds", (time_counter_delay_ms/1000));
79-
popup_set_text(nfc_playlist->popup, display_text, 64, 50, AlignCenter, AlignTop);
80-
furi_delay_ms(500);
81-
time_counter_delay_ms -= 500;
82-
} while(time_counter_delay_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating);
83-
} else {
84-
file_position++;
85-
}
86-
}
8769

88-
if (EmulationState != NfcPlaylistEmulationState_Emulating) {
89-
break;
90-
}
70+
char* file_path = (char*)furi_string_get_cstr(line);
9171

92-
char* file_path = (char*)furi_string_get_cstr(line);
93-
char* file_name;
94-
if (strchr(file_path, '/') != NULL) {
95-
file_name = &strrchr(file_path, '/')[1];
72+
if (strlen(file_path) <= 1) {continue;}
73+
74+
if (nfc_playlist->emulate_delay > 0) {
75+
if (file_position > 0) {
76+
popup_set_header(nfc_playlist->popup, "Delaying", 64, 10, AlignCenter, AlignTop);
77+
start_blink(nfc_playlist, NfcPlaylistLedState_Error);
78+
int time_counter_delay_ms = (options_emulate_delay[nfc_playlist->emulate_delay]*1000);
79+
do {
80+
char display_text[10];
81+
snprintf(display_text, 10, "%ds", (time_counter_delay_ms/1000));
82+
popup_set_text(nfc_playlist->popup, display_text, 64, 50, AlignCenter, AlignTop);
83+
furi_delay_ms(50);
84+
time_counter_delay_ms -= 50;
85+
} while(time_counter_delay_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating);
9686
} else {
97-
file_name = file_path;
87+
file_position++;
9888
}
99-
char const* file_ext = &strrchr(file_path, '.')[1];
100-
int time_counter_ms = (options_emulate_timeout[nfc_playlist->emulate_timeout] * 1000);
89+
}
10190

102-
if (storage_file_exists(storage, file_path) == false) {
103-
char popup_header_text[(18 + strlen(file_name))];
104-
snprintf(popup_header_text, (18 + strlen(file_name)), "%s\n%s", "ERROR not found:", file_name);
105-
popup_set_header(nfc_playlist->popup, popup_header_text, 64, 10, AlignCenter, AlignTop);
106-
start_blink(nfc_playlist, NfcPlaylistLedState_Error);
107-
while(time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
108-
char popup_text[9];
109-
snprintf(popup_text, 9, "%ds", (time_counter_ms/1000));
110-
popup_set_text(nfc_playlist->popup, popup_text, 64, 50, AlignCenter, AlignTop);
111-
furi_delay_ms(500);
112-
time_counter_ms -= 500;
113-
}
91+
if (EmulationState != NfcPlaylistEmulationState_Emulating) {break;}
92+
93+
char* file_name = strchr(file_path, '/') != NULL ? &strrchr(file_path, '/')[1] : file_path;
94+
char const* file_ext = &strrchr(file_path, '.')[1];
95+
int time_counter_ms = (options_emulate_timeout[nfc_playlist->emulate_timeout]*1000);
96+
97+
if (storage_file_exists(storage, file_path) == false) {
98+
char popup_header_text[(18 + strlen(file_name))];
99+
snprintf(popup_header_text, (18 + strlen(file_name)), "%s\n%s", "ERROR not found:", file_name);
100+
popup_set_header(nfc_playlist->popup, popup_header_text, 64, 10, AlignCenter, AlignTop);
101+
start_blink(nfc_playlist, NfcPlaylistLedState_Error);
102+
while(time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
103+
char popup_text[9];
104+
snprintf(popup_text, 9, "%ds", (time_counter_ms/1000));
105+
popup_set_text(nfc_playlist->popup, popup_text, 64, 50, AlignCenter, AlignTop);
106+
furi_delay_ms(50);
107+
time_counter_ms -= 50;
114108
}
109+
}
115110

116-
else if (strcasestr(file_ext, "nfc") == NULL) {
117-
char popup_header_text[(21 + strlen(file_name))];
118-
snprintf(popup_header_text, (21 + strlen(file_name)), "%s\n%s", "ERROR invalid file:", file_name);
119-
popup_set_header(nfc_playlist->popup, popup_header_text, 64, 10, AlignCenter, AlignTop);
120-
start_blink(nfc_playlist, NfcPlaylistLedState_Error);
121-
while(time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
122-
char popup_text[9];
123-
snprintf(popup_text, 9, "%ds", (time_counter_ms/1000));
124-
popup_set_text(nfc_playlist->popup, popup_text, 64, 50, AlignCenter, AlignTop);
125-
furi_delay_ms(500);
126-
time_counter_ms -= 500;
127-
}
111+
else if (strcasestr(file_ext, "nfc") == NULL) {
112+
char popup_header_text[(21 + strlen(file_name))];
113+
snprintf(popup_header_text, (21 + strlen(file_name)), "%s\n%s", "ERROR invalid file:", file_name);
114+
popup_set_header(nfc_playlist->popup, popup_header_text, 64, 10, AlignCenter, AlignTop);
115+
start_blink(nfc_playlist, NfcPlaylistLedState_Error);
116+
while(time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
117+
char popup_text[9];
118+
snprintf(popup_text, 9, "%ds", (time_counter_ms/1000));
119+
popup_set_text(nfc_playlist->popup, popup_text, 64, 50, AlignCenter, AlignTop);
120+
furi_delay_ms(50);
121+
time_counter_ms -= 50;
128122
}
123+
}
129124

130-
else {
131-
char popup_header_text[(12 + strlen(file_name))];
132-
snprintf(popup_header_text, (12 + strlen(file_name)), "%s\n%s", "Emulating:", file_name);
133-
popup_set_header(nfc_playlist->popup, popup_header_text, 64, 10, AlignCenter, AlignTop);
134-
nfc_playlist_worker_set_nfc_data(nfc_playlist->nfc_playlist_worker, file_path);
135-
nfc_playlist_worker_start(nfc_playlist->nfc_playlist_worker);
136-
start_blink(nfc_playlist, NfcPlaylistLedState_Normal);
137-
while(nfc_playlist_worker_is_emulating(nfc_playlist->nfc_playlist_worker) && time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
138-
char popup_text[9];
139-
snprintf(popup_text, 9, "%ds", (time_counter_ms/1000));
140-
popup_set_text(nfc_playlist->popup, popup_text, 64, 50, AlignCenter, AlignTop);
141-
furi_delay_ms(500);
142-
time_counter_ms -= 500;
143-
}
144-
nfc_playlist_worker_stop(nfc_playlist->nfc_playlist_worker);
125+
else {
126+
char popup_header_text[(12 + strlen(file_name))];
127+
snprintf(popup_header_text, (12 + strlen(file_name)), "%s\n%s", "Emulating:", file_name);
128+
popup_set_header(nfc_playlist->popup, popup_header_text, 64, 10, AlignCenter, AlignTop);
129+
nfc_playlist_worker_set_nfc_data(nfc_playlist->nfc_playlist_worker, file_path);
130+
nfc_playlist_worker_start(nfc_playlist->nfc_playlist_worker);
131+
start_blink(nfc_playlist, NfcPlaylistLedState_Normal);
132+
while(nfc_playlist_worker_is_emulating(nfc_playlist->nfc_playlist_worker) && time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
133+
char popup_text[9];
134+
snprintf(popup_text, 9, "%ds", (time_counter_ms/1000));
135+
popup_set_text(nfc_playlist->popup, popup_text, 64, 50, AlignCenter, AlignTop);
136+
furi_delay_ms(50);
137+
time_counter_ms -= 50;
145138
}
139+
nfc_playlist_worker_stop(nfc_playlist->nfc_playlist_worker);
146140
}
147141
}
148142
popup_reset(nfc_playlist->popup);
@@ -154,10 +148,10 @@ int32_t nfc_playlist_emulation_task(void* context) {
154148
popup_set_header(nfc_playlist->popup, "Failed to open playlist", 64, 10, AlignCenter, AlignTop);
155149
popup_set_text(nfc_playlist->popup, "Press back", 64, 50, AlignCenter, AlignTop);
156150
}
157-
// Free/close resources
151+
158152
furi_string_free(line);
159153
file_stream_close(stream);
160154
stream_free(stream);
161-
// Close storage
155+
162156
return 0;
163157
}

scences/main_menu.c

-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ void nfc_playlist_main_menu_menu_callback(void* context, uint32_t index) {
3232

3333
void nfc_playlist_main_menu_scene_on_enter(void* context) {
3434
NfcPlaylist* nfc_playlist = context;
35-
3635
if (!nfc_playlist->file_selected) {
3736
nfc_playlist->file_selected = true;
3837
scene_manager_next_scene(nfc_playlist->scene_manager, NfcPlaylistScene_FileSelect);

scences/settings.c

+2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ void nfc_playlist_settings_options_change_callback(VariableItem* item) {
6464

6565
void nfc_playlist_settings_scene_on_enter(void* context) {
6666
NfcPlaylist* nfc_playlist = context;
67+
6768
variable_item_list_set_header(nfc_playlist->variable_item_list, "Settings");
6869

6970
VariableItem* emulation_timeout_settings = variable_item_list_add(
@@ -100,6 +101,7 @@ void nfc_playlist_settings_scene_on_enter(void* context) {
100101
variable_item_list_add(nfc_playlist->variable_item_list, "Reset settings", 0, NULL, NULL);
101102

102103
variable_item_list_set_enter_callback(nfc_playlist->variable_item_list, nfc_playlist_settings_menu_callback, nfc_playlist);
104+
103105
view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Settings);
104106
}
105107

0 commit comments

Comments
 (0)