From 86023e97ff164bc087fdad8a42e81f38acc9707b Mon Sep 17 00:00:00 2001 From: acegoal07 Date: Sun, 31 Dec 2023 15:50:41 +0000 Subject: [PATCH 1/2] Settings view --- nfc_playlist.c | 16 ++++++-- nfc_playlist.h | 4 ++ nfc_playlist_i.h | 1 + scences/emulation.c | 5 +-- scences/main_menu.c | 88 +++++++++++------------------------------- scences/main_menu.h | 2 +- scences/settings.c | 94 +++++++++++++++++++++++++++++++++++++++++++++ scences/settings.h | 10 +++++ 8 files changed, 146 insertions(+), 74 deletions(-) create mode 100644 scences/settings.c create mode 100644 scences/settings.h diff --git a/nfc_playlist.c b/nfc_playlist.c index 888139a621a..aff0669266c 100644 --- a/nfc_playlist.c +++ b/nfc_playlist.c @@ -3,17 +3,20 @@ static void (*const nfc_playlist_scene_on_enter_handlers[])(void*) = { nfc_playlist_main_menu_scene_on_enter, - nfc_playlist_emulation_scene_on_enter, + nfc_playlist_settings_scene_on_enter, + nfc_playlist_emulation_scene_on_enter }; static bool (*const nfc_playlist_scene_on_event_handlers[])(void*, SceneManagerEvent) = { nfc_playlist_main_menu_scene_on_event, - nfc_playlist_emulation_scene_on_event, + nfc_playlist_settings_scene_on_event, + nfc_playlist_emulation_scene_on_event }; static void (*const nfc_playlist_scene_on_exit_handlers[])(void*) = { nfc_playlist_main_menu_scene_on_exit, - nfc_playlist_emulation_scene_on_exit, + nfc_playlist_settings_scene_on_exit, + nfc_playlist_emulation_scene_on_exit }; static const SceneManagerHandlers nfc_playlist_scene_manager_handlers = { @@ -44,6 +47,7 @@ static NfcPlaylist* nfc_playlist_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->popup = popup_alloc(); nfc_playlist->emulate_timeout = default_emulate_timeout; nfc_playlist->emulate_delay = default_emulate_delay; @@ -54,7 +58,9 @@ static NfcPlaylist* nfc_playlist_alloc() { 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, variable_item_list_get_view(nfc_playlist->variable_item_list)); + 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)); @@ -65,9 +71,11 @@ static void nfc_playlist_free(NfcPlaylist* nfc_playlist) { furi_assert(nfc_playlist); scene_manager_free(nfc_playlist->scene_manager); view_dispatcher_remove_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Menu); + view_dispatcher_remove_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Settings); view_dispatcher_remove_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Popup); view_dispatcher_free(nfc_playlist->view_dispatcher); variable_item_list_free(nfc_playlist->variable_item_list); + submenu_free(nfc_playlist->submenu); popup_free(nfc_playlist->popup); furi_record_close(RECORD_NOTIFICATION); free(nfc_playlist); diff --git a/nfc_playlist.h b/nfc_playlist.h index bf727440de9..4c3d903a453 100644 --- a/nfc_playlist.h +++ b/nfc_playlist.h @@ -7,16 +7,19 @@ #include #include #include +#include #include #include typedef enum { NfcPlaylistView_Menu, + NfcPlaylistView_Settings, NfcPlaylistView_Popup } NfcPlayScenesView; typedef enum { NfcPlaylistScene_MainMenu, + NfcPlaylistScene_Settings, NfcPlaylistScene_EmulatingPopup, NfcPlaylistScene_count } NfcPlaylistScene; @@ -25,6 +28,7 @@ typedef struct { SceneManager* scene_manager; ViewDispatcher* view_dispatcher; VariableItemList* variable_item_list; + Submenu* submenu; Popup* popup; NotificationApp* notification; FuriThread* thread; diff --git a/nfc_playlist_i.h b/nfc_playlist_i.h index fc521555172..24ca413f057 100644 --- a/nfc_playlist_i.h +++ b/nfc_playlist_i.h @@ -1,4 +1,5 @@ #pragma once #include "scences/main_menu.h" +#include "scences/settings.h" #include "scences/emulation.h" \ No newline at end of file diff --git a/scences/emulation.c b/scences/emulation.c index 9ab70c5d1cd..a67e124ec71 100644 --- a/scences/emulation.c +++ b/scences/emulation.c @@ -11,7 +11,6 @@ void nfc_playlist_emulation_scene_on_enter(void* context) { bool nfc_playlist_emulation_scene_on_event(void* context, SceneManagerEvent event) { UNUSED(context); - FURI_LOG_RAW_I("nfc_playlist_emulation_scene_on_event: %ld", event.event); switch (event.event) { case 0: if (EmulationState == NfcPlaylistEmulationState_Emulating) { @@ -152,11 +151,11 @@ int32_t nfc_playlist_emulation_task(void* context) { 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_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; } else { popup_set_header(nfc_playlist->popup, "Error:", 64, 10, AlignCenter, AlignTop); popup_set_text(nfc_playlist->popup, "Failed to open file\n/ext/apps_data/nfc_playlist/playlist.txt", 64, 25, AlignCenter, AlignTop); diff --git a/scences/main_menu.c b/scences/main_menu.c index e5c3993fe76..8db8a727b5d 100644 --- a/scences/main_menu.c +++ b/scences/main_menu.c @@ -2,14 +2,13 @@ #include "scences/main_menu.h" typedef enum { - NfcPlaylistEvent_ShowEmulatingPopup + NfcPlaylistEvent_ShowEmulatingPopup, + NfcPlaylistEvent_ShowSettings } NfcPlaylistMainMenuEvent; typedef enum { - NfcPlaylistSettings_Timeout, - NfcPlaylistSettings_Delay, - NfcPlaylistSettings_LedIndicator, - NfcPlaylistMenuSelection_Start + NfcPlaylistMenuSelection_Start, + NfcPlaylistMenuSelection_Settings } NfcPlaylistMenuSelection; static void nfc_playlist_menu_callback(void* context, uint32_t index) { @@ -18,35 +17,8 @@ static void nfc_playlist_menu_callback(void* context, uint32_t index) { case NfcPlaylistMenuSelection_Start: scene_manager_handle_custom_event(nfc_playlist->scene_manager, NfcPlaylistEvent_ShowEmulatingPopup); break; - default: - break; - } -} - -static void nfc_playlist_settings_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); - uint8_t option_value_index = variable_item_get_current_value_index(item); - - switch(current_option) { - case NfcPlaylistSettings_Timeout: { - nfc_playlist->emulate_timeout = option_value_index; - char emulate_timeout_text[10]; - snprintf(emulate_timeout_text, 10, "%ds", options_emulate_timeout[option_value_index]); - variable_item_set_current_value_text(item, (char*)emulate_timeout_text); - break; - } - case NfcPlaylistSettings_Delay: { - nfc_playlist->emulate_delay = option_value_index; - char emulate_delay_text[10]; - snprintf(emulate_delay_text, 10, "%ds", options_emulate_delay[option_value_index]); - variable_item_set_current_value_text(item, (char*)emulate_delay_text); - break; - } - case NfcPlaylistSettings_LedIndicator: - nfc_playlist->emulate_led_indicator = option_value_index; - variable_item_set_current_value_text(item, nfc_playlist->emulate_led_indicator ? "ON" : "OFF"); + case NfcPlaylistMenuSelection_Settings: + scene_manager_handle_custom_event(nfc_playlist->scene_manager, NfcPlaylistEvent_ShowSettings); break; default: break; @@ -55,42 +27,22 @@ static void nfc_playlist_settings_change_callback(VariableItem* item) { void nfc_playlist_main_menu_scene_on_enter(void* context) { NfcPlaylist* nfc_playlist = context; - variable_item_list_set_header(nfc_playlist->variable_item_list, "NFC Playlist"); - - VariableItem* emulation_timeout_settings = variable_item_list_add( - nfc_playlist->variable_item_list, - "Emulate time", - (sizeof(options_emulate_timeout)/sizeof(options_emulate_timeout[0])), - nfc_playlist_settings_change_callback, - nfc_playlist); - variable_item_set_current_value_index(emulation_timeout_settings, nfc_playlist->emulate_timeout); - char emulation_timeout_settings_text[10]; - snprintf(emulation_timeout_settings_text, 10, "%ds", options_emulate_timeout[nfc_playlist->emulate_timeout]); - variable_item_set_current_value_text(emulation_timeout_settings, (char*)emulation_timeout_settings_text); + submenu_set_header(nfc_playlist->submenu, "NFC Playlist"); - VariableItem* emulation_delay_settings = variable_item_list_add( - nfc_playlist->variable_item_list, - "Delay time", - (sizeof(options_emulate_delay)/sizeof(options_emulate_delay[0])), - nfc_playlist_settings_change_callback, + submenu_add_item( + nfc_playlist->submenu, + "Start", + NfcPlaylistMenuSelection_Start, + nfc_playlist_menu_callback, nfc_playlist); - variable_item_set_current_value_index(emulation_delay_settings, nfc_playlist->emulate_delay); - char emulation_delay_settings_text[10]; - snprintf(emulation_delay_settings_text, 10, "%ds", options_emulate_delay[nfc_playlist->emulate_delay]); - variable_item_set_current_value_text(emulation_delay_settings, (char*)emulation_delay_settings_text); - // add bool setting - VariableItem* emulation_led_indicator_settings = variable_item_list_add( - nfc_playlist->variable_item_list, - "LED Indicator", - 2, - nfc_playlist_settings_change_callback, + submenu_add_item( + nfc_playlist->submenu, + "Settings", + NfcPlaylistMenuSelection_Settings, + nfc_playlist_menu_callback, nfc_playlist); - 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"); - variable_item_list_add(nfc_playlist->variable_item_list, "Start", 0, NULL, NULL); - variable_item_list_set_enter_callback(nfc_playlist->variable_item_list, nfc_playlist_menu_callback, nfc_playlist); view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Menu); } @@ -104,6 +56,10 @@ bool nfc_playlist_main_menu_scene_on_event(void* context, SceneManagerEvent even scene_manager_next_scene(nfc_playlist->scene_manager, NfcPlaylistScene_EmulatingPopup); consumed = true; break; + case NfcPlaylistEvent_ShowSettings: + scene_manager_next_scene(nfc_playlist->scene_manager, NfcPlaylistScene_Settings); + consumed = true; + break; default: break; } @@ -117,5 +73,5 @@ bool nfc_playlist_main_menu_scene_on_event(void* context, SceneManagerEvent even void nfc_playlist_main_menu_scene_on_exit(void* context) { NfcPlaylist* nfc_playlist = context; - variable_item_list_reset(nfc_playlist->variable_item_list); + submenu_reset(nfc_playlist->submenu); } \ No newline at end of file diff --git a/scences/main_menu.h b/scences/main_menu.h index 8f3270609af..4fa0215024e 100644 --- a/scences/main_menu.h +++ b/scences/main_menu.h @@ -3,7 +3,7 @@ #include #include #include -#include +#include void nfc_playlist_main_menu_scene_on_enter(void* context); bool nfc_playlist_main_menu_scene_on_event(void* context, SceneManagerEvent event); diff --git a/scences/settings.c b/scences/settings.c new file mode 100644 index 00000000000..6693bb29554 --- /dev/null +++ b/scences/settings.c @@ -0,0 +1,94 @@ +#include "nfc_playlist.h" +#include "scences/settings.h" + +typedef enum { + NfcPlaylistSettings_Timeout, + NfcPlaylistSettings_Delay, + NfcPlaylistSettings_LedIndicator +} NfcPlaylistMenuSelection; + +static void nfc_playlist_menu_callback(void* context, uint32_t index) { + UNUSED(context); + UNUSED(index); +} + +static 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); + uint8_t option_value_index = variable_item_get_current_value_index(item); + + switch(current_option) { + case NfcPlaylistSettings_Timeout: { + nfc_playlist->emulate_timeout = option_value_index; + char emulate_timeout_text[10]; + snprintf(emulate_timeout_text, 10, "%ds", options_emulate_timeout[option_value_index]); + variable_item_set_current_value_text(item, (char*)emulate_timeout_text); + break; + } + case NfcPlaylistSettings_Delay: { + nfc_playlist->emulate_delay = option_value_index; + char emulate_delay_text[10]; + snprintf(emulate_delay_text, 10, "%ds", options_emulate_delay[option_value_index]); + variable_item_set_current_value_text(item, (char*)emulate_delay_text); + break; + } + case NfcPlaylistSettings_LedIndicator: + nfc_playlist->emulate_led_indicator = option_value_index; + variable_item_set_current_value_text(item, nfc_playlist->emulate_led_indicator ? "ON" : "OFF"); + break; + default: + break; + } +} + +void nfc_playlist_settings_scene_on_enter(void* context) { + NfcPlaylist* nfc_playlist = context; + variable_item_list_set_header(nfc_playlist->variable_item_list, "Settings"); + + VariableItem* emulation_timeout_settings = variable_item_list_add( + nfc_playlist->variable_item_list, + "Emulate time", + (sizeof(options_emulate_timeout)/sizeof(options_emulate_timeout[0])), + nfc_playlist_settings_options_change_callback, + nfc_playlist); + + variable_item_set_current_value_index(emulation_timeout_settings, nfc_playlist->emulate_timeout); + char emulation_timeout_settings_text[10]; + snprintf(emulation_timeout_settings_text, 10, "%ds", options_emulate_timeout[nfc_playlist->emulate_timeout]); + variable_item_set_current_value_text(emulation_timeout_settings, (char*)emulation_timeout_settings_text); + + VariableItem* emulation_delay_settings = variable_item_list_add( + nfc_playlist->variable_item_list, + "Delay time", + (sizeof(options_emulate_delay)/sizeof(options_emulate_delay[0])), + nfc_playlist_settings_options_change_callback, + nfc_playlist); + variable_item_set_current_value_index(emulation_delay_settings, nfc_playlist->emulate_delay); + char emulation_delay_settings_text[10]; + snprintf(emulation_delay_settings_text, 10, "%ds", options_emulate_delay[nfc_playlist->emulate_delay]); + variable_item_set_current_value_text(emulation_delay_settings, (char*)emulation_delay_settings_text); + + VariableItem* emulation_led_indicator_settings = variable_item_list_add( + nfc_playlist->variable_item_list, + "LED Indicator", + 2, + nfc_playlist_settings_options_change_callback, + nfc_playlist); + 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"); + + variable_item_list_set_enter_callback(nfc_playlist->variable_item_list, nfc_playlist_menu_callback, nfc_playlist); + view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Settings); +} + +bool nfc_playlist_settings_scene_on_event(void* context, SceneManagerEvent event) { + UNUSED(context); + UNUSED(event); + return false; +} + +void nfc_playlist_settings_scene_on_exit(void* context) { + NfcPlaylist* nfc_playlist = context; + variable_item_list_reset(nfc_playlist->variable_item_list); +} \ No newline at end of file diff --git a/scences/settings.h b/scences/settings.h new file mode 100644 index 00000000000..31c93498054 --- /dev/null +++ b/scences/settings.h @@ -0,0 +1,10 @@ +#pragma once +#include +#include +#include +#include +#include + +void nfc_playlist_settings_scene_on_enter(void* context); +bool nfc_playlist_settings_scene_on_event(void* context, SceneManagerEvent event); +void nfc_playlist_settings_scene_on_exit(void* context); \ No newline at end of file From 22fb491be22bf85380535fcc53a4b30f82a659bf Mon Sep 17 00:00:00 2001 From: acegoal07 Date: Thu, 4 Jan 2024 14:14:58 +0000 Subject: [PATCH 2/2] Update settings.c --- scences/settings.c | 49 ++++++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/scences/settings.c b/scences/settings.c index 6693bb29554..249d7ead0dd 100644 --- a/scences/settings.c +++ b/scences/settings.c @@ -4,12 +4,36 @@ typedef enum { NfcPlaylistSettings_Timeout, NfcPlaylistSettings_Delay, - NfcPlaylistSettings_LedIndicator + NfcPlaylistSettings_LedIndicator, + NfcPlaylistSettings_Reset } NfcPlaylistMenuSelection; static void nfc_playlist_menu_callback(void* context, uint32_t index) { - UNUSED(context); - UNUSED(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); + + 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; + } } static void nfc_playlist_settings_options_change_callback(VariableItem* item) { @@ -21,15 +45,15 @@ static void nfc_playlist_settings_options_change_callback(VariableItem* item) { switch(current_option) { case NfcPlaylistSettings_Timeout: { nfc_playlist->emulate_timeout = option_value_index; - char emulate_timeout_text[10]; - snprintf(emulate_timeout_text, 10, "%ds", options_emulate_timeout[option_value_index]); + char emulate_timeout_text[3]; + snprintf(emulate_timeout_text, 3, "%ds", options_emulate_timeout[option_value_index]); variable_item_set_current_value_text(item, (char*)emulate_timeout_text); break; } case NfcPlaylistSettings_Delay: { nfc_playlist->emulate_delay = option_value_index; - char emulate_delay_text[10]; - snprintf(emulate_delay_text, 10, "%ds", options_emulate_delay[option_value_index]); + char emulate_delay_text[3]; + snprintf(emulate_delay_text, 3, "%ds", options_emulate_delay[option_value_index]); variable_item_set_current_value_text(item, (char*)emulate_delay_text); break; } @@ -52,10 +76,9 @@ void nfc_playlist_settings_scene_on_enter(void* context) { (sizeof(options_emulate_timeout)/sizeof(options_emulate_timeout[0])), nfc_playlist_settings_options_change_callback, nfc_playlist); - variable_item_set_current_value_index(emulation_timeout_settings, nfc_playlist->emulate_timeout); - char emulation_timeout_settings_text[10]; - snprintf(emulation_timeout_settings_text, 10, "%ds", options_emulate_timeout[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); VariableItem* emulation_delay_settings = variable_item_list_add( @@ -65,8 +88,8 @@ void nfc_playlist_settings_scene_on_enter(void* context) { nfc_playlist_settings_options_change_callback, nfc_playlist); variable_item_set_current_value_index(emulation_delay_settings, nfc_playlist->emulate_delay); - char emulation_delay_settings_text[10]; - snprintf(emulation_delay_settings_text, 10, "%ds", options_emulate_delay[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); VariableItem* emulation_led_indicator_settings = variable_item_list_add( @@ -78,6 +101,8 @@ void nfc_playlist_settings_scene_on_enter(void* context) { 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"); + variable_item_list_add(nfc_playlist->variable_item_list, "Reset settings", 0, NULL, NULL); + variable_item_list_set_enter_callback(nfc_playlist->variable_item_list, nfc_playlist_menu_callback, nfc_playlist); view_dispatcher_switch_to_view(nfc_playlist->view_dispatcher, NfcPlaylistView_Settings); }