t5577_writer.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  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. #define TAG "T5577 Writer"
  22. // Change this to BACKLIGHT_AUTO if you don't want the backlight to be continuously on.
  23. #define BACKLIGHT_AUTO 1
  24. // Our application menu has 3 items. You can add more items if you want.
  25. typedef enum {
  26. T5577WriterSubmenuIndexLoad,
  27. T5577WriterSubmenuIndexSave,
  28. T5577WriterSubmenuIndexConfigure,
  29. T5577WriterSubmenuIndexWrite,
  30. T5577WriterSubmenuIndexAbout,
  31. } T5577WriterSubmenuIndex;
  32. // Each view is a screen we show the user.
  33. typedef enum {
  34. T5577WriterViewSubmenu, // The menu when the app starts
  35. T5577WriterViewTextInput, // Input for configuring text settings
  36. T5577WriterViewLoad,
  37. T5577WriterViewSave,
  38. T5577WriterViewConfigure_i, // The configuration screen
  39. T5577WriterViewConfigure_e, // The configuration screen
  40. T5577WriterViewWrite, // The main screen
  41. T5577WriterViewAbout, // The about screen with directions, link to social channel, etc.
  42. } T5577WriterView;
  43. typedef enum {
  44. T5577WriterEventIdRedrawScreen = 0, // Custom event to redraw the screen
  45. T5577WriterEventIdOkPressed = 42, // Custom event to process OK button getting pressed down
  46. } T5577WriterEventId;
  47. typedef struct {
  48. ViewDispatcher* view_dispatcher; // Switches between our views
  49. NotificationApp* notifications; // Used for controlling the backlight
  50. Submenu* submenu; // The application menu
  51. TextInput* text_input; // The text input screen
  52. VariableItemList* variable_item_list_config; // The configuration screen
  53. View* view_config_e; // The configuration screen
  54. View* view_save;
  55. View* view_write; // The main screen
  56. Widget* widget_about; // The about screen
  57. View* view_load; // The load view
  58. VariableItem* mod_item; //
  59. VariableItem* clock_item; //
  60. VariableItem* block_num_item; //
  61. VariableItem* block_slc_item; //
  62. char* temp_buffer; // Temporary buffer for text input
  63. uint32_t temp_buffer_size; // Size of temporary buffer
  64. DialogsApp* dialogs;
  65. FuriString* file_path;
  66. FuriTimer* timer; // Timer for redrawing the screen
  67. } T5577WriterApp;
  68. typedef struct {
  69. uint8_t modulation_index; // The index for total number of pins
  70. uint8_t rf_clock_index; // The index for total number of pins
  71. FuriString* tag_name_str; // The name setting
  72. uint8_t user_block_num; // The total number of pins we are adjusting
  73. uint32_t* content; // The cutting content
  74. t5577_modulation modulation;
  75. t5577_rf_clock rf_clock;
  76. bool data_loaded[3];
  77. uint8_t edit_block_slc;
  78. } T5577WriterModel;
  79. static inline int min(int a, int b) {
  80. return (a < b) ? a : b;
  81. }
  82. static inline int max(int a, int b) {
  83. return (a > b) ? a : b;
  84. }
  85. void initialize_config(T5577WriterModel* model) {
  86. model->modulation_index = 0;
  87. memcpy(&model->modulation, &all_mods[model->modulation_index], sizeof(t5577_modulation));
  88. model->rf_clock_index = 0;
  89. memcpy(&model->rf_clock, &all_mods[model->rf_clock_index], sizeof(t5577_rf_clock));
  90. }
  91. void initialize_model(T5577WriterModel* model) {
  92. if(model->content != NULL) {
  93. free(model->content);
  94. }
  95. initialize_config(model);
  96. model->user_block_num = 1;
  97. model->edit_block_slc = 1;
  98. model->content = (uint32_t*)malloc(LFRFID_T5577_BLOCK_COUNT * sizeof(uint32_t));
  99. for(uint32_t i = 0; i < LFRFID_T5577_BLOCK_COUNT; i++) {
  100. model->content[i] = 0;
  101. }
  102. memset(model->data_loaded, false, sizeof(model->data_loaded));
  103. }
  104. uint8_t rf_clock_choices[COUNT_OF(all_rf_clocks)];
  105. void initialize_rf_clock_choices(uint8_t* rf_clock_choices) {
  106. // Populate the rf_clock_choices array
  107. for (size_t i = 0; i < COUNT_OF(all_rf_clocks); i++) {
  108. rf_clock_choices[i] = all_rf_clocks[i].rf_clock_num;
  109. }
  110. }
  111. char* modulation_names[COUNT_OF(all_mods)];
  112. void initialize_mod_names(char** modulation_names) {
  113. // Populate the modulation_names array
  114. for (size_t i = 0; i < COUNT_OF(all_mods); i++) {
  115. modulation_names[i] = all_mods[i].modulation_name;
  116. }
  117. }
  118. /**
  119. * @brief Callback for exiting the application.
  120. * @details This function is called when user press back button. We return VIEW_NONE to
  121. * indicate that we want to exit the application.
  122. * @param _context The context - unused
  123. * @return next view id
  124. */
  125. static uint32_t t5577_writer_navigation_exit_callback(void* _context) {
  126. UNUSED(_context);
  127. return VIEW_NONE;
  128. }
  129. /**
  130. * @brief Callback for returning to submenu.
  131. * @details This function is called when user press back button. We return VIEW_NONE to
  132. * indicate that we want to navigate to the submenu.
  133. * @param _context The context - unused
  134. * @return next view id
  135. */
  136. static uint32_t t5577_writer_navigation_submenu_callback(void* _context) {
  137. UNUSED(_context);
  138. return T5577WriterViewSubmenu;
  139. }
  140. /**
  141. * @brief Callback for returning to configure screen.
  142. * @details This function is called when user press back button. We return VIEW_NONE to
  143. * indicate that we want to navigate to the configure screen.
  144. * @param _context The context - unused
  145. * @return next view id
  146. */
  147. //static uint32_t t5577_writer_navigation_configure_callback(void* _context) {
  148. // UNUSED(_context);
  149. // return T5577WriterViewConfigure_i;
  150. //}
  151. static uint32_t t5577_writer_navigation_file_callback(void* _context) {
  152. UNUSED(_context);
  153. return T5577WriterViewSubmenu;
  154. }
  155. /**
  156. * @brief Handle submenu item selection.
  157. * @details This function is called when user selects an item from the submenu.
  158. * @param context The context - T5577WriterApp object.
  159. * @param index The T5577WriterSubmenuIndex item that was clicked.
  160. */
  161. static void t5577_writer_submenu_callback(void* context, uint32_t index) {
  162. T5577WriterApp* app = (T5577WriterApp*)context;
  163. switch(index) {
  164. case T5577WriterSubmenuIndexLoad:
  165. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewLoad);
  166. break;
  167. case T5577WriterSubmenuIndexSave:
  168. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewSave);
  169. break;
  170. case T5577WriterSubmenuIndexConfigure:
  171. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewConfigure_e);
  172. break;
  173. case T5577WriterSubmenuIndexWrite:
  174. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewWrite);
  175. break;
  176. case T5577WriterSubmenuIndexAbout:
  177. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewAbout);
  178. break;
  179. default:
  180. break;
  181. }
  182. }
  183. /**
  184. * Our 1st sample setting is a team color. We have 3 options: red, green, and blue.
  185. */
  186. static const char* modulation_config_label = "Modulation";
  187. //static char* modulation_names[] = {"Direct", "PSK1", "PSK2", "PSK3", "FSK1", "FSK2", "FSK1a", "FSK2a","ASK/Man","Biphase","Diphase"};
  188. static void t5577_writer_modulation_change(VariableItem* item) {
  189. T5577WriterApp* app = variable_item_get_context(item);
  190. FURI_LOG_D(TAG,"app defined");
  191. T5577WriterModel* model = view_get_model(app->view_write);
  192. FURI_LOG_D(TAG,"model defined");
  193. if (model->data_loaded[0]) {
  194. FURI_LOG_D(TAG,"loaded entered");
  195. variable_item_set_current_value_index(item,model->modulation_index);
  196. } else{
  197. FURI_LOG_D(TAG,"else entered");
  198. uint8_t modulation_index = variable_item_get_current_value_index(item);
  199. model->modulation_index = modulation_index;
  200. model->modulation = all_mods[modulation_index];
  201. }
  202. model->data_loaded[0] = false;
  203. variable_item_set_current_value_text(item, modulation_names[model->modulation_index]);
  204. }
  205. static const char* rf_clock_config_label = "RF Clock";
  206. static void t5577_writer_rf_clock_change(VariableItem* item) {
  207. T5577WriterApp* app = variable_item_get_context(item);
  208. T5577WriterModel* model = view_get_model(app->view_write);
  209. if (model->data_loaded[1]) {
  210. variable_item_set_current_value_index(item,model->rf_clock_index);
  211. } else{
  212. uint8_t rf_clock_index = variable_item_get_current_value_index(item);
  213. model->rf_clock_index = rf_clock_index;
  214. model->rf_clock = all_rf_clocks[rf_clock_index];
  215. }
  216. model->data_loaded[1] = false;
  217. FuriString *buffer = furi_string_alloc();
  218. furi_string_printf(buffer, "%u", rf_clock_choices[model->rf_clock_index]);
  219. variable_item_set_current_value_text(item, furi_string_get_cstr(buffer));
  220. furi_string_free(buffer);
  221. }
  222. static const char* user_block_num_config_label = "Num of Blocks";
  223. static void t5577_writer_user_block_num_change(VariableItem* item) {
  224. T5577WriterApp* app = variable_item_get_context(item);
  225. T5577WriterModel* model = view_get_model(app->view_write);
  226. if (model->data_loaded[2]) {
  227. variable_item_set_current_value_index(item,model->user_block_num - 1);
  228. } else {
  229. uint8_t user_block_num_index = variable_item_get_current_value_index(item);
  230. model->user_block_num = user_block_num_index + 1;
  231. }
  232. model->data_loaded[2] = false;
  233. FuriString *buffer = furi_string_alloc();
  234. furi_string_printf(buffer, "%u", model->user_block_num);
  235. variable_item_set_current_value_text(item, furi_string_get_cstr(buffer));
  236. for(uint8_t i = model->user_block_num; i < LFRFID_T5577_BLOCK_COUNT; i++) {
  237. model->content[i] = 0;
  238. }
  239. furi_string_free(buffer);
  240. }
  241. static const char* edit_block_slc_config_label = "Edit Block";
  242. static void t5577_writer_edit_block_slc_change(VariableItem* item) {
  243. T5577WriterApp* app = variable_item_get_context(item);
  244. T5577WriterModel* model = view_get_model(app->view_write);
  245. uint8_t edit_block_slc_index = variable_item_get_current_value_index(item);
  246. model->edit_block_slc = edit_block_slc_index + 1;
  247. variable_item_set_current_value_index(item,model->edit_block_slc - 1);
  248. FuriString *buffer = furi_string_alloc();
  249. furi_string_printf(buffer, "%u", model->edit_block_slc);
  250. variable_item_set_current_value_text(item, furi_string_get_cstr(buffer));
  251. furi_string_free(buffer);
  252. }
  253. void ensure_dir_exists(Storage *storage)
  254. {
  255. // If apps_data directory doesn't exist, create it.
  256. if (!storage_dir_exists(storage, T5577_WRITER_APPS_DATA_FOLDER))
  257. {
  258. FURI_LOG_I(TAG, "Creating directory: %s", T5577_WRITER_APPS_DATA_FOLDER);
  259. storage_simply_mkdir(storage, T5577_WRITER_APPS_DATA_FOLDER);
  260. }
  261. else
  262. {
  263. FURI_LOG_I(TAG, "Directory exists: %s", T5577_WRITER_APPS_DATA_FOLDER);
  264. }
  265. // If wiegand directory doesn't exist, create it.
  266. if (!storage_dir_exists(storage, T5577_WRITER_FILE_FOLDER))
  267. {
  268. FURI_LOG_I(TAG, "Creating directory: %s", T5577_WRITER_FILE_FOLDER);
  269. storage_simply_mkdir(storage, T5577_WRITER_FILE_FOLDER);
  270. }
  271. else
  272. {
  273. FURI_LOG_I(TAG, "Directory exists: %s", T5577_WRITER_FILE_FOLDER);
  274. }
  275. }
  276. /**
  277. * Our 2nd sample setting is a text field. When the user clicks OK on the configuration
  278. * setting we use a text input screen to allow the user to enter a name. This function is
  279. * called when the user clicks OK on the text input screen.
  280. */
  281. static const char* tag_name_entry_text = "Enter name";
  282. static const char* tag_name_default_value = "Tag_1";
  283. static void t5577_writer_file_saver(void* context) {
  284. T5577WriterApp* app = (T5577WriterApp*)context;
  285. T5577WriterModel* model = view_get_model(app->view_write);
  286. model->content[0] = 0;
  287. model->content[0] |= model->modulation.mod_page_zero;
  288. model->content[0] |= model->rf_clock.clock_page_zero;
  289. model->content[0] |= (model->user_block_num << LFRFID_T5577_MAXBLOCK_SHIFT);
  290. bool redraw = true;
  291. with_view_model(
  292. app->view_write,
  293. T5577WriterModel * model,
  294. {
  295. furi_string_set(model->tag_name_str, app->temp_buffer);
  296. },
  297. redraw);
  298. FuriString *buffer = furi_string_alloc();
  299. FuriString *file_path = furi_string_alloc();
  300. furi_string_printf(
  301. file_path, "%s/%s%s", T5577_WRITER_FILE_FOLDER, furi_string_get_cstr(model->tag_name_str), T5577_WRITER_FILE_EXTENSION);
  302. Storage *storage = furi_record_open(RECORD_STORAGE);
  303. ensure_dir_exists(storage);
  304. File *data_file = storage_file_alloc(storage);
  305. if (storage_file_open(
  306. data_file, furi_string_get_cstr(file_path), FSAM_WRITE, FSOM_OPEN_ALWAYS))
  307. {
  308. furi_string_printf(buffer, "Filetype: Flipper T5577 Raw File\n");
  309. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  310. furi_string_printf(buffer, "Version: 1.0\n");
  311. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  312. furi_string_printf(buffer, "Modulation: %s\n", model->modulation.modulation_name);
  313. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  314. furi_string_printf(buffer, "RF Clock: %u\n", model->rf_clock.rf_clock_num);
  315. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  316. furi_string_printf(buffer, "Number of User Blocks: %u\n", model->user_block_num);
  317. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  318. furi_string_printf(buffer, "\nRaw Data:\n");
  319. for (int i = 0; i < model->user_block_num; i++)
  320. {
  321. furi_string_cat_printf(
  322. buffer,
  323. "Block %u: %08lX\n",
  324. i,
  325. model->content[i]);
  326. }
  327. furi_string_push_back(buffer, '\n');
  328. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  329. storage_file_close(data_file);
  330. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewSubmenu);
  331. }
  332. }
  333. void t5577_writer_update_config_from_load(void* context) {
  334. T5577WriterApp* app = (T5577WriterApp*)context;
  335. T5577WriterModel* my_model = view_get_model(app->view_write);
  336. for (size_t i = 0; i < COUNT_OF(all_mods); i++) {
  337. if ((my_model->content[0] & all_mods[i].mod_page_zero) == all_mods[i].mod_page_zero) {
  338. my_model->modulation_index = (uint8_t)i;
  339. my_model->modulation = all_mods[my_model->modulation_index];
  340. }
  341. }
  342. for (size_t i = 0; i < COUNT_OF(all_rf_clocks); i++) {
  343. if ((my_model->content[0] & all_rf_clocks[i].clock_page_zero) == all_rf_clocks[i].clock_page_zero) {
  344. my_model->rf_clock_index = (uint8_t)i;
  345. my_model->rf_clock = all_rf_clocks[my_model->rf_clock_index];
  346. }
  347. }
  348. my_model->user_block_num = (my_model->content[0] >> LFRFID_T5577_MAXBLOCK_SHIFT) & 0x7;
  349. memset(my_model->data_loaded, true, sizeof(my_model->data_loaded));
  350. }
  351. static void t5577_writer_config_enter_callback(void* context) {
  352. T5577WriterApp* app = (T5577WriterApp*)context;
  353. variable_item_list_reset(app->variable_item_list_config);
  354. app->mod_item = variable_item_list_add(
  355. app->variable_item_list_config,
  356. modulation_config_label,
  357. COUNT_OF(modulation_names),
  358. t5577_writer_modulation_change,
  359. app);
  360. app->clock_item = variable_item_list_add(
  361. app->variable_item_list_config,
  362. rf_clock_config_label,
  363. COUNT_OF(rf_clock_choices),
  364. t5577_writer_rf_clock_change,
  365. app);
  366. app->block_num_item = variable_item_list_add(
  367. app->variable_item_list_config,
  368. user_block_num_config_label,
  369. LFRFID_T5577_BLOCK_COUNT,
  370. t5577_writer_user_block_num_change,
  371. app);
  372. app->block_slc_item = variable_item_list_add(
  373. app->variable_item_list_config,
  374. edit_block_slc_config_label,
  375. LFRFID_T5577_BLOCK_COUNT - 1,
  376. t5577_writer_edit_block_slc_change,
  377. app);
  378. View* view_config_i = variable_item_list_get_view(app->variable_item_list_config);
  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. /**
  490. * @brief Callback for drawing the game screen.
  491. * @details This function is called when the screen needs to be redrawn, like when the model gets updated.
  492. * @param canvas The canvas to draw on.
  493. * @param model The model - MyModel object.
  494. */
  495. static void t5577_writer_view_write_callback(Canvas* canvas, void* model) {
  496. T5577WriterModel* my_model = (T5577WriterModel*)model;
  497. my_model->content[0] = 0;
  498. my_model->content[0] |= my_model->modulation.mod_page_zero;
  499. my_model->content[0] |= my_model->rf_clock.clock_page_zero;
  500. my_model->content[0] |= (my_model->user_block_num << LFRFID_T5577_MAXBLOCK_SHIFT);
  501. LFRFIDT5577* data = (LFRFIDT5577*)malloc(sizeof(LFRFIDT5577));
  502. data->blocks_to_write = my_model->user_block_num;
  503. for(size_t i = 0; i < data->blocks_to_write; i++) {
  504. data->block[i] = my_model->content[i];
  505. }
  506. data->block[1] = 0x007F9FB6;
  507. data->block[2] = 0xB59FD7F9;
  508. data->block[3] = 0x006BDFAE;
  509. data->block[4] = 0xACB83B53;
  510. t5577_write(data);
  511. free(data);
  512. canvas_draw_line(canvas, 0, 25, 120, 50);
  513. }
  514. /**
  515. * @brief Callback for timer elapsed.
  516. * @details This function is called when the timer is elapsed. We use this to queue a redraw event.
  517. * @param context The context - T5577WriterApp object.
  518. */
  519. static void t5577_writer_view_write_timer_callback(void* context) {
  520. T5577WriterApp* app = (T5577WriterApp*)context;
  521. view_dispatcher_send_custom_event(app->view_dispatcher, T5577WriterEventIdRedrawScreen);
  522. }
  523. /**
  524. * @brief Callback when the user starts the game screen.
  525. * @details This function is called when the user enters the game screen. We start a timer to
  526. * redraw the screen periodically (so the random number is refreshed).
  527. * @param context The context - T5577WriterApp object.
  528. */
  529. static void t5577_writer_view_write_enter_callback(void* context) {
  530. uint32_t period = furi_ms_to_ticks(500);
  531. T5577WriterApp* app = (T5577WriterApp*)context;
  532. furi_assert(app->timer == NULL);
  533. app->timer =
  534. furi_timer_alloc(t5577_writer_view_write_timer_callback, FuriTimerTypePeriodic, context);
  535. furi_timer_start(app->timer, period);
  536. }
  537. /**
  538. * @brief Callback when the user exits the game screen.
  539. * @details This function is called when the user exits the game 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. furi_timer_stop(app->timer);
  545. furi_timer_free(app->timer);
  546. app->timer = NULL;
  547. }
  548. /**
  549. * @brief Callback for custom events.
  550. * @details This function is called when a custom event is sent to the view dispatcher.
  551. * @param event The event id - T5577WriterEventId value.
  552. * @param context The context - T5577WriterApp object.
  553. */
  554. static bool t5577_writer_view_write_custom_event_callback(uint32_t event, void* context) {
  555. T5577WriterApp* app = (T5577WriterApp*)context;
  556. switch(event) {
  557. case T5577WriterEventIdRedrawScreen:
  558. // Redraw screen by passing true to last parameter of with_view_model.
  559. {
  560. bool redraw = true;
  561. with_view_model(
  562. app->view_write, T5577WriterModel * _model, { UNUSED(_model); }, redraw);
  563. return true;
  564. }
  565. case T5577WriterEventIdOkPressed:
  566. // Process the OK button. We go to the saving scene.
  567. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewSubmenu);
  568. return true;
  569. default:
  570. return false;
  571. }
  572. }
  573. /**
  574. * @brief Callback for game screen input.
  575. * @details This function is called when the user presses a button while on the game screen.
  576. * @param event The event - InputEvent object.
  577. * @param context The context - T5577WriterApp object.
  578. * @return true if the event was handled, false otherwise.
  579. */
  580. static bool t5577_writer_view_write_input_callback(InputEvent* event, void* context) {
  581. T5577WriterApp* app = (T5577WriterApp*)context;
  582. if(event->type == InputTypeShort) {
  583. if(event->key == InputKeyOk) {
  584. // We choose to send a custom event when user presses OK button. t5577_writer_custom_event_callback will
  585. // handle our T5577WriterEventIdOkPressed event. We could have just put the code from
  586. // t5577_writer_custom_event_callback here, it's a matter of preference.
  587. view_dispatcher_send_custom_event(app->view_dispatcher, T5577WriterEventIdOkPressed);
  588. return true;
  589. }
  590. }
  591. return false;
  592. }
  593. /**
  594. * @brief Allocate the t5577_writer application.
  595. * @details This function allocates the t5577_writer application resources.
  596. * @return T5577WriterApp object.
  597. */
  598. static T5577WriterApp* t5577_writer_app_alloc() {
  599. T5577WriterApp* app = (T5577WriterApp*)malloc(sizeof(T5577WriterApp));
  600. Gui* gui = furi_record_open(RECORD_GUI);
  601. app->view_dispatcher = view_dispatcher_alloc();
  602. app->dialogs = furi_record_open(RECORD_DIALOGS);
  603. app->file_path = furi_string_alloc();
  604. view_dispatcher_enable_queue(app->view_dispatcher);
  605. view_dispatcher_attach_to_gui(app->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  606. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  607. app->submenu = submenu_alloc();
  608. submenu_add_item(
  609. app->submenu, "Write", T5577WriterSubmenuIndexWrite, t5577_writer_submenu_callback, app);
  610. submenu_add_item(
  611. app->submenu, "Config", T5577WriterSubmenuIndexConfigure, t5577_writer_submenu_callback, app);
  612. submenu_add_item(
  613. app->submenu, "Save", T5577WriterSubmenuIndexSave, t5577_writer_submenu_callback, app);
  614. submenu_add_item(
  615. app->submenu, "Load", T5577WriterSubmenuIndexLoad, t5577_writer_submenu_callback, app);
  616. submenu_add_item(
  617. app->submenu, "About", T5577WriterSubmenuIndexAbout, t5577_writer_submenu_callback, app);
  618. view_set_previous_callback(submenu_get_view(app->submenu), t5577_writer_navigation_exit_callback);
  619. view_dispatcher_add_view(
  620. app->view_dispatcher, T5577WriterViewSubmenu, submenu_get_view(app->submenu));
  621. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewSubmenu);
  622. app->text_input = text_input_alloc();
  623. view_dispatcher_add_view(
  624. app->view_dispatcher, T5577WriterViewTextInput, text_input_get_view(app->text_input));
  625. app->view_load = view_alloc();
  626. view_set_previous_callback(app->view_load, t5577_writer_navigation_submenu_callback);
  627. view_set_enter_callback(app->view_load, t5577_writer_view_load_callback);
  628. view_set_context(app->view_load, app);
  629. view_dispatcher_add_view(
  630. app->view_dispatcher,
  631. T5577WriterViewLoad,
  632. app->view_load);
  633. app->temp_buffer_size = 32;
  634. app->temp_buffer = (char*)malloc(app->temp_buffer_size);
  635. app->view_write = view_alloc();
  636. view_set_draw_callback(app->view_write, t5577_writer_view_write_callback);
  637. view_set_input_callback(app->view_write, t5577_writer_view_write_input_callback);
  638. view_set_previous_callback(app->view_write, t5577_writer_navigation_submenu_callback);
  639. view_set_enter_callback(app->view_write, t5577_writer_view_write_enter_callback);
  640. view_set_exit_callback(app->view_write, t5577_writer_view_write_exit_callback);
  641. view_set_context(app->view_write, app);
  642. view_set_custom_callback(app->view_write, t5577_writer_view_write_custom_event_callback);
  643. view_allocate_model(app->view_write, ViewModelTypeLockFree, sizeof(T5577WriterModel));
  644. view_dispatcher_add_view(app->view_dispatcher, T5577WriterViewWrite, app->view_write);
  645. T5577WriterModel* model = view_get_model(app->view_write); // initialize model
  646. FuriString* tag_name_str = furi_string_alloc();
  647. furi_string_set_str(tag_name_str, tag_name_default_value);
  648. model->tag_name_str = tag_name_str;
  649. initialize_model(model);
  650. initialize_rf_clock_choices(rf_clock_choices);
  651. initialize_mod_names(modulation_names);
  652. app->view_save = view_alloc();
  653. view_set_previous_callback(app->view_save, t5577_writer_navigation_submenu_callback);
  654. view_set_enter_callback(app->view_save, t5577_writer_view_save_callback);
  655. view_set_context(app->view_save, app);
  656. view_dispatcher_add_view(
  657. app->view_dispatcher,
  658. T5577WriterViewSave,
  659. app->view_save);
  660. app->variable_item_list_config = variable_item_list_alloc();
  661. app->view_config_e = view_alloc();
  662. view_set_previous_callback(
  663. app->view_config_e,
  664. t5577_writer_navigation_submenu_callback);
  665. view_set_enter_callback(app->view_config_e, t5577_writer_config_enter_callback);
  666. view_set_context(app->view_config_e, app);
  667. view_dispatcher_add_view(
  668. app->view_dispatcher,
  669. T5577WriterViewConfigure_e,
  670. app->view_config_e);
  671. View* view_buffer = view_alloc();
  672. view_dispatcher_add_view(
  673. app->view_dispatcher,
  674. T5577WriterViewConfigure_i,
  675. view_buffer);
  676. app->widget_about = widget_alloc();
  677. widget_add_text_scroll_element(
  678. app->widget_about,
  679. 0,
  680. 0,
  681. 128,
  682. 64,
  683. "T5577 Writer v0.1");
  684. view_set_previous_callback(
  685. widget_get_view(app->widget_about), t5577_writer_navigation_submenu_callback);
  686. view_dispatcher_add_view(
  687. app->view_dispatcher, T5577WriterViewAbout, widget_get_view(app->widget_about));
  688. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  689. #ifdef BACKLIGHT_ON
  690. notification_message(app->notifications, &sequence_display_backlight_enforce_on);
  691. #endif
  692. return app;
  693. }
  694. /**
  695. * @brief Free the t5577_writer application.
  696. * @details This function frees the t5577_writer application resources.
  697. * @param app The t5577_writer application object.
  698. */
  699. static void t5577_writer_app_free(T5577WriterApp* app) {
  700. #ifdef BACKLIGHT_ON
  701. notification_message(app->notifications, &sequence_display_backlight_enforce_auto);
  702. #endif
  703. furi_record_close(RECORD_NOTIFICATION);
  704. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewTextInput);
  705. text_input_free(app->text_input);
  706. free(app->temp_buffer);
  707. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewAbout);
  708. widget_free(app->widget_about);
  709. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewWrite);
  710. with_view_model(
  711. app->view_write,
  712. T5577WriterModel * model,
  713. {
  714. if(model->content != NULL) {
  715. free(model->content);
  716. }
  717. },
  718. false);
  719. view_free(app->view_write);
  720. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewLoad);
  721. view_free(app->view_load);
  722. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewConfigure_i);
  723. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewConfigure_e);
  724. variable_item_list_free(app->variable_item_list_config);
  725. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewSave);
  726. view_free(app->view_save);
  727. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewSubmenu);
  728. submenu_free(app->submenu);
  729. view_dispatcher_free(app->view_dispatcher);
  730. furi_record_close(RECORD_GUI);
  731. free(app);
  732. }
  733. /**
  734. * @brief Main function for t5577_writer application.
  735. * @details This function is the entry point for the t5577_writer application. It should be defined in
  736. * application.fam as the entry_point setting.
  737. * @param _p Input parameter - unused
  738. * @return 0 - Success
  739. */
  740. int32_t main_t5577_writer_app(void* _p) {
  741. UNUSED(_p);
  742. T5577WriterApp* app = t5577_writer_app_alloc();
  743. view_dispatcher_run(app->view_dispatcher);
  744. t5577_writer_app_free(app);
  745. return 0;
  746. }