Skip to content

Commit c1ea848

Browse files
authored
Merge pull request #2 from zinongli/popup
Writing ends after 3 second. Config memory loss fixed.
2 parents 33b687f + 7a9ab7d commit c1ea848

File tree

2 files changed

+54
-23
lines changed

2 files changed

+54
-23
lines changed

assets/NFC_manual_60x50.png

3.71 KB
Loading

t5577_writer.c

+54-23
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@
1818
#include <stdio.h>
1919
#include <t5577_config.h>
2020
#include <t5577_writer.h>
21+
#include <dolphin/dolphin.h>
22+
23+
2124

2225
#define TAG "T5577 Writer"
26+
#define MAX_REPEAT_WRITING_TIMES 15
2327

2428
// Change this to BACKLIGHT_AUTO if you don't want the backlight to be continuously on.
2529
#define BACKLIGHT_AUTO 1
@@ -39,22 +43,24 @@ typedef enum {
3943
T5577WriterViewTextInput, // Input for configuring text settings
4044
T5577WriterViewLoad,
4145
T5577WriterViewSave,
46+
T5577WriterViewPopup,
4247
T5577WriterViewConfigure_i, // The configuration screen
4348
T5577WriterViewConfigure_e, // The configuration screen
4449
T5577WriterViewWrite, // The main screen
4550
T5577WriterViewAbout, // The about screen with directions, link to social channel, etc.
4651
} T5577WriterView;
4752

4853
typedef enum {
49-
T5577WriterEventIdRedrawScreen = 0, // Custom event to redraw the screen
50-
T5577WriterEventIdOkPressed = 42, // Custom event to process OK button getting pressed down
54+
T5577WriterEventIdRepeatWriting = 0, // Custom event to redraw the screen
55+
T5577WriterEventIdMaxWriteRep = 42, // Custom event to process OK button getting pressed down
5156
} T5577WriterEventId;
5257

5358
typedef struct {
5459
ViewDispatcher* view_dispatcher; // Switches between our views
5560
NotificationApp* notifications; // Used for controlling the backlight
5661
Submenu* submenu; // The application menu
5762
TextInput* text_input; // The text input screen
63+
Popup* popup;
5864
VariableItemList* variable_item_list_config; // The configuration screen
5965
View* view_config_e; // The configuration screen
6066
View* view_save;
@@ -85,6 +91,7 @@ typedef struct {
8591
t5577_rf_clock rf_clock;
8692
bool data_loaded[3];
8793
uint8_t edit_block_slc;
94+
uint8_t writing_repeat_times;
8895
} T5577WriterModel;
8996

9097
static inline int min(int a, int b) {
@@ -109,6 +116,7 @@ void initialize_model(T5577WriterModel* model) {
109116
initialize_config(model);
110117
model->user_block_num = 1;
111118
model->edit_block_slc = 1;
119+
model->writing_repeat_times = 0;
112120
model->content = (uint32_t*)malloc(LFRFID_T5577_BLOCK_COUNT * sizeof(uint32_t));
113121
for(uint32_t i = 0; i < LFRFID_T5577_BLOCK_COUNT; i++) {
114122
model->content[i] = 0;
@@ -347,7 +355,7 @@ static void t5577_writer_file_saver(void* context) {
347355
furi_string_printf(buffer, "Number of User Blocks: %u\n", model->user_block_num);
348356
storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
349357
furi_string_printf(buffer, "\nRaw Data:\n");
350-
for (int i = 0; i < model->user_block_num; i++)
358+
for (int i = 0; i < LFRFID_T5577_BLOCK_COUNT; i++)
351359
{
352360
furi_string_cat_printf(
353361
buffer,
@@ -366,8 +374,6 @@ static void t5577_writer_file_saver(void* context) {
366374
void t5577_writer_update_config_from_load(void* context) {
367375
T5577WriterApp* app = (T5577WriterApp*)context;
368376
T5577WriterModel* my_model = view_get_model(app->view_write);
369-
370-
371377
for (size_t i = 0; i < COUNT_OF(all_mods); i++) {
372378
if ((my_model->content[0] & all_mods[i].mod_page_zero) == all_mods[i].mod_page_zero) {
373379
my_model->modulation_index = (uint8_t)i;
@@ -390,7 +396,7 @@ void t5577_writer_update_config_from_load(void* context) {
390396

391397
static void t5577_writer_config_enter_callback(void* context) {
392398
T5577WriterApp* app = (T5577WriterApp*)context;
393-
399+
T5577WriterModel* my_model = view_get_model(app->view_write);
394400
variable_item_list_reset(app->variable_item_list_config);
395401
app->mod_item = variable_item_list_add(
396402
app->variable_item_list_config,
@@ -417,6 +423,12 @@ static void t5577_writer_config_enter_callback(void* context) {
417423
t5577_writer_edit_block_slc_change,
418424
app);
419425
View* view_config_i = variable_item_list_get_view(app->variable_item_list_config);
426+
427+
variable_item_set_current_value_index(app->mod_item,my_model->modulation_index);
428+
variable_item_set_current_value_index(app->clock_item,my_model->rf_clock_index);
429+
variable_item_set_current_value_index(app->block_num_item,my_model->user_block_num - 1);
430+
variable_item_set_current_value_index(app->block_slc_item,my_model->edit_block_slc - 1);
431+
420432
t5577_writer_modulation_change(app->mod_item);
421433
t5577_writer_rf_clock_change(app->clock_item);
422434
t5577_writer_user_block_num_change(app->block_num_item);
@@ -535,28 +547,34 @@ static void t5577_writer_view_save_callback(void* context) {
535547

536548
}
537549

538-
/**
539-
* @brief Callback for drawing the game screen.
540-
* @details This function is called when the screen needs to be redrawn, like when the model gets updated.
541-
* @param canvas The canvas to draw on.
542-
* @param model The model - MyModel object.
543-
*/
544-
static void t5577_writer_view_write_callback(Canvas* canvas, void* model) {
550+
static void t5577_writer_actual_writing(void* model) {
545551
T5577WriterModel* my_model = (T5577WriterModel*)model;
546552
my_model->content[0] = 0;
547553
my_model->content[0] |= my_model->modulation.mod_page_zero;
548554
my_model->content[0] |= my_model->rf_clock.clock_page_zero;
549555
my_model->content[0] |= (my_model->user_block_num << LFRFID_T5577_MAXBLOCK_SHIFT);
550-
551556
LFRFIDT5577* data = (LFRFIDT5577*)malloc(sizeof(LFRFIDT5577));
552557
data->blocks_to_write = my_model->user_block_num;
553558
for(size_t i = 0; i < data->blocks_to_write; i++) {
554559
data->block[i] = my_model->content[i];
555560
}
556561
t5577_write(data);
557562
free(data);
563+
}
558564

559-
canvas_draw_str(canvas, 32, 10, "Writing...");
565+
/**
566+
* @brief Callback for drawing the game screen.
567+
* @details This function is called when the screen needs to be redrawn, like when the model gets updated.
568+
* @param canvas The canvas to draw on.
569+
* @param model The model - MyModel object.
570+
*/
571+
static void t5577_writer_view_write_callback(Canvas* canvas, void* model) {
572+
t5577_writer_actual_writing(model);
573+
canvas_set_bitmap_mode(canvas, true);
574+
canvas_draw_icon(canvas, 0, 8, &I_NFC_manual_60x50);
575+
canvas_draw_str_aligned(canvas, 97, 15, AlignCenter, AlignTop, "Writing");
576+
canvas_draw_str_aligned(canvas, 94, 27, AlignCenter, AlignTop, "Hold card next");
577+
canvas_draw_str_aligned(canvas, 93, 39, AlignCenter, AlignTop, "to Flipper's back");
560578
}
561579

562580
/**
@@ -566,7 +584,13 @@ static void t5577_writer_view_write_callback(Canvas* canvas, void* model) {
566584
*/
567585
static void t5577_writer_view_write_timer_callback(void* context) {
568586
T5577WriterApp* app = (T5577WriterApp*)context;
569-
view_dispatcher_send_custom_event(app->view_dispatcher, T5577WriterEventIdRedrawScreen);
587+
T5577WriterModel* model = view_get_model(app->view_write);
588+
if (model->writing_repeat_times < MAX_REPEAT_WRITING_TIMES){
589+
model->writing_repeat_times += 1;
590+
view_dispatcher_send_custom_event(app->view_dispatcher, T5577WriterEventIdRepeatWriting);
591+
} else {
592+
view_dispatcher_send_custom_event(app->view_dispatcher, T5577WriterEventIdMaxWriteRep);
593+
}
570594
}
571595

572596
/**
@@ -576,12 +600,13 @@ static void t5577_writer_view_write_timer_callback(void* context) {
576600
* @param context The context - T5577WriterApp object.
577601
*/
578602
static void t5577_writer_view_write_enter_callback(void* context) {
579-
uint32_t period = furi_ms_to_ticks(500);
603+
uint32_t repeat_writing_period = furi_ms_to_ticks(200);
580604
T5577WriterApp* app = (T5577WriterApp*)context;
581605
furi_assert(app->timer == NULL);
582606
app->timer =
583607
furi_timer_alloc(t5577_writer_view_write_timer_callback, FuriTimerTypePeriodic, context);
584-
furi_timer_start(app->timer, period);
608+
furi_timer_start(app->timer, repeat_writing_period);
609+
dolphin_deed(DolphinDeedRfidEmulate);
585610
}
586611

587612
/**
@@ -591,9 +616,11 @@ static void t5577_writer_view_write_enter_callback(void* context) {
591616
*/
592617
static void t5577_writer_view_write_exit_callback(void* context) {
593618
T5577WriterApp* app = (T5577WriterApp*)context;
619+
T5577WriterModel* model = view_get_model(app->view_write);
594620
furi_timer_stop(app->timer);
595621
furi_timer_free(app->timer);
596622
app->timer = NULL;
623+
model->writing_repeat_times = 0;
597624
}
598625

599626
/**
@@ -605,15 +632,15 @@ static void t5577_writer_view_write_exit_callback(void* context) {
605632
static bool t5577_writer_view_write_custom_event_callback(uint32_t event, void* context) {
606633
T5577WriterApp* app = (T5577WriterApp*)context;
607634
switch(event) {
608-
case T5577WriterEventIdRedrawScreen:
635+
case T5577WriterEventIdRepeatWriting:
609636
// Redraw screen by passing true to last parameter of with_view_model.
610637
{
611638
bool redraw = true;
612639
with_view_model(
613-
app->view_write, T5577WriterModel * _model, { UNUSED(_model); }, redraw);
640+
app->view_write, T5577WriterModel * _model, {UNUSED(_model);}, redraw);
614641
return true;
615642
}
616-
case T5577WriterEventIdOkPressed:
643+
case T5577WriterEventIdMaxWriteRep:
617644
// Process the OK button. We go to the saving scene.
618645
view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewSubmenu);
619646
return true;
@@ -634,9 +661,9 @@ static bool t5577_writer_view_write_input_callback(InputEvent* event, void* cont
634661
if(event->type == InputTypeShort) {
635662
if(event->key == InputKeyOk) {
636663
// We choose to send a custom event when user presses OK button. t5577_writer_custom_event_callback will
637-
// handle our T5577WriterEventIdOkPressed event. We could have just put the code from
664+
// handle our T5577WriterEventIdMaxWriteRep event. We could have just put the code from
638665
// t5577_writer_custom_event_callback here, it's a matter of preference.
639-
view_dispatcher_send_custom_event(app->view_dispatcher, T5577WriterEventIdOkPressed);
666+
view_dispatcher_send_custom_event(app->view_dispatcher, T5577WriterEventIdMaxWriteRep);
640667
return true;
641668
}
642669
}
@@ -679,6 +706,8 @@ static T5577WriterApp* t5577_writer_app_alloc() {
679706
view_dispatcher_add_view(
680707
app->view_dispatcher, T5577WriterViewTextInput, text_input_get_view(app->text_input));
681708

709+
app->popup = popup_alloc();
710+
view_dispatcher_add_view(app->view_dispatcher,T5577WriterViewPopup,popup_get_view(app->popup));
682711

683712
app->view_load = view_alloc();
684713
view_set_previous_callback(app->view_load, t5577_writer_navigation_submenu_callback);
@@ -798,6 +827,8 @@ static void t5577_writer_app_free(T5577WriterApp* app) {
798827
view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewConfigure_i);
799828
view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewConfigure_e);
800829
variable_item_list_free(app->variable_item_list_config);
830+
view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewPopup);
831+
popup_free(app->popup);
801832
view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewSave);
802833
view_free(app->view_save);
803834
view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewSubmenu);

0 commit comments

Comments
 (0)