Skip to content
This repository has been archived by the owner on Jul 28, 2024. It is now read-only.

Commit

Permalink
Merge pull request #2 from kala13x/settings
Browse files Browse the repository at this point in the history
Finished settings implementation
  • Loading branch information
kala13x authored Sep 30, 2023
2 parents 7e4303a + 22055b7 commit 7af6767
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 61 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,13 @@ Button name | Description
- [ ] Full button list
- [ ] Edit remote file
- [ ] Delete remote file
- [ ] Application settings
- [x] Application settings
- [x] Load settings from the file
- [x] Store settings to the file
- [x] Vertical/Horizontal menu
- [x] IR command repeat count
- [ ] GUI to change settings
- [x] GUI to change settings
- [ ] Return button configuration

## Screens

Expand Down
24 changes: 0 additions & 24 deletions views/xremote_settings_view.c

This file was deleted.

13 changes: 0 additions & 13 deletions views/xremote_settings_view.h

This file was deleted.

1 change: 0 additions & 1 deletion xremote.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

#include "views/xremote_about_view.h"
#include "views/xremote_learn_view.h"
#include "views/xremote_settings_view.h"

#define TAG "XRemote"

Expand Down
2 changes: 1 addition & 1 deletion xremote.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@

#define XREMOTE_VERSION_MAJOR 0
#define XREMOTE_VERSION_MINOR 9
#define XREMOTE_BUILD_NUMBER 18
#define XREMOTE_BUILD_NUMBER 19

void xremote_get_version(char *version, size_t length);
26 changes: 9 additions & 17 deletions xremote_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
XRemoteAppSettings* xremote_app_settings_alloc()
{
XRemoteAppSettings* settings = malloc(sizeof(XRemoteAppSettings));
settings->orientation = ViewOrientationVertical;
settings->orientation = ViewOrientationHorizontal;
settings->repeat_count = 1;
return settings;
}
Expand All @@ -32,7 +32,6 @@ bool xremote_app_settings_store(XRemoteAppSettings* settings)
FlipperFormat* ff = flipper_format_file_alloc(storage);

FURI_LOG_I(TAG, "store config file: \'%s\'", XREMOTE_APP_SETTINGS);
bool vertical = settings->orientation == ViewOrientationVertical;
bool success = false;

do {
Expand All @@ -42,9 +41,9 @@ bool xremote_app_settings_store(XRemoteAppSettings* settings)
if (!flipper_format_write_comment_cstr(ff, "")) break;

/* Write actual configuration to the settings file */
const char *orientation = vertical ? "vertical" : "horizontal";
if (!flipper_format_write_string_cstr(ff, "orientation", orientation)) break;
if (!flipper_format_write_uint32(ff, "cmd_repeat", &settings->repeat_count, 1)) break;
uint32_t orientation = settings->orientation;
if (!flipper_format_write_uint32(ff, "orientation", &orientation, 1)) break;
if (!flipper_format_write_uint32(ff, "repeat", &settings->repeat_count, 1)) break;

success = true;
} while(false);
Expand All @@ -60,11 +59,9 @@ bool xremote_app_settings_load(XRemoteAppSettings* settings)
Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* ff = flipper_format_buffered_file_alloc(storage);
FuriString* header = furi_string_alloc();
FuriString* orient = furi_string_alloc();

FURI_LOG_I(TAG, "load config file: \'%s\'", XREMOTE_APP_SETTINGS);
uint32_t version = 0;
uint32_t repeat = 0;
uint32_t version, orientation, repeat = 0;
bool success = false;

do {
Expand All @@ -73,22 +70,17 @@ bool xremote_app_settings_load(XRemoteAppSettings* settings)
if (!flipper_format_read_header(ff, header, &version)) break;
if (!furi_string_equal(header, "XRemote settings file") || (version != 1)) break;

/* Read config data from the file */
if (!flipper_format_read_string(ff, "orientation", orient)) break;
if (!flipper_format_read_uint32(ff, "cmd_repeat", &repeat, 1)) break;

/* Parse config data from the buffer */
if (furi_string_equal(orient, "vertical"))
settings->orientation = ViewOrientationVertical;
else if (furi_string_equal(orient, "horizontal"))
settings->orientation = ViewOrientationHorizontal;
if (!flipper_format_read_uint32(ff, "orientation", &orientation, 1)) break;
if (!flipper_format_read_uint32(ff, "repeat", &repeat, 1)) break;

settings->orientation = orientation;
settings->repeat_count = repeat;

success = true;
} while(false);

furi_record_close(RECORD_STORAGE);
furi_string_free(orient);
furi_string_free(header);
flipper_format_free(ff);

Expand Down
1 change: 1 addition & 0 deletions xremote_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <gui/view_dispatcher.h>
#include <gui/modules/submenu.h>
#include <gui/modules/dialog_ex.h>
#include <gui/modules/variable_item_list.h>

#include <notification/notification.h>
#include <notification/notification_messages.h>
Expand Down
123 changes: 120 additions & 3 deletions xremote_settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,135 @@
*/

#include "xremote_settings.h"
#include "views/xremote_settings_view.h"

typedef struct {
VariableItemList *item_list;
XRemoteAppContext* app_ctx;
} XRemoteSettingsContext;

#define XREMOTE_ORIENTATION_TEXT_HORIZONTAL "Horizontal"
#define XREMOTE_ORIENTATION_INDEX_HORIZONTAL 0

#define XREMOTE_ORIENTATION_TEXT_VERTICAL "Vertical"
#define XREMOTE_ORIENTATION_INDEX_VERTICAL 1

#define XREMOTE_ORIENTATION_TEXT "Orientation"
#define XREMOTE_ORIENTATION_MAX 2

#define XREMOTE_REPEAT_TEXT "IR Msg Repeat"
#define XREMOTE_REPEAT_MAX 128

static uint32_t xremote_settings_view_exit_callback(void* context)
{
UNUSED(context);
return XRemoteViewSubmenu;
}

static uint32_t xremote_settings_get_orientation_index(XRemoteAppSettings* settings)
{
return settings->orientation == ViewOrientationHorizontal ?
XREMOTE_ORIENTATION_INDEX_HORIZONTAL :
XREMOTE_ORIENTATION_INDEX_VERTICAL;
}

static const char* xremote_settings_get_orientation_str(XRemoteAppSettings* settings)
{
return settings->orientation == ViewOrientationHorizontal ?
XREMOTE_ORIENTATION_TEXT_HORIZONTAL :
XREMOTE_ORIENTATION_TEXT_VERTICAL;
}

static ViewOrientation xremote_settings_get_orientation(uint8_t orientation)
{
return orientation == XREMOTE_ORIENTATION_INDEX_HORIZONTAL ?
ViewOrientationHorizontal :
ViewOrientationVertical;
}

static void infrared_settings_orientation_changed(VariableItem* item)
{
XRemoteSettingsContext* ctx = variable_item_get_context(item);
XRemoteAppSettings* settings = ctx->app_ctx->app_settings;

uint8_t orientation = variable_item_get_current_value_index(item);
settings->orientation = xremote_settings_get_orientation(orientation);
const char* orientation_str = xremote_settings_get_orientation_str(settings);

variable_item_set_current_value_text(item, orientation_str);
xremote_app_settings_store(settings);
}

static void infrared_settings_repeat_changed(VariableItem* item)
{
XRemoteSettingsContext* ctx = variable_item_get_context(item);
XRemoteAppSettings* settings = ctx->app_ctx->app_settings;
char repeat_str[8];

settings->repeat_count = variable_item_get_current_value_index(item);
snprintf(repeat_str, sizeof(repeat_str), "%lu", settings->repeat_count);

variable_item_set_current_value_text(item, repeat_str);
xremote_app_settings_store(settings);
}

XRemoteSettingsContext* xremote_settings_context_alloc(XRemoteAppContext* app_ctx)
{
XRemoteSettingsContext *context = malloc(sizeof(XRemoteSettingsContext));
XRemoteAppSettings* settings = app_ctx->app_settings;

context->item_list = variable_item_list_alloc();
context->app_ctx = app_ctx;
char repeat_str[8];

/* Configure variable item list view */
View *view = variable_item_list_get_view(context->item_list);
view_set_previous_callback(view, xremote_settings_view_exit_callback);
view_dispatcher_add_view(app_ctx->view_dispatcher, XRemoteViewSettings, view);

/* Add settings to variable item list */
VariableItem* item = variable_item_list_add(
context->item_list,
XREMOTE_ORIENTATION_TEXT,
XREMOTE_ORIENTATION_MAX,
infrared_settings_orientation_changed,
context);

/* Get application orientation settings */
const char* orient_str = xremote_settings_get_orientation_str(settings);
uint32_t orient_index = xremote_settings_get_orientation_index(settings);

/* Set current orientation item index and string */
variable_item_set_current_value_index(item, orient_index);
variable_item_set_current_value_text(item, orient_str);

/* Add IR message repeat counter to variable item list */
item = variable_item_list_add(
context->item_list,
XREMOTE_REPEAT_TEXT,
XREMOTE_REPEAT_MAX,
infrared_settings_repeat_changed,
context);

/* Set repeat count item index and string */
snprintf(repeat_str, sizeof(repeat_str), "%lu", settings->repeat_count);
variable_item_set_current_value_index(item, settings->repeat_count);
variable_item_set_current_value_text(item, repeat_str);

return context;
}

static void xremote_settings_context_clear_callback(void *context)
{
XRemoteSettingsContext *ctx = (XRemoteSettingsContext*)context;
ViewDispatcher* view_disp = ctx->app_ctx->view_dispatcher;
view_dispatcher_remove_view(view_disp, XRemoteViewSettings);
variable_item_list_free(ctx->item_list);
}

XRemoteApp* xremote_settings_alloc(XRemoteAppContext* app_ctx)
{
XRemoteApp* app = xremote_app_alloc(app_ctx);
xremote_app_view_alloc(app, XRemoteViewSettings, xremote_settings_view_alloc);
xremote_app_view_set_previous_callback(app, xremote_settings_view_exit_callback);
XRemoteSettingsContext* context = xremote_settings_context_alloc(app_ctx);
xremote_app_set_user_context(app, context, xremote_settings_context_clear_callback);
return app;
}

0 comments on commit 7af6767

Please sign in to comment.