t5577_writer.c 33 KB

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