Skip to content

Commit

Permalink
Merge pull request #1 from kala13x/settings
Browse files Browse the repository at this point in the history
Allocate application settings and load from the file
  • Loading branch information
kala13x authored Sep 29, 2023
2 parents 845666e + 3137949 commit 958af1d
Show file tree
Hide file tree
Showing 10 changed files with 144 additions and 14 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"xremote_app.h": "c",
"xremote_saved_view.h": "c",
"infrared_remote.h": "c",
"xremote_navigation_view.h": "c"
"xremote_navigation_view.h": "c",
"xremote_settings_view.h": "c"
}
}
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ Button name | Description
`Vol_dn` | Volume down
`Ch_next` | Next channel
`Ch_prev` | Previous channel
`Jump_fo` | Jump forward
`Jump_ba` | Jump backward
`Next` | Jump forward
`Prev` | Jump backward
`Fast_fo` | Fast forward
`Fast_ba` | Fast backward
`Play_pa` | Play/Pause
Expand Down
19 changes: 18 additions & 1 deletion infrared/infrared_signal.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
https://github.com/DarkFlippers/unleashed-firmware
The original project is licensed under the GNU GPLv3
No modifications were made to this file.
Modifications made:
- Added function infrared_signal_transmit_times()
*/

#include "infrared_signal.h"
Expand Down Expand Up @@ -321,3 +323,18 @@ void infrared_signal_transmit(InfraredSignal* signal) {
infrared_send(message, 1);
}
}

void infrared_signal_transmit_times(InfraredSignal* signal, int times) {
if(signal->is_raw) {
InfraredRawSignal* raw_signal = &signal->payload.raw;
infrared_send_raw_ext(
raw_signal->timings,
raw_signal->timings_size,
true,
raw_signal->frequency,
raw_signal->duty_cycle);
} else {
InfraredMessage* message = &signal->payload.message;
infrared_send(message, times);
}
}
5 changes: 4 additions & 1 deletion infrared/infrared_signal.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
https://github.com/DarkFlippers/unleashed-firmware
The original project is licensed under the GNU GPLv3
No modifications were made to this file.
Modifications made:
- Added function infrared_signal_transmit_times()
*/

#pragma once
Expand Down Expand Up @@ -51,3 +53,4 @@ bool infrared_signal_search_and_read(
const FuriString* name);

void infrared_signal_transmit(InfraredSignal* signal);
void infrared_signal_transmit_times(InfraredSignal* signal, int times);
4 changes: 2 additions & 2 deletions views/xremote_common_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
#define XREMOTE_COMMAND_DOWN "Down"
#define XREMOTE_COMMAND_LEFT "Left"
#define XREMOTE_COMMAND_RIGHT "Right"
#define XREMOTE_COMMAND_JUMP_FORWARD "Jump_fo"
#define XREMOTE_COMMAND_JUMP_BACKWARD "Jump_ba"
#define XREMOTE_COMMAND_JUMP_FORWARD "Next"
#define XREMOTE_COMMAND_JUMP_BACKWARD "Prev"
#define XREMOTE_COMMAND_FAST_FORWARD "Fast_fo"
#define XREMOTE_COMMAND_FAST_BACKWARD "Fast_ba"
#define XREMOTE_COMMAND_PLAY_PAUSE "Play_pa"
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 16
#define XREMOTE_BUILD_NUMBER 18

void xremote_get_version(char *version, size_t length);
102 changes: 99 additions & 3 deletions xremote_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,108 @@

#include "xremote_app.h"


#define XREMOTE_APP_SETTINGS ANY_PATH("infrared/assets/xremote.cfg")
#define TAG "XRemoteApp"

XRemoteAppSettings* xremote_app_settings_alloc()
{
XRemoteAppSettings* settings = malloc(sizeof(XRemoteAppSettings));
settings->orientation = ViewOrientationVertical;
settings->repeat_count = 1;
return settings;
}

void xremote_app_settings_free(XRemoteAppSettings* settings)
{
xremote_app_assert_void(settings);
free(settings);
}

bool xremote_app_settings_store(XRemoteAppSettings* settings)
{
Storage* storage = furi_record_open(RECORD_STORAGE);
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 {
/* Write header in config file */
if (!flipper_format_file_open_always(ff, XREMOTE_APP_SETTINGS)) break;
if (!flipper_format_write_header_cstr(ff, "XRemote settings file", 1)) break;
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;

success = true;
} while(false);

furi_record_close(RECORD_STORAGE);
flipper_format_free(ff);

return success;
}

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;
bool success = false;

do {
/* Open file and read the header */
if (!flipper_format_buffered_file_open_existing(ff, XREMOTE_APP_SETTINGS)) break;
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;

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

return success;
}

XRemoteAppContext* xremote_app_context_alloc(void *arg)
{
XRemoteAppContext* ctx = malloc(sizeof(XRemoteAppContext));
ctx->app_argument = arg;

/* Open GUI and norification records */
ctx->gui = furi_record_open(RECORD_GUI);
ctx->notifications = furi_record_open(RECORD_NOTIFICATION);
ctx->view_dispatcher = view_dispatcher_alloc();
ctx->arg = arg;

/* Allocate and load global app settings */
ctx->app_settings = xremote_app_settings_alloc();
xremote_app_settings_load(ctx->app_settings);

/* Allocate and setup view dispatcher */
ctx->view_dispatcher = view_dispatcher_alloc();
view_dispatcher_enable_queue(ctx->view_dispatcher);
view_dispatcher_attach_to_gui(ctx->view_dispatcher, ctx->gui, ViewDispatcherTypeFullscreen);
return ctx;
Expand All @@ -25,6 +119,7 @@ void xremote_app_context_free(XRemoteAppContext* ctx)
{
xremote_app_assert_void(ctx);
notification_internal_message(ctx->notifications, &sequence_reset_blue);
xremote_app_settings_free(ctx->app_settings);
view_dispatcher_free(ctx->view_dispatcher);
furi_record_close(RECORD_NOTIFICATION);
furi_record_close(RECORD_GUI);
Expand Down Expand Up @@ -103,7 +198,8 @@ void xremote_app_submenu_alloc(XRemoteApp* app, uint32_t index, ViewNavigationCa
app->submenu = submenu_alloc();
app->submenu_id = index;

submenu_set_orientation(app->submenu, ViewOrientationVertical);
XRemoteAppSettings *settings = app->app_ctx->app_settings;
submenu_set_orientation(app->submenu, settings->orientation);
view_set_previous_callback(submenu_get_view(app->submenu), prev_cb);

ViewDispatcher* view_disp = app->app_ctx->view_dispatcher;
Expand Down
15 changes: 14 additions & 1 deletion xremote_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <notification/notification.h>
#include <notification/notification_messages.h>

#include <flipper_format/flipper_format.h>
#include <storage/storage.h>
#include <dialogs/dialogs.h>

Expand All @@ -28,15 +29,27 @@
#define xremote_app_assert(cond, var) if (!cond) return var

typedef struct {
ViewOrientation orientation;
uint32_t repeat_count;
} XRemoteAppSettings;

XRemoteAppSettings* xremote_app_settings_alloc();
void xremote_app_settings_free(XRemoteAppSettings* settings);

typedef struct {
XRemoteAppSettings* app_settings;
NotificationApp* notifications;
ViewDispatcher* view_dispatcher;
void* app_argument;
Gui* gui;
void* arg;
} XRemoteAppContext;

XRemoteAppContext* xremote_app_context_alloc(void* arg);
void xremote_app_context_free(XRemoteAppContext* ctx);

bool xremote_app_settings_store(XRemoteAppSettings* settings);
bool xremote_app_settings_load(XRemoteAppSettings* settings);

typedef XRemoteView* (*XRemoteViewAllocator)(NotificationApp* notifications);
typedef void (*XRemoteAppClearCallback)(void *context);

Expand Down
2 changes: 1 addition & 1 deletion xremote_settings.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@license This project is released under the GNU GPLv3 License
* @copyright (c) 2023 Sandro Kalatozishvili ([email protected])
*
* @brief Remote controller application menu and view factory.
* @brief XRemote settins functionality and menu.
*/

#include "xremote_settings.h"
Expand Down
2 changes: 1 addition & 1 deletion xremote_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@license This project is released under the GNU GPLv3 License
* @copyright (c) 2023 Sandro Kalatozishvili ([email protected])
*
* @brief Settings application menu and view factory.
* @brief XRemote settins functionality and menu.
*/

#pragma once
Expand Down

0 comments on commit 958af1d

Please sign in to comment.