1
1
#include "nfc_playlist.h"
2
2
#include "scences/emulation.h"
3
3
4
+ bool cancel = false;
5
+
4
6
void nfc_playlist_emulation_scene_on_enter (void * context ) {
5
7
NfcPlaylist * nfc_playlist = context ;
8
+ nfc_playlist_emulation_setup (nfc_playlist );
9
+ nfc_playlist_emulation_start (nfc_playlist );
10
+ }
11
+
12
+ bool nfc_playlist_emulation_scene_on_event (void * context , SceneManagerEvent event ) {
13
+ NfcPlaylist * nfc_playlist = context ;
14
+ UNUSED (nfc_playlist );
15
+ FURI_LOG_RAW_I ("nfc_playlist_emulation_scene_on_event: %ld" , event .event );
16
+ switch (event .event ) {
17
+ case 0 :
18
+ if (nfc_playlist_worker_is_emulating (nfc_playlist -> nfc_playlist_worker ) && cancel != true) {
19
+ cancel = true;
20
+ return true;
21
+ }
22
+ default :
23
+ break ;
24
+ }
25
+
26
+ return false;
27
+ }
28
+
29
+ void nfc_playlist_emulation_scene_on_exit (void * context ) {
30
+ NfcPlaylist * nfc_playlist = context ;
31
+ cancel = false;
32
+ nfc_playlist_emulation_stop (nfc_playlist );
33
+ nfc_playlist_emulation_free (nfc_playlist );
34
+ popup_reset (nfc_playlist -> popup );
35
+ }
36
+
37
+ void nfc_playlist_emulation_setup (void * context ) {
38
+ NfcPlaylist * nfc_playlist = context ;
39
+
40
+ nfc_playlist -> thread = furi_thread_alloc_ex (
41
+ "NfcPlaylistEmulationWorker" , 8192 , nfc_playlist_emulation_task , nfc_playlist );
42
+ nfc_playlist -> nfc_playlist_worker = nfc_playlist_worker_alloc ();
43
+ }
44
+
45
+ void nfc_playlist_emulation_free (NfcPlaylist * nfc_playlist ) {
46
+ furi_assert (nfc_playlist );
47
+ furi_thread_free (nfc_playlist -> thread );
48
+ nfc_playlist_worker_free (nfc_playlist -> nfc_playlist_worker );
49
+ nfc_playlist -> thread = NULL ;
50
+ nfc_playlist -> nfc_playlist_worker = NULL ;
51
+ }
52
+
53
+ void nfc_playlist_emulation_start (NfcPlaylist * nfc_playlist ) {
54
+ furi_assert (nfc_playlist );
55
+ furi_thread_start (nfc_playlist -> thread );
56
+ }
57
+
58
+ void nfc_playlist_emulation_stop (NfcPlaylist * nfc_playlist ) {
59
+ furi_assert (nfc_playlist );
60
+ furi_thread_join (nfc_playlist -> thread );
61
+ }
6
62
63
+ int32_t nfc_playlist_emulation_task (void * context ) {
64
+ NfcPlaylist * nfc_playlist = context ;
7
65
// open/alloc resources
8
66
Storage * storage = furi_record_open (RECORD_STORAGE );
9
67
Stream * stream = file_stream_alloc (storage );
10
68
FuriString * line = furi_string_alloc ();
11
- NfcPlaylistWorker * nfc_worker = nfc_playlist_worker_alloc ();
69
+
70
+ popup_reset (nfc_playlist -> popup );
71
+ popup_set_context (nfc_playlist -> popup , nfc_playlist );
72
+ view_dispatcher_switch_to_view (nfc_playlist -> view_dispatcher , NfcPlaylistView_Popup );
73
+
12
74
// Read file
13
75
if (file_stream_open (stream , APP_DATA_PATH ("playlist.txt" ), FSAM_READ , FSOM_OPEN_EXISTING )) {
14
- popup_reset (nfc_playlist -> popup );
15
- popup_set_context (nfc_playlist -> popup , nfc_playlist );
76
+
16
77
popup_set_header (nfc_playlist -> popup , "Emulating:" , 64 , 10 , AlignCenter , AlignTop );
17
- view_dispatcher_switch_to_view (nfc_playlist -> view_dispatcher , NfcPlaylistView_Popup );
18
78
19
79
int file_position = 0 ;
20
80
// read the file line by line and print the text
21
- while (stream_read_line (stream , line )) {
81
+ while (stream_read_line (stream , line ) && cancel == false ) {
22
82
if (options_emulate_delay [nfc_playlist -> emulate_delay ] > 0 ) {
23
83
if (file_position > 0 ) {
24
84
int time_counter_delay_ms = options_emulate_delay [nfc_playlist -> emulate_delay ];
@@ -28,7 +88,7 @@ void nfc_playlist_emulation_scene_on_enter(void* context) {
28
88
popup_set_text (nfc_playlist -> popup , display_text , 64 , 25 , AlignCenter , AlignTop );
29
89
furi_delay_ms (500 );
30
90
time_counter_delay_ms -= 500 ;
31
- } while (time_counter_delay_ms > 0 );
91
+ } while (time_counter_delay_ms > 0 && cancel == false );
32
92
} else {
33
93
file_position ++ ;
34
94
}
@@ -50,8 +110,8 @@ void nfc_playlist_emulation_scene_on_enter(void* context) {
50
110
time_counter_ms -= 500 ;
51
111
} while (time_counter_ms > 0 );
52
112
} else {
53
- nfc_playlist_worker_set_nfc_data (nfc_worker , file_path );
54
- nfc_playlist_worker_start (nfc_worker );
113
+ nfc_playlist_worker_set_nfc_data (nfc_playlist -> nfc_playlist_worker , file_path );
114
+ nfc_playlist_worker_start (nfc_playlist -> nfc_playlist_worker );
55
115
56
116
int popup_text_size = (strlen (file_name ) + 4 );
57
117
char popup_text [popup_text_size ];
@@ -61,38 +121,22 @@ void nfc_playlist_emulation_scene_on_enter(void* context) {
61
121
popup_set_text (nfc_playlist -> popup , popup_text , 64 , 25 , AlignCenter , AlignTop );
62
122
furi_delay_ms (500 );
63
123
time_counter_ms -= 500 ;
64
- } while (nfc_playlist_worker_is_emulating (nfc_worker ) && time_counter_ms > 0 );
124
+ } while (nfc_playlist_worker_is_emulating (nfc_playlist -> nfc_playlist_worker ) && time_counter_ms > 0 && cancel == false );
65
125
66
- if (nfc_playlist_worker_is_emulating (nfc_worker )) {
67
- nfc_playlist_worker_stop (nfc_worker );
68
- }
126
+ nfc_playlist_worker_stop (nfc_playlist -> nfc_playlist_worker );
69
127
}
70
128
}
71
129
popup_reset (nfc_playlist -> popup );
72
- scene_manager_previous_scene (nfc_playlist -> scene_manager );
130
+ popup_set_header (nfc_playlist -> popup , "Emulation finished" , 64 , 10 , AlignCenter , AlignTop );
131
+ popup_set_text (nfc_playlist -> popup , "Press back" , 64 , 25 , AlignCenter , AlignTop );
73
132
} else {
74
- popup_reset (nfc_playlist -> popup );
75
- popup_set_context (nfc_playlist -> popup , nfc_playlist );
76
133
popup_set_header (nfc_playlist -> popup , "Error:" , 64 , 10 , AlignCenter , AlignTop );
77
134
popup_set_text (nfc_playlist -> popup , "Failed to open file\n/ext/apps_data/nfc_playlist/playlist.txt" , 64 , 25 , AlignCenter , AlignTop );
78
- view_dispatcher_switch_to_view (nfc_playlist -> view_dispatcher , NfcPlaylistView_Popup );
79
135
}
80
136
// Free/close resources
81
137
furi_string_free (line );
82
138
file_stream_close (stream );
83
139
stream_free (stream );
84
- nfc_playlist_worker_free (nfc_worker );
85
- // Close storage
86
- furi_record_close (RECORD_STORAGE );
87
- }
88
-
89
- bool nfc_playlist_emulation_scene_on_event (void * context , SceneManagerEvent event ) {
90
- UNUSED (context );
91
- UNUSED (event );
92
- return false;
93
- }
94
-
95
- void nfc_playlist_emulation_scene_on_exit (void * context ) {
96
- NfcPlaylist * nfc_playlist = context ;
97
- popup_reset (nfc_playlist -> popup );
140
+ // Close storage
141
+ return 0 ;
98
142
}
0 commit comments