Save Infrared GPIO settings

This commit is contained in:
Sil333033
2023-11-20 20:38:30 +01:00
parent ca6846fa3d
commit 23c40f397a
5 changed files with 138 additions and 3 deletions

View File

@@ -204,6 +204,21 @@ static InfraredApp* infrared_alloc() {
infrared->loading = loading_alloc();
infrared->progress = infrared_progress_view_alloc();
infrared->last_settings = infrared_last_settings_alloc();
infrared_last_settings_load(infrared->last_settings);
if(infrared->last_settings->ext_5v) {
uint8_t attempts = 0;
while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
furi_hal_power_enable_otg();
furi_delay_ms(10);
}
}
if(infrared->last_settings->ext_out && !furi_hal_infrared_get_debug_out_status()) {
furi_hal_infrared_set_debug_out(true);
}
return infrared;
}
@@ -271,6 +286,14 @@ static void infrared_free(InfraredApp* infrared) {
furi_string_free(infrared->file_path);
furi_string_free(infrared->button_name);
if(infrared->last_settings->ext_5v) {
if(furi_hal_power_is_otg_enabled()) {
furi_hal_power_disable_otg();
}
}
infrared_last_settings_free(infrared->last_settings);
free(infrared);
}

View File

@@ -31,6 +31,7 @@
#include "infrared_remote.h"
#include "infrared_brute_force.h"
#include "infrared_custom_event.h"
#include "infrared_last_settings.h"
#include "scenes/infrared_scene.h"
#include "views/infrared_progress_view.h"
@@ -38,6 +39,7 @@
#include "views/infrared_move_view.h"
#include "rpc/rpc_app.h"
#include <furi_hal_infrared.h>
#define INFRARED_FILE_NAME_SIZE 100
#define INFRARED_TEXT_STORE_NUM 2
@@ -127,6 +129,7 @@ struct InfraredApp {
/** Arbitrary text storage for various inputs. */
char text_store[INFRARED_TEXT_STORE_NUM][INFRARED_TEXT_STORE_SIZE + 1];
InfraredAppState app_state; /**< Application state. */
InfraredLastSettings* last_settings; /**< Last settings. */
void* rpc_ctx; /**< Pointer to the RPC context object. */
};

View File

@@ -0,0 +1,88 @@
#include "infrared_last_settings.h"
#define TAG "InfraredLastSettings"
#define INFRARED_LAST_SETTINGS_FILE_TYPE "Flipper Infrared Last Settings File"
#define INFRARED_LAST_SETTINGS_FILE_VERSION 1
#define INFRARED_LAST_SETTINGS_PATH EXT_PATH("infrared/assets/last_infrared.settings")
#define INFRARED_LAST_SETTINGS_FIELD_EXTPOWER "External5V"
#define INFRARED_LAST_SETTINGS_FIELD_EXTOUT "ExternalOut"
InfraredLastSettings* infrared_last_settings_alloc(void) {
InfraredLastSettings* instance = malloc(sizeof(InfraredLastSettings));
return instance;
}
void infrared_last_settings_free(InfraredLastSettings* instance) {
furi_assert(instance);
free(instance);
}
void infrared_last_settings_load(InfraredLastSettings* instance) {
furi_assert(instance);
Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* fff_data_file = flipper_format_file_alloc(storage);
bool temp_extpower = false;
bool temp_extout = false;
if(FSE_OK == storage_sd_status(storage) && INFRARED_LAST_SETTINGS_PATH &&
flipper_format_file_open_existing(fff_data_file, INFRARED_LAST_SETTINGS_PATH)) {
flipper_format_read_bool(
fff_data_file, INFRARED_LAST_SETTINGS_FIELD_EXTPOWER, (bool*)&temp_extpower, 1);
flipper_format_read_bool(
fff_data_file, INFRARED_LAST_SETTINGS_FIELD_EXTOUT, (bool*)&temp_extout, 1);
} else {
FURI_LOG_E(TAG, "Error open file %s", INFRARED_LAST_SETTINGS_PATH);
}
instance->ext_5v = temp_extpower;
instance->ext_out = temp_extout;
flipper_format_file_close(fff_data_file);
flipper_format_free(fff_data_file);
furi_record_close(RECORD_STORAGE);
}
bool infrared_last_settings_save(InfraredLastSettings* instance) {
furi_assert(instance);
bool saved = false;
Storage* storage = furi_record_open(RECORD_STORAGE);
FlipperFormat* file = flipper_format_file_alloc(storage);
do {
if(FSE_OK != storage_sd_status(storage)) {
break;
}
// Open file
if(!flipper_format_file_open_always(file, INFRARED_LAST_SETTINGS_PATH)) break;
// Write header
if(!flipper_format_write_header_cstr(
file, INFRARED_LAST_SETTINGS_FILE_TYPE, INFRARED_LAST_SETTINGS_FILE_VERSION))
break;
if(!flipper_format_insert_or_update_bool(
file, INFRARED_LAST_SETTINGS_FIELD_EXTPOWER, &instance->ext_5v, 1))
break;
if(!flipper_format_insert_or_update_bool(
file, INFRARED_LAST_SETTINGS_FIELD_EXTOUT, &instance->ext_out, 1))
break;
saved = true;
} while(0);
if(!saved) {
FURI_LOG_E(TAG, "Error save file %s", INFRARED_LAST_SETTINGS_PATH);
}
flipper_format_file_close(file);
flipper_format_free(file);
furi_record_close(RECORD_STORAGE);
return saved;
}

View File

@@ -0,0 +1,15 @@
#pragma once
#include <furi_hal.h>
#include <storage/storage.h>
#include <lib/flipper_format/flipper_format.h>
typedef struct {
bool ext_5v;
bool ext_out;
} InfraredLastSettings;
InfraredLastSettings* infrared_last_settings_alloc(void);
void infrared_last_settings_free(InfraredLastSettings* instance);
void infrared_last_settings_load(InfraredLastSettings* instance);
bool infrared_last_settings_save(InfraredLastSettings* instance);

View File

@@ -1,5 +1,4 @@
#include "../infrared_app_i.h"
#include <furi_hal_infrared.h>
uint8_t value_index_ir;
@@ -12,14 +11,17 @@ const char* const infrared_debug_cfg_variables_text[] = {
static void infrared_scene_debug_settings_changed(VariableItem* item) {
InfraredApp* infrared = variable_item_get_context(item);
value_index_ir = variable_item_get_current_value_index(item);
UNUSED(infrared);
variable_item_set_current_value_text(item, infrared_debug_cfg_variables_text[value_index_ir]);
furi_hal_infrared_set_debug_out(value_index_ir);
infrared->last_settings->ext_out = value_index_ir == 1;
infrared_last_settings_save(infrared->last_settings);
}
static void infrared_scene_debug_settings_power_changed(VariableItem* item) {
InfraredApp* infrared = variable_item_get_context(item);
bool value = variable_item_get_current_value_index(item);
if(value) {
for(int i = 0; i < 5 && !furi_hal_power_is_otg_enabled(); i++) {
@@ -32,6 +34,9 @@ static void infrared_scene_debug_settings_power_changed(VariableItem* item) {
}
}
variable_item_set_current_value_text(item, value ? "ON" : "OFF");
infrared->last_settings->ext_5v = value;
infrared_last_settings_save(infrared->last_settings);
}
static void infrared_debug_settings_start_var_list_enter_callback(void* context, uint32_t index) {
@@ -64,7 +69,8 @@ void infrared_scene_debug_settings_on_enter(void* context) {
2,
infrared_scene_debug_settings_power_changed,
infrared);
bool enabled = furi_hal_power_is_otg_enabled();
bool enabled = furi_hal_power_is_otg_enabled() ||
furi_hal_power_is_charging(); // 5v is enabled via hardware if charging
variable_item_set_current_value_index(item, enabled);
variable_item_set_current_value_text(item, enabled ? "ON" : "OFF");