Skip to content

Commit a9ae66c

Browse files
committed
Stop early
- Adds a basic way of adding the ability to exit early
1 parent 85b58f3 commit a9ae66c

File tree

3 files changed

+85
-31
lines changed

3 files changed

+85
-31
lines changed

nfc_playlist.h

+4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#pragma once
22
#include <furi.h>
3+
#include <furi_hal.h>
34
#include <string.h>
45
#include <gui/gui.h>
56
#include <gui/view_dispatcher.h>
67
#include <gui/scene_manager.h>
78
#include <gui/modules/popup.h>
89
#include <gui/modules/variable_item_list.h>
10+
#include <lib/worker/nfc_playlist_worker.h>
911

1012
typedef enum {
1113
NfcPlaylistView_Menu,
@@ -23,6 +25,8 @@ typedef struct {
2325
ViewDispatcher* view_dispatcher;
2426
VariableItemList* variable_item_list;
2527
Popup* popup;
28+
FuriThread* thread;
29+
NfcPlaylistWorker* nfc_playlist_worker;
2630
uint8_t emulate_timeout;
2731
uint8_t emulate_delay;
2832
} NfcPlaylist;

scences/emulation.c

+74-30
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,84 @@
11
#include "nfc_playlist.h"
22
#include "scences/emulation.h"
33

4+
bool cancel = false;
5+
46
void nfc_playlist_emulation_scene_on_enter(void* context) {
57
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+
}
662

63+
int32_t nfc_playlist_emulation_task(void* context) {
64+
NfcPlaylist* nfc_playlist = context;
765
// open/alloc resources
866
Storage* storage = furi_record_open(RECORD_STORAGE);
967
Stream* stream = file_stream_alloc(storage);
1068
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+
1274
// Read file
1375
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+
1677
popup_set_header(nfc_playlist->popup, "Emulating:", 64, 10, AlignCenter, AlignTop);
17-
view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Popup);
1878

1979
int file_position = 0;
2080
// 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) {
2282
if (options_emulate_delay[nfc_playlist->emulate_delay] > 0) {
2383
if (file_position > 0) {
2484
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) {
2888
popup_set_text(nfc_playlist->popup, display_text, 64, 25, AlignCenter, AlignTop);
2989
furi_delay_ms(500);
3090
time_counter_delay_ms -= 500;
31-
} while(time_counter_delay_ms > 0);
91+
} while(time_counter_delay_ms > 0 && cancel == false);
3292
} else {
3393
file_position++;
3494
}
@@ -50,8 +110,8 @@ void nfc_playlist_emulation_scene_on_enter(void* context) {
50110
time_counter_ms -= 500;
51111
} while(time_counter_ms > 0);
52112
} 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);
55115

56116
int popup_text_size = (strlen(file_name) + 4);
57117
char popup_text[popup_text_size];
@@ -61,38 +121,22 @@ void nfc_playlist_emulation_scene_on_enter(void* context) {
61121
popup_set_text(nfc_playlist->popup, popup_text, 64, 25, AlignCenter, AlignTop);
62122
furi_delay_ms(500);
63123
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);
65125

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);
69127
}
70128
}
71129
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);
73132
} else {
74-
popup_reset(nfc_playlist->popup);
75-
popup_set_context(nfc_playlist->popup, nfc_playlist);
76133
popup_set_header(nfc_playlist->popup, "Error:", 64, 10, AlignCenter, AlignTop);
77134
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);
79135
}
80136
// Free/close resources
81137
furi_string_free(line);
82138
file_stream_close(stream);
83139
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;
98142
}

scences/emulation.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,10 @@
1111

1212
void nfc_playlist_emulation_scene_on_enter(void* context);
1313
bool nfc_playlist_emulation_scene_on_event(void* context, SceneManagerEvent event);
14-
void nfc_playlist_emulation_scene_on_exit(void* context);
14+
void nfc_playlist_emulation_scene_on_exit(void* context);
15+
16+
void nfc_playlist_emulation_setup(void* context);
17+
void nfc_playlist_emulation_free(NfcPlaylist* nfc_playlist);
18+
void nfc_playlist_emulation_start(NfcPlaylist* nfc_playlist);
19+
void nfc_playlist_emulation_stop(NfcPlaylist* nfc_playlist);
20+
int32_t nfc_playlist_emulation_task(void* context);

0 commit comments

Comments
 (0)