t5577_writer.c 33 KB

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