Skip to content

Commit

Permalink
Implemented settings load and store functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
Sandro Kalatozishvili committed Sep 29, 2023
1 parent 845666e commit 2cdc442
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 9 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);
86 changes: 85 additions & 1 deletion xremote_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,94 @@

#include "xremote_app.h"

#define XREMOTE_APP_SETTINGS "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 file: \'%s\'", XREMOTE_APP_SETTINGS);
bool success = false;

do {
if (!flipper_format_file_open_always(ff, XREMOTE_APP_SETTINGS)) break;
if (!flipper_format_write_header_cstr(ff, "XRemote settings file", 1)) break;

if (settings->orientation == ViewOrientationHorizontal &&
!flipper_format_write_string_cstr(ff, "orientation", "horizontal")) break;
else if (settings->orientation == ViewOrientationVertical &&
!flipper_format_write_string_cstr(ff, "orientation", "vertical")) break;

if (!flipper_format_write_uint32(ff, "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 file: \'%s\'", XREMOTE_APP_SETTINGS);
uint32_t version = 0;
uint32_t repeat = 0;
bool success = false;

do {
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;

if (!flipper_format_read_string(ff, "orientation", orient)) break;
if (!flipper_format_read_uint32(ff, "repeat", &repeat, 1)) break;

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->gui = furi_record_open(RECORD_GUI);
ctx->notifications = furi_record_open(RECORD_NOTIFICATION);
ctx->view_dispatcher = view_dispatcher_alloc();
ctx->app_settings = xremote_app_settings_alloc();
ctx->arg = arg;

view_dispatcher_enable_queue(ctx->view_dispatcher);
Expand All @@ -25,6 +107,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 +186,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
13 changes: 13 additions & 0 deletions 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,6 +29,15 @@
#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;
Gui* gui;
Expand All @@ -37,6 +47,9 @@ typedef struct {
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

0 comments on commit 2cdc442

Please sign in to comment.