Skip to content

Commit 51179f0

Browse files
committed
Force-allow UART MSR setting
1 parent beada60 commit 51179f0

File tree

4 files changed

+79
-6
lines changed

4 files changed

+79
-6
lines changed

mag_state.c

+7
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ bool mag_state_load(MagState* out_state) {
5858
state.pin_output = tmp;
5959
if(!flipper_format_read_uint32(file, "pin_enable", &tmp, 1)) break;
6060
state.pin_enable = tmp;
61+
if(!flipper_format_read_bool(file, "allow_uart", &state.allow_uart, 1)) break;
6162

6263
loaded_from_file = true;
6364
} while(0);
@@ -68,10 +69,15 @@ bool mag_state_load(MagState* out_state) {
6869
// If could not be read from file
6970
// Or file GPIO config is invalid (pins overlap)
7071
// Set defaults
72+
// Additionally raise message to user?
7173
if(!loaded_from_file || !mag_state_gpio_is_valid(&state)) {
7274
mag_state_gpio_reset(&state);
7375
}
7476

77+
if(!loaded_from_file) {
78+
state.allow_uart = MAG_STATE_DEFAULT_ALLOW_UART;
79+
}
80+
7581
// set defaults we don't save
7682
state.tx = MAG_STATE_DEFAULT_TX;
7783
state.track = MAG_STATE_DEFAULT_TRACK;
@@ -102,6 +108,7 @@ void mag_state_save(MagState* state) {
102108
if(!flipper_format_write_uint32(file, "pin_output", &tmp, 1)) break;
103109
tmp = state->pin_enable;
104110
if(!flipper_format_write_uint32(file, "pin_enable", &tmp, 1)) break;
111+
if(!flipper_format_write_bool(file, "allow_uart", &state->allow_uart, 1)) break;
105112

106113
} while(0);
107114
flipper_format_free(file);

mag_state.h

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ typedef enum {
3737
#define MAG_STATE_DEFAULT_PIN_INPUT MagPinA7
3838
#define MAG_STATE_DEFAULT_PIN_OUTPUT MagPinA6
3939
#define MAG_STATE_DEFAULT_PIN_ENABLE MagPinA4
40+
#define MAG_STATE_DEFAULT_ALLOW_UART false
4041

4142
typedef struct {
4243
MagTxState tx;
@@ -47,6 +48,7 @@ typedef struct {
4748
MagPin pin_input;
4849
MagPin pin_output;
4950
MagPin pin_enable;
51+
bool allow_uart;
5052
bool is_debug;
5153
} MagState;
5254

scenes/mag_scene_settings.c

+69-5
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44

55
#define TAG "MagSceneEmulateConfig"
66

7+
enum VarItemListIndex {
8+
VarItemListIndexPinInput,
9+
VarItemListIndexPinOutput,
10+
VarItemListIndexPinEnable,
11+
VarItemListIndexAllowUART,
12+
};
13+
714
static const char* gpio[] = {
815
[MagPinA7] = "2 (A7)",
916
[MagPinA6] = "3 (A6)",
@@ -15,6 +22,13 @@ static const char* gpio[] = {
1522
[MagPinC0] = "16 (C0)",
1623
};
1724
const uint8_t GPIO_COUNT = COUNT_OF(gpio);
25+
// static const char* uart_pins[] = {[DapUartTypeUSART1] = "13,14", [DapUartTypeLPUART1] = "15,16"};
26+
// static const char* uart_swap[] = {[DapUartTXRXNormal] = "No", [DapUartTXRXSwap] = "Yes"};
27+
28+
void mag_scene_settings_var_item_list_callback(void* context, uint32_t index) {
29+
Mag* mag = context;
30+
view_dispatcher_send_custom_event(mag->view_dispatcher, index);
31+
}
1832

1933
static void mag_scene_settings_set_gpio(VariableItem* item, MagPin* pin_out) {
2034
MagPin pin = variable_item_get_current_value_index(item);
@@ -47,11 +61,11 @@ static void mag_pin_variable_item_list_add(
4761
variable_item_set_current_value_index(item, pin);
4862
variable_item_set_current_value_text(item, gpio[pin]);
4963
}
50-
// static const char* uart_pins[] = {[DapUartTypeUSART1] = "13,14", [DapUartTypeLPUART1] = "15,16"};
51-
// static const char* uart_swap[] = {[DapUartTXRXNormal] = "No", [DapUartTXRXSwap] = "Yes"};
5264

5365
void mag_scene_settings_on_enter(void* context) {
5466
Mag* mag = context;
67+
VariableItem* item;
68+
VariableItemList* var_item_list = mag->variable_item_list;
5569

5670
mag_pin_variable_item_list_add(
5771
mag, "Input pin:", mag->state.pin_input, mag_scene_settings_set_gpio_input);
@@ -60,10 +74,19 @@ void mag_scene_settings_on_enter(void* context) {
6074
mag_pin_variable_item_list_add(
6175
mag, "Enable pin:", mag->state.pin_enable, mag_scene_settings_set_gpio_enable);
6276

77+
item = variable_item_list_add(var_item_list, "UART MSR: ", 1, NULL, mag);
78+
variable_item_set_current_value_text(item, mag->state.allow_uart ? "ON" : "OFF");
79+
80+
variable_item_list_set_enter_callback(
81+
var_item_list, mag_scene_settings_var_item_list_callback, mag);
82+
83+
variable_item_list_set_selected_item(
84+
var_item_list, scene_manager_get_scene_state(mag->scene_manager, MagSceneSettings));
85+
6386
view_dispatcher_switch_to_view(mag->view_dispatcher, MagViewVariableItemList);
6487
}
6588

66-
void mag_scene_invalid_dialog(Mag* mag) {
89+
void mag_scene_settings_dialog_invalid_pins(Mag* mag) {
6790
SceneManager* scene_manager = mag->scene_manager;
6891

6992
DialogMessage* message = dialog_message_alloc();
@@ -86,19 +109,60 @@ void mag_scene_invalid_dialog(Mag* mag) {
86109
}
87110
}
88111

112+
void mag_scene_settings_dialog_allow_uart(Mag* mag) {
113+
bool change = mag->state.allow_uart;
114+
if(!change) {
115+
DialogMessage* msg = dialog_message_alloc();
116+
dialog_message_set_header(msg, "UART MSR", 64, 0, AlignCenter, AlignTop);
117+
dialog_message_set_buttons(msg, "No", NULL, "Yes");
118+
dialog_message_set_text(
119+
msg,
120+
"This option requires a\nUART-compatible mag reader.\nIs it installed?\n",
121+
64,
122+
32,
123+
AlignCenter,
124+
AlignCenter);
125+
DialogMessageButton res = dialog_message_show(furi_record_open(RECORD_DIALOGS), msg);
126+
if(res == DialogMessageButtonRight) {
127+
change = true;
128+
}
129+
dialog_message_free(msg);
130+
furi_record_close(RECORD_DIALOGS);
131+
}
132+
if(change) {
133+
mag->state.allow_uart = !mag->state.allow_uart;
134+
variable_item_set_current_value_text(
135+
variable_item_list_get(mag->variable_item_list, VarItemListIndexAllowUART),
136+
mag->state.allow_uart ? "ON" : "OFF");
137+
}
138+
}
139+
89140
bool mag_scene_settings_on_event(void* context, SceneManagerEvent event) {
90141
Mag* mag = context;
91142
SceneManager* scene_manager = mag->scene_manager;
92143
bool consumed = false;
93144

94-
if(event.type == SceneManagerEventTypeBack) {
145+
switch(event.type) {
146+
case SceneManagerEventTypeBack:
147+
// when attempting to exit, validate pin configuration
148+
// if invalid, prompt
95149
consumed = true;
96150

97151
if(!mag_state_gpio_is_valid(&mag->state)) {
98-
mag_scene_invalid_dialog(mag);
152+
mag_scene_settings_dialog_invalid_pins(mag);
99153
} else {
100154
scene_manager_previous_scene(scene_manager);
101155
}
156+
break;
157+
case SceneManagerEventTypeCustom:
158+
scene_manager_set_scene_state(mag->scene_manager, MagSceneSettings, event.event);
159+
consumed = true;
160+
if(event.event == VarItemListIndexAllowUART) {
161+
mag_scene_settings_dialog_allow_uart(mag);
162+
}
163+
break;
164+
default:
165+
break;
102166
}
103167

104168
return consumed;

scenes/mag_scene_start.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ void mag_scene_start_on_enter(void* context) {
2525
SubmenuIndexRead,
2626
mag_scene_start_submenu_callback,
2727
mag,
28-
!mag->state.is_debug,
28+
(!mag->state.is_debug && !mag->state.allow_uart),
2929
"Enable Debug!");
3030
//submenu_add_item(
3131
// submenu, "Add Manually", SubmenuIndexAddManually, mag_scene_start_submenu_callback, mag);

0 commit comments

Comments
 (0)