Loading States & fix label length

This commit is contained in:
gid9798 2023-05-19 23:21:18 +03:00
parent 1d79e149cf
commit 47f6d78737
6 changed files with 60 additions and 26 deletions

View File

@ -25,8 +25,23 @@ typedef enum {
SubRemViewIDRemote, SubRemViewIDRemote,
} SubRemViewID; } SubRemViewID;
typedef enum {
SubRemLoadSubStateNotSet,
SubRemLoadSubStatePreloaded,
SubRemLoadSubStateError,
SubRemLoadSubStateErrorNoFile,
SubRemLoadSubStateErrorFreq,
SubRemLoadSubStateErrorMod,
SubRemLoadSubStateErrorProtocol,
SubRemLoadSubStateOK,
} SubRemLoadSubState;
typedef enum { typedef enum {
SubRemLoadMapStateBack = 0, SubRemLoadMapStateBack = 0,
SubRemLoadMapStateError, SubRemLoadMapStateError,
SubRemLoadMapStateErrorOpenError,
SubRemLoadMapStateErrorStorage,
SubRemLoadMapStateErrorBrokenFile,
SubRemLoadMapStateNotAllOK,
SubRemLoadMapStateOK, SubRemLoadMapStateOK,
} SubRemLoadMapState; } SubRemLoadMapState;

View File

@ -18,7 +18,7 @@ void subrem_scene_openmapfile_on_enter(void* context) {
dialog_message_free(message); dialog_message_free(message);
#endif #endif
} }
if(load_state == SubRemLoadMapStateOK) { if(load_state == SubRemLoadMapStateOK || load_state == SubRemLoadMapStateNotAllOK) {
scene_manager_next_scene(app->scene_manager, SubRemSceneRemote); scene_manager_next_scene(app->scene_manager, SubRemSceneRemote);
} else { } else {
// TODO: Map Preset Reset // TODO: Map Preset Reset

View File

@ -34,8 +34,10 @@ void subrem_scene_start_on_enter(void* context) {
// subrem_scene_start_submenu_callback, // subrem_scene_start_submenu_callback,
// app); // app);
submenu_set_selected_item( // TODO: set scene state in subrem alloc
submenu, scene_manager_get_scene_state(app->scene_manager, SubRemSceneStart)); // submenu_set_selected_item(
// submenu, scene_manager_get_scene_state(app->scene_manager, SubRemSceneStart));
submenu_set_selected_item(submenu, SubmenuIndexSubRemOpenMapFile);
view_dispatcher_switch_to_view(app->view_dispatcher, SubRemViewSubmenu); view_dispatcher_switch_to_view(app->view_dispatcher, SubRemViewSubmenu);
} }

View File

@ -51,6 +51,7 @@ SubRemSubFilePreset* subrem_sub_file_preset_alloc() {
sub_preset->label = furi_string_alloc_set_str("N/A"); sub_preset->label = furi_string_alloc_set_str("N/A");
sub_preset->type = SubGhzProtocolTypeUnknown; sub_preset->type = SubGhzProtocolTypeUnknown;
sub_preset->load_state = SubRemLoadSubStateNotSet;
return sub_preset; return sub_preset;
} }
@ -77,6 +78,7 @@ static void subrem_sub_file_preset_reset(SubRemSubFilePreset* sub_preset) {
stream_clean(fff_data_stream); stream_clean(fff_data_stream);
sub_preset->type = SubGhzProtocolTypeUnknown; sub_preset->type = SubGhzProtocolTypeUnknown;
sub_preset->load_state = SubRemLoadSubStateNotSet;
} }
void subrem_map_preset_reset(SubGhzRemoteApp* app) { void subrem_map_preset_reset(SubGhzRemoteApp* app) {
@ -108,6 +110,7 @@ static bool subrem_map_preset_load(SubGhzRemoteApp* app, FlipperFormat* fff_data
#endif #endif
path_extract_filename(sub_preset->file_path, sub_preset->label, true); path_extract_filename(sub_preset->file_path, sub_preset->label, true);
} else { } else {
// Preload seccesful
FURI_LOG_I( FURI_LOG_I(
TAG, TAG,
"%-5s: %s %s", "%-5s: %s %s",
@ -115,6 +118,7 @@ static bool subrem_map_preset_load(SubGhzRemoteApp* app, FlipperFormat* fff_data
furi_string_get_cstr(sub_preset->label), furi_string_get_cstr(sub_preset->label),
furi_string_get_cstr(sub_preset->file_path)); furi_string_get_cstr(sub_preset->file_path));
ret = true; ret = true;
sub_preset->load_state = SubRemLoadSubStatePreloaded;
} }
flipper_format_rewind(fff_data_file); flipper_format_rewind(fff_data_file);
} }
@ -268,21 +272,25 @@ bool subrem_tx_stop_sub(SubGhzRemoteApp* app, bool forced) {
return false; return false;
} }
static bool subrem_map_preset_check(SubGhzRemoteApp* app, FlipperFormat* fff_data_file) { static SubRemLoadMapState
subrem_map_preset_check(SubGhzRemoteApp* app, FlipperFormat* fff_data_file) {
furi_assert(app); furi_assert(app);
FuriString* temp_str = furi_string_alloc(); FuriString* temp_str = furi_string_alloc();
uint32_t temp_data32; uint32_t temp_data32;
bool ret = false; bool all_loaded = true;
bool sub_preset_loaded = false; SubRemLoadMapState ret = SubRemLoadMapStateErrorBrokenFile;
SubRemLoadSubState sub_preset_loaded;
SubRemSubFilePreset* sub_preset; SubRemSubFilePreset* sub_preset;
uint32_t repeat = 200; uint32_t repeat = 200;
for(uint8_t i = 0; i < SubRemSubKeyNameMaxCount; i++) { for(uint8_t i = 0; i < SubRemSubKeyNameMaxCount; i++) {
sub_preset = app->subs_preset[i]; sub_preset = app->subs_preset[i];
sub_preset_loaded = false;
if(furi_string_empty(sub_preset->file_path)) { if(furi_string_empty(sub_preset->file_path)) {
// FURI_LOG_I(TAG, "Empty file path"); // FURI_LOG_I(TAG, "Empty file path");
continue; continue;
} }
sub_preset_loaded = SubRemLoadSubStateErrorNoFile;
repeat = 200; repeat = 200;
do { do {
if(!flipper_format_file_open_existing( if(!flipper_format_file_open_existing(
@ -305,6 +313,7 @@ static bool subrem_map_preset_check(SubGhzRemoteApp* app, FlipperFormat* fff_dat
} }
//Load frequency //Load frequency
sub_preset_loaded = SubRemLoadSubStateErrorFreq;
if(!flipper_format_read_uint32(fff_data_file, "Frequency", &temp_data32, 1)) { if(!flipper_format_read_uint32(fff_data_file, "Frequency", &temp_data32, 1)) {
FURI_LOG_W(TAG, "Cannot read frequency. Set default frequency"); FURI_LOG_W(TAG, "Cannot read frequency. Set default frequency");
sub_preset->freq_preset.frequency = sub_preset->freq_preset.frequency =
@ -317,6 +326,7 @@ static bool subrem_map_preset_check(SubGhzRemoteApp* app, FlipperFormat* fff_dat
} }
//Load preset //Load preset
sub_preset_loaded = SubRemLoadSubStateErrorMod;
if(!flipper_format_read_string(fff_data_file, "Preset", temp_str)) { if(!flipper_format_read_string(fff_data_file, "Preset", temp_str)) {
FURI_LOG_E(TAG, "Missing Preset"); FURI_LOG_E(TAG, "Missing Preset");
break; break;
@ -329,6 +339,7 @@ static bool subrem_map_preset_check(SubGhzRemoteApp* app, FlipperFormat* fff_dat
} }
//Load protocol //Load protocol
sub_preset_loaded = SubRemLoadSubStateErrorProtocol;
if(!flipper_format_read_string(fff_data_file, "Protocol", temp_str)) { if(!flipper_format_read_string(fff_data_file, "Protocol", temp_str)) {
FURI_LOG_E(TAG, "Missing Protocol"); FURI_LOG_E(TAG, "Missing Protocol");
break; break;
@ -375,8 +386,9 @@ static bool subrem_map_preset_check(SubGhzRemoteApp* app, FlipperFormat* fff_dat
break; break;
} }
sub_preset_loaded = true; sub_preset_loaded = SubRemLoadSubStateOK;
ret |= true; ret = SubRemLoadMapStateNotAllOK;
#if FURI_DEBUG #if FURI_DEBUG
FURI_LOG_I(TAG, "%-16s - protocol Loaded", furi_string_get_cstr(sub_preset->label)); FURI_LOG_I(TAG, "%-16s - protocol Loaded", furi_string_get_cstr(sub_preset->label));
#endif #endif
@ -385,9 +397,15 @@ static bool subrem_map_preset_check(SubGhzRemoteApp* app, FlipperFormat* fff_dat
// TODO: // TODO:
// Load file state logic // Load file state logic
// Label depending on the state // Label depending on the state
// Move to remote scene
if(!sub_preset_loaded) { if(sub_preset_loaded != SubRemLoadSubStateOK) {
furi_string_set_str(sub_preset->label, "N/A"); furi_string_set_str(sub_preset->label, "N/A");
all_loaded = false;
}
if(ret != SubRemLoadMapStateErrorBrokenFile && all_loaded) {
ret = SubRemLoadMapStateOK;
} }
flipper_format_file_close(fff_data_file); flipper_format_file_close(fff_data_file);
@ -397,7 +415,7 @@ static bool subrem_map_preset_check(SubGhzRemoteApp* app, FlipperFormat* fff_dat
return ret; return ret;
} }
bool subrem_map_file_load(SubGhzRemoteApp* app, const char* file_path) { SubRemLoadMapState subrem_map_file_load(SubGhzRemoteApp* app, const char* file_path) {
furi_assert(app); furi_assert(app);
furi_assert(file_path); furi_assert(file_path);
#if FURI_DEBUG #if FURI_DEBUG
@ -405,7 +423,7 @@ bool subrem_map_file_load(SubGhzRemoteApp* app, const char* file_path) {
#endif #endif
Storage* storage = furi_record_open(RECORD_STORAGE); Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* fff_data_file = flipper_format_file_alloc(storage); FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
bool ret = false; SubRemLoadMapState ret = SubRemLoadMapStateErrorOpenError;
#if FURI_DEBUG #if FURI_DEBUG
FURI_LOG_I(TAG, "Open Map File.."); FURI_LOG_I(TAG, "Open Map File..");
#endif #endif
@ -413,20 +431,23 @@ bool subrem_map_file_load(SubGhzRemoteApp* app, const char* file_path) {
if(!flipper_format_file_open_existing(fff_data_file, file_path)) { if(!flipper_format_file_open_existing(fff_data_file, file_path)) {
FURI_LOG_E(TAG, "Could not open MAP file %s", file_path); FURI_LOG_E(TAG, "Could not open MAP file %s", file_path);
ret = SubRemLoadMapStateErrorOpenError;
} else { } else {
if(!subrem_map_preset_load(app, fff_data_file)) { if(!subrem_map_preset_load(app, fff_data_file)) {
FURI_LOG_E(TAG, "Could no Sub file path in MAP file"); FURI_LOG_E(TAG, "Could no Sub file path in MAP file");
// ret = // error for popup // ret = // error for popup
} else if( } else if(!flipper_format_file_close(fff_data_file)) {
(flipper_format_file_close(fff_data_file)) && ret = SubRemLoadMapStateErrorOpenError;
(subrem_map_preset_check(app, fff_data_file))) { } else {
FURI_LOG_I(TAG, "Load Map File Seccesful"); ret = subrem_map_preset_check(app, fff_data_file);
ret = true;
} }
} }
// TODO: Popup for error or return error type if(ret == SubRemLoadMapStateOK) {
if(!ret) { FURI_LOG_I(TAG, "Load Map File Seccesful");
} else if(ret == SubRemLoadMapStateNotAllOK) {
FURI_LOG_I(TAG, "Load Map File Seccesful [Not all files]");
} else {
FURI_LOG_E(TAG, "Broken Map File"); FURI_LOG_E(TAG, "Broken Map File");
} }
@ -434,7 +455,6 @@ bool subrem_map_file_load(SubGhzRemoteApp* app, const char* file_path) {
flipper_format_free(fff_data_file); flipper_format_free(fff_data_file);
furi_record_close(RECORD_STORAGE); furi_record_close(RECORD_STORAGE);
return ret; return ret;
} }
@ -450,10 +470,8 @@ SubRemLoadMapState subrem_load_from_file(SubGhzRemoteApp* app) {
// Input events and views are managed by file_select // Input events and views are managed by file_select
if(!dialog_file_browser_show(app->dialogs, app->file_path, app->file_path, &browser_options)) { if(!dialog_file_browser_show(app->dialogs, app->file_path, app->file_path, &browser_options)) {
} else if(subrem_map_file_load(app, furi_string_get_cstr(app->file_path))) {
ret = SubRemLoadMapStateOK;
} else { } else {
ret = SubRemLoadMapStateError; ret = subrem_map_file_load(app, furi_string_get_cstr(app->file_path));
} }
furi_string_free(file_path); furi_string_free(file_path);

View File

@ -42,6 +42,7 @@ typedef struct {
FuriString* protocaol_name; FuriString* protocaol_name;
FuriString* label; FuriString* label;
SubGhzProtocolType type; SubGhzProtocolType type;
SubRemLoadSubState load_state;
} SubRemSubFilePreset; } SubRemSubFilePreset;
SubRemSubFilePreset* subrem_sub_file_preset_alloc(); SubRemSubFilePreset* subrem_sub_file_preset_alloc();
@ -70,8 +71,6 @@ typedef struct {
bool tx_running; bool tx_running;
uint8_t chusen_sub; uint8_t chusen_sub;
// TODO: LoadFileError
} SubGhzRemoteApp; } SubGhzRemoteApp;
SubRemLoadMapState subrem_load_from_file(SubGhzRemoteApp* app); SubRemLoadMapState subrem_load_from_file(SubGhzRemoteApp* app);

View File

@ -4,7 +4,7 @@
#include <input/input.h> #include <input/input.h>
#include <gui/elements.h> #include <gui/elements.h>
#define SUBREM_VIEW_REMOTE_MAX_LABEL_LENGTH 16 #define SUBREM_VIEW_REMOTE_MAX_LABEL_LENGTH 12
struct SubRemViewRemote { struct SubRemViewRemote {
View* view; View* view;