diff --git a/applications/main/subghz/scenes/subghz_scene_frequency_analyzer.c b/applications/main/subghz/scenes/subghz_scene_frequency_analyzer.c index f067f9859..a78fccd3f 100644 --- a/applications/main/subghz/scenes/subghz_scene_frequency_analyzer.c +++ b/applications/main/subghz/scenes/subghz_scene_frequency_analyzer.c @@ -1,5 +1,4 @@ #include "../subghz_i.h" -#include "../views/subghz_frequency_analyzer.h" #include void subghz_scene_frequency_analyzer_callback(SubGhzCustomEvent event, void* context) { @@ -17,8 +16,18 @@ void subghz_scene_frequency_analyzer_on_enter(void* context) { } bool subghz_scene_frequency_analyzer_on_event(void* context, SceneManagerEvent event) { - UNUSED(context); - UNUSED(event); + SubGhz* subghz = context; + if(event.type == SceneManagerEventTypeCustom && + event.event == SubGhzCustomEventViewReceiverOK) { + uint32_t frequency = + subghz_frequency_analyzer_get_frequency_to_save(subghz->subghz_frequency_analyzer); + if(frequency > 0) { + subghz->last_settings->frequency = frequency; + subghz_last_settings_save(subghz->last_settings); + } + + return true; + } return false; } diff --git a/applications/main/subghz/views/subghz_frequency_analyzer.c b/applications/main/subghz/views/subghz_frequency_analyzer.c index 5be00888f..7d1239cd8 100644 --- a/applications/main/subghz/views/subghz_frequency_analyzer.c +++ b/applications/main/subghz/views/subghz_frequency_analyzer.c @@ -30,6 +30,43 @@ static const NotificationSequence sequence_hw_blink_stop = { NULL, }; +static const NotificationSequence sequence_saved = { + &message_blink_stop, + &message_blue_0, + &message_green_0, + &message_red_255, + &message_vibro_on, + &message_delay_100, + &message_vibro_off, + NULL, +}; +static const NotificationSequence sequence_not_saved = { + &message_blink_stop, + &message_green_255, + &message_blue_255, + &message_red_255, + NULL, +}; + +static const uint32_t subghz_frequency_list[17] = { + 300000000, + 303875000, + 304250000, + 310000000, + 315000000, + 318000000, + 390000000, + 418000000, + 433075000, + 433420000, + 433920000, + 434420000, + 434775000, + 438900000, + 868350000, + 915000000, + 925000000}; + typedef enum { SubGhzFrequencyAnalyzerStatusIDLE, } SubGhzFrequencyAnalyzerStatus; @@ -50,6 +87,7 @@ struct SubGhzFrequencyAnalyzer { typedef struct { uint32_t frequency; uint32_t frequency_last; + uint32_t frequency_to_save; float rssi; float rssi_last; float trigger; @@ -163,6 +201,33 @@ void subghz_frequency_analyzer_draw(Canvas* canvas, SubGhzFrequencyAnalyzerModel elements_button_right(canvas, "T+"); } +uint32_t subghz_frequency_find_correct(uint32_t input) { + uint32_t prev_freq = 0; + uint32_t current = 0; + uint32_t result = 0; +#if FURI_DEBUG + FURI_LOG_D(TAG, "input: %d", input); +#endif + for(size_t i = 0; i < sizeof(subghz_frequency_list); i++) { + current = subghz_frequency_list[i]; + if(current == input) { + result = current; + break; + } + if(current > input && prev_freq < input) { + if(current - input < input - prev_freq) { + result = current; + } else { + result = prev_freq; + } + break; + } + prev_freq = current; + } + + return result; +} + bool subghz_frequency_analyzer_input(InputEvent* event, void* context) { furi_assert(context); SubGhzFrequencyAnalyzer* instance = context; @@ -201,6 +266,43 @@ bool subghz_frequency_analyzer_input(InputEvent* event, void* context) { need_redraw = true; } + if(event->type == InputTypeShort && event->key == InputKeyOk) { + with_view_model( + instance->view, (SubGhzFrequencyAnalyzerModel * model) { + uint32_t prev_freq_to_save = model->frequency_to_save; + uint32_t frequency_candidate = 0; + if(model->frequency != 0) { + frequency_candidate = model->frequency; + } else if(model->frequency_last != 0) { + frequency_candidate = model->frequency_last; + } + if(frequency_candidate == 0 || + !furi_hal_subghz_is_frequency_valid(frequency_candidate) || + prev_freq_to_save == frequency_candidate) { + frequency_candidate = 0; + } else { + frequency_candidate = subghz_frequency_find_correct(frequency_candidate); + } + if(frequency_candidate > 0 && frequency_candidate != model->frequency_to_save) { +#if FURI_DEBUG + FURI_LOG_D( + TAG, + "frequency_to_save: %d, candidate: %d", + model->frequency_to_save, + frequency_candidate); +#endif + model->frequency_to_save = frequency_candidate; + notification_message(instance->notifications, &sequence_saved); + instance->callback(SubGhzCustomEventViewReceiverOK, instance->context); + notification_message(instance->notifications, &sequence_hw_blink); + } else { + notification_message(instance->notifications, &sequence_not_saved); + notification_message(instance->notifications, &sequence_hw_blink); + } + return true; + }); + } + if(need_redraw) { SubGhzFrequencyAnalyzer* instance = context; with_view_model( @@ -309,6 +411,7 @@ void subghz_frequency_analyzer_enter(void* context) { model->rssi_last = 0; model->frequency = 0; model->frequency_last = 0; + model->frequency_to_save = 0; model->trigger = RSSI_MIN; return true; }); @@ -359,4 +462,15 @@ void subghz_frequency_analyzer_free(SubGhzFrequencyAnalyzer* instance) { View* subghz_frequency_analyzer_get_view(SubGhzFrequencyAnalyzer* instance) { furi_assert(instance); return instance->view; +} + +uint32_t subghz_frequency_analyzer_get_frequency_to_save(SubGhzFrequencyAnalyzer* instance) { + uint32_t frequency; + with_view_model( + instance->view, (SubGhzFrequencyAnalyzerModel * model) { + frequency = model->frequency_to_save; + return false; + }); + + return frequency; } \ No newline at end of file diff --git a/applications/main/subghz/views/subghz_frequency_analyzer.h b/applications/main/subghz/views/subghz_frequency_analyzer.h index 3de003bf8..5e00c6444 100644 --- a/applications/main/subghz/views/subghz_frequency_analyzer.h +++ b/applications/main/subghz/views/subghz_frequency_analyzer.h @@ -17,3 +17,5 @@ SubGhzFrequencyAnalyzer* subghz_frequency_analyzer_alloc(); void subghz_frequency_analyzer_free(SubGhzFrequencyAnalyzer* subghz_static); View* subghz_frequency_analyzer_get_view(SubGhzFrequencyAnalyzer* subghz_static); + +uint32_t subghz_frequency_analyzer_get_frequency_to_save(SubGhzFrequencyAnalyzer* instance);