10
10
#include <gui/gui.h>
11
11
#include <gui/view_dispatcher.h>
12
12
#include <gui/scene_manager.h>
13
- #include <gui/modules/menu.h>
14
13
#include <gui/modules/submenu.h>
15
14
#include <gui/modules/popup.h>
16
15
17
16
// Define log tag
18
17
#define TAG "NfcPlaylist"
19
18
20
- /** ids for all scenes used by the app */
19
+ // IDs for all scenes used by the app
21
20
typedef enum {
22
21
NfcPlaylistScene_MainMenu ,
23
- NfcPlaylistScene_FirstPopup ,
22
+ NfcPlaylistScene_EmulatingPopup ,
24
23
NfcPlaylistScene_count
25
24
} NfcPlaylistScene ;
26
25
27
- /** ids for the 2 types of view used by the app */
26
+ // IDs for the view used by the app
28
27
typedef enum { NfcPlaylistView_Menu , NfcPlaylistView_Popup } NfcPlaylistView ;
29
28
30
- /** the app context struct */
29
+ // The app context struct
31
30
typedef struct {
32
31
SceneManager * scene_manager ;
33
32
ViewDispatcher * view_dispatcher ;
34
- Submenu * menu ;
33
+ Submenu * submenu ;
35
34
Popup * popup ;
36
35
NfcPlaylistWorker * nfc_worker ;
37
36
int emulate_timeout ;
38
- bool cancel_emulate ;
39
37
} NfcPlaylist ;
40
38
41
- /** all custom events */
42
- typedef enum { NfcPlaylistEvent_ShowPopupOne } NfcPlaylistEvent ;
39
+ // All custom events
40
+ typedef enum { NfcPlaylistEvent_ShowEmulatingPopup } NfcPlaylistEvent ;
43
41
44
42
/* main menu scene */
45
43
46
- /** indices for menu items */
47
- typedef enum { NfcPlaylistMenuSelection_One } NfcPlaylistMenuSelection ;
44
+ // Indices for menu items
45
+ typedef enum { NfcPlaylistMenuSelection_Start , NfcPlaylistMenuSelection_Settings } NfcPlaylistMenuSelection ;
48
46
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
50
48
void nfc_playlist_menu_callback_main_menu (void * context , uint32_t index ) {
51
49
FURI_LOG_T (TAG , "nfc_playlist_menu_callback_main_menu" );
52
50
NfcPlaylist * app = context ;
53
51
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 );
56
54
break ;
57
55
}
58
56
}
59
57
60
- /** resets the menu, gives it content, callbacks and selection enums */
58
+ // Resets the menu, gives it content, callbacks and selection enums
61
59
void nfc_playlist_scene_on_enter_main_menu (void * context ) {
62
60
FURI_LOG_T (TAG , "nfc_playlist_scene_on_enter_main_menu" );
63
61
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" );
66
64
submenu_add_item (
67
- app -> menu ,
65
+ app -> submenu ,
68
66
"Start" ,
69
- NfcPlaylistMenuSelection_One ,
67
+ NfcPlaylistMenuSelection_Start ,
70
68
nfc_playlist_menu_callback_main_menu ,
71
69
app );
72
70
view_dispatcher_switch_to_view (app -> view_dispatcher , NfcPlaylistView_Menu );
73
71
}
74
72
75
- /** main menu event handler - switches scene based on the event */
73
+ // Main menu event handler - switches scene based on the event
76
74
bool nfc_playlist_scene_on_event_main_menu (void * context , SceneManagerEvent event ) {
77
75
FURI_LOG_T (TAG , "nfc_playlist_scene_on_event_main_menu" );
78
76
NfcPlaylist * app = context ;
79
77
bool consumed = false;
80
78
switch (event .type ) {
81
79
case SceneManagerEventTypeCustom :
82
80
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 );
85
83
consumed = true;
86
84
break ;
87
85
}
@@ -96,11 +94,10 @@ bool nfc_playlist_scene_on_event_main_menu(void* context, SceneManagerEvent even
96
94
void nfc_playlist_scene_on_exit_main_menu (void * context ) {
97
95
FURI_LOG_T (TAG , "nfc_playlist_scene_on_exit_main_menu" );
98
96
NfcPlaylist * app = context ;
99
- submenu_reset (app -> menu );
97
+ submenu_reset (app -> submenu );
100
98
}
101
99
102
- /* popup 1 scene */
103
-
100
+ // Emulating scene
104
101
void nfc_playlist_scene_on_enter_popup_emulating (void * context ) {
105
102
FURI_LOG_T (TAG , "nfc_playlist_scene_on_enter_popup_emulating" );
106
103
NfcPlaylist * app = context ;
@@ -117,31 +114,31 @@ void nfc_playlist_scene_on_enter_popup_emulating(void* context) {
117
114
popup_set_header (app -> popup , "Emulating" , 64 , 10 , AlignCenter , AlignTop );
118
115
view_dispatcher_switch_to_view (app -> view_dispatcher , NfcPlaylistView_Popup );
119
116
// 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 )) {
122
118
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 ;
126
121
127
122
nfc_playlist_worker_set_nfc_data (app -> nfc_worker , file_path );
128
123
nfc_playlist_worker_start (app -> nfc_worker );
129
124
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 ;
134
132
if (time_counter_ms <= 0 ) {
135
133
break ;
136
134
}
137
- }
135
+ } while ( nfc_playlist_worker_is_emulating ( app -> nfc_worker ));
138
136
139
137
if (nfc_playlist_worker_is_emulating (app -> nfc_worker )) {
140
138
nfc_playlist_worker_stop (app -> nfc_worker );
141
139
}
142
140
143
141
furi_string_reset (line );
144
-
145
142
}
146
143
} else {
147
144
FURI_LOG_E (TAG , "Failed to open file" );
@@ -152,7 +149,6 @@ void nfc_playlist_scene_on_enter_popup_emulating(void* context) {
152
149
stream_free (stream );
153
150
nfc_playlist_worker_free (app -> nfc_worker );
154
151
app -> nfc_worker = NULL ;
155
- app -> cancel_emulate = false;
156
152
// Close storage
157
153
furi_record_close (RECORD_STORAGE );
158
154
@@ -170,87 +166,80 @@ bool nfc_playlist_scene_on_event_popup_emulating(void* context, SceneManagerEven
170
166
void nfc_playlist_scene_on_exit_popup_emulating (void * context ) {
171
167
FURI_LOG_T (TAG , "nfc_playlist_scene_on_exit_popup_emulating" );
172
168
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
- }
177
169
popup_reset (app -> popup );
178
170
}
179
171
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
181
173
void (* const nfc_playlist_scene_on_enter_handlers [])(void * ) = {
182
174
nfc_playlist_scene_on_enter_main_menu ,
183
175
nfc_playlist_scene_on_enter_popup_emulating };
184
176
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
186
178
bool (* const nfc_playlist_scene_on_event_handlers [])(void * , SceneManagerEvent ) = {
187
179
nfc_playlist_scene_on_event_main_menu ,
188
180
nfc_playlist_scene_on_event_popup_emulating };
189
181
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
191
183
void (* const nfc_playlist_scene_on_exit_handlers [])(void * ) = {
192
184
nfc_playlist_scene_on_exit_main_menu ,
193
185
nfc_playlist_scene_on_exit_popup_emulating };
194
186
195
- /** collection of all on_enter, on_event, on_exit handlers */
187
+ // Collection of all on_enter, on_event, on_exit handlers */
196
188
const SceneManagerHandlers nfc_playlist_scene_event_handlers = {
197
189
.on_enter_handlers = nfc_playlist_scene_on_enter_handlers ,
198
190
.on_event_handlers = nfc_playlist_scene_on_event_handlers ,
199
191
.on_exit_handlers = nfc_playlist_scene_on_exit_handlers ,
200
192
.scene_num = NfcPlaylistScene_count };
201
193
202
- /** custom event handler - passes the event to the scene manager */
194
+ // Custom event handler - passes the event to the scene manager
203
195
bool nfc_playlist_scene_manager_custom_event_callback (void * context , uint32_t custom_event ) {
204
196
FURI_LOG_T (TAG , "nfc_playlist_scene_manager_custom_event_callback" );
205
197
furi_assert (context );
206
198
NfcPlaylist * app = context ;
207
199
return scene_manager_handle_custom_event (app -> scene_manager , custom_event );
208
200
}
209
201
210
- /** navigation event handler - passes the event to the scene manager */
202
+ // Navigation event handler - passes the event to the scene manager
211
203
bool nfc_playlist_scene_manager_navigation_event_callback (void * context ) {
212
204
FURI_LOG_T (TAG , "nfc_playlist_scene_manager_navigation_event_callback" );
213
205
furi_assert (context );
214
206
NfcPlaylist * app = context ;
215
207
return scene_manager_handle_back_event (app -> scene_manager );
216
208
}
217
209
218
- /** initialise the scene manager with all handlers */
210
+ // Initialise the scene manager with all handlers
219
211
void nfc_playlist_scene_manager_init (NfcPlaylist * app ) {
220
212
FURI_LOG_T (TAG , "nfc_playlist_scene_manager_init" );
221
213
app -> scene_manager = scene_manager_alloc (& nfc_playlist_scene_event_handlers , app );
222
214
}
223
215
224
- /** initialise the views, and initialise the view dispatcher with all views */
216
+ // Initialise the views, and initialise the view dispatcher with all views
225
217
void nfc_playlist_view_dispatcher_init (NfcPlaylist * app ) {
226
218
FURI_LOG_T (TAG , "nfc_playlist_view_dispatcher_init" );
227
219
app -> view_dispatcher = view_dispatcher_alloc ();
228
220
view_dispatcher_enable_queue (app -> view_dispatcher );
229
221
230
222
// allocate each view
231
223
FURI_LOG_D (TAG , "nfc_playlist_view_dispatcher_init allocating views" );
232
- app -> menu = submenu_alloc ();
224
+ app -> submenu = submenu_alloc ();
233
225
app -> popup = popup_alloc ();
234
- app -> emulate_timeout = 2000 ;
235
- app -> cancel_emulate = false;
226
+ app -> emulate_timeout = 4000 ;
236
227
237
228
// assign callback that pass events from views to the scene manager
238
229
FURI_LOG_D (TAG , "nfc_playlist_view_dispatcher_init setting callbacks" );
239
230
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 );
244
233
245
234
// add views to the dispatcher, indexed by their enum value
246
235
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 ));
248
237
249
238
FURI_LOG_D (TAG , "nfc_playlist_view_dispatcher_init adding view popup" );
250
239
view_dispatcher_add_view (app -> view_dispatcher , NfcPlaylistView_Popup , popup_get_view (app -> popup ));
251
240
}
252
241
253
- /** initialise app data, scene manager, and view dispatcher */
242
+ // Initialise app data, scene manager, and view dispatcher
254
243
NfcPlaylist * nfc_playlist_init () {
255
244
FURI_LOG_T (TAG , "nfc_playlist_init" );
256
245
NfcPlaylist * app = malloc (sizeof (NfcPlaylist ));
@@ -259,19 +248,19 @@ NfcPlaylist* nfc_playlist_init() {
259
248
return app ;
260
249
}
261
250
262
- /** free all app data, scene manager, and view dispatcher */
251
+ // Free all app data, scene manager, and view dispatcher
263
252
void nfc_playlist_free (NfcPlaylist * app ) {
264
253
FURI_LOG_T (TAG , "nfc_playlist_free" );
265
254
scene_manager_free (app -> scene_manager );
266
255
view_dispatcher_remove_view (app -> view_dispatcher , NfcPlaylistView_Menu );
267
256
view_dispatcher_remove_view (app -> view_dispatcher , NfcPlaylistView_Popup );
268
257
view_dispatcher_free (app -> view_dispatcher );
269
- submenu_free (app -> menu );
258
+ submenu_free (app -> submenu );
270
259
popup_free (app -> popup );
271
260
free (app );
272
261
}
273
262
274
- /** go to trace log level in the dev environment */
263
+ // Go to trace log level in the dev environment
275
264
void nfc_playlist_set_log_level () {
276
265
#ifdef FURI_DEBUG
277
266
furi_log_set_level (FuriLogLevelTrace );
0 commit comments