t5577_writer.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848
  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 Callback for returning to configure screen.
  137. * @details This function is called when user press back button. We return VIEW_NONE to
  138. * indicate that we want to navigate to the configure screen.
  139. * @param _context The context - unused
  140. * @return next view id
  141. */
  142. //static uint32_t t5577_writer_navigation_configure_callback(void* _context) {
  143. // UNUSED(_context);
  144. // return T5577WriterViewConfigure_i;
  145. //}
  146. static uint32_t t5577_writer_navigation_file_callback(void* _context) {
  147. UNUSED(_context);
  148. return T5577WriterViewSubmenu;
  149. }
  150. /**
  151. * @brief Handle submenu item selection.
  152. * @details This function is called when user selects an item from the submenu.
  153. * @param context The context - T5577WriterApp object.
  154. * @param index The T5577WriterSubmenuIndex item that was clicked.
  155. */
  156. static void t5577_writer_submenu_callback(void* context, uint32_t index) {
  157. T5577WriterApp* app = (T5577WriterApp*)context;
  158. switch(index) {
  159. case T5577WriterSubmenuIndexLoad:
  160. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewLoad);
  161. break;
  162. case T5577WriterSubmenuIndexSave:
  163. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewSave);
  164. break;
  165. case T5577WriterSubmenuIndexConfigure:
  166. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewConfigure_e);
  167. break;
  168. case T5577WriterSubmenuIndexWrite:
  169. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewWrite);
  170. break;
  171. case T5577WriterSubmenuIndexAbout:
  172. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewAbout);
  173. break;
  174. default:
  175. break;
  176. }
  177. }
  178. /**
  179. * Our 1st sample setting is a team color. We have 3 options: red, green, and blue.
  180. */
  181. static const char* modulation_config_label = "Modulation";
  182. //static char* modulation_names[] = {"Direct", "PSK1", "PSK2", "PSK3", "FSK1", "FSK2", "FSK1a", "FSK2a","ASK/Man","Biphase","Diphase"};
  183. static void t5577_writer_modulation_change(VariableItem* item) {
  184. T5577WriterApp* app = variable_item_get_context(item);
  185. FURI_LOG_D(TAG,"app defined");
  186. T5577WriterModel* model = view_get_model(app->view_write);
  187. FURI_LOG_D(TAG,"model defined");
  188. if (model->data_loaded[0]) {
  189. FURI_LOG_D(TAG,"loaded entered");
  190. variable_item_set_current_value_index(item,model->modulation_index);
  191. } else{
  192. FURI_LOG_D(TAG,"else entered");
  193. uint8_t modulation_index = variable_item_get_current_value_index(item);
  194. model->modulation_index = modulation_index;
  195. model->modulation = all_mods[modulation_index];
  196. }
  197. model->data_loaded[0] = false;
  198. variable_item_set_current_value_text(item, modulation_names[model->modulation_index]);
  199. }
  200. static const char* rf_clock_config_label = "RF Clock";
  201. static void t5577_writer_rf_clock_change(VariableItem* item) {
  202. T5577WriterApp* app = variable_item_get_context(item);
  203. T5577WriterModel* model = view_get_model(app->view_write);
  204. if (model->data_loaded[1]) {
  205. variable_item_set_current_value_index(item,model->rf_clock_index);
  206. } else{
  207. uint8_t rf_clock_index = variable_item_get_current_value_index(item);
  208. model->rf_clock_index = rf_clock_index;
  209. model->rf_clock = all_rf_clocks[rf_clock_index];
  210. }
  211. model->data_loaded[1] = false;
  212. FuriString *buffer = furi_string_alloc();
  213. furi_string_printf(buffer, "%u", rf_clock_choices[model->rf_clock_index]);
  214. variable_item_set_current_value_text(item, furi_string_get_cstr(buffer));
  215. furi_string_free(buffer);
  216. }
  217. static const char* user_block_num_config_label = "Num of Blocks";
  218. static void t5577_writer_user_block_num_change(VariableItem* item) {
  219. T5577WriterApp* app = variable_item_get_context(item);
  220. T5577WriterModel* model = view_get_model(app->view_write);
  221. if (model->data_loaded[2]) {
  222. variable_item_set_current_value_index(item,model->user_block_num - 1);
  223. } else {
  224. uint8_t user_block_num_index = variable_item_get_current_value_index(item);
  225. model->user_block_num = user_block_num_index + 1;
  226. }
  227. model->data_loaded[2] = false;
  228. FuriString *buffer = furi_string_alloc();
  229. furi_string_printf(buffer, "%u", model->user_block_num);
  230. variable_item_set_current_value_text(item, furi_string_get_cstr(buffer));
  231. for(uint8_t i = model->user_block_num; i < LFRFID_T5577_BLOCK_COUNT; i++) {
  232. model->content[i] = 0;
  233. }
  234. furi_string_free(buffer);
  235. }
  236. static const char* edit_block_slc_config_label = "Edit Block";
  237. static void t5577_writer_edit_block_slc_change(VariableItem* item) {
  238. T5577WriterApp* app = variable_item_get_context(item);
  239. T5577WriterModel* model = view_get_model(app->view_write);
  240. uint8_t edit_block_slc_index = variable_item_get_current_value_index(item);
  241. model->edit_block_slc = edit_block_slc_index + 1;
  242. variable_item_set_current_value_index(item,model->edit_block_slc - 1);
  243. FuriString *buffer = furi_string_alloc();
  244. furi_string_printf(buffer, "%u", model->edit_block_slc);
  245. variable_item_set_current_value_text(item, furi_string_get_cstr(buffer));
  246. furi_string_free(buffer);
  247. }
  248. void ensure_dir_exists(Storage *storage)
  249. {
  250. // If apps_data directory doesn't exist, create it.
  251. if (!storage_dir_exists(storage, T5577_WRITER_APPS_DATA_FOLDER))
  252. {
  253. FURI_LOG_I(TAG, "Creating directory: %s", T5577_WRITER_APPS_DATA_FOLDER);
  254. storage_simply_mkdir(storage, T5577_WRITER_APPS_DATA_FOLDER);
  255. }
  256. else
  257. {
  258. FURI_LOG_I(TAG, "Directory exists: %s", T5577_WRITER_APPS_DATA_FOLDER);
  259. }
  260. // If wiegand directory doesn't exist, create it.
  261. if (!storage_dir_exists(storage, T5577_WRITER_FILE_FOLDER))
  262. {
  263. FURI_LOG_I(TAG, "Creating directory: %s", T5577_WRITER_FILE_FOLDER);
  264. storage_simply_mkdir(storage, T5577_WRITER_FILE_FOLDER);
  265. }
  266. else
  267. {
  268. FURI_LOG_I(TAG, "Directory exists: %s", T5577_WRITER_FILE_FOLDER);
  269. }
  270. }
  271. /**
  272. * Our 2nd sample setting is a text field. When the user clicks OK on the configuration
  273. * setting we use a text input screen to allow the user to enter a name. This function is
  274. * called when the user clicks OK on the text input screen.
  275. */
  276. static const char* tag_name_entry_text = "Enter name";
  277. static const char* tag_name_default_value = "Tag_1";
  278. static void t5577_writer_file_saver(void* context) {
  279. T5577WriterApp* app = (T5577WriterApp*)context;
  280. T5577WriterModel* model = view_get_model(app->view_write);
  281. model->content[0] = 0;
  282. model->content[0] |= model->modulation.mod_page_zero;
  283. model->content[0] |= model->rf_clock.clock_page_zero;
  284. model->content[0] |= (model->user_block_num << LFRFID_T5577_MAXBLOCK_SHIFT);
  285. bool redraw = true;
  286. with_view_model(
  287. app->view_write,
  288. T5577WriterModel * model,
  289. {
  290. furi_string_set(model->tag_name_str, app->temp_buffer);
  291. },
  292. redraw);
  293. FuriString *buffer = furi_string_alloc();
  294. FuriString *file_path = furi_string_alloc();
  295. furi_string_printf(
  296. file_path, "%s/%s%s", T5577_WRITER_FILE_FOLDER, furi_string_get_cstr(model->tag_name_str), T5577_WRITER_FILE_EXTENSION);
  297. Storage *storage = furi_record_open(RECORD_STORAGE);
  298. ensure_dir_exists(storage);
  299. File *data_file = storage_file_alloc(storage);
  300. if (storage_file_open(
  301. data_file, furi_string_get_cstr(file_path), FSAM_WRITE, FSOM_OPEN_ALWAYS))
  302. {
  303. furi_string_printf(buffer, "Filetype: Flipper T5577 Raw File\n");
  304. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  305. furi_string_printf(buffer, "Version: 1.0\n");
  306. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  307. furi_string_printf(buffer, "Modulation: %s\n", model->modulation.modulation_name);
  308. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  309. furi_string_printf(buffer, "RF Clock: %u\n", model->rf_clock.rf_clock_num);
  310. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  311. furi_string_printf(buffer, "Number of User Blocks: %u\n", model->user_block_num);
  312. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  313. furi_string_printf(buffer, "\nRaw Data:\n");
  314. for (int i = 0; i < LFRFID_T5577_BLOCK_COUNT; i++)
  315. {
  316. furi_string_cat_printf(
  317. buffer,
  318. "Block %u: %08lX\n",
  319. i,
  320. model->content[i]);
  321. }
  322. furi_string_push_back(buffer, '\n');
  323. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  324. storage_file_close(data_file);
  325. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewSubmenu);
  326. }
  327. }
  328. void t5577_writer_update_config_from_load(void* context) {
  329. T5577WriterApp* app = (T5577WriterApp*)context;
  330. T5577WriterModel* my_model = view_get_model(app->view_write);
  331. for (size_t i = 0; i < COUNT_OF(all_mods); i++) {
  332. if ((my_model->content[0] & all_mods[i].mod_page_zero) == all_mods[i].mod_page_zero) {
  333. my_model->modulation_index = (uint8_t)i;
  334. my_model->modulation = all_mods[my_model->modulation_index];
  335. }
  336. }
  337. for (size_t i = 0; i < COUNT_OF(all_rf_clocks); i++) {
  338. if ((my_model->content[0] & all_rf_clocks[i].clock_page_zero) == all_rf_clocks[i].clock_page_zero) {
  339. my_model->rf_clock_index = (uint8_t)i;
  340. my_model->rf_clock = all_rf_clocks[my_model->rf_clock_index];
  341. }
  342. }
  343. my_model->user_block_num = (my_model->content[0] >> LFRFID_T5577_MAXBLOCK_SHIFT) & 0xE;
  344. memset(my_model->data_loaded, true, sizeof(my_model->data_loaded));
  345. }
  346. static void t5577_writer_config_enter_callback(void* context) {
  347. T5577WriterApp* app = (T5577WriterApp*)context;
  348. T5577WriterModel* my_model = view_get_model(app->view_write);
  349. variable_item_list_reset(app->variable_item_list_config);
  350. app->mod_item = variable_item_list_add(
  351. app->variable_item_list_config,
  352. modulation_config_label,
  353. COUNT_OF(modulation_names),
  354. t5577_writer_modulation_change,
  355. app);
  356. app->clock_item = variable_item_list_add(
  357. app->variable_item_list_config,
  358. rf_clock_config_label,
  359. COUNT_OF(rf_clock_choices),
  360. t5577_writer_rf_clock_change,
  361. app);
  362. app->block_num_item = variable_item_list_add(
  363. app->variable_item_list_config,
  364. user_block_num_config_label,
  365. LFRFID_T5577_BLOCK_COUNT,
  366. t5577_writer_user_block_num_change,
  367. app);
  368. app->block_slc_item = variable_item_list_add(
  369. app->variable_item_list_config,
  370. edit_block_slc_config_label,
  371. LFRFID_T5577_BLOCK_COUNT - 1,
  372. t5577_writer_edit_block_slc_change,
  373. app);
  374. View* view_config_i = variable_item_list_get_view(app->variable_item_list_config);
  375. variable_item_set_current_value_index(app->mod_item,my_model->modulation_index);
  376. variable_item_set_current_value_index(app->clock_item,my_model->rf_clock_index);
  377. variable_item_set_current_value_index(app->block_num_item,my_model->user_block_num - 1);
  378. variable_item_set_current_value_index(app->block_slc_item,my_model->edit_block_slc - 1);
  379. t5577_writer_modulation_change(app->mod_item);
  380. t5577_writer_rf_clock_change(app->clock_item);
  381. t5577_writer_user_block_num_change(app->block_num_item);
  382. t5577_writer_edit_block_slc_change(app->block_slc_item);
  383. view_set_previous_callback(
  384. view_config_i,
  385. t5577_writer_navigation_submenu_callback);
  386. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewConfigure_i);
  387. view_dispatcher_add_view(
  388. app->view_dispatcher,
  389. T5577WriterViewConfigure_i,
  390. view_config_i);
  391. view_dispatcher_switch_to_view(app->view_dispatcher,T5577WriterViewConfigure_i);
  392. FURI_LOG_D(TAG,"enter_callback_finished");
  393. }
  394. void t5577_writer_view_load_callback(void* context) {
  395. T5577WriterApp* app = (T5577WriterApp*)context;
  396. T5577WriterModel* model = view_get_model(app->view_write);
  397. DialogsFileBrowserOptions browser_options;
  398. Storage* storage = furi_record_open(RECORD_STORAGE);
  399. ensure_dir_exists(storage);
  400. File* data_file = storage_file_alloc(storage);
  401. dialog_file_browser_set_basic_options(&browser_options, T5577_WRITER_FILE_EXTENSION, NULL);
  402. browser_options.base_path = T5577_WRITER_FILE_FOLDER;
  403. furi_string_set(app->file_path, browser_options.base_path);
  404. FuriString* buffer = furi_string_alloc();
  405. if(dialog_file_browser_show(app->dialogs, app->file_path, app->file_path, &browser_options)) {
  406. if(storage_file_open(
  407. data_file, furi_string_get_cstr(app->file_path), FSAM_READ, FSOM_OPEN_EXISTING)) {
  408. while(!storage_file_eof(data_file)) { // fill buffer with every line because ch == '\n'
  409. char ch;
  410. furi_string_reset(buffer);
  411. while(storage_file_read(data_file, &ch, 1) && !storage_file_eof(data_file)) {
  412. furi_string_push_back(buffer, ch);
  413. if(ch == '\n') {
  414. break;
  415. }
  416. }
  417. if(furi_string_start_with(buffer, "Block ")) {
  418. uint32_t row_data_buffer = 0;
  419. char row_data_char_buffer[] = "00000000";
  420. uint32_t row_num_buffer = 0;
  421. char row_num_char_buffer[] = "0";
  422. int length = furi_string_size(buffer);
  423. char ch;
  424. int i = 0;
  425. while(i < length) {
  426. if(furi_string_get_char(buffer, i) == ':') {
  427. row_num_char_buffer[0] = furi_string_get_char(buffer, i - 1); //the number before ":" is block num
  428. i += 2; // skip a space
  429. for(size_t j = 0; j < sizeof(row_data_char_buffer); j++) {
  430. ch = furi_string_get_char(buffer, i);
  431. row_data_char_buffer[j] = ch;
  432. i++;
  433. }
  434. break;
  435. }
  436. i++;
  437. }
  438. sscanf(row_num_char_buffer, "%lx", &row_num_buffer);
  439. sscanf(row_data_char_buffer, "%lx", &row_data_buffer);
  440. model->content[row_num_buffer] = row_data_buffer;
  441. }
  442. }
  443. storage_file_close(data_file);
  444. t5577_writer_update_config_from_load(app);
  445. }
  446. storage_file_free(data_file);
  447. furi_record_close(RECORD_STORAGE);
  448. }
  449. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewSubmenu);
  450. }
  451. /**
  452. * @brief Callback when item in configuration screen is clicked.
  453. * @details This function is called when user clicks OK on an item in the configuration screen.
  454. * If the item clicked is our text field then we switch to the text input screen.
  455. * @param context The context - T5577WriterApp object.
  456. * @param index - The index of the item that was clicked.
  457. */
  458. static void t5577_writer_view_save_callback(void* context) {
  459. T5577WriterApp* app = (T5577WriterApp*)context;
  460. // Header to display on the text input screen.
  461. text_input_set_header_text(app->text_input, tag_name_entry_text);
  462. // Copy the current name into the temporary buffer.
  463. bool redraw = false;
  464. with_view_model(
  465. app->view_write,
  466. T5577WriterModel * model,
  467. {
  468. strncpy(
  469. app->temp_buffer,
  470. furi_string_get_cstr(model->tag_name_str),
  471. app->temp_buffer_size);
  472. },
  473. redraw);
  474. // Configure the text input. When user enters text and clicks OK, t5577_writer_setting_text_updated be called.
  475. bool clear_previous_text = false;
  476. text_input_set_result_callback(
  477. app->text_input,
  478. t5577_writer_file_saver,
  479. app,
  480. app->temp_buffer,
  481. app->temp_buffer_size,
  482. clear_previous_text);
  483. // Pressing the BACK button will reload the configure screen.
  484. view_set_previous_callback(
  485. text_input_get_view(app->text_input), t5577_writer_navigation_file_callback);
  486. // Show text input dialog.
  487. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewTextInput);
  488. }
  489. static void t5577_writer_actual_writing(void* model) {
  490. T5577WriterModel* my_model = (T5577WriterModel*)model;
  491. my_model->content[0] = 0;
  492. my_model->content[0] |= my_model->modulation.mod_page_zero;
  493. my_model->content[0] |= my_model->rf_clock.clock_page_zero;
  494. my_model->content[0] |= (my_model->user_block_num << LFRFID_T5577_MAXBLOCK_SHIFT);
  495. LFRFIDT5577* data = (LFRFIDT5577*)malloc(sizeof(LFRFIDT5577));
  496. data->blocks_to_write = my_model->user_block_num;
  497. for(size_t i = 0; i < data->blocks_to_write; i++) {
  498. data->block[i] = my_model->content[i];
  499. }
  500. t5577_write(data);
  501. free(data);
  502. }
  503. /**
  504. * @brief Callback for drawing the game screen.
  505. * @details This function is called when the screen needs to be redrawn, like when the model gets updated.
  506. * @param canvas The canvas to draw on.
  507. * @param model The model - MyModel object.
  508. */
  509. static void t5577_writer_view_write_callback(Canvas* canvas, void* model) {
  510. T5577WriterModel* my_model = (T5577WriterModel*) model;
  511. if (my_model->writing_repeat_times < MAX_REPEAT_WRITING_FRAMES) {
  512. t5577_writer_actual_writing(model);
  513. canvas_set_bitmap_mode(canvas, true);
  514. canvas_draw_icon(canvas, 0, 8, &I_NFC_manual_60x50);
  515. canvas_draw_str_aligned(canvas, 97, 15, AlignCenter, AlignTop, "Writing");
  516. canvas_draw_str_aligned(canvas, 94, 27, AlignCenter, AlignTop, "Hold card next");
  517. canvas_draw_str_aligned(canvas, 93, 39, AlignCenter, AlignTop, "to Flipper's back");
  518. } else {
  519. canvas_set_bitmap_mode(canvas, true);
  520. canvas_draw_icon(canvas, 0, 7, &I_RFID_dolphinsuccess_108x57);
  521. canvas_set_font(canvas, FontPrimary);
  522. canvas_draw_str(canvas, 72, 20, "Finished!");
  523. }
  524. }
  525. /**
  526. * @brief Callback for timer elapsed.
  527. * @details This function is called when the timer is elapsed. We use this to queue a redraw event.
  528. * @param context The context - T5577WriterApp object.
  529. */
  530. static void t5577_writer_view_write_timer_callback(void* context) {
  531. T5577WriterApp* app = (T5577WriterApp*)context;
  532. T5577WriterModel* model = view_get_model(app->view_write);
  533. if (model->writing_repeat_times < MAX_REPEAT_WRITING_FRAMES + ENDING_WRITING_ICON_FRAMES){
  534. model->writing_repeat_times += 1;
  535. view_dispatcher_send_custom_event(app->view_dispatcher, T5577WriterEventIdRepeatWriting);
  536. } else {
  537. view_dispatcher_send_custom_event(app->view_dispatcher, T5577WriterEventIdMaxWriteRep);
  538. }
  539. }
  540. /**
  541. * @brief Callback when the user starts the game screen.
  542. * @details This function is called when the user enters the game screen. We start a timer to
  543. * redraw the screen periodically (so the random number is refreshed).
  544. * @param context The context - T5577WriterApp object.
  545. */
  546. static void t5577_writer_view_write_enter_callback(void* context) {
  547. uint32_t repeat_writing_period = furi_ms_to_ticks(200);
  548. T5577WriterApp* app = (T5577WriterApp*)context;
  549. furi_assert(app->timer == NULL);
  550. app->timer =
  551. furi_timer_alloc(t5577_writer_view_write_timer_callback, FuriTimerTypePeriodic, context);
  552. furi_timer_start(app->timer, repeat_writing_period);
  553. dolphin_deed(DolphinDeedRfidEmulate);
  554. }
  555. /**
  556. * @brief Callback when the user exits the game screen.
  557. * @details This function is called when the user exits the game screen. We stop the timer.
  558. * @param context The context - T5577WriterApp object.
  559. */
  560. static void t5577_writer_view_write_exit_callback(void* context) {
  561. T5577WriterApp* app = (T5577WriterApp*)context;
  562. T5577WriterModel* model = view_get_model(app->view_write);
  563. furi_timer_stop(app->timer);
  564. furi_timer_free(app->timer);
  565. app->timer = NULL;
  566. model->writing_repeat_times = 0;
  567. }
  568. /**
  569. * @brief Callback for custom events.
  570. * @details This function is called when a custom event is sent to the view dispatcher.
  571. * @param event The event id - T5577WriterEventId value.
  572. * @param context The context - T5577WriterApp object.
  573. */
  574. static bool t5577_writer_view_write_custom_event_callback(uint32_t event, void* context) {
  575. T5577WriterApp* app = (T5577WriterApp*)context;
  576. switch(event) {
  577. case T5577WriterEventIdRepeatWriting:
  578. // Redraw screen by passing true to last parameter of with_view_model.
  579. {
  580. bool redraw = true;
  581. with_view_model(
  582. app->view_write, T5577WriterModel * _model, {UNUSED(_model);}, redraw);
  583. return true;
  584. }
  585. case T5577WriterEventIdMaxWriteRep:
  586. // Process the OK button. We go to the saving scene.
  587. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewSubmenu);
  588. return true;
  589. default:
  590. return false;
  591. }
  592. }
  593. /**
  594. * @brief Callback for game screen input.
  595. * @details This function is called when the user presses a button while on the game screen.
  596. * @param event The event - InputEvent object.
  597. * @param context The context - T5577WriterApp object.
  598. * @return true if the event was handled, false otherwise.
  599. */
  600. static bool t5577_writer_view_write_input_callback(InputEvent* event, void* context) {
  601. T5577WriterApp* app = (T5577WriterApp*)context;
  602. if(event->type == InputTypeShort) {
  603. if(event->key == InputKeyOk) {
  604. // We choose to send a custom event when user presses OK button. t5577_writer_custom_event_callback will
  605. // handle our T5577WriterEventIdMaxWriteRep event. We could have just put the code from
  606. // t5577_writer_custom_event_callback here, it's a matter of preference.
  607. view_dispatcher_send_custom_event(app->view_dispatcher, T5577WriterEventIdMaxWriteRep);
  608. return true;
  609. }
  610. }
  611. return false;
  612. }
  613. /**
  614. * @brief Allocate the t5577_writer application.
  615. * @details This function allocates the t5577_writer application resources.
  616. * @return T5577WriterApp object.
  617. */
  618. static T5577WriterApp* t5577_writer_app_alloc() {
  619. T5577WriterApp* app = (T5577WriterApp*)malloc(sizeof(T5577WriterApp));
  620. Gui* gui = furi_record_open(RECORD_GUI);
  621. app->view_dispatcher = view_dispatcher_alloc();
  622. app->dialogs = furi_record_open(RECORD_DIALOGS);
  623. app->file_path = furi_string_alloc();
  624. view_dispatcher_enable_queue(app->view_dispatcher);
  625. view_dispatcher_attach_to_gui(app->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  626. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  627. app->submenu = submenu_alloc();
  628. submenu_add_item(
  629. app->submenu, "Write", T5577WriterSubmenuIndexWrite, t5577_writer_submenu_callback, app);
  630. submenu_add_item(
  631. app->submenu, "Config", T5577WriterSubmenuIndexConfigure, t5577_writer_submenu_callback, app);
  632. submenu_add_item(
  633. app->submenu, "Save", T5577WriterSubmenuIndexSave, t5577_writer_submenu_callback, app);
  634. submenu_add_item(
  635. app->submenu, "Load", T5577WriterSubmenuIndexLoad, t5577_writer_submenu_callback, app);
  636. submenu_add_item(
  637. app->submenu, "About", T5577WriterSubmenuIndexAbout, t5577_writer_submenu_callback, app);
  638. view_set_previous_callback(submenu_get_view(app->submenu), t5577_writer_navigation_exit_callback);
  639. view_dispatcher_add_view(
  640. app->view_dispatcher, T5577WriterViewSubmenu, submenu_get_view(app->submenu));
  641. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewSubmenu);
  642. app->text_input = text_input_alloc();
  643. view_dispatcher_add_view(
  644. app->view_dispatcher, T5577WriterViewTextInput, text_input_get_view(app->text_input));
  645. app->view_load = view_alloc();
  646. view_set_previous_callback(app->view_load, t5577_writer_navigation_submenu_callback);
  647. view_set_enter_callback(app->view_load, t5577_writer_view_load_callback);
  648. view_set_context(app->view_load, app);
  649. view_dispatcher_add_view(
  650. app->view_dispatcher,
  651. T5577WriterViewLoad,
  652. app->view_load);
  653. app->temp_buffer_size = 32;
  654. app->temp_buffer = (char*)malloc(app->temp_buffer_size);
  655. app->view_write = view_alloc();
  656. view_set_draw_callback(app->view_write, t5577_writer_view_write_callback);
  657. view_set_input_callback(app->view_write, t5577_writer_view_write_input_callback);
  658. view_set_previous_callback(app->view_write, t5577_writer_navigation_submenu_callback);
  659. view_set_enter_callback(app->view_write, t5577_writer_view_write_enter_callback);
  660. view_set_exit_callback(app->view_write, t5577_writer_view_write_exit_callback);
  661. view_set_context(app->view_write, app);
  662. view_set_custom_callback(app->view_write, t5577_writer_view_write_custom_event_callback);
  663. view_allocate_model(app->view_write, ViewModelTypeLockFree, sizeof(T5577WriterModel));
  664. view_dispatcher_add_view(app->view_dispatcher, T5577WriterViewWrite, app->view_write);
  665. T5577WriterModel* model = view_get_model(app->view_write); // initialize model
  666. FuriString* tag_name_str = furi_string_alloc();
  667. furi_string_set_str(tag_name_str, tag_name_default_value);
  668. model->tag_name_str = tag_name_str;
  669. initialize_model(model);
  670. initialize_rf_clock_choices(rf_clock_choices);
  671. initialize_mod_names(modulation_names);
  672. app->view_save = view_alloc();
  673. view_set_previous_callback(app->view_save, t5577_writer_navigation_submenu_callback);
  674. view_set_enter_callback(app->view_save, t5577_writer_view_save_callback);
  675. view_set_context(app->view_save, app);
  676. view_dispatcher_add_view(
  677. app->view_dispatcher,
  678. T5577WriterViewSave,
  679. app->view_save);
  680. app->variable_item_list_config = variable_item_list_alloc();
  681. app->view_config_e = view_alloc();
  682. view_set_previous_callback(
  683. app->view_config_e,
  684. t5577_writer_navigation_submenu_callback);
  685. view_set_enter_callback(app->view_config_e, t5577_writer_config_enter_callback);
  686. view_set_context(app->view_config_e, app);
  687. view_dispatcher_add_view(
  688. app->view_dispatcher,
  689. T5577WriterViewConfigure_e,
  690. app->view_config_e);
  691. View* view_buffer = view_alloc();
  692. view_dispatcher_add_view(
  693. app->view_dispatcher,
  694. T5577WriterViewConfigure_i,
  695. view_buffer);
  696. app->widget_about = widget_alloc();
  697. widget_add_text_scroll_element(
  698. app->widget_about,
  699. 0,
  700. 0,
  701. 128,
  702. 64,
  703. "T5577 Writer v0.1");
  704. view_set_previous_callback(
  705. widget_get_view(app->widget_about), t5577_writer_navigation_submenu_callback);
  706. view_dispatcher_add_view(
  707. app->view_dispatcher, T5577WriterViewAbout, widget_get_view(app->widget_about));
  708. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  709. #ifdef BACKLIGHT_ON
  710. notification_message(app->notifications, &sequence_display_backlight_enforce_on);
  711. #endif
  712. return app;
  713. }
  714. /**
  715. * @brief Free the t5577_writer application.
  716. * @details This function frees the t5577_writer application resources.
  717. * @param app The t5577_writer application object.
  718. */
  719. static void t5577_writer_app_free(T5577WriterApp* app) {
  720. #ifdef BACKLIGHT_ON
  721. notification_message(app->notifications, &sequence_display_backlight_enforce_auto);
  722. #endif
  723. furi_record_close(RECORD_NOTIFICATION);
  724. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewTextInput);
  725. text_input_free(app->text_input);
  726. free(app->temp_buffer);
  727. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewAbout);
  728. widget_free(app->widget_about);
  729. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewWrite);
  730. with_view_model(
  731. app->view_write,
  732. T5577WriterModel * model,
  733. {
  734. if(model->content != NULL) {
  735. free(model->content);
  736. }
  737. },
  738. false);
  739. view_free(app->view_write);
  740. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewLoad);
  741. view_free(app->view_load);
  742. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewConfigure_i);
  743. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewConfigure_e);
  744. variable_item_list_free(app->variable_item_list_config);
  745. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewSave);
  746. view_free(app->view_save);
  747. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewSubmenu);
  748. submenu_free(app->submenu);
  749. view_dispatcher_free(app->view_dispatcher);
  750. furi_record_close(RECORD_GUI);
  751. free(app);
  752. }
  753. /**
  754. * @brief Main function for t5577_writer application.
  755. * @details This function is the entry point for the t5577_writer application. It should be defined in
  756. * application.fam as the entry_point setting.
  757. * @param _p Input parameter - unused
  758. * @return 0 - Success
  759. */
  760. int32_t main_t5577_writer_app(void* _p) {
  761. UNUSED(_p);
  762. T5577WriterApp* app = t5577_writer_app_alloc();
  763. view_dispatcher_run(app->view_dispatcher);
  764. t5577_writer_app_free(app);
  765. return 0;
  766. }