Skip to content

Commit

Permalink
[FL-2669] SubGhz: add support for loading custom presets (#1398)
Browse files Browse the repository at this point in the history
* SubGhz: load custom -preset
* SubGhz: fix error prt=0
* SubGhz: load custom preset
* SubGhz: code refactoring to support custom preset
* SubGhz: add custom presert refactoring
* SubGhz: fix alloc history alloc preset
* SubGhz: fix error load file
* SubGhz: fix start custom preset
* SubGhz: fix delete custom preset
* SubGhz: add description Custom_preset_data for CC1101
* SubGhz: debug logging and buffer size rounding

Co-authored-by: あく <[email protected]>
Co-authored-by: Aleksandr Kutuzov <[email protected]>
  • Loading branch information
3 people authored Jul 26, 2022
1 parent ed7db33 commit ec19c11
Show file tree
Hide file tree
Showing 75 changed files with 681 additions and 454 deletions.
13 changes: 13 additions & 0 deletions applications/subghz/helpers/subghz_types.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#pragma once

#include "m-string.h"
#include <furi.h>
#include <furi_hal.h>

/** SubGhzNotification state */
typedef enum {
SubGhzNotificationStateStarting,
Expand Down Expand Up @@ -67,3 +71,12 @@ typedef enum {
SubGhzViewIdTestCarrier,
SubGhzViewIdTestPacket,
} SubGhzViewId;

struct SubGhzPesetDefinition {
string_t name;
uint32_t frequency;
uint8_t* data;
size_t data_size;
};

typedef struct SubGhzPesetDefinition SubGhzPesetDefinition;
8 changes: 6 additions & 2 deletions applications/subghz/scenes/subghz_scene_need_saving.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,12 @@ bool subghz_scene_need_saving_on_event(void* context, SceneManagerEvent event) {
} else if(event.event == SubGhzCustomEventSceneExit) {
if(subghz->txrx->rx_key_state == SubGhzRxKeyStateExit) {
subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE;
subghz->txrx->frequency = subghz_setting_get_default_frequency(subghz->setting);
subghz->txrx->preset = FuriHalSubGhzPresetOok650Async;
subghz_preset_init(
subghz,
"AM650",
subghz_setting_get_default_frequency(subghz->setting),
NULL,
0);
scene_manager_search_and_switch_to_previous_scene(
subghz->scene_manager, SubGhzSceneStart);
} else {
Expand Down
16 changes: 11 additions & 5 deletions applications/subghz/scenes/subghz_scene_read_raw.c
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,12 @@ bool subghz_scene_read_raw_on_event(void* context, SceneManagerEvent event) {
scene_manager_next_scene(subghz->scene_manager, SubGhzSceneNeedSaving);
} else {
//Restore default setting
subghz->txrx->frequency = subghz_setting_get_default_frequency(subghz->setting);
subghz->txrx->preset = FuriHalSubGhzPresetOok650Async;
subghz_preset_init(
subghz,
"AM650",
subghz_setting_get_default_frequency(subghz->setting),
NULL,
0);
if(!scene_manager_search_and_switch_to_previous_scene(
subghz->scene_manager, SubGhzSceneSaved)) {
if(!scene_manager_search_and_switch_to_previous_scene(
Expand Down Expand Up @@ -268,13 +272,15 @@ bool subghz_scene_read_raw_on_event(void* context, SceneManagerEvent event) {
if(subghz_protocol_raw_save_to_file_init(
(SubGhzProtocolDecoderRAW*)subghz->txrx->decoder_result,
RAW_FILE_NAME,
subghz->txrx->frequency,
subghz->txrx->preset)) {
DOLPHIN_DEED(DolphinDeedSubGhzRawRec);
if((subghz->txrx->txrx_state == SubGhzTxRxStateIDLE) ||
(subghz->txrx->txrx_state == SubGhzTxRxStateSleep)) {
subghz_begin(subghz, subghz->txrx->preset);
subghz_rx(subghz, subghz->txrx->frequency);
subghz_begin(
subghz,
subghz_setting_get_preset_data_by_name(
subghz->setting, string_get_cstr(subghz->txrx->preset->name)));
subghz_rx(subghz, subghz->txrx->preset->frequency);
}
subghz->state_notifications = SubGhzNotificationStateRx;
subghz->txrx->rx_key_state = SubGhzRxKeyStateAddKey;
Expand Down
22 changes: 14 additions & 8 deletions applications/subghz/scenes/subghz_scene_receiver.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ static void subghz_scene_add_to_history_callback(
string_t str_buff;
string_init(str_buff);

if(subghz_history_add_to_history(
subghz->txrx->history, decoder_base, subghz->txrx->frequency, subghz->txrx->preset)) {
if(subghz_history_add_to_history(subghz->txrx->history, decoder_base, subghz->txrx->preset)) {
string_reset(str_buff);

subghz->state_notifications = SubGhzNotificationStateRxDone;
Expand All @@ -103,8 +102,8 @@ void subghz_scene_receiver_on_enter(void* context) {
string_init(str_buff);

if(subghz->txrx->rx_key_state == SubGhzRxKeyStateIDLE) {
subghz->txrx->frequency = subghz_setting_get_default_frequency(subghz->setting);
subghz->txrx->preset = FuriHalSubGhzPresetOok650Async;
subghz_preset_init(
subghz, "AM650", subghz_setting_get_default_frequency(subghz->setting), NULL, 0);
subghz_history_reset(subghz->txrx->history);
subghz->txrx->rx_key_state = SubGhzRxKeyStateStart;
}
Expand Down Expand Up @@ -135,8 +134,11 @@ void subghz_scene_receiver_on_enter(void* context) {
};
if((subghz->txrx->txrx_state == SubGhzTxRxStateIDLE) ||
(subghz->txrx->txrx_state == SubGhzTxRxStateSleep)) {
subghz_begin(subghz, subghz->txrx->preset);
subghz_rx(subghz, subghz->txrx->frequency);
subghz_begin(
subghz,
subghz_setting_get_preset_data_by_name(
subghz->setting, string_get_cstr(subghz->txrx->preset->name)));
subghz_rx(subghz, subghz->txrx->preset->frequency);
}
subghz_view_receiver_set_idx_menu(subghz->subghz_receiver, subghz->txrx->idx_menu_chosen);

Expand Down Expand Up @@ -164,8 +166,12 @@ bool subghz_scene_receiver_on_event(void* context, SceneManagerEvent event) {
scene_manager_next_scene(subghz->scene_manager, SubGhzSceneNeedSaving);
} else {
subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE;
subghz->txrx->frequency = subghz_setting_get_default_frequency(subghz->setting);
subghz->txrx->preset = FuriHalSubGhzPresetOok650Async;
subghz_preset_init(
subghz,
"AM650",
subghz_setting_get_default_frequency(subghz->setting),
NULL,
0);
scene_manager_search_and_switch_to_previous_scene(
subghz->scene_manager, SubGhzSceneStart);
}
Expand Down
64 changes: 28 additions & 36 deletions applications/subghz/scenes/subghz_scene_receiver_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,6 @@ enum SubGhzSettingIndex {
SubGhzSettingIndexLock,
};

#define PRESET_COUNT 4
const char* const preset_text[PRESET_COUNT] = {
"AM270",
"AM650",
"FM238",
"FM476",
};
const uint32_t preset_value[PRESET_COUNT] = {
FuriHalSubGhzPresetOok270Async, /** OOK, bandwidth 270kHz, asynchronous */
FuriHalSubGhzPresetOok650Async, /** OOK, bandwidth 650kHz, asynchronous */
FuriHalSubGhzPreset2FSKDev238Async, /** FM, deviation 2.380371 kHz, asynchronous */
FuriHalSubGhzPreset2FSKDev476Async, /** FM, deviation 4.760742 kHz, asynchronous */
};

#define HOPPING_COUNT 2
const char* const hopping_text[HOPPING_COUNT] = {
"OFF",
Expand All @@ -31,32 +17,31 @@ const uint32_t hopping_value[HOPPING_COUNT] = {
SubGhzHopperStateRunnig,
};

uint8_t subghz_scene_receiver_config_uint32_value_index(
const uint32_t value,
const uint32_t values[],
uint8_t values_count) {
int64_t last_value = INT64_MIN;
uint8_t subghz_scene_receiver_config_next_frequency(const uint32_t value, void* context) {
furi_assert(context);
SubGhz* subghz = context;
uint8_t index = 0;
for(uint8_t i = 0; i < values_count; i++) {
if((value >= last_value) && (value <= values[i])) {
for(uint8_t i = 0; i < subghz_setting_get_frequency_count(subghz->setting); i++) {
if(value == subghz_setting_get_frequency(subghz->setting, i)) {
index = i;
break;
} else {
index = subghz_setting_get_frequency_default_index(subghz->setting);
}
last_value = values[i];
}
return index;
}

uint8_t subghz_scene_receiver_config_next_frequency(const uint32_t value, void* context) {
uint8_t subghz_scene_receiver_config_next_preset(const char* preset_name, void* context) {
furi_assert(context);
SubGhz* subghz = context;
uint8_t index = 0;
for(uint8_t i = 0; i < subghz_setting_get_frequency_count(subghz->setting); i++) {
if(value == subghz_setting_get_frequency(subghz->setting, i)) {
for(uint8_t i = 0; i < subghz_setting_get_preset_count(subghz->setting); i++) {
if(!strcmp(subghz_setting_get_preset_name(subghz->setting, i), preset_name)) {
index = i;
break;
} else {
index = subghz_setting_get_frequency_default_index(subghz->setting);
// index = subghz_setting_get_frequency_default_index(subghz->setting);
}
}
return index;
Expand Down Expand Up @@ -94,7 +79,7 @@ static void subghz_scene_receiver_config_set_frequency(VariableItem* item) {
subghz_setting_get_frequency(subghz->setting, index) / 1000000,
(subghz_setting_get_frequency(subghz->setting, index) % 1000000) / 10000);
variable_item_set_current_value_text(item, text_buf);
subghz->txrx->frequency = subghz_setting_get_frequency(subghz->setting, index);
subghz->txrx->preset->frequency = subghz_setting_get_frequency(subghz->setting, index);
} else {
variable_item_set_current_value_index(
item, subghz_setting_get_frequency_default_index(subghz->setting));
Expand All @@ -104,9 +89,14 @@ static void subghz_scene_receiver_config_set_frequency(VariableItem* item) {
static void subghz_scene_receiver_config_set_preset(VariableItem* item) {
SubGhz* subghz = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);

variable_item_set_current_value_text(item, preset_text[index]);
subghz->txrx->preset = preset_value[index];
variable_item_set_current_value_text(
item, subghz_setting_get_preset_name(subghz->setting, index));
subghz_preset_init(
subghz,
subghz_setting_get_preset_name(subghz->setting, index),
subghz->txrx->preset->frequency,
subghz_setting_get_preset_data(subghz->setting, index),
subghz_setting_get_preset_data_size(subghz->setting, index));
}

static void subghz_scene_receiver_config_set_hopping_runing(VariableItem* item) {
Expand All @@ -125,7 +115,7 @@ static void subghz_scene_receiver_config_set_hopping_runing(VariableItem* item)
(VariableItem*)scene_manager_get_scene_state(
subghz->scene_manager, SubGhzSceneReceiverConfig),
text_buf);
subghz->txrx->frequency = subghz_setting_get_default_frequency(subghz->setting);
subghz->txrx->preset->frequency = subghz_setting_get_default_frequency(subghz->setting);
variable_item_set_current_value_index(
(VariableItem*)scene_manager_get_scene_state(
subghz->scene_manager, SubGhzSceneReceiverConfig),
Expand Down Expand Up @@ -164,7 +154,8 @@ void subghz_scene_receiver_config_on_enter(void* context) {
subghz_setting_get_frequency_count(subghz->setting),
subghz_scene_receiver_config_set_frequency,
subghz);
value_index = subghz_scene_receiver_config_next_frequency(subghz->txrx->frequency, subghz);
value_index =
subghz_scene_receiver_config_next_frequency(subghz->txrx->preset->frequency, subghz);
scene_manager_set_scene_state(
subghz->scene_manager, SubGhzSceneReceiverConfig, (uint32_t)item);
variable_item_set_current_value_index(item, value_index);
Expand Down Expand Up @@ -193,13 +184,14 @@ void subghz_scene_receiver_config_on_enter(void* context) {
item = variable_item_list_add(
subghz->variable_item_list,
"Modulation:",
PRESET_COUNT,
subghz_setting_get_preset_count(subghz->setting),
subghz_scene_receiver_config_set_preset,
subghz);
value_index = subghz_scene_receiver_config_uint32_value_index(
subghz->txrx->preset, preset_value, PRESET_COUNT);
value_index = subghz_scene_receiver_config_next_preset(
string_get_cstr(subghz->txrx->preset->name), subghz);
variable_item_set_current_value_index(item, value_index);
variable_item_set_current_value_text(item, preset_text[value_index]);
variable_item_set_current_value_text(
item, subghz_setting_get_preset_name(subghz->setting, value_index));

if(scene_manager_get_scene_state(subghz->scene_manager, SubGhzSceneReadRAW) !=
SubGhzCustomEventManagerSet) {
Expand Down
21 changes: 15 additions & 6 deletions applications/subghz/scenes/subghz_scene_receiver_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,16 @@ static bool subghz_scene_receiver_info_update_parser(void* context) {
subghz_protocol_decoder_base_deserialize(
subghz->txrx->decoder_result,
subghz_history_get_raw_data(subghz->txrx->history, subghz->txrx->idx_menu_chosen));
subghz->txrx->frequency =
subghz_history_get_frequency(subghz->txrx->history, subghz->txrx->idx_menu_chosen);
subghz->txrx->preset =
subghz_history_get_preset(subghz->txrx->history, subghz->txrx->idx_menu_chosen);

SubGhzPesetDefinition* preset =
subghz_history_get_presset(subghz->txrx->history, subghz->txrx->idx_menu_chosen);
subghz_preset_init(
subghz,
string_get_cstr(preset->name),
preset->frequency,
preset->data,
preset->data_size);

return true;
}
return false;
Expand Down Expand Up @@ -137,8 +143,11 @@ bool subghz_scene_receiver_info_on_event(void* context, SceneManagerEvent event)
subghz_tx_stop(subghz);
}
if(subghz->txrx->txrx_state == SubGhzTxRxStateIDLE) {
subghz_begin(subghz, subghz->txrx->preset);
subghz_rx(subghz, subghz->txrx->frequency);
subghz_begin(
subghz,
subghz_setting_get_preset_data_by_name(
subghz->setting, string_get_cstr(subghz->txrx->preset->name)));
subghz_rx(subghz, subghz->txrx->preset->frequency);
}
if(subghz->txrx->hopper_state == SubGhzHopperStatePause) {
subghz->txrx->hopper_state = SubGhzHopperStateRunnig;
Expand Down
Loading

0 comments on commit ec19c11

Please sign in to comment.