Skip to content

Commit

Permalink
Merge pull request #6 from acegoal07/Settings
Browse files Browse the repository at this point in the history
Settings
  • Loading branch information
acegoal07 authored Jan 4, 2024
2 parents 82ed1da + 22fb491 commit c4abdfa
Show file tree
Hide file tree
Showing 8 changed files with 171 additions and 74 deletions.
16 changes: 12 additions & 4 deletions nfc_playlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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;
Expand All @@ -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));

Expand All @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions nfc_playlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,19 @@
#include <gui/scene_manager.h>
#include <gui/modules/popup.h>
#include <gui/modules/variable_item_list.h>
#include <gui/modules/submenu.h>
#include <notification/notification_messages.h>
#include <nfc_playlist_worker.h>

typedef enum {
NfcPlaylistView_Menu,
NfcPlaylistView_Settings,
NfcPlaylistView_Popup
} NfcPlayScenesView;

typedef enum {
NfcPlaylistScene_MainMenu,
NfcPlaylistScene_Settings,
NfcPlaylistScene_EmulatingPopup,
NfcPlaylistScene_count
} NfcPlaylistScene;
Expand All @@ -25,6 +28,7 @@ typedef struct {
SceneManager* scene_manager;
ViewDispatcher* view_dispatcher;
VariableItemList* variable_item_list;
Submenu* submenu;
Popup* popup;
NotificationApp* notification;
FuriThread* thread;
Expand Down
1 change: 1 addition & 0 deletions nfc_playlist_i.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once

#include "scences/main_menu.h"
#include "scences/settings.h"
#include "scences/emulation.h"
5 changes: 2 additions & 3 deletions scences/emulation.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -153,11 +152,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);
Expand Down
88 changes: 22 additions & 66 deletions scences/main_menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -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);
}

Expand All @@ -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;
}
Expand All @@ -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);
}
2 changes: 1 addition & 1 deletion scences/main_menu.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#include <gui/gui.h>
#include <gui/view_dispatcher.h>
#include <gui/scene_manager.h>
#include <gui/modules/variable_item_list.h>
#include <gui/modules/submenu.h>

void nfc_playlist_main_menu_scene_on_enter(void* context);
bool nfc_playlist_main_menu_scene_on_event(void* context, SceneManagerEvent event);
Expand Down
119 changes: 119 additions & 0 deletions scences/settings.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#include "nfc_playlist.h"
#include "scences/settings.h"

typedef enum {
NfcPlaylistSettings_Timeout,
NfcPlaylistSettings_Delay,
NfcPlaylistSettings_LedIndicator,
NfcPlaylistSettings_Reset
} NfcPlaylistMenuSelection;

static void nfc_playlist_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);

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) {
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[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[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;
}
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[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(
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[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(
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_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);
}

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);
}
10 changes: 10 additions & 0 deletions scences/settings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once
#include <furi.h>
#include <gui/gui.h>
#include <gui/view_dispatcher.h>
#include <gui/scene_manager.h>
#include <gui/modules/variable_item_list.h>

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);

0 comments on commit c4abdfa

Please sign in to comment.