Skip to content

Commit

Permalink
State
Browse files Browse the repository at this point in the history
- Adds a state to tell if its possible to cancel the emulation early
  • Loading branch information
acegoal07 committed Dec 25, 2023
1 parent d8a7c3e commit d2620fa
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 20 deletions.
5 changes: 2 additions & 3 deletions lib/worker/nfc_playlist_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ typedef struct NfcPlaylistWorker {
Nfc* nfc;
} NfcPlaylistWorker;

/// Worker
NfcPlaylistWorker* nfc_playlist_worker_alloc();
void nfc_playlist_worker_free(NfcPlaylistWorker* nfc_playlist_worker);
void nfc_playlist_worker_stop(NfcPlaylistWorker* nfc_playlist_worker);
void nfc_playlist_worker_start(NfcPlaylistWorker* nfc_playlist_worker);
// task

int32_t nfc_playlist_worker_task(void* context);
//

bool nfc_playlist_worker_is_emulating(NfcPlaylistWorker* nfc_playlist_worker);
void nfc_playlist_worker_set_nfc_data(NfcPlaylistWorker* nfc_playlist_worker, char* file_path);
NfcDeviceData* nfc_playlist_worker_get_nfc_data(NfcPlaylistWorker* nfc_playlist_worker);
36 changes: 20 additions & 16 deletions scences/emulation.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "nfc_playlist.h"
#include "scences/emulation.h"

bool cancel = false;
NfcPlaylistEmulationState EmulationState = NfcPlaylistEmulationState_Stopped;

void nfc_playlist_emulation_scene_on_enter(void* context) {
NfcPlaylist* nfc_playlist = context;
Expand All @@ -10,13 +10,12 @@ void nfc_playlist_emulation_scene_on_enter(void* context) {
}

bool nfc_playlist_emulation_scene_on_event(void* context, SceneManagerEvent event) {
NfcPlaylist* nfc_playlist = context;
UNUSED(nfc_playlist);
UNUSED(context);
FURI_LOG_RAW_I("nfc_playlist_emulation_scene_on_event: %ld", event.event);
switch (event.event) {
case 0:
if (nfc_playlist_worker_is_emulating(nfc_playlist->nfc_playlist_worker) && cancel != true) {
cancel = true;
if (EmulationState == NfcPlaylistEmulationState_Emulating) {
EmulationState = NfcPlaylistEmulationState_Canceled;
return true;
}
default:
Expand All @@ -28,7 +27,7 @@ bool nfc_playlist_emulation_scene_on_event(void* context, SceneManagerEvent even

void nfc_playlist_emulation_scene_on_exit(void* context) {
NfcPlaylist* nfc_playlist = context;
cancel = false;
EmulationState = NfcPlaylistEmulationState_Stopped;
nfc_playlist_emulation_stop(nfc_playlist);
nfc_playlist_emulation_free(nfc_playlist);
popup_reset(nfc_playlist->popup);
Expand Down Expand Up @@ -73,10 +72,10 @@ int32_t nfc_playlist_emulation_task(void* context) {

// Read file
if(file_stream_open(stream, APP_DATA_PATH("playlist.txt"), FSAM_READ, FSOM_OPEN_EXISTING)) {

EmulationState = NfcPlaylistEmulationState_Emulating;
int file_position = 0;
// read the file line by line and print the text
while(stream_read_line(stream, line) && cancel == false) {
while(stream_read_line(stream, line) && EmulationState == NfcPlaylistEmulationState_Emulating) {
if (options_emulate_delay[nfc_playlist->emulate_delay] > 0) {
if (file_position > 0) {
popup_set_header(nfc_playlist->popup, "Delaying", 64, 10, AlignCenter, AlignTop);
Expand All @@ -88,12 +87,16 @@ int32_t nfc_playlist_emulation_task(void* context) {
popup_set_text(nfc_playlist->popup, display_text, 64, 50, AlignCenter, AlignTop);
furi_delay_ms(500);
time_counter_delay_ms -= 500;
} while(time_counter_delay_ms > 0 && cancel == false);
} while(time_counter_delay_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating);
} else {
file_position++;
}
}

if (EmulationState != NfcPlaylistEmulationState_Emulating) {
break;
}

char* file_path = (char*)furi_string_get_cstr(line);
char* file_name;
if (strchr(file_path, '/') != NULL) {
Expand All @@ -109,27 +112,27 @@ int32_t nfc_playlist_emulation_task(void* context) {
snprintf(popup_header_text, 80, "%s\n%s", "ERROR not found:", file_name);
popup_set_header(nfc_playlist->popup, popup_header_text, 64, 10, AlignCenter, AlignTop);
start_error_blink(nfc_playlist);
do {
while(time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
char popup_text[10];
snprintf(popup_text, 10, "%ds", (time_counter_ms/1000));
popup_set_text(nfc_playlist->popup, popup_text, 64, 50, AlignCenter, AlignTop);
furi_delay_ms(500);
time_counter_ms -= 500;
} while(time_counter_ms > 0);
}
}

else if (strstr(file_ext, "nfc") == NULL) {
else if (strcasestr(file_ext, "nfc") == NULL) {
char popup_header_text[80];
snprintf(popup_header_text, 80, "%s\n%s", "ERROR invalid file:", file_name);
popup_set_header(nfc_playlist->popup, popup_header_text, 64, 10, AlignCenter, AlignTop);
start_error_blink(nfc_playlist);
do {
while(time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
char popup_text[10];
snprintf(popup_text, 10, "%ds", (time_counter_ms/1000));
popup_set_text(nfc_playlist->popup, popup_text, 64, 50, AlignCenter, AlignTop);
furi_delay_ms(500);
time_counter_ms -= 500;
} while(time_counter_ms > 0);
}
}

else {
Expand All @@ -139,16 +142,17 @@ int32_t nfc_playlist_emulation_task(void* context) {
nfc_playlist_worker_set_nfc_data(nfc_playlist->nfc_playlist_worker, file_path);
nfc_playlist_worker_start(nfc_playlist->nfc_playlist_worker);
start_normal_blink(nfc_playlist);
do {
while(nfc_playlist_worker_is_emulating(nfc_playlist->nfc_playlist_worker) && time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
char popup_text[10];
snprintf(popup_text, 10, "%ds", (time_counter_ms/1000));
popup_set_text(nfc_playlist->popup, popup_text, 64, 50, AlignCenter, AlignTop);
furi_delay_ms(500);
time_counter_ms -= 500;
} while(nfc_playlist_worker_is_emulating(nfc_playlist->nfc_playlist_worker) && time_counter_ms > 0 && cancel == false);
}
nfc_playlist_worker_stop(nfc_playlist->nfc_playlist_worker);
}
}
EmulationState = NfcPlaylistEmulationState_Stopped;
popup_reset(nfc_playlist->popup);
popup_set_header(nfc_playlist->popup, "Emulation finished", 64, 10, AlignCenter, AlignTop);
popup_set_text(nfc_playlist->popup, "Press back", 64, 50, AlignCenter, AlignTop);
Expand Down
8 changes: 7 additions & 1 deletion scences/emulation.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,10 @@ void nfc_playlist_emulation_setup(void* context);
void nfc_playlist_emulation_free(NfcPlaylist* nfc_playlist);
void nfc_playlist_emulation_start(NfcPlaylist* nfc_playlist);
void nfc_playlist_emulation_stop(NfcPlaylist* nfc_playlist);
int32_t nfc_playlist_emulation_task(void* context);
int32_t nfc_playlist_emulation_task(void* context);

typedef enum NfcPlaylistEmulationState {
NfcPlaylistEmulationState_Emulating,
NfcPlaylistEmulationState_Stopped,
NfcPlaylistEmulationState_Canceled,
} NfcPlaylistEmulationState;

0 comments on commit d2620fa

Please sign in to comment.