Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add settings to subghz read functionality to allow setting RSSI threshold (raw only) #184

Merged
merged 1 commit into from
Aug 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 62 additions & 3 deletions applications/subghz/scenes/subghz_scene_receiver_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,27 @@ const char* const detect_raw_text[DETECT_RAW_COUNT] = {
"OFF",
"ON",
};

const SubGhzProtocolFlag detect_raw_value[DETECT_RAW_COUNT] = {
SubGhzProtocolFlag_Decodable,
SubGhzProtocolFlag_Decodable | SubGhzProtocolFlag_RAW,
};

#define RSSI_THRESHOLD_COUNT 4
const char* const rssi_threshold_text[RSSI_THRESHOLD_COUNT] = {
"-72db",
"-67db",
"-62db",
"-57db",
};

const int rssi_threshold_value[RSSI_THRESHOLD_COUNT] = {
-72,
-67,
-62,
-57,
};

uint8_t subghz_scene_receiver_config_next_frequency(const uint32_t value, void* context) {
furi_assert(context);
SubGhz* subghz = context;
Expand Down Expand Up @@ -94,6 +110,20 @@ uint8_t subghz_scene_receiver_config_detect_raw_value_index(
return index;
}

uint8_t subghz_scene_receiver_config_rssi_threshold_value_index(
const int value,
const int values[],
uint8_t values_count) {
uint8_t index = 0;
for(uint8_t i = 0; i < values_count; i++) {
if(value == values[i]) {
index = i;
break;
}
}
return index;
}

static void subghz_scene_receiver_config_set_frequency(VariableItem* item) {
SubGhz* subghz = variable_item_get_context(item);
uint8_t index = variable_item_get_current_value_index(item);
Expand Down Expand Up @@ -127,16 +157,27 @@ static void subghz_scene_receiver_config_set_preset(VariableItem* item) {
subghz_setting_get_preset_data_size(subghz->setting, index));
}

static void subghz_scene_receiver_config_set_rssi_threshold(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, rssi_threshold_text[index]);
subghz_protocol_decoder_raw_set_rssi_threshold(
subghz_receiver_search_decoder_base_by_name(
subghz->txrx->receiver, SUBGHZ_PROTOCOL_RAW_NAME),
rssi_threshold_value[index]);
}

static void subghz_scene_receiver_config_set_detect_raw(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, detect_raw_text[index]);
subghz_receiver_set_filter(subghz->txrx->receiver, detect_raw_value[index]);
subghz_protocol_decoder_raw_set_auto_mode(
subghz_receiver_search_decoder_base_by_name(
subghz_protocol_decoder_raw_set_auto_mode(subghz_receiver_search_decoder_base_by_name(
subghz->txrx->receiver, SUBGHZ_PROTOCOL_RAW_NAME),
(index == 1));
(index == 1));
}

static void subghz_scene_receiver_config_set_hopping_runing(VariableItem* item) {
Expand Down Expand Up @@ -251,6 +292,23 @@ void subghz_scene_receiver_config_on_enter(void* context) {
variable_item_set_current_value_text(item, detect_raw_text[value_index]);
}

if(scene_manager_get_scene_state(subghz->scene_manager, SubGhzSceneReadRAW) !=
SubGhzCustomEventManagerSet) {
item = variable_item_list_add(
subghz->variable_item_list,
"RSSI for Raw:",
RSSI_THRESHOLD_COUNT,
subghz_scene_receiver_config_set_rssi_threshold,
subghz);
value_index = subghz_scene_receiver_config_rssi_threshold_value_index(
subghz_protocol_encoder_get_rssi_threshold(subghz_receiver_search_decoder_base_by_name(
subghz->txrx->receiver, SUBGHZ_PROTOCOL_RAW_NAME)),
rssi_threshold_value,
RSSI_THRESHOLD_COUNT);
variable_item_set_current_value_index(item, value_index);
variable_item_set_current_value_text(item, rssi_threshold_text[value_index]);
}

if(scene_manager_get_scene_state(subghz->scene_manager, SubGhzSceneReadRAW) !=
SubGhzCustomEventManagerSet) {
variable_item_list_add(subghz->variable_item_list, "Lock Keyboard", 1, NULL, NULL);
Expand All @@ -259,6 +317,7 @@ void subghz_scene_receiver_config_on_enter(void* context) {
subghz_scene_receiver_config_var_list_enter_callback,
subghz);
}

view_dispatcher_switch_to_view(subghz->view_dispatcher, SubGhzViewIdVariableItemList);
}

Expand Down
24 changes: 23 additions & 1 deletion lib/subghz/protocols/raw.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ struct SubGhzProtocolDecoderRAW {
bool last_level;
bool auto_mode;
bool has_rssi_above_threshold;
int rssi_threshold;
uint8_t postroll_frames;
};

Expand Down Expand Up @@ -208,6 +209,18 @@ void subghz_protocol_raw_save_to_file_stop(SubGhzProtocolDecoderRAW* instance) {
instance->file_is_open = RAWFileIsOpenClose;
}

void subghz_protocol_decoder_raw_set_rssi_threshold(void* context, int rssi_threshold)
{
furi_assert(context);
SubGhzProtocolDecoderRAW* instance = context;

FURI_LOG_E(TAG, "RSSI set: (%d)", rssi_threshold);

instance->rssi_threshold = rssi_threshold;

subghz_protocol_decoder_raw_reset(context);
}

void subghz_protocol_decoder_raw_set_auto_mode(void* context, bool auto_mode) {
furi_assert(context);
SubGhzProtocolDecoderRAW* instance = context;
Expand Down Expand Up @@ -295,7 +308,8 @@ void subghz_protocol_decoder_raw_feed(void* context, bool level, uint32_t durati
if(instance->upload_raw != NULL && duration > subghz_protocol_raw_const.te_short) {
if(instance->auto_mode) {
float rssi = furi_hal_subghz_get_rssi();
if(rssi >= SUBGHZ_AUTO_DETECT_RAW_THRESHOLD) {

if(rssi >= instance->rssi_threshold) {
subghz_protocol_decoder_raw_write_data(context, level, duration);
instance->has_rssi_above_threshold = true;
instance->postroll_frames = 0;
Expand Down Expand Up @@ -355,6 +369,14 @@ void* subghz_protocol_encoder_raw_alloc(SubGhzEnvironment* environment) {
return instance;
}

int subghz_protocol_encoder_get_rssi_threshold(void* context)
{
furi_assert(context);
SubGhzProtocolDecoderRAW* instance = context;

return instance->rssi_threshold;
}

void subghz_protocol_encoder_raw_stop(void* context) {
SubGhzProtocolEncoderRAW* instance = context;
instance->is_runing = false;
Expand Down
14 changes: 14 additions & 0 deletions lib/subghz/protocols/raw.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ bool subghz_protocol_raw_save_to_file_init(
*/
void subghz_protocol_decoder_raw_set_auto_mode(void* context, bool auto_mode);

/**
* Set RSSI threshold ("sensitivity" level).
* @param context Pointer to a SubGhzProtocolDecoderRAW instance
* @param rssi_threshold The desired RSSI threshold
*/
void subghz_protocol_decoder_raw_set_rssi_threshold(void* context, int rssi_threshold);

/**
* Get RSSI threshold ("sensitivity" level).
* @param context Pointer to a SubGhzProtocolDecoderRAW instance
* @return rssi threshold in db
*/
int subghz_protocol_encoder_get_rssi_threshold(void* context);

/**
* Stop writing file to flash
* @param instance Pointer to a SubGhzProtocolDecoderRAW instance
Expand Down