Skip to content

Commit

Permalink
SubGhz: Refactoring Read RAW (flipperdevices#791)
Browse files Browse the repository at this point in the history
* SubGhz: rename save_raw -> read_raw
* SubGhz: add manually saving files to read_raw, confirming that saving is unnecessary, refactoring
* Format sources
* SubGhz: fix runglish

Co-authored-by: あく <[email protected]>
  • Loading branch information
Skorpionm and skotopes authored Oct 27, 2021
1 parent 10c65da commit 26c53f3
Show file tree
Hide file tree
Showing 22 changed files with 632 additions and 450 deletions.
16 changes: 11 additions & 5 deletions applications/subghz/helpers/subghz_custom_event.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#pragma once

typedef enum {
SubghzCustomEventManagerNoSet = 0,
SubghzCustomEventManagerSet,

SubghzCustomEventSceneDeleteSuccess = 100,
SubghzCustomEventSceneDelete,
SubghzCustomEventSceneReceiverInfoTxStart,
Expand All @@ -11,15 +14,18 @@ typedef enum {
SubghzCustomEventSceneShowError,
SubghzCustomEventSceneShowOnlyRX,

SubghzCustomEventSceneNeedSavingNo,
SubghzCustomEventSceneNeedSavingYes,

SubghzCustomEventViewReceverOK,
SubghzCustomEventViewReceverConfig,
SubghzCustomEventViewReceverBack,

SubghzCustomEventViewSaveRAWBack,
SubghzCustomEventViewSaveRAWIDLE,
SubghzCustomEventViewSaveRAWREC,
SubghzCustomEventViewSaveRAWConfig,
SubghzCustomEventViewSaveRAWMore,
SubghzCustomEventViewReadRAWBack,
SubghzCustomEventViewReadRAWIDLE,
SubghzCustomEventViewReadRAWREC,
SubghzCustomEventViewReadRAWConfig,
SubghzCustomEventViewReadRAWMore,

SubghzCustomEventViewTransmitterBack,
SubghzCustomEventViewTransmitterSendStart,
Expand Down
4 changes: 3 additions & 1 deletion applications/subghz/scenes/subghz_scene_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ ADD_SCENE(subghz, test_carrier, TestCarrier)
ADD_SCENE(subghz, test_packet, TestPacket)
ADD_SCENE(subghz, set_type, SetType)
ADD_SCENE(subghz, frequency_analyzer, FrequencyAnalyzer)
ADD_SCENE(subghz, save_raw, SaveRAW)
ADD_SCENE(subghz, read_raw, ReadRAW)
ADD_SCENE(subghz, read_raw_menu, ReadRAWMenu)
ADD_SCENE(subghz, need_saving, NeedSaving)
63 changes: 63 additions & 0 deletions applications/subghz/scenes/subghz_scene_need_saving.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "../subghz_i.h"
#include "../helpers/subghz_custom_event.h"

void subghz_scene_need_saving_callback(GuiButtonType result, InputType type, void* context) {
furi_assert(context);
SubGhz* subghz = context;

if((result == GuiButtonTypeRight) && (type == InputTypeShort)) {
view_dispatcher_send_custom_event(
subghz->view_dispatcher, SubghzCustomEventSceneNeedSavingYes);
} else if((result == GuiButtonTypeLeft) && (type == InputTypeShort)) {
view_dispatcher_send_custom_event(
subghz->view_dispatcher, SubghzCustomEventSceneNeedSavingNo);
}
}

void subghz_scene_need_saving_on_enter(void* context) {
SubGhz* subghz = context;

widget_add_string_multiline_element(
subghz->widget,
64,
25,
AlignCenter,
AlignCenter,
FontSecondary,
"There is an unsaved data.\nDo you want to save it?");

widget_add_button_element(
subghz->widget, GuiButtonTypeRight, "Save", subghz_scene_need_saving_callback, subghz);
widget_add_button_element(
subghz->widget, GuiButtonTypeLeft, "Delete", subghz_scene_need_saving_callback, subghz);

view_dispatcher_switch_to_view(subghz->view_dispatcher, SubGhzViewWidget);
}

bool subghz_scene_need_saving_on_event(void* context, SceneManagerEvent event) {
SubGhz* subghz = context;
if(event.type == SceneManagerEventTypeCustom) {
if(event.event == SubghzCustomEventSceneNeedSavingYes) {
subghz->txrx->rx_key_state = SubGhzRxKeyStateNeedSave;
scene_manager_previous_scene(subghz->scene_manager);
return true;
} else if(event.event == SubghzCustomEventSceneNeedSavingNo) {
if(subghz->txrx->rx_key_state == SubGhzRxKeyStateExit) {
subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE;
scene_manager_search_and_switch_to_previous_scene(
subghz->scene_manager, SubGhzSceneStart);
} else {
subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE;
scene_manager_previous_scene(subghz->scene_manager);
}

return true;
}
}
return false;
}

void subghz_scene_need_saving_on_exit(void* context) {
SubGhz* subghz = context;
widget_clear(subghz->widget);
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#include "../subghz_i.h"
#include "../views/subghz_save_raw.h"
#include "../views/subghz_read_raw.h"
#include <lib/subghz/protocols/subghz_protocol_raw.h>
#include <lib/subghz/subghz_parser.h>

static void subghz_scene_save_raw_update_statusbar(void* context) {
static void subghz_scene_read_raw_update_statusbar(void* context) {
furi_assert(context);
SubGhz* subghz = context;
char frequency_str[20];
Expand All @@ -25,86 +25,107 @@ static void subghz_scene_save_raw_update_statusbar(void* context) {
} else {
furi_crash(NULL);
}
subghz_save_raw_add_data_statusbar(subghz->subghz_save_raw, frequency_str, preset_str);

subghz_read_raw_add_data_statusbar(subghz->subghz_read_raw, frequency_str, preset_str);
}

void subghz_scene_save_raw_callback(SubghzCustomEvent event, void* context) {
void subghz_scene_read_raw_callback(SubghzCustomEvent event, void* context) {
furi_assert(context);
SubGhz* subghz = context;
view_dispatcher_send_custom_event(subghz->view_dispatcher, event);
}

void subghz_scene_save_raw_on_enter(void* context) {
void subghz_scene_read_raw_on_enter(void* context) {
SubGhz* subghz = context;
subghz_scene_save_raw_update_statusbar(subghz);
subghz_save_raw_set_callback(subghz->subghz_save_raw, subghz_scene_save_raw_callback, subghz);

if(subghz->txrx->rx_key_state == SubGhzRxKeyStateNeedSave) {
view_dispatcher_send_custom_event(
subghz->view_dispatcher, SubghzCustomEventViewReadRAWMore);
} else {
subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE;
}

subghz_scene_read_raw_update_statusbar(subghz);
subghz_read_raw_set_callback(subghz->subghz_read_raw, subghz_scene_read_raw_callback, subghz);

subghz->txrx->protocol_result = subghz_parser_get_by_name(subghz->txrx->parser, "RAW");
furi_assert(subghz->txrx->protocol_result);

subghz_worker_set_pair_callback(
subghz->txrx->worker, (SubGhzWorkerPairCallback)subghz_parser_raw_parse);

view_dispatcher_switch_to_view(subghz->view_dispatcher, SubGhzViewSaveRAW);
view_dispatcher_switch_to_view(subghz->view_dispatcher, SubGhzViewReadRAW);
}

bool subghz_scene_save_raw_on_event(void* context, SceneManagerEvent event) {
bool subghz_scene_read_raw_on_event(void* context, SceneManagerEvent event) {
SubGhz* subghz = context;
if(event.type == SceneManagerEventTypeCustom) {
switch(event.event) {
case SubghzCustomEventViewSaveRAWBack:
case SubghzCustomEventViewReadRAWBack:
if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
subghz_rx_end(subghz);
subghz_sleep(subghz);
};
subghz->txrx->frequency = subghz_frequencies[subghz_frequencies_433_92];
subghz->txrx->preset = FuriHalSubGhzPresetOok650Async;
subghz_protocol_save_raw_to_file_stop(
subghz_protocol_raw_save_to_file_stop(
(SubGhzProtocolRAW*)subghz->txrx->protocol_result);
scene_manager_search_and_switch_to_previous_scene(
subghz->scene_manager, SubGhzSceneStart);
subghz->state_notifications = NOTIFICATION_IDLE_STATE;

if(subghz->txrx->rx_key_state == SubGhzRxKeyStateAddKey) {
subghz->txrx->rx_key_state = SubGhzRxKeyStateExit;
scene_manager_next_scene(subghz->scene_manager, SubGhzSceneNeedSaving);
} else {
scene_manager_search_and_switch_to_previous_scene(
subghz->scene_manager, SubGhzSceneStart);
}

return true;
break;
case SubghzCustomEventViewSaveRAWConfig:
scene_manager_set_scene_state(subghz->scene_manager, SubGhzSceneSaveRAW, 1);
case SubghzCustomEventViewReadRAWConfig:
scene_manager_set_scene_state(
subghz->scene_manager, SubGhzSceneReadRAW, SubghzCustomEventManagerSet);
scene_manager_next_scene(subghz->scene_manager, SubGhzSceneReceiverConfig);
return true;
break;
case SubghzCustomEventViewSaveRAWIDLE:
case SubghzCustomEventViewReadRAWIDLE:
if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
subghz_rx_end(subghz);
subghz_sleep(subghz);
};
subghz_protocol_save_raw_to_file_stop(
subghz_protocol_raw_save_to_file_stop(
(SubGhzProtocolRAW*)subghz->txrx->protocol_result);
subghz->state_notifications = NOTIFICATION_IDLE_STATE;
//send the name of the saved file to the account
subghz_save_raw_set_file_name(
subghz->subghz_save_raw,
subghz_protocol_get_last_file_name(
(SubGhzProtocolRAW*)subghz->txrx->protocol_result));

subghz->txrx->rx_key_state = SubGhzRxKeyStateAddKey;

return true;
break;
case SubghzCustomEventViewSaveRAWREC:
if(subghz_protocol_save_raw_to_file_init(
(SubGhzProtocolRAW*)subghz->txrx->protocol_result,
"Raw",
subghz->txrx->frequency,
subghz->txrx->preset)) {
if((subghz->txrx->txrx_state == SubGhzTxRxStateIdle) ||
(subghz->txrx->txrx_state == SubGhzTxRxStateSleep)) {
subghz_begin(subghz, subghz->txrx->preset);
subghz_rx(subghz, subghz->txrx->frequency);
}
subghz->state_notifications = NOTIFICATION_RX_STATE;
case SubghzCustomEventViewReadRAWREC:

if(subghz->txrx->rx_key_state != SubGhzRxKeyStateIDLE) {
scene_manager_next_scene(subghz->scene_manager, SubGhzSceneNeedSaving);
} else {
string_set(subghz->error_str, "No SD card");
scene_manager_next_scene(subghz->scene_manager, SubGhzSceneShowError);
if(subghz_protocol_raw_save_to_file_init(
(SubGhzProtocolRAW*)subghz->txrx->protocol_result,
"Raw_temp",
subghz->txrx->frequency,
subghz->txrx->preset)) {
if((subghz->txrx->txrx_state == SubGhzTxRxStateIDLE) ||
(subghz->txrx->txrx_state == SubGhzTxRxStateSleep)) {
subghz_begin(subghz, subghz->txrx->preset);
subghz_rx(subghz, subghz->txrx->frequency);
}
subghz->state_notifications = NOTIFICATION_RX_STATE;
} else {
string_set(subghz->error_str, "No SD card");
scene_manager_next_scene(subghz->scene_manager, SubGhzSceneShowError);
}
}

return true;
break;
case SubghzCustomEventViewSaveRAWMore:
case SubghzCustomEventViewReadRAWMore:
if(strcmp(
subghz_protocol_get_last_file_name(
(SubGhzProtocolRAW*)subghz->txrx->protocol_result),
Expand All @@ -128,7 +149,7 @@ bool subghz_scene_save_raw_on_event(void* context, SceneManagerEvent event) {
(SubGhzProtocolRAW*)subghz->txrx->protocol_result, string_get_cstr(temp_str));
string_clear(temp_str);

scene_manager_next_scene(subghz->scene_manager, SubGhzSceneSavedMenu);
scene_manager_next_scene(subghz->scene_manager, SubGhzSceneReadRAWMenu);
}
return true;
break;
Expand All @@ -140,11 +161,11 @@ bool subghz_scene_save_raw_on_event(void* context, SceneManagerEvent event) {
switch(subghz->state_notifications) {
case NOTIFICATION_RX_STATE:
notification_message(subghz->notifications, &sequence_blink_blue_10);
subghz_save_raw_update_sample_write(
subghz->subghz_save_raw,
subghz_save_protocol_raw_get_sample_write(
subghz_read_raw_update_sample_write(
subghz->subghz_read_raw,
subghz_protocol_raw_get_sample_write(
(SubGhzProtocolRAW*)subghz->txrx->protocol_result));
subghz_save_raw_add_data_rssi(subghz->subghz_save_raw, furi_hal_subghz_get_rssi());
subghz_read_raw_add_data_rssi(subghz->subghz_read_raw, furi_hal_subghz_get_rssi());
break;
default:
break;
Expand All @@ -153,7 +174,7 @@ bool subghz_scene_save_raw_on_event(void* context, SceneManagerEvent event) {
return false;
}

void subghz_scene_save_raw_on_exit(void* context) {
void subghz_scene_read_raw_on_exit(void* context) {
SubGhz* subghz = context;

//Stop CC1101
Expand Down
72 changes: 72 additions & 0 deletions applications/subghz/scenes/subghz_scene_read_raw_menu.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#include "../subghz_i.h"

enum SubmenuIndex {
SubmenuIndexEmulate,
SubmenuIndexEdit,
SubmenuIndexDelete,
};

void subghz_scene_read_raw_menu_submenu_callback(void* context, uint32_t index) {
SubGhz* subghz = context;
view_dispatcher_send_custom_event(subghz->view_dispatcher, index);
}

void subghz_scene_read_raw_menu_on_enter(void* context) {
SubGhz* subghz = context;
submenu_add_item(
subghz->submenu,
"Emulate",
SubmenuIndexEmulate,
subghz_scene_read_raw_menu_submenu_callback,
subghz);

submenu_add_item(
subghz->submenu,
"Save",
SubmenuIndexEdit,
subghz_scene_read_raw_menu_submenu_callback,
subghz);

submenu_add_item(
subghz->submenu,
"Delete",
SubmenuIndexDelete,
subghz_scene_read_raw_menu_submenu_callback,
subghz);

submenu_set_selected_item(
subghz->submenu,
scene_manager_get_scene_state(subghz->scene_manager, SubGhzSceneSavedMenu));

view_dispatcher_switch_to_view(subghz->view_dispatcher, SubGhzViewMenu);
}

bool subghz_scene_read_raw_menu_on_event(void* context, SceneManagerEvent event) {
SubGhz* subghz = context;

if(event.type == SceneManagerEventTypeCustom) {
if(event.event == SubmenuIndexEmulate) {
scene_manager_set_scene_state(
subghz->scene_manager, SubGhzSceneReadRAWMenu, SubmenuIndexEmulate);
scene_manager_next_scene(subghz->scene_manager, SubGhzSceneTransmitter);
return true;
} else if(event.event == SubmenuIndexDelete) {
scene_manager_set_scene_state(
subghz->scene_manager, SubGhzSceneReadRAWMenu, SubmenuIndexDelete);
scene_manager_next_scene(subghz->scene_manager, SubGhzSceneDelete);
return true;
} else if(event.event == SubmenuIndexEdit) {
scene_manager_set_scene_state(
subghz->scene_manager, SubGhzSceneReadRAWMenu, SubghzCustomEventManagerSet);
scene_manager_next_scene(subghz->scene_manager, SubGhzSceneSaveName);
return true;
}
}
return false;
}

void subghz_scene_read_raw_menu_on_exit(void* context) {
SubGhz* subghz = context;
submenu_clean(subghz->submenu);
subghz->txrx->rx_key_state = SubGhzRxKeyStateIDLE;
}
2 changes: 1 addition & 1 deletion applications/subghz/scenes/subghz_scene_receiver.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ void subghz_scene_receiver_on_enter(void* context) {
if(subghz->txrx->txrx_state == SubGhzTxRxStateRx) {
subghz_rx_end(subghz);
};
if((subghz->txrx->txrx_state == SubGhzTxRxStateIdle) ||
if((subghz->txrx->txrx_state == SubGhzTxRxStateIDLE) ||
(subghz->txrx->txrx_state == SubGhzTxRxStateSleep)) {
subghz_begin(subghz, subghz->txrx->preset);
subghz_rx(subghz, subghz->txrx->frequency);
Expand Down
6 changes: 4 additions & 2 deletions applications/subghz/scenes/subghz_scene_receiver_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ void subghz_scene_receiver_config_on_enter(void* context) {
variable_item_set_current_value_index(item, value_index);
variable_item_set_current_value_text(item, subghz_frequencies_text[value_index]);

if(!scene_manager_get_scene_state(subghz->scene_manager, SubGhzSceneSaveRAW)) {
if(scene_manager_get_scene_state(subghz->scene_manager, SubGhzSceneReadRAW) !=
SubghzCustomEventManagerSet) {
item = variable_item_list_add(
subghz->variable_item_list,
"Hopping:",
Expand Down Expand Up @@ -161,5 +162,6 @@ bool subghz_scene_receiver_config_on_event(void* context, SceneManagerEvent even
void subghz_scene_receiver_config_on_exit(void* context) {
SubGhz* subghz = context;
variable_item_list_clean(subghz->variable_item_list);
scene_manager_set_scene_state(subghz->scene_manager, SubGhzSceneSaveRAW, 0);
scene_manager_set_scene_state(
subghz->scene_manager, SubGhzSceneReadRAW, SubghzCustomEventManagerNoSet);
}
Loading

0 comments on commit 26c53f3

Please sign in to comment.