Skip to content

Commit 0d7fc57

Browse files
committed
Update nfc_playlist.c
- Adds timer - Trims most of the useless information form the file name
1 parent 4e8be3d commit 0d7fc57

File tree

1 file changed

+50
-61
lines changed

1 file changed

+50
-61
lines changed

nfc_playlist.c

Lines changed: 50 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -10,78 +10,76 @@
1010
#include <gui/gui.h>
1111
#include <gui/view_dispatcher.h>
1212
#include <gui/scene_manager.h>
13-
#include <gui/modules/menu.h>
1413
#include <gui/modules/submenu.h>
1514
#include <gui/modules/popup.h>
1615

1716
// Define log tag
1817
#define TAG "NfcPlaylist"
1918

20-
/** ids for all scenes used by the app */
19+
// IDs for all scenes used by the app
2120
typedef enum {
2221
NfcPlaylistScene_MainMenu,
23-
NfcPlaylistScene_FirstPopup,
22+
NfcPlaylistScene_EmulatingPopup,
2423
NfcPlaylistScene_count
2524
} NfcPlaylistScene;
2625

27-
/** ids for the 2 types of view used by the app */
26+
// IDs for the view used by the app
2827
typedef enum { NfcPlaylistView_Menu, NfcPlaylistView_Popup } NfcPlaylistView;
2928

30-
/** the app context struct */
29+
// The app context struct
3130
typedef struct {
3231
SceneManager* scene_manager;
3332
ViewDispatcher* view_dispatcher;
34-
Submenu* menu;
33+
Submenu* submenu;
3534
Popup* popup;
3635
NfcPlaylistWorker* nfc_worker;
3736
int emulate_timeout;
38-
bool cancel_emulate;
3937
} NfcPlaylist;
4038

41-
/** all custom events */
42-
typedef enum { NfcPlaylistEvent_ShowPopupOne } NfcPlaylistEvent;
39+
// All custom events
40+
typedef enum { NfcPlaylistEvent_ShowEmulatingPopup } NfcPlaylistEvent;
4341

4442
/* main menu scene */
4543

46-
/** indices for menu items */
47-
typedef enum { NfcPlaylistMenuSelection_One } NfcPlaylistMenuSelection;
44+
// Indices for menu items
45+
typedef enum { NfcPlaylistMenuSelection_Start, NfcPlaylistMenuSelection_Settings } NfcPlaylistMenuSelection;
4846

49-
/** main menu callback - sends a custom event to the scene manager based on the menu selection */
47+
// Main menu callback - sends a custom event to the scene manager based on the menu selection
5048
void nfc_playlist_menu_callback_main_menu(void* context, uint32_t index) {
5149
FURI_LOG_T(TAG, "nfc_playlist_menu_callback_main_menu");
5250
NfcPlaylist* app = context;
5351
switch(index) {
54-
case NfcPlaylistMenuSelection_One:
55-
scene_manager_handle_custom_event(app->scene_manager, NfcPlaylistEvent_ShowPopupOne);
52+
case NfcPlaylistMenuSelection_Start:
53+
scene_manager_handle_custom_event(app->scene_manager, NfcPlaylistEvent_ShowEmulatingPopup);
5654
break;
5755
}
5856
}
5957

60-
/** resets the menu, gives it content, callbacks and selection enums */
58+
// Resets the menu, gives it content, callbacks and selection enums
6159
void nfc_playlist_scene_on_enter_main_menu(void* context) {
6260
FURI_LOG_T(TAG, "nfc_playlist_scene_on_enter_main_menu");
6361
NfcPlaylist* app = context;
64-
submenu_reset(app->menu);
65-
submenu_set_header(app->menu, "NFC Playlist");
62+
submenu_reset(app->submenu);
63+
submenu_set_header(app->submenu, "NFC Playlist");
6664
submenu_add_item(
67-
app->menu,
65+
app->submenu,
6866
"Start",
69-
NfcPlaylistMenuSelection_One,
67+
NfcPlaylistMenuSelection_Start,
7068
nfc_playlist_menu_callback_main_menu,
7169
app);
7270
view_dispatcher_switch_to_view(app->view_dispatcher, NfcPlaylistView_Menu);
7371
}
7472

75-
/** main menu event handler - switches scene based on the event */
73+
// Main menu event handler - switches scene based on the event
7674
bool nfc_playlist_scene_on_event_main_menu(void* context, SceneManagerEvent event) {
7775
FURI_LOG_T(TAG, "nfc_playlist_scene_on_event_main_menu");
7876
NfcPlaylist* app = context;
7977
bool consumed = false;
8078
switch(event.type) {
8179
case SceneManagerEventTypeCustom:
8280
switch(event.event) {
83-
case NfcPlaylistEvent_ShowPopupOne:
84-
scene_manager_next_scene(app->scene_manager, NfcPlaylistScene_FirstPopup);
81+
case NfcPlaylistEvent_ShowEmulatingPopup:
82+
scene_manager_next_scene(app->scene_manager, NfcPlaylistScene_EmulatingPopup);
8583
consumed = true;
8684
break;
8785
}
@@ -96,11 +94,10 @@ bool nfc_playlist_scene_on_event_main_menu(void* context, SceneManagerEvent even
9694
void nfc_playlist_scene_on_exit_main_menu(void* context) {
9795
FURI_LOG_T(TAG, "nfc_playlist_scene_on_exit_main_menu");
9896
NfcPlaylist* app = context;
99-
submenu_reset(app->menu);
97+
submenu_reset(app->submenu);
10098
}
10199

102-
/* popup 1 scene */
103-
100+
// Emulating scene
104101
void nfc_playlist_scene_on_enter_popup_emulating(void* context) {
105102
FURI_LOG_T(TAG, "nfc_playlist_scene_on_enter_popup_emulating");
106103
NfcPlaylist* app = context;
@@ -117,31 +114,31 @@ void nfc_playlist_scene_on_enter_popup_emulating(void* context) {
117114
popup_set_header(app->popup, "Emulating", 64, 10, AlignCenter, AlignTop);
118115
view_dispatcher_switch_to_view(app->view_dispatcher, NfcPlaylistView_Popup);
119116
// read the file line by line and print the text
120-
while(stream_read_line(stream, line) && !app->cancel_emulate) {
121-
117+
while(stream_read_line(stream, line)) {
122118
char* file_path = (char*)furi_string_get_cstr(line);
123-
char* file_name = strrchr(file_path, (int)"/");
124-
125-
popup_set_text(app->popup, file_name, 64, 30, AlignCenter, AlignTop);
119+
char* file_name = &strrchr(file_path, '/')[1];
120+
int time_counter_ms = app->emulate_timeout;
126121

127122
nfc_playlist_worker_set_nfc_data(app->nfc_worker, file_path);
128123
nfc_playlist_worker_start(app->nfc_worker);
129124

130-
int time_counter_ms = app->emulate_timeout;
131-
while(nfc_playlist_worker_is_emulating(app->nfc_worker)) {
132-
furi_delay_ms(50);
133-
time_counter_ms -= 50;
125+
do {
126+
int size = (strlen(file_name) + 4);
127+
char display_text[size];
128+
snprintf(display_text, size, "%s\n%ds", file_name, (time_counter_ms/1000));
129+
popup_set_text(app->popup, display_text, 64, 25, AlignCenter, AlignTop);
130+
furi_delay_ms(500);
131+
time_counter_ms -= 500;
134132
if (time_counter_ms <= 0) {
135133
break;
136134
}
137-
}
135+
} while(nfc_playlist_worker_is_emulating(app->nfc_worker));
138136

139137
if (nfc_playlist_worker_is_emulating(app->nfc_worker)) {
140138
nfc_playlist_worker_stop(app->nfc_worker);
141139
}
142140

143141
furi_string_reset(line);
144-
145142
}
146143
} else {
147144
FURI_LOG_E(TAG, "Failed to open file");
@@ -152,7 +149,6 @@ void nfc_playlist_scene_on_enter_popup_emulating(void* context) {
152149
stream_free(stream);
153150
nfc_playlist_worker_free(app->nfc_worker);
154151
app->nfc_worker = NULL;
155-
app->cancel_emulate = false;
156152
// Close storage
157153
furi_record_close(RECORD_STORAGE);
158154

@@ -170,87 +166,80 @@ bool nfc_playlist_scene_on_event_popup_emulating(void* context, SceneManagerEven
170166
void nfc_playlist_scene_on_exit_popup_emulating(void* context) {
171167
FURI_LOG_T(TAG, "nfc_playlist_scene_on_exit_popup_emulating");
172168
NfcPlaylist* app = context;
173-
if (nfc_playlist_worker_is_emulating(app->nfc_worker)) {
174-
nfc_playlist_worker_stop(app->nfc_worker);
175-
app->cancel_emulate = true;
176-
}
177169
popup_reset(app->popup);
178170
}
179171

180-
/** collection of all scene on_enter handlers - in the same order as their enum */
172+
// Collection of all scene on_enter handlers - in the same order as their enum
181173
void (*const nfc_playlist_scene_on_enter_handlers[])(void*) = {
182174
nfc_playlist_scene_on_enter_main_menu,
183175
nfc_playlist_scene_on_enter_popup_emulating};
184176

185-
/** collection of all scene on event handlers - in the same order as their enum */
177+
// Collection of all scene on event handlers - in the same order as their enum
186178
bool (*const nfc_playlist_scene_on_event_handlers[])(void*, SceneManagerEvent) = {
187179
nfc_playlist_scene_on_event_main_menu,
188180
nfc_playlist_scene_on_event_popup_emulating};
189181

190-
/** collection of all scene on exit handlers - in the same order as their enum */
182+
// Collection of all scene on exit handlers - in the same order as their enum
191183
void (*const nfc_playlist_scene_on_exit_handlers[])(void*) = {
192184
nfc_playlist_scene_on_exit_main_menu,
193185
nfc_playlist_scene_on_exit_popup_emulating};
194186

195-
/** collection of all on_enter, on_event, on_exit handlers */
187+
// Collection of all on_enter, on_event, on_exit handlers */
196188
const SceneManagerHandlers nfc_playlist_scene_event_handlers = {
197189
.on_enter_handlers = nfc_playlist_scene_on_enter_handlers,
198190
.on_event_handlers = nfc_playlist_scene_on_event_handlers,
199191
.on_exit_handlers = nfc_playlist_scene_on_exit_handlers,
200192
.scene_num = NfcPlaylistScene_count};
201193

202-
/** custom event handler - passes the event to the scene manager */
194+
// Custom event handler - passes the event to the scene manager
203195
bool nfc_playlist_scene_manager_custom_event_callback(void* context, uint32_t custom_event) {
204196
FURI_LOG_T(TAG, "nfc_playlist_scene_manager_custom_event_callback");
205197
furi_assert(context);
206198
NfcPlaylist* app = context;
207199
return scene_manager_handle_custom_event(app->scene_manager, custom_event);
208200
}
209201

210-
/** navigation event handler - passes the event to the scene manager */
202+
// Navigation event handler - passes the event to the scene manager
211203
bool nfc_playlist_scene_manager_navigation_event_callback(void* context) {
212204
FURI_LOG_T(TAG, "nfc_playlist_scene_manager_navigation_event_callback");
213205
furi_assert(context);
214206
NfcPlaylist* app = context;
215207
return scene_manager_handle_back_event(app->scene_manager);
216208
}
217209

218-
/** initialise the scene manager with all handlers */
210+
// Initialise the scene manager with all handlers
219211
void nfc_playlist_scene_manager_init(NfcPlaylist* app) {
220212
FURI_LOG_T(TAG, "nfc_playlist_scene_manager_init");
221213
app->scene_manager = scene_manager_alloc(&nfc_playlist_scene_event_handlers, app);
222214
}
223215

224-
/** initialise the views, and initialise the view dispatcher with all views */
216+
// Initialise the views, and initialise the view dispatcher with all views
225217
void nfc_playlist_view_dispatcher_init(NfcPlaylist* app) {
226218
FURI_LOG_T(TAG, "nfc_playlist_view_dispatcher_init");
227219
app->view_dispatcher = view_dispatcher_alloc();
228220
view_dispatcher_enable_queue(app->view_dispatcher);
229221

230222
// allocate each view
231223
FURI_LOG_D(TAG, "nfc_playlist_view_dispatcher_init allocating views");
232-
app->menu = submenu_alloc();
224+
app->submenu = submenu_alloc();
233225
app->popup = popup_alloc();
234-
app->emulate_timeout = 2000;
235-
app->cancel_emulate = false;
226+
app->emulate_timeout = 4000;
236227

237228
// assign callback that pass events from views to the scene manager
238229
FURI_LOG_D(TAG, "nfc_playlist_view_dispatcher_init setting callbacks");
239230
view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
240-
view_dispatcher_set_custom_event_callback(
241-
app->view_dispatcher, nfc_playlist_scene_manager_custom_event_callback);
242-
view_dispatcher_set_navigation_event_callback(
243-
app->view_dispatcher, nfc_playlist_scene_manager_navigation_event_callback);
231+
view_dispatcher_set_custom_event_callback( app->view_dispatcher, nfc_playlist_scene_manager_custom_event_callback);
232+
view_dispatcher_set_navigation_event_callback(app->view_dispatcher, nfc_playlist_scene_manager_navigation_event_callback);
244233

245234
// add views to the dispatcher, indexed by their enum value
246235
FURI_LOG_D(TAG, "nfc_playlist_view_dispatcher_init adding view menu");
247-
view_dispatcher_add_view(app->view_dispatcher, NfcPlaylistView_Menu, submenu_get_view(app->menu));
236+
view_dispatcher_add_view(app->view_dispatcher, NfcPlaylistView_Menu, submenu_get_view(app->submenu));
248237

249238
FURI_LOG_D(TAG, "nfc_playlist_view_dispatcher_init adding view popup");
250239
view_dispatcher_add_view(app->view_dispatcher, NfcPlaylistView_Popup, popup_get_view(app->popup));
251240
}
252241

253-
/** initialise app data, scene manager, and view dispatcher */
242+
// Initialise app data, scene manager, and view dispatcher
254243
NfcPlaylist* nfc_playlist_init() {
255244
FURI_LOG_T(TAG, "nfc_playlist_init");
256245
NfcPlaylist* app = malloc(sizeof(NfcPlaylist));
@@ -259,19 +248,19 @@ NfcPlaylist* nfc_playlist_init() {
259248
return app;
260249
}
261250

262-
/** free all app data, scene manager, and view dispatcher */
251+
// Free all app data, scene manager, and view dispatcher
263252
void nfc_playlist_free(NfcPlaylist* app) {
264253
FURI_LOG_T(TAG, "nfc_playlist_free");
265254
scene_manager_free(app->scene_manager);
266255
view_dispatcher_remove_view(app->view_dispatcher, NfcPlaylistView_Menu);
267256
view_dispatcher_remove_view(app->view_dispatcher, NfcPlaylistView_Popup);
268257
view_dispatcher_free(app->view_dispatcher);
269-
submenu_free(app->menu);
258+
submenu_free(app->submenu);
270259
popup_free(app->popup);
271260
free(app);
272261
}
273262

274-
/** go to trace log level in the dev environment */
263+
// Go to trace log level in the dev environment
275264
void nfc_playlist_set_log_level() {
276265
#ifdef FURI_DEBUG
277266
furi_log_set_level(FuriLogLevelTrace);

0 commit comments

Comments
 (0)