Skip to content

Commit

Permalink
Optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
acegoal07 committed Jan 7, 2024
1 parent b861e0e commit 99c9556
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 79 deletions.
15 changes: 8 additions & 7 deletions lib/led/nfc_playlist_led.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,27 @@ const NotificationSequence blink_sequence_normal = {
&message_do_not_reset,
NULL,
};
void start_normal_blink(NfcPlaylist* nfc_playlist) {
if (nfc_playlist->emulate_led_indicator) {
notification_message_block(nfc_playlist->notification, &blink_sequence_normal);
}
}

NotificationMessage blink_message_error = {
.type = NotificationMessageTypeLedBlinkStart,
.data.led_blink.color = LightRed,
.data.led_blink.on_time = 10,
.data.led_blink.period = 100,
};

const NotificationSequence blink_sequence_error = {
&blink_message_error,
&message_do_not_reset,
NULL,
};
void start_error_blink(NfcPlaylist* nfc_playlist) {

void start_blink(NfcPlaylist* nfc_playlist, int state) {
if (nfc_playlist->emulate_led_indicator) {
notification_message_block(nfc_playlist->notification, &blink_sequence_error);
if (state == NfcPlaylistLedState_Normal) {
notification_message_block(nfc_playlist->notification, &blink_sequence_normal);
} else if (state == NfcPlaylistLedState_Error) {
notification_message_block(nfc_playlist->notification, &blink_sequence_error);
}
}
}

Expand Down
8 changes: 6 additions & 2 deletions lib/led/nfc_playlist_led.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
#include <../../nfc_playlist.h>
#include <notification/notification_messages.h>

void start_normal_blink(NfcPlaylist* nfc_playlist);
void start_error_blink(NfcPlaylist* nfc_playlist);
typedef enum NfcPlaylistLedState {
NfcPlaylistLedState_Normal,
NfcPlaylistLedState_Error
} NfcPlaylistLedState;

void start_blink(NfcPlaylist* nfc_playlist, int state);
void stop_blink(NfcPlaylist* nfc_playlist);
2 changes: 1 addition & 1 deletion lib/worker/nfc_playlist_worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

typedef enum NfcPlaylistWorkerState {
NfcPlaylistWorkerState_Emulating,
NfcPlaylistWorkerState_Stopped,
NfcPlaylistWorkerState_Stopped
} NfcPlaylistWorkerState;

typedef struct NfcPlaylistWorker {
Expand Down
5 changes: 0 additions & 5 deletions nfc_playlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ static NfcPlaylist* nfc_playlist_alloc() {
NfcPlaylist* nfc_playlist = malloc(sizeof(NfcPlaylist));
furi_assert(nfc_playlist);
nfc_playlist->scene_manager = scene_manager_alloc(&nfc_playlist_scene_manager_handlers, nfc_playlist);

nfc_playlist->view_dispatcher = view_dispatcher_alloc();
view_dispatcher_enable_queue(nfc_playlist->view_dispatcher);

nfc_playlist->variable_item_list = variable_item_list_alloc();
nfc_playlist->submenu = submenu_alloc();
nfc_playlist->base_file_path = furi_string_alloc_set_str("/ext/apps_data/nfc_playlist/");
Expand All @@ -60,16 +58,13 @@ static NfcPlaylist* nfc_playlist_alloc() {
nfc_playlist->emulate_delay = default_emulate_delay;
nfc_playlist->emulate_led_indicator = default_emulate_led_indicator;
nfc_playlist->notification = furi_record_open(RECORD_NOTIFICATION);

view_dispatcher_set_event_callback_context(nfc_playlist->view_dispatcher, nfc_playlist);
view_dispatcher_set_custom_event_callback(nfc_playlist->view_dispatcher, nfc_playlist_custom_callback);
view_dispatcher_set_navigation_event_callback(nfc_playlist->view_dispatcher, nfc_playlist_back_event_callback);

view_dispatcher_add_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Menu, submenu_get_view(nfc_playlist->submenu));
view_dispatcher_add_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Settings, variable_item_list_get_view(nfc_playlist->variable_item_list));
view_dispatcher_add_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Popup, popup_get_view(nfc_playlist->popup));
view_dispatcher_add_view(nfc_playlist->view_dispatcher, NfcPlaylistView_FileSelect, file_browser_get_view(nfc_playlist->file_browser));

return nfc_playlist;
}

Expand Down
1 change: 0 additions & 1 deletion nfc_playlist_i.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once

#include "scences/main_menu.h"
#include "scences/settings.h"
#include "scences/emulation.h"
Expand Down
22 changes: 8 additions & 14 deletions scences/emulation.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,9 @@ void nfc_playlist_emulation_scene_on_enter(void* context) {

bool nfc_playlist_emulation_scene_on_event(void* context, SceneManagerEvent event) {
UNUSED(context);
switch (event.event) {
case 0:
if (EmulationState == NfcPlaylistEmulationState_Emulating) {
EmulationState = NfcPlaylistEmulationState_Canceled;
return true;
}
break;
default:
break;
if (event.event == 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
EmulationState = NfcPlaylistEmulationState_Canceled;
return true;
}
return false;
}
Expand Down Expand Up @@ -77,7 +71,7 @@ int32_t nfc_playlist_emulation_task(void* context) {
if (options_emulate_delay[nfc_playlist->emulate_delay] > 0) {
if (file_position > 0) {
popup_set_header(nfc_playlist->popup, "Delaying", 64, 10, AlignCenter, AlignTop);
start_error_blink(nfc_playlist);
start_blink(nfc_playlist, NfcPlaylistLedState_Error);
int time_counter_delay_ms = (options_emulate_delay[nfc_playlist->emulate_delay] * 1000);
do {
char display_text[10];
Expand Down Expand Up @@ -109,7 +103,7 @@ int32_t nfc_playlist_emulation_task(void* context) {
char popup_header_text[(18 + strlen(file_name))];
snprintf(popup_header_text, (18 + strlen(file_name)), "%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);
start_blink(nfc_playlist, NfcPlaylistLedState_Error);
while(time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
char popup_text[9];
snprintf(popup_text, 9, "%ds", (time_counter_ms/1000));
Expand All @@ -123,7 +117,7 @@ int32_t nfc_playlist_emulation_task(void* context) {
char popup_header_text[(21 + strlen(file_name))];
snprintf(popup_header_text, (21 + strlen(file_name)), "%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);
start_blink(nfc_playlist, NfcPlaylistLedState_Error);
while(time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
char popup_text[9];
snprintf(popup_text, 9, "%ds", (time_counter_ms/1000));
Expand All @@ -139,7 +133,7 @@ int32_t nfc_playlist_emulation_task(void* context) {
popup_set_header(nfc_playlist->popup, popup_header_text, 64, 10, AlignCenter, AlignTop);
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);
start_blink(nfc_playlist, NfcPlaylistLedState_Normal);
while(nfc_playlist_worker_is_emulating(nfc_playlist->nfc_playlist_worker) && time_counter_ms > 0 && EmulationState == NfcPlaylistEmulationState_Emulating) {
char popup_text[9];
snprintf(popup_text, 9, "%ds", (time_counter_ms/1000));
Expand All @@ -155,7 +149,7 @@ int32_t nfc_playlist_emulation_task(void* context) {
popup_set_header(nfc_playlist->popup, EmulationState == NfcPlaylistEmulationState_Canceled ? "Emulation stopped" : "Emulation finished", 64, 10, AlignCenter, AlignTop);
popup_set_text(nfc_playlist->popup, "Press back", 64, 50, AlignCenter, AlignTop);
stop_blink(nfc_playlist);
EmulationState = NfcPlaylistEmulationState_Stopped;
EmulationState = NfcPlaylistEmulationState_Stopped;
} else {
popup_set_header(nfc_playlist->popup, "Failed to open playlist", 64, 10, AlignCenter, AlignTop);
popup_set_text(nfc_playlist->popup, "Press back", 64, 50, AlignCenter, AlignTop);
Expand Down
3 changes: 1 addition & 2 deletions scences/file_select.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ void nfc_playlist_file_select_scene_on_enter(void* context) {
bool nfc_playlist_file_select_scene_on_event(void* context, SceneManagerEvent event) {
UNUSED(event);
UNUSED(context);
bool consumed = false;
return consumed;
return false;
}

void nfc_playlist_file_select_scene_on_exit(void* context) {
Expand Down
41 changes: 18 additions & 23 deletions scences/main_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,29 +68,24 @@ void nfc_playlist_main_menu_scene_on_enter(void* context) {
bool nfc_playlist_main_menu_scene_on_event(void* context, SceneManagerEvent event) {
NfcPlaylist* nfc_playlist = context;
bool consumed = false;
switch(event.type) {
case SceneManagerEventTypeCustom:
switch(event.event) {
case NfcPlaylistEvent_ShowEmulatingPopup:
scene_manager_next_scene(nfc_playlist->scene_manager, NfcPlaylistScene_EmulatingPopup);
consumed = true;
break;
case NfcPlaylistEvent_ShowFileSelect:
scene_manager_next_scene(nfc_playlist->scene_manager, NfcPlaylistScene_FileSelect);
consumed = true;
break;
case NfcPlaylistEvent_ShowSettings:
scene_manager_next_scene(nfc_playlist->scene_manager, NfcPlaylistScene_Settings);
consumed = true;
break;
default:
break;
}
break;
default:
consumed = false;
break;
}
if (event.type == SceneManagerEventTypeCustom) {
switch(event.event) {
case NfcPlaylistEvent_ShowEmulatingPopup:
scene_manager_next_scene(nfc_playlist->scene_manager, NfcPlaylistScene_EmulatingPopup);
consumed = true;
break;
case NfcPlaylistEvent_ShowFileSelect:
scene_manager_next_scene(nfc_playlist->scene_manager, NfcPlaylistScene_FileSelect);
consumed = true;
break;
case NfcPlaylistEvent_ShowSettings:
scene_manager_next_scene(nfc_playlist->scene_manager, NfcPlaylistScene_Settings);
consumed = true;
break;
default:
break;
}
}
return consumed;
}

Expand Down
44 changes: 20 additions & 24 deletions scences/settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,31 @@ typedef enum {
NfcPlaylistSettings_Reset
} NfcPlaylistMenuSelection;

static void nfc_playlist_settings_menu_callback(void* context, uint32_t index) {
void nfc_playlist_settings_menu_callback(void* context, uint32_t index) {
NfcPlaylist* nfc_playlist = context;
switch(index) {
case NfcPlaylistSettings_Reset:
nfc_playlist->emulate_timeout = default_emulate_timeout;
VariableItem* emulation_timeout_settings = variable_item_list_get(nfc_playlist->variable_item_list, NfcPlaylistSettings_Timeout);
variable_item_set_current_value_index(emulation_timeout_settings, nfc_playlist->emulate_timeout);
char emulation_timeout_settings_text[3];
snprintf(emulation_timeout_settings_text, 3, "%ds", options_emulate_timeout[nfc_playlist->emulate_timeout]);
variable_item_set_current_value_text(emulation_timeout_settings, (char*)emulation_timeout_settings_text);
if (index == NfcPlaylistSettings_Reset) {
nfc_playlist->emulate_timeout = default_emulate_timeout;
VariableItem* emulation_timeout_settings = variable_item_list_get(nfc_playlist->variable_item_list, NfcPlaylistSettings_Timeout);
variable_item_set_current_value_index(emulation_timeout_settings, nfc_playlist->emulate_timeout);
char emulation_timeout_settings_text[3];
snprintf(emulation_timeout_settings_text, 3, "%ds", options_emulate_timeout[nfc_playlist->emulate_timeout]);
variable_item_set_current_value_text(emulation_timeout_settings, (char*)emulation_timeout_settings_text);

nfc_playlist->emulate_delay = default_emulate_delay;
VariableItem* emulation_delay_settings = variable_item_list_get(nfc_playlist->variable_item_list, NfcPlaylistSettings_Delay);
variable_item_set_current_value_index(emulation_delay_settings, nfc_playlist->emulate_delay);
char emulation_delay_settings_text[3];
snprintf(emulation_delay_settings_text, 3, "%ds", options_emulate_delay[nfc_playlist->emulate_delay]);
variable_item_set_current_value_text(emulation_delay_settings, (char*)emulation_delay_settings_text);
nfc_playlist->emulate_delay = default_emulate_delay;
VariableItem* emulation_delay_settings = variable_item_list_get(nfc_playlist->variable_item_list, NfcPlaylistSettings_Delay);
variable_item_set_current_value_index(emulation_delay_settings, nfc_playlist->emulate_delay);
char emulation_delay_settings_text[3];
snprintf(emulation_delay_settings_text, 3, "%ds", options_emulate_delay[nfc_playlist->emulate_delay]);
variable_item_set_current_value_text(emulation_delay_settings, (char*)emulation_delay_settings_text);

nfc_playlist->emulate_led_indicator = default_emulate_led_indicator;
VariableItem* emulation_led_indicator_settings = variable_item_list_get(nfc_playlist->variable_item_list, NfcPlaylistSettings_LedIndicator);
variable_item_set_current_value_index(emulation_led_indicator_settings, nfc_playlist->emulate_led_indicator);
variable_item_set_current_value_text(emulation_led_indicator_settings, nfc_playlist->emulate_led_indicator ? "ON" : "OFF");
break;
default:
break;
}
nfc_playlist->emulate_led_indicator = default_emulate_led_indicator;
VariableItem* emulation_led_indicator_settings = variable_item_list_get(nfc_playlist->variable_item_list, NfcPlaylistSettings_LedIndicator);
variable_item_set_current_value_index(emulation_led_indicator_settings, nfc_playlist->emulate_led_indicator);
variable_item_set_current_value_text(emulation_led_indicator_settings, nfc_playlist->emulate_led_indicator ? "ON" : "OFF");
}
}

static void nfc_playlist_settings_options_change_callback(VariableItem* item) {
void nfc_playlist_settings_options_change_callback(VariableItem* item) {
NfcPlaylist* nfc_playlist = variable_item_get_context(item);

uint8_t current_option = variable_item_list_get_selected_item_index(nfc_playlist->variable_item_list);
Expand Down

0 comments on commit 99c9556

Please sign in to comment.