Skip to content

Commit 51ee3ef

Browse files
authored
Merge pull request #7 from zinongli/config_rework
Config rework
2 parents 47e7438 + c0cc986 commit 51ee3ef

File tree

1 file changed

+125
-41
lines changed

1 file changed

+125
-41
lines changed

t5577_writer.c

Lines changed: 125 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include <gui/modules/popup.h>
77
#include <gui/modules/submenu.h>
88
#include <gui/modules/text_input.h>
9+
#include <gui/modules/byte_input.h>
910
#include <gui/modules/widget.h>
1011
#include <gui/modules/variable_item_list.h>
1112
#include <notification/notification.h>
@@ -34,58 +35,69 @@ typedef enum {
3435
T5577WriterSubmenuIndexAbout,
3536
} T5577WriterSubmenuIndex;
3637

38+
// Each view is a screen we show the user.
3739
typedef enum {
38-
T5577WriterViewSubmenu,
39-
T5577WriterViewTextInput,
40+
T5577WriterViewSubmenu, // The menu when the app starts
41+
T5577WriterViewTextInput, // Input for configuring text settings
42+
T5577WriterViewByteInput,
4043
T5577WriterViewLoad,
4144
T5577WriterViewSave,
42-
T5577WriterViewConfigure_i, // The configuration screen that's recreated every time we enter it
43-
T5577WriterViewConfigure_e, // The configuration screen store front that's constantly there
44-
T5577WriterViewWrite,
45-
T5577WriterViewAbout,
45+
T5577WriterViewPopup,
46+
T5577WriterViewConfigure_i, // The configuration screen
47+
T5577WriterViewConfigure_e, // The configuration screen
48+
T5577WriterViewWrite, // The main screen
49+
T5577WriterViewAbout, // The about screen with directions, link to social channel, etc.
4650
} T5577WriterView;
4751

4852
typedef enum {
49-
T5577WriterEventIdRepeatWriting = 0, // Custom event to repeat sending writing commands
50-
T5577WriterEventIdMaxWriteRep = 42, // Custom event to exit writing view
53+
T5577WriterEventIdRepeatWriting = 0, // Custom event to redraw the screen
54+
T5577WriterEventIdMaxWriteRep = 42, // Custom event to process OK button getting pressed down
5155
} T5577WriterEventId;
5256

5357
typedef struct {
5458
ViewDispatcher* view_dispatcher; // Switches between our views
5559
NotificationApp* notifications; // Used for controlling the backlight
5660
Submenu* submenu; // The application menu
61+
5762
TextInput* text_input; // The text input screen
58-
VariableItemList* variable_item_list_config; // The internal configuration view
59-
View* view_config_e; // The external configuration view
60-
View* view_save; // The save view
61-
View* view_write; // The writing view
63+
Popup* popup;
64+
VariableItemList* variable_item_list_config; // The configuration screen
65+
View* view_config_e; // The configuration screen
66+
View* view_save;
67+
View* view_write; // The main screen
6268
Widget* widget_about; // The about screen
6369
View* view_load; // The load view
6470

65-
VariableItem* mod_item;
66-
VariableItem* clock_item;
67-
VariableItem* block_num_item;
68-
VariableItem* block_slc_item;
71+
VariableItem* mod_item; //
72+
VariableItem* clock_item; //
73+
VariableItem* block_num_item; //
74+
VariableItem* block_slc_item; //
75+
VariableItem* byte_buffer_item; //
76+
ByteInput* byte_input; // The byte input view
77+
uint8_t bytes_buffer[4];
78+
uint8_t bytes_count;
79+
6980
char* temp_buffer; // Temporary buffer for text input
7081
uint32_t temp_buffer_size; // Size of temporary buffer
7182

72-
DialogsApp* dialogs; // dialog for file browser
73-
FuriString* file_path; // apps_data/t5577_writer
83+
DialogsApp* dialogs;
84+
FuriString* file_path;
7485
FuriTimer* timer; // Timer for redrawing the screen
86+
ViewNavigationCallback config_enter_callback;
7587
} T5577WriterApp;
7688

7789

7890
typedef struct {
79-
uint8_t modulation_index; // The index for modulation
80-
uint8_t rf_clock_index; // The index for RF clock
91+
uint8_t modulation_index; // The index for total number of pins
92+
uint8_t rf_clock_index; // The index for total number of pins
8193
FuriString* tag_name_str; // The name setting
82-
uint8_t user_block_num; // The total number of blocks to be used, i.e. signal length
83-
uint32_t* content; // The content, 8 blocks of uint32
94+
uint8_t user_block_num; // The total number of pins we are adjusting
95+
uint32_t content[LFRFID_T5577_BLOCK_COUNT]; // The cutting content
8496
t5577_modulation modulation;
8597
t5577_rf_clock rf_clock;
86-
bool data_loaded[3]; // The on/off knobs recording whether the config screen is showing loaded data
87-
uint8_t edit_block_slc; // Select the block to edit
88-
uint8_t writing_repeat_times; // How many times have the write command been sent
98+
bool data_loaded[3];
99+
uint8_t edit_block_slc;
100+
uint8_t writing_repeat_times;
89101
} T5577WriterModel;
90102

91103
void initialize_config(T5577WriterModel* model) {
@@ -96,14 +108,10 @@ void initialize_config(T5577WriterModel* model) {
96108
}
97109

98110
void initialize_model(T5577WriterModel* model) {
99-
if(model->content != NULL) {
100-
free(model->content);
101-
}
102111
initialize_config(model);
103112
model->user_block_num = 1;
104113
model->edit_block_slc = 1;
105114
model->writing_repeat_times = 0;
106-
model->content = (uint32_t*)malloc(LFRFID_T5577_BLOCK_COUNT * sizeof(uint32_t));
107115
for(uint32_t i = 0; i < LFRFID_T5577_BLOCK_COUNT; i++) {
108116
model->content[i] = 0;
109117
}
@@ -150,6 +158,11 @@ static uint32_t t5577_writer_navigation_submenu_callback(void* _context) {
150158
return T5577WriterViewSubmenu;
151159
}
152160

161+
static uint32_t t5577_writer_navigation_config_e_callback(void* _context) {
162+
UNUSED(_context);
163+
return T5577WriterViewConfigure_e;
164+
}
165+
153166
/**
154167
* @brief Handle submenu item selection.
155168
* @details This function is called when user selects an item from the submenu.
@@ -243,6 +256,10 @@ static void t5577_writer_edit_block_slc_change(VariableItem* item) {
243256
FuriString *buffer = furi_string_alloc();
244257
furi_string_printf(buffer, "%u", model->edit_block_slc);
245258
variable_item_set_current_value_text(item, furi_string_get_cstr(buffer));
259+
260+
furi_string_printf(buffer, "%08lX", model->content[model->edit_block_slc]);
261+
variable_item_set_current_value_text(app->byte_buffer_item, furi_string_get_cstr(buffer));
262+
246263
furi_string_free(buffer);
247264
}
248265

@@ -342,13 +359,75 @@ void t5577_writer_update_config_from_load(void* context) {
342359
my_model->rf_clock = all_rf_clocks[my_model->rf_clock_index];
343360
}
344361
}
345-
346362
my_model->user_block_num = (my_model->content[0] >> LFRFID_T5577_MAXBLOCK_SHIFT) & 0xF;
347-
348363
memset(my_model->data_loaded, true, sizeof(my_model->data_loaded)); // Everything is loaded
364+
}
349365

366+
static const char* edit_block_data_config_label = "Block Data";
367+
void uint32_to_byte_buffer(uint32_t block_data, uint8_t byte_buffer[4]) {
368+
byte_buffer[0] = (block_data >> 24) & 0xFF;
369+
byte_buffer[1] = (block_data >> 16) & 0xFF;
370+
byte_buffer[2] = (block_data >> 8) & 0xFF;
371+
byte_buffer[3] = block_data & 0xFF;
372+
}
373+
374+
uint32_t byte_buffer_to_uint32(uint8_t byte_buffer[4]) {
375+
uint32_t block_data = 0;
376+
block_data |= ((uint32_t)byte_buffer[0] << 24);
377+
block_data |= ((uint32_t)byte_buffer[1] << 16);
378+
block_data |= ((uint32_t)byte_buffer[2] << 8);
379+
block_data |= ((uint32_t)byte_buffer[3]);
380+
return block_data;
381+
}
382+
383+
384+
static void t5577_writer_content_byte_input_confirmed(void* context) {
385+
T5577WriterApp* app = (T5577WriterApp*)context;
386+
T5577WriterModel* my_model = view_get_model(app->view_write);
387+
my_model->content[my_model->edit_block_slc] = byte_buffer_to_uint32(app->bytes_buffer);
388+
view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewConfigure_e);
350389
}
351390

391+
static void t5577_writer_content_byte_changed(void* context) {
392+
UNUSED(context);
393+
}
394+
static void t5577_writer_config_item_clicked(void* context, uint32_t index) {
395+
T5577WriterApp* app = (T5577WriterApp*)context;
396+
T5577WriterModel* my_model = view_get_model(app->view_write);
397+
FuriString *buffer = furi_string_alloc();
398+
furi_string_printf(buffer, "Enter Block %u Data", my_model->edit_block_slc);
399+
// Our hex input UI is the 5th in the config menue.
400+
if(index == 4) {
401+
// Header to display on the text input screen.
402+
byte_input_set_header_text(app->byte_input, furi_string_get_cstr(buffer));
403+
404+
// Copy the current name into the temporary buffer.
405+
bool redraw = false;
406+
with_view_model(
407+
app->view_write,
408+
T5577WriterModel * model,
409+
{
410+
uint32_to_byte_buffer(model->content[model->edit_block_slc],app->bytes_buffer);
411+
},
412+
redraw);
413+
414+
// Configure the text input. When user enters text and clicks OK, key_copier_setting_text_updated be called.
415+
byte_input_set_result_callback(
416+
app->byte_input,
417+
t5577_writer_content_byte_input_confirmed,
418+
t5577_writer_content_byte_changed,
419+
app,
420+
app->bytes_buffer,
421+
app->bytes_count
422+
);
423+
424+
// Pressing the BACK button will reload the configure screen.
425+
view_set_previous_callback(byte_input_get_view(app->byte_input), t5577_writer_navigation_config_e_callback);
426+
427+
// Show text input dialog.
428+
view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewByteInput);
429+
}
430+
}
352431
static void t5577_writer_config_enter_callback(void* context) {
353432
T5577WriterApp* app = (T5577WriterApp*)context;
354433
T5577WriterModel* my_model = view_get_model(app->view_write);
@@ -378,6 +457,14 @@ static void t5577_writer_config_enter_callback(void* context) {
378457
LFRFID_T5577_BLOCK_COUNT - 1,
379458
t5577_writer_edit_block_slc_change,
380459
app);
460+
app->byte_buffer_item = variable_item_list_add(
461+
app->variable_item_list_config,
462+
edit_block_data_config_label,
463+
1,
464+
NULL,
465+
app);
466+
variable_item_list_set_enter_callback(app->variable_item_list_config, t5577_writer_config_item_clicked, app);
467+
381468
View* view_config_i = variable_item_list_get_view(app->variable_item_list_config);
382469

383470
variable_item_set_current_value_index(app->mod_item,my_model->modulation_index);
@@ -687,7 +774,12 @@ static T5577WriterApp* t5577_writer_app_alloc() {
687774
T5577WriterViewSave,
688775
app->view_save);
689776

690-
777+
app->bytes_count = 4;
778+
memset(app->bytes_buffer, 0, sizeof(app->bytes_buffer));
779+
780+
app->byte_input = byte_input_alloc();
781+
view_dispatcher_add_view(
782+
app->view_dispatcher, T5577WriterViewByteInput, byte_input_get_view(app->byte_input));
691783
app->variable_item_list_config = variable_item_list_alloc();
692784

693785
app->view_config_e = view_alloc();
@@ -747,20 +839,12 @@ static void t5577_writer_app_free(T5577WriterApp* app) {
747839
view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewAbout);
748840
widget_free(app->widget_about);
749841
view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewWrite);
750-
with_view_model(
751-
app->view_write,
752-
T5577WriterModel * model,
753-
{
754-
if(model->content != NULL) {
755-
free(model->content);
756-
}
757-
},
758-
false);
759842
view_free(app->view_write);
760843
view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewLoad);
761844
view_free(app->view_load);
762845
view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewConfigure_i);
763846
view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewConfigure_e);
847+
view_dispatcher_remove_view(app->view_dispatcher,T5577WriterViewByteInput);
764848
variable_item_list_free(app->variable_item_list_config);
765849
view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewSave);
766850
view_free(app->view_save);

0 commit comments

Comments
 (0)