t5577_writer.c 32 KB

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