From 7c4f6de06fd0625ad8cd308b3fdace26ae7d9d2d Mon Sep 17 00:00:00 2001 From: Sil333033 <94360907+Sil333033@users.noreply.github.com> Date: Sat, 27 Jan 2024 20:33:10 +0100 Subject: [PATCH] Add automatic IR blaster detection removed last_settings because it isnt needed anymore --- applications/main/infrared/infrared_app.c | 17 ++-- applications/main/infrared/infrared_app_i.h | 4 +- .../main/infrared/infrared_last_settings.c | 88 ------------------- .../main/infrared/infrared_last_settings.h | 15 ---- .../scenes/infrared_scene_debug_settings.c | 34 ++++--- targets/f7/api_symbols.csv | 3 + targets/f7/furi_hal/furi_hal_infrared.c | 35 ++++++++ targets/furi_hal_include/furi_hal_infrared.h | 19 ++++ 8 files changed, 87 insertions(+), 128 deletions(-) delete mode 100644 applications/main/infrared/infrared_last_settings.c delete mode 100644 applications/main/infrared/infrared_last_settings.h diff --git a/applications/main/infrared/infrared_app.c b/applications/main/infrared/infrared_app.c index dc66e748b..88dcb7559 100644 --- a/applications/main/infrared/infrared_app.c +++ b/applications/main/infrared/infrared_app.c @@ -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); } diff --git a/applications/main/infrared/infrared_app_i.h b/applications/main/infrared/infrared_app_i.h index 544fde426..72797d1de 100644 --- a/applications/main/infrared/infrared_app_i.h +++ b/applications/main/infrared/infrared_app_i.h @@ -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. */ }; diff --git a/applications/main/infrared/infrared_last_settings.c b/applications/main/infrared/infrared_last_settings.c deleted file mode 100644 index 265c66ed4..000000000 --- a/applications/main/infrared/infrared_last_settings.c +++ /dev/null @@ -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; -} \ No newline at end of file diff --git a/applications/main/infrared/infrared_last_settings.h b/applications/main/infrared/infrared_last_settings.h deleted file mode 100644 index 45a15b2dd..000000000 --- a/applications/main/infrared/infrared_last_settings.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#include -#include -#include - -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); \ No newline at end of file diff --git a/applications/main/infrared/scenes/infrared_scene_debug_settings.c b/applications/main/infrared/scenes/infrared_scene_debug_settings.c index c1bed99cb..4cebc8fa9 100644 --- a/applications/main/infrared/scenes/infrared_scene_debug_settings.c +++ b/applications/main/infrared/scenes/infrared_scene_debug_settings.c @@ -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); } diff --git a/targets/f7/api_symbols.csv b/targets/f7/api_symbols.csv index 160085085..5c1d310c0 100644 --- a/targets/f7/api_symbols.csv +++ b/targets/f7/api_symbols.csv @@ -1319,8 +1319,11 @@ Function,+,furi_hal_infrared_async_tx_set_signal_sent_isr_callback,void,"FuriHal Function,+,furi_hal_infrared_async_tx_start,void,"uint32_t, float" Function,+,furi_hal_infrared_async_tx_stop,void, Function,+,furi_hal_infrared_async_tx_wait_termination,void, +Function,+,furi_hal_infrared_block_external_output,void,_Bool Function,+,furi_hal_infrared_get_debug_out_status,_Bool, Function,+,furi_hal_infrared_is_busy,_Bool, +Function,+,furi_hal_infrared_is_external_connected,_Bool, +Function,+,furi_hal_infrared_is_external_output_blocked,_Bool, Function,+,furi_hal_infrared_set_debug_out,void,_Bool Function,-,furi_hal_init,void, Function,-,furi_hal_init_early,void, diff --git a/targets/f7/furi_hal/furi_hal_infrared.c b/targets/f7/furi_hal/furi_hal_infrared.c index 03a16b70f..0e6dc5c00 100644 --- a/targets/f7/furi_hal/furi_hal_infrared.c +++ b/targets/f7/furi_hal/furi_hal_infrared.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include @@ -79,6 +80,7 @@ static volatile InfraredState furi_hal_infrared_state = InfraredStateIdle; static InfraredTimTx infrared_tim_tx; static InfraredTimRx infrared_tim_rx; static bool infrared_external_output; +static bool block_external; static void furi_hal_infrared_tx_fill_buffer(uint8_t buf_num, uint8_t polarity_shift); static void furi_hal_infrared_async_tx_free_resources(void); @@ -648,12 +650,29 @@ void furi_hal_infrared_async_tx_start(uint32_t freq, float duty_cycle) { furi_delay_us(5); LL_TIM_GenerateEvent_UPDATE(INFRARED_DMA_TIMER); /* DMA -> TIMx_RCR */ furi_delay_us(5); + + if(block_external) { + infrared_external_output = false; + } else { + infrared_external_output = furi_hal_infrared_is_external_connected(); + } + if(infrared_external_output) { + if(!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); + } + furi_delay_ms(100); + } + LL_GPIO_ResetOutputPin( gpio_ext_pa7.port, gpio_ext_pa7.pin); /* when disable it prevents false pulse */ furi_hal_gpio_init_ex( &gpio_ext_pa7, GpioModeAltFunctionPushPull, GpioPullUp, GpioSpeedHigh, GpioAltFn1TIM1); } else { + furi_hal_power_disable_otg(); LL_GPIO_ResetOutputPin( gpio_infrared_tx.port, gpio_infrared_tx.pin); /* when disable it prevents false pulse */ @@ -708,3 +727,19 @@ void furi_hal_infrared_async_tx_set_signal_sent_isr_callback( infrared_tim_tx.signal_sent_callback = callback; infrared_tim_tx.signal_sent_context = context; } + +bool furi_hal_infrared_is_external_connected() { + furi_hal_gpio_init(&gpio_ext_pa7, GpioModeInput, GpioPullUp, GpioSpeedHigh); + furi_delay_ms(1); + bool is_external_connected = !furi_hal_gpio_read(&gpio_ext_pa7); + furi_hal_gpio_init(&gpio_ext_pa7, GpioModeAnalog, GpioPullDown, GpioSpeedLow); + return is_external_connected; +} + +void furi_hal_infrared_block_external_output(bool block) { + block_external = block; +} + +bool furi_hal_infrared_is_external_output_blocked(void) { + return block_external; +} \ No newline at end of file diff --git a/targets/furi_hal_include/furi_hal_infrared.h b/targets/furi_hal_include/furi_hal_infrared.h index bac3aba1e..01362e366 100644 --- a/targets/furi_hal_include/furi_hal_infrared.h +++ b/targets/furi_hal_include/furi_hal_infrared.h @@ -149,6 +149,25 @@ void furi_hal_infrared_async_tx_set_signal_sent_isr_callback( FuriHalInfraredTxSignalSentISRCallback callback, void* context); +/** Check if a module (like IR Blaster) is connected to PA7 + * + * return true if a module is connected, false otherwise + */ +bool furi_hal_infrared_is_external_connected(); + +/** Block external output on PA7 + * + * if blocked, its forced to internal IR. If unblocked, external IR is used if connected + * @param block true to block, false to unblock + */ +void furi_hal_infrared_block_external_output(bool block); + +/** Check if external output on PA7 is blocked + * + * @return true if blocked, false otherwise + */ +bool furi_hal_infrared_is_external_output_blocked(); + #ifdef __cplusplus } #endif