progress.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923
  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/byte_input.h>
  7. #include <gui/modules/popup.h>
  8. #include <gui/modules/submenu.h>
  9. #include <gui/modules/text_input.h>
  10. #include <gui/modules/widget.h>
  11. #include <gui/modules/variable_item_list.h>
  12. #include <notification/notification.h>
  13. #include <notification/notification_messages.h>
  14. #include "t5577_writer_icons.h"
  15. #include <applications/services/storage/storage.h>
  16. #include <applications/services/dialogs/dialogs.h>
  17. #include <stdbool.h>
  18. #include <stdint.h>
  19. #include <stdio.h>
  20. #include <t5577_config.h>
  21. #include <t5577_writer.h>
  22. #define TAG "T5577 Writer"
  23. // Change this to BACKLIGHT_AUTO if you don't want the backlight to be continuously on.
  24. #define BACKLIGHT_AUTO 1
  25. // Our application menu has 3 items. You can add more items if you want.
  26. typedef enum {
  27. T5577WriterSubmenuIndexLoad,
  28. T5577WriterSubmenuIndexSave,
  29. T5577WriterSubmenuIndexConfigure,
  30. T5577WriterSubmenuIndexWrite,
  31. T5577WriterSubmenuIndexAbout,
  32. } T5577WriterSubmenuIndex;
  33. // Each view is a screen we show the user.
  34. typedef enum {
  35. T5577WriterViewSubmenu, // The menu when the app starts
  36. T5577WriterViewTextInput, // Input for configuring text settings
  37. T5577WriterViewByteInput,
  38. T5577WriterViewLoad,
  39. T5577WriterViewSave,
  40. T5577WriterViewConfigure_i, // The configuration screen
  41. T5577WriterViewConfigure_e, // The configuration screen
  42. T5577WriterViewWrite, // The main screen
  43. T5577WriterViewAbout, // The about screen with directions, link to social channel, etc.
  44. } T5577WriterView;
  45. typedef enum {
  46. T5577WriterEventIdRedrawScreen = 0, // Custom event to redraw the screen
  47. T5577WriterEventIdOkPressed = 42, // Custom event to process OK button getting pressed down
  48. } T5577WriterEventId;
  49. typedef struct {
  50. ViewDispatcher* view_dispatcher; // Switches between our views
  51. NotificationApp* notifications; // Used for controlling the backlight
  52. Submenu* submenu; // The application menu
  53. TextInput* text_input; // The text input screen
  54. VariableItemList* variable_item_list_config; // The configuration screen
  55. View* view_config_e; // The configuration screen
  56. View* view_save;
  57. View* view_write; // The main screen
  58. Widget* widget_about; // The about screen
  59. View* view_load; // The load view
  60. VariableItem* mod_item; //
  61. VariableItem* clock_item; //
  62. VariableItem* block_num_item; //
  63. VariableItem* block_slc_item; //
  64. VariableItem* byte_buffer_item; //
  65. ByteInput* byte_input; // The byte input view
  66. uint8_t* bytes_buffer[4];
  67. uint8_t bytes_count;
  68. char* temp_buffer; // Temporary buffer for text input
  69. uint32_t temp_buffer_size; // Size of temporary buffer
  70. DialogsApp* dialogs;
  71. FuriString* file_path;
  72. FuriTimer* timer; // Timer for redrawing the screen
  73. ViewNavigationCallback config_enter_callback;
  74. } T5577WriterApp;
  75. typedef struct {
  76. uint8_t modulation_index; // The index for total number of pins
  77. uint8_t rf_clock_index; // The index for total number of pins
  78. FuriString* tag_name_str; // The name setting
  79. uint8_t user_block_num; // The total number of pins we are adjusting
  80. uint32_t* content; // The cutting content
  81. t5577_modulation modulation;
  82. t5577_rf_clock rf_clock;
  83. bool data_loaded[3];
  84. uint8_t edit_block_slc;
  85. } T5577WriterModel;
  86. static inline int min(int a, int b) {
  87. return (a < b) ? a : b;
  88. }
  89. static inline int max(int a, int b) {
  90. return (a > b) ? a : b;
  91. }
  92. void initialize_config(T5577WriterModel* model) {
  93. model->modulation_index = 0;
  94. memcpy(&model->modulation, &all_mods[model->modulation_index], sizeof(t5577_modulation));
  95. model->rf_clock_index = 0;
  96. memcpy(&model->rf_clock, &all_mods[model->rf_clock_index], sizeof(t5577_rf_clock));
  97. }
  98. void initialize_model(T5577WriterModel* model) {
  99. if(model->content != NULL) {
  100. free(model->content);
  101. }
  102. initialize_config(model);
  103. model->user_block_num = 1;
  104. model->edit_block_slc = 1;
  105. model->content = (uint32_t*)malloc(LFRFID_T5577_BLOCK_COUNT * sizeof(uint32_t));
  106. for(uint32_t i = 0; i < LFRFID_T5577_BLOCK_COUNT; i++) {
  107. model->content[i] = 0;
  108. }
  109. memset(model->data_loaded, false, sizeof(model->data_loaded));
  110. }
  111. uint8_t rf_clock_choices[COUNT_OF(all_rf_clocks)];
  112. void initialize_rf_clock_choices(uint8_t* rf_clock_choices) {
  113. // Populate the rf_clock_choices array
  114. for (size_t i = 0; i < COUNT_OF(all_rf_clocks); i++) {
  115. rf_clock_choices[i] = all_rf_clocks[i].rf_clock_num;
  116. }
  117. }
  118. char* modulation_names[COUNT_OF(all_mods)];
  119. void initialize_mod_names(char** modulation_names) {
  120. // Populate the modulation_names array
  121. for (size_t i = 0; i < COUNT_OF(all_mods); i++) {
  122. modulation_names[i] = all_mods[i].modulation_name;
  123. }
  124. }
  125. /**
  126. * @brief Callback for exiting the application.
  127. * @details This function is called when user press back button. We return VIEW_NONE to
  128. * indicate that we want to exit the application.
  129. * @param _context The context - unused
  130. * @return next view id
  131. */
  132. static uint32_t t5577_writer_navigation_exit_callback(void* _context) {
  133. UNUSED(_context);
  134. return VIEW_NONE;
  135. }
  136. /**
  137. * @brief Callback for returning to submenu.
  138. * @details This function is called when user press back button. We return VIEW_NONE to
  139. * indicate that we want to navigate to the submenu.
  140. * @param _context The context - unused
  141. * @return next view id
  142. */
  143. static uint32_t t5577_writer_navigation_submenu_callback(void* _context) {
  144. UNUSED(_context);
  145. return T5577WriterViewSubmenu;
  146. }
  147. /**
  148. * @brief Callback for returning to configure screen.
  149. * @details This function is called when user press back button. We return VIEW_NONE to
  150. * indicate that we want to navigate to the configure screen.
  151. * @param _context The context - unused
  152. * @return next view id
  153. */
  154. //static uint32_t t5577_writer_navigation_configure_callback(void* _context) {
  155. // UNUSED(_context);
  156. // return T5577WriterViewConfigure_i;
  157. //}
  158. static uint32_t t5577_writer_navigation_file_callback(void* _context) {
  159. UNUSED(_context);
  160. return T5577WriterViewSubmenu;
  161. }
  162. /**
  163. * @brief Handle submenu item selection.
  164. * @details This function is called when user selects an item from the submenu.
  165. * @param context The context - T5577WriterApp object.
  166. * @param index The T5577WriterSubmenuIndex item that was clicked.
  167. */
  168. static void t5577_writer_submenu_callback(void* context, uint32_t index) {
  169. T5577WriterApp* app = (T5577WriterApp*)context;
  170. switch(index) {
  171. case T5577WriterSubmenuIndexLoad:
  172. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewLoad);
  173. break;
  174. case T5577WriterSubmenuIndexSave:
  175. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewSave);
  176. break;
  177. case T5577WriterSubmenuIndexConfigure:
  178. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewConfigure_e);
  179. break;
  180. case T5577WriterSubmenuIndexWrite:
  181. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewWrite);
  182. break;
  183. case T5577WriterSubmenuIndexAbout:
  184. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewAbout);
  185. break;
  186. default:
  187. break;
  188. }
  189. }
  190. /**
  191. * Our 1st sample setting is a team color. We have 3 options: red, green, and blue.
  192. */
  193. static const char* modulation_config_label = "Modulation";
  194. //static char* modulation_names[] = {"Direct", "PSK1", "PSK2", "PSK3", "FSK1", "FSK2", "FSK1a", "FSK2a","ASK/Man","Biphase","Diphase"};
  195. static void t5577_writer_modulation_change(VariableItem* item) {
  196. T5577WriterApp* app = variable_item_get_context(item);
  197. FURI_LOG_D(TAG,"app defined");
  198. T5577WriterModel* model = view_get_model(app->view_write);
  199. FURI_LOG_D(TAG,"model defined");
  200. if (model->data_loaded[0]) {
  201. FURI_LOG_D(TAG,"loaded entered");
  202. variable_item_set_current_value_index(item,model->modulation_index);
  203. } else{
  204. FURI_LOG_D(TAG,"else entered");
  205. uint8_t modulation_index = variable_item_get_current_value_index(item);
  206. model->modulation_index = modulation_index;
  207. model->modulation = all_mods[modulation_index];
  208. }
  209. model->data_loaded[0] = false;
  210. variable_item_set_current_value_text(item, modulation_names[model->modulation_index]);
  211. }
  212. static const char* rf_clock_config_label = "RF Clock";
  213. static void t5577_writer_rf_clock_change(VariableItem* item) {
  214. T5577WriterApp* app = variable_item_get_context(item);
  215. T5577WriterModel* model = view_get_model(app->view_write);
  216. if (model->data_loaded[1]) {
  217. variable_item_set_current_value_index(item,model->rf_clock_index);
  218. } else{
  219. uint8_t rf_clock_index = variable_item_get_current_value_index(item);
  220. model->rf_clock_index = rf_clock_index;
  221. model->rf_clock = all_rf_clocks[rf_clock_index];
  222. }
  223. model->data_loaded[1] = false;
  224. FuriString *buffer = furi_string_alloc();
  225. furi_string_printf(buffer, "%u", rf_clock_choices[model->rf_clock_index]);
  226. variable_item_set_current_value_text(item, furi_string_get_cstr(buffer));
  227. furi_string_free(buffer);
  228. }
  229. static const char* user_block_num_config_label = "Num of Blocks";
  230. static void t5577_writer_user_block_num_change(VariableItem* item) {
  231. T5577WriterApp* app = variable_item_get_context(item);
  232. T5577WriterModel* model = view_get_model(app->view_write);
  233. if (model->data_loaded[2]) {
  234. variable_item_set_current_value_index(item,model->user_block_num - 1);
  235. } else {
  236. uint8_t user_block_num_index = variable_item_get_current_value_index(item);
  237. model->user_block_num = user_block_num_index + 1;
  238. }
  239. model->data_loaded[2] = false;
  240. FuriString *buffer = furi_string_alloc();
  241. furi_string_printf(buffer, "%u", model->user_block_num);
  242. variable_item_set_current_value_text(item, furi_string_get_cstr(buffer));
  243. for(uint8_t i = model->user_block_num; i < LFRFID_T5577_BLOCK_COUNT; i++) {
  244. model->content[i] = 0;
  245. }
  246. furi_string_free(buffer);
  247. }
  248. static const char* edit_block_slc_config_label = "Edit Block";
  249. static void t5577_writer_edit_block_slc_change(VariableItem* item) {
  250. T5577WriterApp* app = variable_item_get_context(item);
  251. T5577WriterModel* model = view_get_model(app->view_write);
  252. uint8_t edit_block_slc_index = variable_item_get_current_value_index(item);
  253. model->edit_block_slc = edit_block_slc_index + 1;
  254. variable_item_set_current_value_index(item,model->edit_block_slc - 1);
  255. FuriString *buffer = furi_string_alloc();
  256. furi_string_printf(buffer, "%u", model->edit_block_slc);
  257. variable_item_set_current_value_text(item, furi_string_get_cstr(buffer));
  258. furi_string_free(buffer);
  259. }
  260. static const char* edit_block_data_config_label = "Block Data";
  261. void ensure_dir_exists(Storage *storage)
  262. {
  263. // If apps_data directory doesn't exist, create it.
  264. if (!storage_dir_exists(storage, T5577_WRITER_APPS_DATA_FOLDER))
  265. {
  266. FURI_LOG_I(TAG, "Creating directory: %s", T5577_WRITER_APPS_DATA_FOLDER);
  267. storage_simply_mkdir(storage, T5577_WRITER_APPS_DATA_FOLDER);
  268. }
  269. else
  270. {
  271. FURI_LOG_I(TAG, "Directory exists: %s", T5577_WRITER_APPS_DATA_FOLDER);
  272. }
  273. // If wiegand directory doesn't exist, create it.
  274. if (!storage_dir_exists(storage, T5577_WRITER_FILE_FOLDER))
  275. {
  276. FURI_LOG_I(TAG, "Creating directory: %s", T5577_WRITER_FILE_FOLDER);
  277. storage_simply_mkdir(storage, T5577_WRITER_FILE_FOLDER);
  278. }
  279. else
  280. {
  281. FURI_LOG_I(TAG, "Directory exists: %s", T5577_WRITER_FILE_FOLDER);
  282. }
  283. }
  284. /**
  285. * Our 2nd sample setting is a text field. When the user clicks OK on the configuration
  286. * setting we use a text input screen to allow the user to enter a name. This function is
  287. * called when the user clicks OK on the text input screen.
  288. */
  289. static const char* tag_name_entry_text = "Enter name";
  290. static const char* tag_name_default_value = "Tag_1";
  291. static void t5577_writer_file_saver(void* context) {
  292. T5577WriterApp* app = (T5577WriterApp*)context;
  293. T5577WriterModel* model = view_get_model(app->view_write);
  294. model->content[0] = 0;
  295. model->content[0] |= model->modulation.mod_page_zero;
  296. model->content[0] |= model->rf_clock.clock_page_zero;
  297. model->content[0] |= (model->user_block_num << LFRFID_T5577_MAXBLOCK_SHIFT);
  298. bool redraw = true;
  299. with_view_model(
  300. app->view_write,
  301. T5577WriterModel * model,
  302. {
  303. furi_string_set(model->tag_name_str, app->temp_buffer);
  304. },
  305. redraw);
  306. FuriString *buffer = furi_string_alloc();
  307. FuriString *file_path = furi_string_alloc();
  308. furi_string_printf(
  309. file_path, "%s/%s%s", T5577_WRITER_FILE_FOLDER, furi_string_get_cstr(model->tag_name_str), T5577_WRITER_FILE_EXTENSION);
  310. Storage *storage = furi_record_open(RECORD_STORAGE);
  311. ensure_dir_exists(storage);
  312. File *data_file = storage_file_alloc(storage);
  313. if (storage_file_open(
  314. data_file, furi_string_get_cstr(file_path), FSAM_WRITE, FSOM_OPEN_ALWAYS))
  315. {
  316. furi_string_printf(buffer, "Filetype: Flipper T5577 Raw File\n");
  317. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  318. furi_string_printf(buffer, "Version: 1.0\n");
  319. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  320. furi_string_printf(buffer, "Modulation: %s\n", model->modulation.modulation_name);
  321. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  322. furi_string_printf(buffer, "RF Clock: %u\n", model->rf_clock.rf_clock_num);
  323. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  324. furi_string_printf(buffer, "Number of User Blocks: %u\n", model->user_block_num);
  325. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  326. furi_string_printf(buffer, "\nRaw Data:\n");
  327. for (int i = 0; i < model->user_block_num; i++)
  328. {
  329. furi_string_cat_printf(
  330. buffer,
  331. "Block %u: %08lX\n",
  332. i,
  333. model->content[i]);
  334. }
  335. furi_string_push_back(buffer, '\n');
  336. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  337. storage_file_close(data_file);
  338. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewSubmenu);
  339. }
  340. }
  341. void t5577_writer_update_config_from_load(void* context) {
  342. T5577WriterApp* app = (T5577WriterApp*)context;
  343. T5577WriterModel* my_model = view_get_model(app->view_write);
  344. for (size_t i = 0; i < COUNT_OF(all_mods); i++) {
  345. if ((my_model->content[0] & all_mods[i].mod_page_zero) == all_mods[i].mod_page_zero) {
  346. my_model->modulation_index = (uint8_t)i;
  347. my_model->modulation = all_mods[my_model->modulation_index];
  348. }
  349. }
  350. for (size_t i = 0; i < COUNT_OF(all_rf_clocks); i++) {
  351. if ((my_model->content[0] & all_rf_clocks[i].clock_page_zero) == all_rf_clocks[i].clock_page_zero) {
  352. my_model->rf_clock_index = (uint8_t)i;
  353. my_model->rf_clock = all_rf_clocks[my_model->rf_clock_index];
  354. }
  355. }
  356. my_model->user_block_num = (my_model->content[0] >> LFRFID_T5577_MAXBLOCK_SHIFT) & 0x7;
  357. memset(my_model->data_loaded, true, sizeof(my_model->data_loaded));
  358. }
  359. void uint32_to_byte_buffer(uint32_t block_data, uint8_t* byte_buffer[4]) {
  360. byte_buffer[0] = (block_data >> 24) & 0xFF;
  361. byte_buffer[1] = (block_data >> 16) & 0xFF;
  362. byte_buffer[2] = (block_data >> 8) & 0xFF;
  363. byte_buffer[3] = block_data & 0xFF;
  364. }
  365. uint32_t byte_buffer_to_uint32(const uint8_t** byte_buffer[4]) {
  366. return ((uint32_t)(**byte_buffer[0] << 24)) ;
  367. }
  368. static void t5577_writer_content_byte_input_confirmed(void* context) {
  369. T5577WriterApp* app = (T5577WriterApp*)context;
  370. T5577WriterModel* my_model = view_get_model(app->view_write);
  371. my_model->content[my_model->edit_block_slc] = byte_buffer_to_uint32(app->bytes_buffer);
  372. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewConfigure_e);
  373. }
  374. static void t5577_writer_content_byte_changed(void* context) {
  375. UNUSED(context);
  376. }
  377. static void t5577_writer_config_item_clicked(void* context, uint32_t index) {
  378. T5577WriterApp* app = (T5577WriterApp*)context;
  379. T5577WriterModel* my_model = view_get_model(app->view_write);
  380. FuriString *buffer = furi_string_alloc();
  381. furi_string_printf(buffer, "Enter Block %u Data", my_model->edit_block_slc);
  382. // Our hex input UI is the 5th in the config menue.
  383. if(index == 4) {
  384. // Header to display on the text input screen.
  385. byte_input_set_header_text(app->byte_input, furi_string_get_cstr(buffer));
  386. // Copy the current name into the temporary buffer.
  387. bool redraw = false;
  388. with_view_model(
  389. app->view_write,
  390. T5577WriterModel * model,
  391. {
  392. uint32_to_byte_buffer(model->content[model->edit_block_slc],app->bytes_buffer);
  393. },
  394. redraw);
  395. // Configure the text input. When user enters text and clicks OK, key_copier_setting_text_updated be called.
  396. byte_input_set_result_callback(
  397. app->byte_input,
  398. t5577_writer_content_byte_input_confirmed,
  399. t5577_writer_content_byte_changed,
  400. app,
  401. app->bytes_buffer,
  402. app->bytes_count
  403. );
  404. // Pressing the BACK button will reload the configure screen.
  405. view_set_previous_callback(byte_input_get_view(app->byte_input), app->config_enter_callback);
  406. // Show text input dialog.
  407. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewByteInput);
  408. }
  409. }
  410. static void t5577_writer_config_enter_callback(void* context) {
  411. T5577WriterApp* app = (T5577WriterApp*)context;
  412. T5577WriterModel* my_model = view_get_model(app->view_write);
  413. variable_item_list_reset(app->variable_item_list_config);
  414. app->mod_item = variable_item_list_add(
  415. app->variable_item_list_config,
  416. modulation_config_label,
  417. COUNT_OF(modulation_names),
  418. t5577_writer_modulation_change,
  419. app);
  420. app->clock_item = variable_item_list_add(
  421. app->variable_item_list_config,
  422. rf_clock_config_label,
  423. COUNT_OF(rf_clock_choices),
  424. t5577_writer_rf_clock_change,
  425. app);
  426. app->block_num_item = variable_item_list_add(
  427. app->variable_item_list_config,
  428. user_block_num_config_label,
  429. LFRFID_T5577_BLOCK_COUNT,
  430. t5577_writer_user_block_num_change,
  431. app);
  432. app->block_slc_item = variable_item_list_add(
  433. app->variable_item_list_config,
  434. edit_block_slc_config_label,
  435. LFRFID_T5577_BLOCK_COUNT - 1,
  436. t5577_writer_edit_block_slc_change,
  437. app);
  438. app->byte_buffer_item = variable_item_list_add(
  439. app->variable_item_list_config, edit_block_data_config_label, 1, NULL, NULL);
  440. FuriString *buffer = furi_string_alloc();
  441. furi_string_printf(buffer, "%08lX", my_model->content[my_model->edit_block_slc]);
  442. variable_item_set_current_value_text(
  443. app->byte_buffer_item, furi_string_get_cstr(buffer));
  444. View* view_config_i = variable_item_list_get_view(app->variable_item_list_config);
  445. t5577_writer_modulation_change(app->mod_item);
  446. t5577_writer_rf_clock_change(app->clock_item);
  447. t5577_writer_user_block_num_change(app->block_num_item);
  448. t5577_writer_edit_block_slc_change(app->block_slc_item);
  449. view_set_previous_callback(
  450. view_config_i,
  451. t5577_writer_navigation_submenu_callback);
  452. variable_item_list_set_enter_callback(
  453. app->variable_item_list_config, t5577_writer_config_item_clicked, app);
  454. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewConfigure_i);
  455. view_dispatcher_add_view(
  456. app->view_dispatcher,
  457. T5577WriterViewConfigure_i,
  458. view_config_i);
  459. view_dispatcher_switch_to_view(app->view_dispatcher,T5577WriterViewConfigure_i);
  460. FURI_LOG_D(TAG,"enter_callback_finished");
  461. }
  462. void t5577_writer_view_load_callback(void* context) {
  463. T5577WriterApp* app = (T5577WriterApp*)context;
  464. T5577WriterModel* model = view_get_model(app->view_write);
  465. DialogsFileBrowserOptions browser_options;
  466. Storage* storage = furi_record_open(RECORD_STORAGE);
  467. ensure_dir_exists(storage);
  468. File* data_file = storage_file_alloc(storage);
  469. dialog_file_browser_set_basic_options(&browser_options, T5577_WRITER_FILE_EXTENSION, NULL);
  470. browser_options.base_path = T5577_WRITER_FILE_FOLDER;
  471. furi_string_set(app->file_path, browser_options.base_path);
  472. FuriString* buffer = furi_string_alloc();
  473. if(dialog_file_browser_show(app->dialogs, app->file_path, app->file_path, &browser_options)) {
  474. if(storage_file_open(
  475. data_file, furi_string_get_cstr(app->file_path), FSAM_READ, FSOM_OPEN_EXISTING)) {
  476. while(!storage_file_eof(data_file)) { // fill buffer with every line because ch == '\n'
  477. char ch;
  478. furi_string_reset(buffer);
  479. while(storage_file_read(data_file, &ch, 1) && !storage_file_eof(data_file)) {
  480. furi_string_push_back(buffer, ch);
  481. if(ch == '\n') {
  482. break;
  483. }
  484. }
  485. if(furi_string_start_with(buffer, "Block ")) {
  486. uint32_t row_data_buffer = 0;
  487. char row_data_char_buffer[] = "00000000";
  488. uint32_t row_num_buffer = 0;
  489. char row_num_char_buffer[] = "0";
  490. int length = furi_string_size(buffer);
  491. char ch;
  492. int i = 0;
  493. while(i < length) {
  494. if(furi_string_get_char(buffer, i) == ':') {
  495. row_num_char_buffer[0] = furi_string_get_char(buffer, i - 1); //the number before ":" is block num
  496. i += 2; // skip a space
  497. for(size_t j = 0; j < sizeof(row_data_char_buffer); j++) {
  498. ch = furi_string_get_char(buffer, i);
  499. row_data_char_buffer[j] = ch;
  500. i++;
  501. }
  502. break;
  503. }
  504. i++;
  505. }
  506. sscanf(row_num_char_buffer, "%lx", &row_num_buffer);
  507. sscanf(row_data_char_buffer, "%lx", &row_data_buffer);
  508. model->content[row_num_buffer] = row_data_buffer;
  509. }
  510. }
  511. storage_file_close(data_file);
  512. t5577_writer_update_config_from_load(app);
  513. }
  514. storage_file_free(data_file);
  515. furi_record_close(RECORD_STORAGE);
  516. }
  517. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewSubmenu);
  518. }
  519. /**
  520. * @brief Callback when item in configuration screen is clicked.
  521. * @details This function is called when user clicks OK on an item in the configuration screen.
  522. * If the item clicked is our text field then we switch to the text input screen.
  523. * @param context The context - T5577WriterApp object.
  524. * @param index - The index of the item that was clicked.
  525. */
  526. static void t5577_writer_view_save_callback(void* context) {
  527. T5577WriterApp* app = (T5577WriterApp*)context;
  528. // Header to display on the text input screen.
  529. text_input_set_header_text(app->text_input, tag_name_entry_text);
  530. // Copy the current name into the temporary buffer.
  531. bool redraw = false;
  532. with_view_model(
  533. app->view_write,
  534. T5577WriterModel * model,
  535. {
  536. strncpy(
  537. app->temp_buffer,
  538. furi_string_get_cstr(model->tag_name_str),
  539. app->temp_buffer_size);
  540. },
  541. redraw);
  542. // Configure the text input. When user enters text and clicks OK, t5577_writer_setting_text_updated be called.
  543. bool clear_previous_text = false;
  544. text_input_set_result_callback(
  545. app->text_input,
  546. t5577_writer_file_saver,
  547. app,
  548. app->temp_buffer,
  549. app->temp_buffer_size,
  550. clear_previous_text);
  551. // Pressing the BACK button will reload the configure screen.
  552. view_set_previous_callback(
  553. text_input_get_view(app->text_input), t5577_writer_navigation_file_callback);
  554. // Show text input dialog.
  555. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewTextInput);
  556. }
  557. /**
  558. * @brief Callback for drawing the game screen.
  559. * @details This function is called when the screen needs to be redrawn, like when the model gets updated.
  560. * @param canvas The canvas to draw on.
  561. * @param model The model - MyModel object.
  562. */
  563. static void t5577_writer_view_write_callback(Canvas* canvas, void* model) {
  564. T5577WriterModel* my_model = (T5577WriterModel*)model;
  565. my_model->content[0] = 0;
  566. my_model->content[0] |= my_model->modulation.mod_page_zero;
  567. my_model->content[0] |= my_model->rf_clock.clock_page_zero;
  568. my_model->content[0] |= (my_model->user_block_num << LFRFID_T5577_MAXBLOCK_SHIFT);
  569. LFRFIDT5577* data = (LFRFIDT5577*)malloc(sizeof(LFRFIDT5577));
  570. data->blocks_to_write = my_model->user_block_num;
  571. for(size_t i = 0; i < data->blocks_to_write; i++) {
  572. data->block[i] = my_model->content[i];
  573. }
  574. data->block[1] = 0x007F9FB6;
  575. data->block[2] = 0xB59FD7F9;
  576. data->block[3] = 0x006BDFAE;
  577. data->block[4] = 0xACB83B53;
  578. t5577_write(data);
  579. free(data);
  580. canvas_draw_line(canvas, 0, 25, 120, 50);
  581. }
  582. /**
  583. * @brief Callback for timer elapsed.
  584. * @details This function is called when the timer is elapsed. We use this to queue a redraw event.
  585. * @param context The context - T5577WriterApp object.
  586. */
  587. static void t5577_writer_view_write_timer_callback(void* context) {
  588. T5577WriterApp* app = (T5577WriterApp*)context;
  589. view_dispatcher_send_custom_event(app->view_dispatcher, T5577WriterEventIdRedrawScreen);
  590. }
  591. /**
  592. * @brief Callback when the user starts the game screen.
  593. * @details This function is called when the user enters the game screen. We start a timer to
  594. * redraw the screen periodically (so the random number is refreshed).
  595. * @param context The context - T5577WriterApp object.
  596. */
  597. static void t5577_writer_view_write_enter_callback(void* context) {
  598. uint32_t period = furi_ms_to_ticks(500);
  599. T5577WriterApp* app = (T5577WriterApp*)context;
  600. furi_assert(app->timer == NULL);
  601. app->timer =
  602. furi_timer_alloc(t5577_writer_view_write_timer_callback, FuriTimerTypePeriodic, context);
  603. furi_timer_start(app->timer, period);
  604. }
  605. /**
  606. * @brief Callback when the user exits the game screen.
  607. * @details This function is called when the user exits the game screen. We stop the timer.
  608. * @param context The context - T5577WriterApp object.
  609. */
  610. static void t5577_writer_view_write_exit_callback(void* context) {
  611. T5577WriterApp* app = (T5577WriterApp*)context;
  612. furi_timer_stop(app->timer);
  613. furi_timer_free(app->timer);
  614. app->timer = NULL;
  615. }
  616. /**
  617. * @brief Callback for custom events.
  618. * @details This function is called when a custom event is sent to the view dispatcher.
  619. * @param event The event id - T5577WriterEventId value.
  620. * @param context The context - T5577WriterApp object.
  621. */
  622. static bool t5577_writer_view_write_custom_event_callback(uint32_t event, void* context) {
  623. T5577WriterApp* app = (T5577WriterApp*)context;
  624. switch(event) {
  625. case T5577WriterEventIdRedrawScreen:
  626. // Redraw screen by passing true to last parameter of with_view_model.
  627. {
  628. bool redraw = true;
  629. with_view_model(
  630. app->view_write, T5577WriterModel * _model, { UNUSED(_model); }, redraw);
  631. return true;
  632. }
  633. case T5577WriterEventIdOkPressed:
  634. // Process the OK button. We go to the saving scene.
  635. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewSubmenu);
  636. return true;
  637. default:
  638. return false;
  639. }
  640. }
  641. /**
  642. * @brief Callback for game screen input.
  643. * @details This function is called when the user presses a button while on the game screen.
  644. * @param event The event - InputEvent object.
  645. * @param context The context - T5577WriterApp object.
  646. * @return true if the event was handled, false otherwise.
  647. */
  648. static bool t5577_writer_view_write_input_callback(InputEvent* event, void* context) {
  649. T5577WriterApp* app = (T5577WriterApp*)context;
  650. if(event->type == InputTypeShort) {
  651. if(event->key == InputKeyOk) {
  652. // We choose to send a custom event when user presses OK button. t5577_writer_custom_event_callback will
  653. // handle our T5577WriterEventIdOkPressed event. We could have just put the code from
  654. // t5577_writer_custom_event_callback here, it's a matter of preference.
  655. view_dispatcher_send_custom_event(app->view_dispatcher, T5577WriterEventIdOkPressed);
  656. return true;
  657. }
  658. }
  659. return false;
  660. }
  661. /**
  662. * @brief Allocate the t5577_writer application.
  663. * @details This function allocates the t5577_writer application resources.
  664. * @return T5577WriterApp object.
  665. */
  666. static T5577WriterApp* t5577_writer_app_alloc() {
  667. T5577WriterApp* app = (T5577WriterApp*)malloc(sizeof(T5577WriterApp));
  668. Gui* gui = furi_record_open(RECORD_GUI);
  669. app->view_dispatcher = view_dispatcher_alloc();
  670. app->dialogs = furi_record_open(RECORD_DIALOGS);
  671. app->file_path = furi_string_alloc();
  672. view_dispatcher_enable_queue(app->view_dispatcher);
  673. view_dispatcher_attach_to_gui(app->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  674. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  675. app->submenu = submenu_alloc();
  676. submenu_add_item(
  677. app->submenu, "Write", T5577WriterSubmenuIndexWrite, t5577_writer_submenu_callback, app);
  678. submenu_add_item(
  679. app->submenu, "Config", T5577WriterSubmenuIndexConfigure, t5577_writer_submenu_callback, app);
  680. submenu_add_item(
  681. app->submenu, "Save", T5577WriterSubmenuIndexSave, t5577_writer_submenu_callback, app);
  682. submenu_add_item(
  683. app->submenu, "Load", T5577WriterSubmenuIndexLoad, t5577_writer_submenu_callback, app);
  684. submenu_add_item(
  685. app->submenu, "About", T5577WriterSubmenuIndexAbout, t5577_writer_submenu_callback, app);
  686. view_set_previous_callback(submenu_get_view(app->submenu), t5577_writer_navigation_exit_callback);
  687. view_dispatcher_add_view(
  688. app->view_dispatcher, T5577WriterViewSubmenu, submenu_get_view(app->submenu));
  689. view_dispatcher_switch_to_view(app->view_dispatcher, T5577WriterViewSubmenu);
  690. app->text_input = text_input_alloc();
  691. view_dispatcher_add_view(
  692. app->view_dispatcher, T5577WriterViewTextInput, text_input_get_view(app->text_input));
  693. app->view_load = view_alloc();
  694. view_set_previous_callback(app->view_load, t5577_writer_navigation_submenu_callback);
  695. view_set_enter_callback(app->view_load, t5577_writer_view_load_callback);
  696. view_set_context(app->view_load, app);
  697. view_dispatcher_add_view(
  698. app->view_dispatcher,
  699. T5577WriterViewLoad,
  700. app->view_load);
  701. app->temp_buffer_size = 32;
  702. app->temp_buffer = (char*)malloc(app->temp_buffer_size);
  703. app->bytes_count = 4;
  704. memset(app->bytes_buffer, 0, sizeof(app->bytes_buffer));
  705. app->byte_input = byte_input_alloc();
  706. view_dispatcher_add_view(
  707. app->view_dispatcher, T5577WriterViewByteInput, byte_input_get_view(app->byte_input));
  708. app->variable_item_list_config = variable_item_list_alloc();
  709. app->view_write = view_alloc();
  710. view_set_draw_callback(app->view_write, t5577_writer_view_write_callback);
  711. view_set_input_callback(app->view_write, t5577_writer_view_write_input_callback);
  712. view_set_previous_callback(app->view_write, t5577_writer_navigation_submenu_callback);
  713. view_set_enter_callback(app->view_write, t5577_writer_view_write_enter_callback);
  714. view_set_exit_callback(app->view_write, t5577_writer_view_write_exit_callback);
  715. view_set_context(app->view_write, app);
  716. view_set_custom_callback(app->view_write, t5577_writer_view_write_custom_event_callback);
  717. view_allocate_model(app->view_write, ViewModelTypeLockFree, sizeof(T5577WriterModel));
  718. view_dispatcher_add_view(app->view_dispatcher, T5577WriterViewWrite, app->view_write);
  719. T5577WriterModel* model = view_get_model(app->view_write); // initialize model
  720. FuriString* tag_name_str = furi_string_alloc();
  721. furi_string_set_str(tag_name_str, tag_name_default_value);
  722. model->tag_name_str = tag_name_str;
  723. initialize_model(model);
  724. initialize_rf_clock_choices(rf_clock_choices);
  725. initialize_mod_names(modulation_names);
  726. app->view_save = view_alloc();
  727. view_set_previous_callback(app->view_save, t5577_writer_navigation_submenu_callback);
  728. view_set_enter_callback(app->view_save, t5577_writer_view_save_callback);
  729. view_set_context(app->view_save, app);
  730. view_dispatcher_add_view(
  731. app->view_dispatcher,
  732. T5577WriterViewSave,
  733. app->view_save);
  734. app->variable_item_list_config = variable_item_list_alloc();
  735. app->config_enter_callback = t5577_writer_config_enter_callback;
  736. app->view_config_e = view_alloc();
  737. view_set_previous_callback(
  738. app->view_config_e,
  739. t5577_writer_navigation_submenu_callback);
  740. view_set_enter_callback(app->view_config_e, t5577_writer_config_enter_callback);
  741. view_set_context(app->view_config_e, app);
  742. view_dispatcher_add_view(
  743. app->view_dispatcher,
  744. T5577WriterViewConfigure_e,
  745. app->view_config_e);
  746. View* view_buffer = view_alloc();
  747. view_dispatcher_add_view(
  748. app->view_dispatcher,
  749. T5577WriterViewConfigure_i,
  750. view_buffer);
  751. app->widget_about = widget_alloc();
  752. widget_add_text_scroll_element(
  753. app->widget_about,
  754. 0,
  755. 0,
  756. 128,
  757. 64,
  758. "T5577 Writer v0.1");
  759. view_set_previous_callback(
  760. widget_get_view(app->widget_about), t5577_writer_navigation_submenu_callback);
  761. view_dispatcher_add_view(
  762. app->view_dispatcher, T5577WriterViewAbout, widget_get_view(app->widget_about));
  763. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  764. #ifdef BACKLIGHT_ON
  765. notification_message(app->notifications, &sequence_display_backlight_enforce_on);
  766. #endif
  767. return app;
  768. }
  769. /**
  770. * @brief Free the t5577_writer application.
  771. * @details This function frees the t5577_writer application resources.
  772. * @param app The t5577_writer application object.
  773. */
  774. static void t5577_writer_app_free(T5577WriterApp* app) {
  775. #ifdef BACKLIGHT_ON
  776. notification_message(app->notifications, &sequence_display_backlight_enforce_auto);
  777. #endif
  778. furi_record_close(RECORD_NOTIFICATION);
  779. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewTextInput);
  780. text_input_free(app->text_input);
  781. free(app->temp_buffer);
  782. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewAbout);
  783. widget_free(app->widget_about);
  784. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewWrite);
  785. with_view_model(
  786. app->view_write,
  787. T5577WriterModel * model,
  788. {
  789. if(model->content != NULL) {
  790. free(model->content);
  791. }
  792. },
  793. false);
  794. view_free(app->view_write);
  795. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewLoad);
  796. view_free(app->view_load);
  797. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewConfigure_i);
  798. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewConfigure_e);
  799. variable_item_list_free(app->variable_item_list_config);
  800. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewSave);
  801. view_dispatcher_remove_view(app->view_dispatcher,T5577WriterViewByteInput);
  802. view_free(app->view_save);
  803. view_dispatcher_remove_view(app->view_dispatcher, T5577WriterViewSubmenu);
  804. submenu_free(app->submenu);
  805. view_dispatcher_free(app->view_dispatcher);
  806. furi_record_close(RECORD_GUI);
  807. free(app);
  808. }
  809. /**
  810. * @brief Main function for t5577_writer application.
  811. * @details This function is the entry point for the t5577_writer application. It should be defined in
  812. * application.fam as the entry_point setting.
  813. * @param _p Input parameter - unused
  814. * @return 0 - Success
  815. */
  816. int32_t main_t5577_writer_app(void* _p) {
  817. UNUSED(_p);
  818. T5577WriterApp* app = t5577_writer_app_alloc();
  819. view_dispatcher_run(app->view_dispatcher);
  820. t5577_writer_app_free(app);
  821. return 0;
  822. }