t5577_writer.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <gui/gui.h>
  4. #include <gui/view.h>
  5. #include <gui/view_dispatcher.h>
  6. #include <gui/modules/submenu.h>
  7. #include <gui/modules/text_input.h>
  8. #include <gui/modules/byte_input.h>
  9. #include <gui/modules/widget.h>
  10. #include <gui/modules/variable_item_list.h>
  11. #include <notification/notification.h>
  12. #include <notification/notification_messages.h>
  13. #include <applications/services/storage/storage.h>
  14. #include <applications/services/dialogs/dialogs.h>
  15. #include <dolphin/dolphin.h>
  16. #include <flipper_format.h>
  17. #include <stdbool.h>
  18. #include <stdint.h>
  19. #include <stdio.h>
  20. #include <t5577_config.h>
  21. #include <t5577_writer.h>
  22. #include "t5577_writer_icons.h"
  23. #define TAG "T5577 Writer"
  24. #define MAX_REPEAT_WRITING_FRAMES 10
  25. #define ENDING_WRITING_ICON_FRAMES 5
  26. typedef enum {
  27. T5577WriterSubmenuIndexLoad,
  28. T5577WriterSubmenuIndexSave,
  29. T5577WriterSubmenuIndexConfigure,
  30. T5577WriterSubmenuIndexWrite,
  31. T5577WriterSubmenuIndexAbout,
  32. } T5577WriterSubmenuIndex;
  33. // Each view is a screen we show the user.
  34. typedef enum {
  35. T5577WriterViewSubmenu, // The menu when the app starts
  36. T5577WriterViewTextInput, // Input for configuring text settings
  37. T5577WriterViewByteInput,
  38. T5577WriterViewLoad,
  39. T5577WriterViewSave,
  40. T5577WriterViewConfigure_i, // The configuration screen
  41. T5577WriterViewConfigure_e, // The configuration screen
  42. T5577WriterViewWrite, // The main screen
  43. T5577WriterViewAbout, // The about screen with directions, link to social channel, etc.
  44. } T5577WriterView;
  45. typedef enum {
  46. T5577WriterEventIdRepeatWriting = 0, // Custom event to redraw the screen
  47. T5577WriterEventIdMaxWriteRep = 42, // Custom event to process OK button getting pressed down
  48. } T5577WriterEventId;
  49. typedef struct {
  50. ViewDispatcher* view_dispatcher; // Switches between our views
  51. NotificationApp* notifications; // Used for controlling the backlight
  52. Submenu* submenu; // The application menu
  53. TextInput* text_input; // The text input screen
  54. VariableItemList* variable_item_list_config; // The configuration screen
  55. View* view_config_e; // The configuration screen
  56. View* view_save;
  57. View* view_write; // The main screen
  58. Widget* widget_about; // The about screen
  59. View* view_load; // The load view
  60. VariableItem* mod_item; //
  61. VariableItem* clock_item; //
  62. VariableItem* block_num_item; //
  63. VariableItem* block_slc_item; //
  64. VariableItem* byte_buffer_item; //
  65. ByteInput* byte_input; // The byte input view
  66. uint8_t bytes_buffer[4];
  67. uint8_t bytes_count;
  68. char* temp_buffer; // Temporary buffer for text input
  69. uint32_t temp_buffer_size; // Size of temporary buffer
  70. DialogsApp* dialogs;
  71. FuriString* file_path;
  72. FuriTimer* timer; // Timer for redrawing the screen
  73. } T5577WriterApp;
  74. typedef struct {
  75. uint8_t modulation_index; // The index for total number of pins
  76. uint8_t rf_clock_index; // The index for total number of pins
  77. FuriString* tag_name_str; // The name setting
  78. uint8_t user_block_num; // The total number of pins we are adjusting
  79. uint32_t content[LFRFID_T5577_BLOCK_COUNT]; // The cutting content
  80. t5577_modulation modulation;
  81. t5577_rf_clock rf_clock;
  82. bool data_loaded[3];
  83. uint8_t edit_block_slc;
  84. uint8_t writing_repeat_times;
  85. } T5577WriterModel;
  86. void initialize_config(T5577WriterModel* model) {
  87. model->modulation_index = 0;
  88. memcpy(&model->modulation, &all_mods[model->modulation_index], sizeof(t5577_modulation));
  89. model->rf_clock_index = 0;
  90. memcpy(&model->rf_clock, &all_mods[model->rf_clock_index], sizeof(t5577_rf_clock));
  91. }
  92. void initialize_model(T5577WriterModel* model) {
  93. initialize_config(model);
  94. model->user_block_num = 0;
  95. model->edit_block_slc = 1;
  96. model->writing_repeat_times = 0;
  97. for(uint32_t i = 0; i < LFRFID_T5577_BLOCK_COUNT; i++) {
  98. model->content[i] = 0;
  99. }
  100. memset(model->data_loaded, false, sizeof(model->data_loaded));
  101. }
  102. uint8_t rf_clock_choices[COUNT_OF(all_rf_clocks)];
  103. void initialize_rf_clock_choices(uint8_t* rf_clock_choices) {
  104. // Populate the rf_clock_choices array
  105. for(size_t i = 0; i < COUNT_OF(all_rf_clocks); i++) {
  106. rf_clock_choices[i] = all_rf_clocks[i].rf_clock_num;
  107. }
  108. }
  109. char* modulation_names[COUNT_OF(all_mods)];
  110. void initialize_mod_names(char** modulation_names) {
  111. // Populate the modulation_names array
  112. for(size_t i = 0; i < COUNT_OF(all_mods); i++) {
  113. modulation_names[i] = all_mods[i].modulation_name;
  114. }
  115. }
  116. /**
  117. * @brief Callback for exiting the application.
  118. * @details This function is called when user press back button. We return VIEW_NONE to
  119. * indicate that we want to exit the application.
  120. * @param _context The context - unused
  121. * @return next view id
  122. */
  123. static uint32_t t5577_writer_navigation_exit_callback(void* _context) {
  124. UNUSED(_context);
  125. return VIEW_NONE;
  126. }
  127. /**
  128. * @brief Callback for returning to submenu.
  129. * @details This function is called when user press back button. We return VIEW_NONE to
  130. * indicate that we want to navigate to the submenu.
  131. * @param _context The context - unused
  132. * @return next view id
  133. */
  134. static uint32_t t5577_writer_navigation_submenu_callback(void* _context) {
  135. UNUSED(_context);
  136. return T5577WriterViewSubmenu;
  137. }
  138. static uint32_t t5577_writer_navigation_config_e_callback(void* _context) {
  139. UNUSED(_context);
  140. return T5577WriterViewConfigure_e;
  141. }
  142. /**
  143. * @brief Handle submenu item selection.
  144. * @details This function is called when user selects an item from the submenu.
  145. * @param context The context - T5577WriterApp object.
  146. * @param index The T5577WriterSubmenuIndex item that was clicked.
  147. */
  148. static void t5577_writer_submenu_callback(void* context, uint32_t index) {
  149. T5577WriterApp* app = (T5577WriterApp*)context;
  150. switch(index) {
  151. case T5577WriterSubmenuIndexLoad:
  152. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewLoad);
  153. break;
  154. case T5577WriterSubmenuIndexSave:
  155. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewSave);
  156. break;
  157. case T5577WriterSubmenuIndexConfigure:
  158. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewConfigure_e);
  159. break;
  160. case T5577WriterSubmenuIndexWrite:
  161. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewWrite);
  162. break;
  163. case T5577WriterSubmenuIndexAbout:
  164. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewAbout);
  165. break;
  166. default:
  167. break;
  168. }
  169. }
  170. static const char* modulation_config_label = "Modulation";
  171. static void t5577_writer_modulation_change(VariableItem* item) {
  172. T5577WriterApp* app = variable_item_get_context(item);
  173. T5577WriterModel* model = view_get_model(app->view_write);
  174. if(model->data_loaded[0]) {
  175. variable_item_set_current_value_index(item, model->modulation_index);
  176. } else {
  177. uint8_t modulation_index = variable_item_get_current_value_index(item);
  178. model->modulation_index = modulation_index;
  179. model->modulation = all_mods[modulation_index];
  180. }
  181. model->data_loaded[0] = false;
  182. variable_item_set_current_value_text(item, modulation_names[model->modulation_index]);
  183. }
  184. static const char* rf_clock_config_label = "RF Clock";
  185. static void t5577_writer_rf_clock_change(VariableItem* item) {
  186. T5577WriterApp* app = variable_item_get_context(item);
  187. T5577WriterModel* model = view_get_model(app->view_write);
  188. if(model->data_loaded[1]) {
  189. variable_item_set_current_value_index(item, model->rf_clock_index);
  190. } else {
  191. uint8_t rf_clock_index = variable_item_get_current_value_index(item);
  192. model->rf_clock_index = rf_clock_index;
  193. model->rf_clock = all_rf_clocks[rf_clock_index];
  194. }
  195. model->data_loaded[1] = false;
  196. FuriString* buffer = furi_string_alloc();
  197. furi_string_printf(buffer, "%u", rf_clock_choices[model->rf_clock_index]);
  198. variable_item_set_current_value_text(item, furi_string_get_cstr(buffer));
  199. furi_string_free(buffer);
  200. }
  201. static const char* user_block_num_config_label = "Max User Block";
  202. static void t5577_writer_user_block_num_change(VariableItem* item) {
  203. T5577WriterApp* app = variable_item_get_context(item);
  204. T5577WriterModel* model = view_get_model(app->view_write);
  205. if(model->data_loaded[2]) {
  206. variable_item_set_current_value_index(item, model->user_block_num);
  207. } else {
  208. uint8_t user_block_num_index = variable_item_get_current_value_index(item);
  209. model->user_block_num = user_block_num_index;
  210. }
  211. model->data_loaded[2] = false;
  212. FuriString* buffer = furi_string_alloc();
  213. furi_string_printf(buffer, "%u", model->user_block_num);
  214. variable_item_set_current_value_text(item, furi_string_get_cstr(buffer));
  215. for(uint8_t i = model->user_block_num + 1; i < LFRFID_T5577_BLOCK_COUNT; i++) {
  216. model->content[i] = 0; // pad the unneeded blocks with zeros
  217. }
  218. furi_string_free(buffer);
  219. }
  220. static const char* edit_block_slc_config_label = "Edit Block";
  221. static void t5577_writer_edit_block_slc_change(VariableItem* item) {
  222. T5577WriterApp* app = variable_item_get_context(item);
  223. T5577WriterModel* model = view_get_model(app->view_write);
  224. uint8_t edit_block_slc_index = variable_item_get_current_value_index(item);
  225. model->edit_block_slc = edit_block_slc_index + 1;
  226. variable_item_set_current_value_index(item, model->edit_block_slc - 1);
  227. FuriString* buffer = furi_string_alloc();
  228. furi_string_printf(buffer, "%u", model->edit_block_slc);
  229. variable_item_set_current_value_text(item, furi_string_get_cstr(buffer));
  230. furi_string_printf(buffer, "%08lX", model->content[model->edit_block_slc]);
  231. variable_item_set_current_value_text(app->byte_buffer_item, furi_string_get_cstr(buffer));
  232. furi_string_free(buffer);
  233. }
  234. static const char* tag_name_entry_text = "Enter name";
  235. static const char* tag_name_default_value = "Tag_1";
  236. static void t5577_writer_file_saver(void* context) {
  237. T5577WriterApp* app = (T5577WriterApp*)context;
  238. T5577WriterModel* model = view_get_model(app->view_write);
  239. model->content[0] = 0; // clean up first block before deciding to write or save
  240. model->content[0] |= model->modulation.mod_page_zero;
  241. model->content[0] |= model->rf_clock.clock_page_zero;
  242. model->content[0] |= (model->user_block_num << LFRFID_T5577_MAXBLOCK_SHIFT);
  243. bool redraw = true;
  244. with_view_model(
  245. app->view_write,
  246. T5577WriterModel * model,
  247. { furi_string_set(model->tag_name_str, app->temp_buffer); },
  248. redraw);
  249. FuriString* file_path = furi_string_alloc();
  250. furi_string_printf(
  251. file_path,
  252. "%s/%s%s",
  253. STORAGE_APP_DATA_PATH_PREFIX,
  254. furi_string_get_cstr(model->tag_name_str),
  255. T5577_WRITER_FILE_EXTENSION);
  256. Storage* storage = furi_record_open(RECORD_STORAGE);
  257. storage_simply_mkdir(storage, STORAGE_APP_DATA_PATH_PREFIX);
  258. FuriString* buffer = furi_string_alloc();
  259. FlipperFormat* format = flipper_format_file_alloc(storage);
  260. do {
  261. const uint32_t version = 2;
  262. const uint32_t clock_buffer = (uint32_t)model->rf_clock.rf_clock_num;
  263. const uint32_t block_num_buffer = (uint32_t)model->user_block_num;
  264. if(!flipper_format_file_open_always(format, furi_string_get_cstr(file_path))) break;
  265. if(!flipper_format_write_header_cstr(format, "Flipper T5577 Raw File", version)) break;
  266. if(!flipper_format_write_string_cstr(
  267. format, "Modulation", model->modulation.modulation_name))
  268. break;
  269. if(!flipper_format_write_uint32(format, "RF Clock", &clock_buffer, 1)) break;
  270. if(!flipper_format_write_uint32(format, "Max User Block", &block_num_buffer, 1)) break;
  271. if(!flipper_format_write_string_cstr(format, "Raw Data", "")) break; // raw data begins
  272. for(int i = 0; i < LFRFID_T5577_BLOCK_COUNT; i++) {
  273. furi_string_printf(buffer, "Block %u", i);
  274. uint8_t byte_array_buffer[app->bytes_count];
  275. uint32_to_byte_buffer(model->content[i], byte_array_buffer);
  276. if(!flipper_format_write_hex(
  277. format, furi_string_get_cstr(buffer), byte_array_buffer, app->bytes_count))
  278. break;
  279. }
  280. // signal that the file was written successfully
  281. } while(0);
  282. flipper_format_free(format);
  283. view_dispatcher_switch_to_view(
  284. app->view_dispatcher, T5577WriterViewSubmenu); // maybe add a pop up later
  285. }
  286. void t5577_writer_update_config_from_load(void* context) {
  287. T5577WriterApp* app = (T5577WriterApp*)context;
  288. T5577WriterModel* my_model = view_get_model(app->view_write);
  289. for(size_t i = 0; i < COUNT_OF(all_mods); i++) {
  290. if((my_model->content[0] & all_mods[i].mod_page_zero) == all_mods[i].mod_page_zero) {
  291. my_model->modulation_index = (uint8_t)i;
  292. my_model->modulation = all_mods[my_model->modulation_index];
  293. }
  294. }
  295. for(size_t i = 0; i < COUNT_OF(all_rf_clocks); i++) {
  296. if((my_model->content[0] & all_rf_clocks[i].clock_page_zero) ==
  297. all_rf_clocks[i].clock_page_zero) {
  298. my_model->rf_clock_index = (uint8_t)i;
  299. my_model->rf_clock = all_rf_clocks[my_model->rf_clock_index];
  300. }
  301. }
  302. my_model->user_block_num = ((my_model->content[0] >> LFRFID_T5577_MAXBLOCK_SHIFT) & 0x7);
  303. FURI_LOG_D(TAG, "BLOCK 0 %08lX", my_model->content[0]);
  304. FURI_LOG_D(TAG, "bit 25-27 %ld", (my_model->content[0] >> LFRFID_T5577_MAXBLOCK_SHIFT) & 0x7);
  305. memset(my_model->data_loaded, true, sizeof(my_model->data_loaded)); // Everything is loaded
  306. }
  307. static const char* edit_block_data_config_label = "Block Data";
  308. static void t5577_writer_content_byte_input_confirmed(void* context) {
  309. T5577WriterApp* app = (T5577WriterApp*)context;
  310. T5577WriterModel* my_model = view_get_model(app->view_write);
  311. my_model->content[my_model->edit_block_slc] = byte_buffer_to_uint32(app->bytes_buffer);
  312. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewConfigure_e);
  313. }
  314. static void t5577_writer_content_byte_changed(void* context) {
  315. UNUSED(context);
  316. }
  317. static void t5577_writer_config_item_clicked(void* context, uint32_t index) {
  318. T5577WriterApp* app = (T5577WriterApp*)context;
  319. T5577WriterModel* my_model = view_get_model(app->view_write);
  320. FuriString* buffer = furi_string_alloc();
  321. furi_string_printf(buffer, "Enter Block %u Data", my_model->edit_block_slc);
  322. // Our hex input UI is the 5th in the config menue.
  323. if(index == 4) {
  324. // Header to display on the text input screen.
  325. byte_input_set_header_text(app->byte_input, furi_string_get_cstr(buffer));
  326. // Copy the current name into the temporary buffer.
  327. bool redraw = false;
  328. with_view_model(
  329. app->view_write,
  330. T5577WriterModel * model,
  331. { uint32_to_byte_buffer(model->content[model->edit_block_slc], app->bytes_buffer); },
  332. redraw);
  333. // Configure the text input. When user enters text and clicks OK, key_copier_setting_text_updated be called.
  334. byte_input_set_result_callback(
  335. app->byte_input,
  336. t5577_writer_content_byte_input_confirmed,
  337. t5577_writer_content_byte_changed,
  338. app,
  339. app->bytes_buffer,
  340. app->bytes_count);
  341. // Pressing the BACK button will reload the configure screen.
  342. view_set_previous_callback(
  343. byte_input_get_view(app->byte_input), t5577_writer_navigation_config_e_callback);
  344. // Show text input dialog.
  345. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewByteInput);
  346. }
  347. }
  348. static void t5577_writer_config_enter_callback(void* context) {
  349. T5577WriterApp* app = (T5577WriterApp*)context;
  350. T5577WriterModel* my_model = view_get_model(app->view_write);
  351. variable_item_list_reset(app->variable_item_list_config);
  352. // Recreate this view every time we enter it so that it's always updated
  353. app->mod_item = variable_item_list_add(
  354. app->variable_item_list_config,
  355. modulation_config_label,
  356. COUNT_OF(modulation_names),
  357. t5577_writer_modulation_change,
  358. app);
  359. app->clock_item = variable_item_list_add(
  360. app->variable_item_list_config,
  361. rf_clock_config_label,
  362. COUNT_OF(rf_clock_choices),
  363. t5577_writer_rf_clock_change,
  364. app);
  365. app->block_num_item = variable_item_list_add(
  366. app->variable_item_list_config,
  367. user_block_num_config_label,
  368. LFRFID_T5577_BLOCK_COUNT,
  369. t5577_writer_user_block_num_change,
  370. app);
  371. app->block_slc_item = variable_item_list_add(
  372. app->variable_item_list_config,
  373. edit_block_slc_config_label,
  374. LFRFID_T5577_BLOCK_COUNT - 1,
  375. t5577_writer_edit_block_slc_change,
  376. app);
  377. app->byte_buffer_item = variable_item_list_add(
  378. app->variable_item_list_config, edit_block_data_config_label, 1, NULL, app);
  379. variable_item_list_set_enter_callback(
  380. app->variable_item_list_config, t5577_writer_config_item_clicked, app);
  381. View* view_config_i = variable_item_list_get_view(app->variable_item_list_config);
  382. variable_item_set_current_value_index(app->mod_item, my_model->modulation_index);
  383. variable_item_set_current_value_index(app->clock_item, my_model->rf_clock_index);
  384. variable_item_set_current_value_index(app->block_num_item, my_model->user_block_num);
  385. variable_item_set_current_value_index(app->block_slc_item, my_model->edit_block_slc - 1);
  386. t5577_writer_modulation_change(app->mod_item);
  387. t5577_writer_rf_clock_change(app->clock_item);
  388. t5577_writer_user_block_num_change(app->block_num_item);
  389. t5577_writer_edit_block_slc_change(app->block_slc_item);
  390. view_set_previous_callback(view_config_i, t5577_writer_navigation_submenu_callback);
  391. view_dispatcher_remove_view(
  392. app->view_dispatcher, T5577WriterViewConfigure_i); // delete the last one
  393. view_dispatcher_add_view(app->view_dispatcher, T5577WriterViewConfigure_i, view_config_i);
  394. view_dispatcher_switch_to_view(
  395. app->view_dispatcher, T5577WriterViewConfigure_i); // recreate it
  396. }
  397. void t5577_writer_view_load_callback(void* context) {
  398. T5577WriterApp* app = (T5577WriterApp*)context;
  399. T5577WriterModel* model = view_get_model(app->view_write);
  400. DialogsFileBrowserOptions browser_options;
  401. Storage* storage = furi_record_open(RECORD_STORAGE);
  402. storage_simply_mkdir(storage, STORAGE_APP_DATA_PATH_PREFIX);
  403. dialog_file_browser_set_basic_options(&browser_options, T5577_WRITER_FILE_EXTENSION, &I_icon);
  404. browser_options.base_path = STORAGE_APP_DATA_PATH_PREFIX;
  405. furi_string_set(app->file_path, browser_options.base_path);
  406. FuriString* buffer = furi_string_alloc();
  407. if(dialog_file_browser_show(app->dialogs, app->file_path, app->file_path, &browser_options)) {
  408. FlipperFormat* format = flipper_format_file_alloc(storage);
  409. do {
  410. if(!flipper_format_file_open_existing(format, furi_string_get_cstr(app->file_path)))
  411. break;
  412. uint8_t byte_array_buffer[app->bytes_count];
  413. for(int i = 0; i < LFRFID_T5577_BLOCK_COUNT; i++) {
  414. furi_string_printf(buffer, "Block %u", i);
  415. if(!flipper_format_read_hex(
  416. format, furi_string_get_cstr(buffer), byte_array_buffer, app->bytes_count))
  417. break;
  418. model->content[i] = byte_buffer_to_uint32(
  419. byte_array_buffer); // we only extract the raw data. configs are then updated from block 0
  420. }
  421. // signal that the file was read successfully
  422. } while(0);
  423. flipper_format_free(format);
  424. t5577_writer_update_config_from_load(app);
  425. furi_record_close(RECORD_STORAGE);
  426. }
  427. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewSubmenu);
  428. }
  429. /**
  430. * @brief Callback when item in configuration screen is clicked.
  431. * @details This function is called when user clicks OK on an item in the text input screen.
  432. * @param context The context - T5577WriterApp object.
  433. * @param index - The index of the item that was clicked.
  434. */
  435. static void t5577_writer_view_save_callback(void* context) {
  436. T5577WriterApp* app = (T5577WriterApp*)context;
  437. // Header to display on the text input screen.
  438. text_input_set_header_text(app->text_input, tag_name_entry_text);
  439. // Copy the current name into the temporary buffer.
  440. bool redraw = false;
  441. with_view_model(
  442. app->view_write,
  443. T5577WriterModel * model,
  444. {
  445. strncpy(
  446. app->temp_buffer,
  447. furi_string_get_cstr(model->tag_name_str),
  448. app->temp_buffer_size);
  449. },
  450. redraw);
  451. // Configure the text input. When user enters text and clicks OK, t5577_writer_setting_text_updated be called.
  452. bool clear_previous_text = false;
  453. text_input_set_result_callback(
  454. app->text_input,
  455. t5577_writer_file_saver,
  456. app,
  457. app->temp_buffer,
  458. app->temp_buffer_size,
  459. clear_previous_text);
  460. // Pressing the BACK button will return to the main menu.
  461. view_set_previous_callback(
  462. text_input_get_view(app->text_input), t5577_writer_navigation_submenu_callback);
  463. // Show text input dialog.
  464. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewTextInput);
  465. }
  466. static void t5577_writer_actual_writing(void* model) {
  467. T5577WriterModel* my_model = (T5577WriterModel*)model;
  468. my_model->content[0] = 0; // clear up block 0
  469. my_model->content[0] |= my_model->modulation.mod_page_zero;
  470. my_model->content[0] |= my_model->rf_clock.clock_page_zero;
  471. my_model->content[0] |= (my_model->user_block_num << LFRFID_T5577_MAXBLOCK_SHIFT);
  472. LFRFIDT5577* data = (LFRFIDT5577*)malloc(sizeof(LFRFIDT5577));
  473. data->blocks_to_write = my_model->user_block_num + 1;
  474. for(size_t i = 0; i < data->blocks_to_write; i++) {
  475. data->block[i] = my_model->content[i];
  476. }
  477. t5577_write(data);
  478. free(data);
  479. }
  480. /**
  481. * @brief Callback for drawing the writing screen.
  482. * @details This function is called when the screen needs to be redrawn, so that the writing command is repeated.
  483. * @param canvas The canvas to draw on.
  484. * @param model The model - MyModel object.
  485. */
  486. static void t5577_writer_view_write_callback(Canvas* canvas, void* model) {
  487. T5577WriterModel* my_model = (T5577WriterModel*)model;
  488. if(my_model->writing_repeat_times < MAX_REPEAT_WRITING_FRAMES) {
  489. t5577_writer_actual_writing(model);
  490. canvas_set_bitmap_mode(canvas, true);
  491. canvas_draw_icon(canvas, 0, 8, &I_NFC_manual_60x50);
  492. canvas_draw_str_aligned(canvas, 97, 15, AlignCenter, AlignTop, "Writing");
  493. canvas_draw_str_aligned(canvas, 94, 27, AlignCenter, AlignTop, "Hold card next");
  494. canvas_draw_str_aligned(canvas, 93, 39, AlignCenter, AlignTop, "to Flipper's back");
  495. } else {
  496. canvas_set_bitmap_mode(canvas, true);
  497. canvas_draw_icon(canvas, 0, 9, &I_DolphinSuccess_91x55);
  498. canvas_set_font(canvas, FontPrimary);
  499. canvas_draw_str(canvas, 75, 16, "Finished!");
  500. }
  501. }
  502. /**
  503. * @brief Callback for timer elapsed.
  504. * @details This function is called when the timer is elapsed. We use this to queue a redraw event.
  505. * @param context The context - T5577WriterApp object.
  506. */
  507. static void t5577_writer_view_write_timer_callback(void* context) {
  508. T5577WriterApp* app = (T5577WriterApp*)context;
  509. T5577WriterModel* model = view_get_model(app->view_write);
  510. if(model->writing_repeat_times < MAX_REPEAT_WRITING_FRAMES + ENDING_WRITING_ICON_FRAMES) {
  511. model->writing_repeat_times += 1;
  512. view_dispatcher_send_custom_event(app->view_dispatcher, T5577WriterEventIdRepeatWriting);
  513. if(model->writing_repeat_times == MAX_REPEAT_WRITING_FRAMES) {
  514. notification_message(app->notifications, &sequence_blink_stop);
  515. notification_message(app->notifications, &sequence_success);
  516. }
  517. } else {
  518. view_dispatcher_send_custom_event(app->view_dispatcher, T5577WriterEventIdMaxWriteRep);
  519. }
  520. }
  521. /**
  522. * @brief Callback when the user starts the writing screen.
  523. * @details This function is called when the user enters the writing screen. We start a timer to
  524. * redraw the screen periodically. (exactly like PsychToolBox lol)
  525. * @param context The context - T5577WriterApp object.
  526. */
  527. static void t5577_writer_view_write_enter_callback(void* context) {
  528. uint32_t repeat_writing_period = furi_ms_to_ticks(200);
  529. T5577WriterApp* app = (T5577WriterApp*)context;
  530. furi_assert(app->timer == NULL);
  531. app->timer =
  532. furi_timer_alloc(t5577_writer_view_write_timer_callback, FuriTimerTypePeriodic, context);
  533. furi_timer_start(app->timer, repeat_writing_period);
  534. dolphin_deed(DolphinDeedRfidEmulate);
  535. notification_message(app->notifications, &sequence_blink_start_magenta);
  536. }
  537. /**
  538. * @brief Callback when the user exits the writing screen.
  539. * @details This function is called when the user exits the writing screen. We stop the timer.
  540. * @param context The context - T5577WriterApp object.
  541. */
  542. static void t5577_writer_view_write_exit_callback(void* context) {
  543. T5577WriterApp* app = (T5577WriterApp*)context;
  544. T5577WriterModel* model = view_get_model(app->view_write);
  545. furi_timer_stop(app->timer);
  546. furi_timer_free(app->timer);
  547. app->timer = NULL;
  548. model->writing_repeat_times = 0;
  549. notification_message(app->notifications, &sequence_blink_stop);
  550. }
  551. /**
  552. * @brief Callback for custom events.
  553. * @details This function is called when a custom event is sent to the view dispatcher.
  554. * @param event The event id - T5577WriterEventId value.
  555. * @param context The context - T5577WriterApp object.
  556. */
  557. static bool t5577_writer_view_write_custom_event_callback(uint32_t event, void* context) {
  558. T5577WriterApp* app = (T5577WriterApp*)context;
  559. switch(event) {
  560. case T5577WriterEventIdRepeatWriting:
  561. // Redraw screen by passing true to last parameter of with_view_model.
  562. {
  563. bool redraw = true;
  564. with_view_model(
  565. app->view_write, T5577WriterModel * _model, { UNUSED(_model); }, redraw);
  566. return true;
  567. }
  568. case T5577WriterEventIdMaxWriteRep:
  569. // Process the OK button. We go to the saving scene.
  570. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewSubmenu);
  571. return true;
  572. default:
  573. return false;
  574. }
  575. }
  576. /**
  577. * @brief Allocate the t5577_writer application.
  578. * @details This function allocates the t5577_writer application resources.
  579. * @return T5577WriterApp object.
  580. */
  581. static T5577WriterApp* t5577_writer_app_alloc() {
  582. T5577WriterApp* app = (T5577WriterApp*)malloc(sizeof(T5577WriterApp));
  583. Gui* gui = furi_record_open(RECORD_GUI);
  584. app->view_dispatcher = view_dispatcher_alloc();
  585. app->dialogs = furi_record_open(RECORD_DIALOGS);
  586. app->file_path = furi_string_alloc();
  587. view_dispatcher_attach_to_gui(app->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  588. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  589. app->submenu = submenu_alloc();
  590. submenu_add_item(
  591. app->submenu, "Write", T5577WriterSubmenuIndexWrite, t5577_writer_submenu_callback, app);
  592. submenu_add_item(
  593. app->submenu,
  594. "Config",
  595. T5577WriterSubmenuIndexConfigure,
  596. t5577_writer_submenu_callback,
  597. app);
  598. submenu_add_item(
  599. app->submenu, "Save", T5577WriterSubmenuIndexSave, t5577_writer_submenu_callback, app);
  600. submenu_add_item(
  601. app->submenu, "Load", T5577WriterSubmenuIndexLoad, t5577_writer_submenu_callback, app);
  602. submenu_add_item(
  603. app->submenu, "About", T5577WriterSubmenuIndexAbout, t5577_writer_submenu_callback, app);
  604. view_set_previous_callback(
  605. submenu_get_view(app->submenu), t5577_writer_navigation_exit_callback);
  606. view_dispatcher_add_view(
  607. app->view_dispatcher, T5577WriterViewSubmenu, submenu_get_view(app->submenu));
  608. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewSubmenu);
  609. app->text_input = text_input_alloc();
  610. view_dispatcher_add_view(
  611. app->view_dispatcher, T5577WriterViewTextInput, text_input_get_view(app->text_input));
  612. app->view_load = view_alloc();
  613. view_set_previous_callback(app->view_load, t5577_writer_navigation_submenu_callback);
  614. view_set_enter_callback(app->view_load, t5577_writer_view_load_callback);
  615. view_set_context(app->view_load, app);
  616. view_dispatcher_add_view(app->view_dispatcher, T5577WriterViewLoad, app->view_load);
  617. app->temp_buffer_size = 32;
  618. app->temp_buffer = (char*)malloc(app->temp_buffer_size);
  619. app->view_write = view_alloc();
  620. view_set_draw_callback(app->view_write, t5577_writer_view_write_callback);
  621. view_set_previous_callback(app->view_write, t5577_writer_navigation_submenu_callback);
  622. view_set_enter_callback(app->view_write, t5577_writer_view_write_enter_callback);
  623. view_set_exit_callback(app->view_write, t5577_writer_view_write_exit_callback);
  624. view_set_context(app->view_write, app);
  625. view_set_custom_callback(app->view_write, t5577_writer_view_write_custom_event_callback);
  626. view_allocate_model(app->view_write, ViewModelTypeLockFree, sizeof(T5577WriterModel));
  627. view_dispatcher_add_view(app->view_dispatcher, T5577WriterViewWrite, app->view_write);
  628. T5577WriterModel* model = view_get_model(app->view_write); // initialize model
  629. FuriString* tag_name_str = furi_string_alloc();
  630. furi_string_set_str(tag_name_str, tag_name_default_value);
  631. model->tag_name_str = tag_name_str;
  632. initialize_model(model);
  633. initialize_rf_clock_choices(rf_clock_choices);
  634. initialize_mod_names(modulation_names);
  635. app->view_save = view_alloc();
  636. view_set_previous_callback(app->view_save, t5577_writer_navigation_submenu_callback);
  637. view_set_enter_callback(app->view_save, t5577_writer_view_save_callback);
  638. view_set_context(app->view_save, app);
  639. view_dispatcher_add_view(app->view_dispatcher, T5577WriterViewSave, app->view_save);
  640. app->bytes_count = 4;
  641. memset(app->bytes_buffer, 0, sizeof(app->bytes_buffer));
  642. app->byte_input = byte_input_alloc();
  643. view_dispatcher_add_view(
  644. app->view_dispatcher, T5577WriterViewByteInput, byte_input_get_view(app->byte_input));
  645. app->variable_item_list_config = variable_item_list_alloc();
  646. app->view_config_e = view_alloc();
  647. view_set_previous_callback(app->view_config_e, t5577_writer_navigation_submenu_callback);
  648. view_set_enter_callback(app->view_config_e, t5577_writer_config_enter_callback);
  649. view_set_context(app->view_config_e, app);
  650. view_dispatcher_add_view(app->view_dispatcher, T5577WriterViewConfigure_e, app->view_config_e);
  651. View* view_buffer = view_alloc();
  652. view_dispatcher_add_view(app->view_dispatcher, T5577WriterViewConfigure_i, view_buffer);
  653. app->widget_about = widget_alloc();
  654. widget_add_text_scroll_element(
  655. app->widget_about,
  656. 0,
  657. 0,
  658. 128,
  659. 64,
  660. "T5577 Raw Writer v1.2\n\nAuthor: @Torron\n\nGithub: https://github.com/zinongli/T5577_Raw_Writer");
  661. view_set_previous_callback(
  662. widget_get_view(app->widget_about), t5577_writer_navigation_submenu_callback);
  663. view_dispatcher_add_view(
  664. app->view_dispatcher, T5577WriterViewAbout, widget_get_view(app->widget_about));
  665. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  666. return app;
  667. }
  668. /**
  669. * @brief Free the t5577_writer application.
  670. * @details This function frees the t5577_writer application resources.
  671. * @param app The t5577_writer application object.
  672. */
  673. static void t5577_writer_app_free(T5577WriterApp* app) {
  674. furi_record_close(RECORD_NOTIFICATION);
  675. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewTextInput);
  676. text_input_free(app->text_input);
  677. free(app->temp_buffer);
  678. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewAbout);
  679. widget_free(app->widget_about);
  680. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewWrite);
  681. view_free(app->view_write);
  682. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewLoad);
  683. view_free(app->view_load);
  684. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewConfigure_i);
  685. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewConfigure_e);
  686. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewByteInput);
  687. variable_item_list_free(app->variable_item_list_config);
  688. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewSave);
  689. view_free(app->view_save);
  690. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewSubmenu);
  691. submenu_free(app->submenu);
  692. view_dispatcher_free(app->view_dispatcher);
  693. furi_record_close(RECORD_GUI);
  694. free(app);
  695. }
  696. /**
  697. * @brief Main function for t5577_writer application.
  698. * @details This function is the entry point for the t5577_writer application. It should be defined in
  699. * application.fam as the entry_point setting.
  700. * @param _p Input parameter - unused
  701. * @return 0 - Success
  702. */
  703. int32_t main_t5577_writer_app(void* _p) {
  704. UNUSED(_p);
  705. T5577WriterApp* app = t5577_writer_app_alloc();
  706. view_dispatcher_run(app->view_dispatcher);
  707. t5577_writer_app_free(app);
  708. return 0;
  709. }