Add automatic IR blaster detection

removed last_settings because it isnt needed anymore
This commit is contained in:
Sil333033
2024-01-27 20:33:10 +01:00
parent cb635ff2d2
commit 7c4f6de06f
8 changed files with 87 additions and 128 deletions

View File

@@ -204,10 +204,7 @@ 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) {
if(furi_hal_infrared_is_external_connected()) {
uint8_t attempts = 0;
while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
furi_hal_power_enable_otg();
@@ -215,9 +212,7 @@ static InfraredApp* infrared_alloc() {
}
}
if(infrared->last_settings->ext_out && !furi_hal_infrared_get_debug_out_status()) {
furi_hal_infrared_set_debug_out(true);
}
furi_hal_infrared_block_external_output(false);
return infrared;
}
@@ -286,13 +281,11 @@ 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();
}
if(furi_hal_power_is_otg_enabled()) {
furi_hal_power_disable_otg();
}
infrared_last_settings_free(infrared->last_settings);
furi_hal_infrared_block_external_output(false);
free(infrared);
}

View File

@@ -31,7 +31,7 @@
#include "infrared_remote.h"
#include "infrared_brute_force.h"
#include "infrared_custom_event.h"
#include "infrared_last_settings.h"
// #include "infrared_last_settings.h"
#include "scenes/infrared_scene.h"
#include "views/infrared_progress_view.h"
@@ -129,7 +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. */
//InfraredLastSettings* last_settings; /**< Last settings. */
void* rpc_ctx; /**< Pointer to the RPC context object. */
};

View File

@@ -1,88 +0,0 @@
#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

@@ -1,15 +0,0 @@
#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

@@ -9,19 +9,22 @@ 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);
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);
if(value_index_ir == 0) {
furi_hal_infrared_set_debug_out(false);
furi_hal_infrared_block_external_output(true);
if(furi_hal_power_is_otg_enabled()) {
furi_hal_power_disable_otg();
}
} else {
furi_hal_infrared_block_external_output(false);
}
}
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++) {
@@ -34,9 +37,6 @@ 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) {
@@ -49,7 +49,10 @@ void infrared_scene_debug_settings_on_enter(void* context) {
VariableItemList* variable_item_list = infrared->variable_item_list;
value_index_ir = furi_hal_infrared_get_debug_out_status();
value_index_ir =
(furi_hal_infrared_is_external_connected() &&
!furi_hal_infrared_is_external_output_blocked());
VariableItem* item = variable_item_list_add(
variable_item_list,
"Send signal to",
@@ -70,10 +73,19 @@ void infrared_scene_debug_settings_on_enter(void* context) {
infrared_scene_debug_settings_power_changed,
infrared);
bool enabled = furi_hal_power_is_otg_enabled() ||
furi_hal_power_is_charging(); // 5v is enabled via hardware if charging
furi_hal_power_is_charging() || // 5v is enabled via hardware if charging
furi_hal_infrared_is_external_connected();
variable_item_set_current_value_index(item, enabled);
variable_item_set_current_value_text(item, enabled ? "ON" : "OFF");
if(furi_hal_infrared_is_external_connected() && !furi_hal_power_is_otg_enabled()) {
uint8_t attempts = 0;
while(!furi_hal_power_is_otg_enabled() && attempts++ < 5) {
furi_hal_power_enable_otg();
furi_delay_ms(10);
}
}
view_dispatcher_switch_to_view(infrared->view_dispatcher, InfraredViewVariableItemList);
}