key_copier.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  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/submenu.h>
  7. #include <gui/modules/text_input.h>
  8. #include <gui/modules/widget.h>
  9. #include <gui/modules/variable_item_list.h>
  10. #include <notification/notification.h>
  11. #include <notification/notification_messages.h>
  12. #include <applications/services/storage/storage.h>
  13. #include <applications/services/dialogs/dialogs.h>
  14. #include <stdbool.h>
  15. #include <math.h>
  16. #include <flipper_format.h>
  17. #include "key_copier_icons.h"
  18. #include "key_formats.h"
  19. #include "key_copier.h"
  20. #define TAG "KeyCopier"
  21. #define BACKLIGHT_ON 1
  22. typedef enum {
  23. KeyCopierSubmenuIndexMeasure,
  24. KeyCopierSubmenuIndexConfigure,
  25. KeyCopierSubmenuIndexSave,
  26. KeyCopierSubmenuIndexLoad,
  27. KeyCopierSubmenuIndexAbout,
  28. } KeyCopierSubmenuIndex;
  29. typedef enum {
  30. KeyCopierViewSubmenu,
  31. KeyCopierViewTextInput,
  32. KeyCopierViewConfigure_i,
  33. KeyCopierViewConfigure_e,
  34. KeyCopierViewSave,
  35. KeyCopierViewLoad,
  36. KeyCopierViewMeasure,
  37. KeyCopierViewAbout,
  38. } KeyCopierView;
  39. typedef struct {
  40. ViewDispatcher* view_dispatcher;
  41. NotificationApp* notifications;
  42. Submenu* submenu;
  43. TextInput* text_input;
  44. VariableItemList* variable_item_list_config;
  45. View* view_measure;
  46. View* view_config_e;
  47. View* view_save;
  48. View* view_load;
  49. Widget* widget_about;
  50. VariableItem* key_name_item;
  51. VariableItem* format_item;
  52. char* temp_buffer;
  53. uint32_t temp_buffer_size;
  54. DialogsApp* dialogs;
  55. FuriString* file_path;
  56. } KeyCopierApp;
  57. typedef struct {
  58. uint32_t format_index;
  59. FuriString* key_name_str;
  60. uint8_t pin_slc; // The pin that is being adjusted
  61. uint8_t* depth; // The cutting depth
  62. bool data_loaded;
  63. KeyFormat format;
  64. } KeyCopierModel;
  65. void initialize_model(KeyCopierModel* model) {
  66. if(model->depth != NULL) {
  67. free(model->depth);
  68. }
  69. model->format_index = 0;
  70. memcpy(&model->format, &all_formats[model->format_index], sizeof(KeyFormat));
  71. model->depth = (uint8_t*)malloc((model->format.pin_num + 1) * sizeof(uint8_t));
  72. for(uint8_t i = 0; i <= model->format.pin_num; i++) {
  73. model->depth[i] = model->format.min_depth_ind;
  74. }
  75. model->pin_slc = 1;
  76. model->data_loaded = 0;
  77. model->key_name_str = furi_string_alloc();
  78. }
  79. static uint32_t key_copier_navigation_exit_callback(void* _context) {
  80. UNUSED(_context);
  81. return VIEW_NONE;
  82. }
  83. static uint32_t key_copier_navigation_submenu_callback(void* _context) {
  84. UNUSED(_context);
  85. return KeyCopierViewSubmenu;
  86. }
  87. static void key_copier_submenu_callback(void* context, uint32_t index) {
  88. KeyCopierApp* app = (KeyCopierApp*)context;
  89. switch(index) {
  90. case KeyCopierSubmenuIndexMeasure:
  91. view_dispatcher_switch_to_view(app->view_dispatcher, KeyCopierViewMeasure);
  92. break;
  93. case KeyCopierSubmenuIndexConfigure:
  94. view_dispatcher_switch_to_view(app->view_dispatcher, KeyCopierViewConfigure_e);
  95. break;
  96. case KeyCopierSubmenuIndexSave:
  97. view_dispatcher_switch_to_view(app->view_dispatcher, KeyCopierViewSave);
  98. break;
  99. case KeyCopierSubmenuIndexLoad:
  100. view_dispatcher_switch_to_view(app->view_dispatcher, KeyCopierViewLoad);
  101. break;
  102. case KeyCopierSubmenuIndexAbout:
  103. view_dispatcher_switch_to_view(app->view_dispatcher, KeyCopierViewAbout);
  104. break;
  105. default:
  106. break;
  107. }
  108. }
  109. char* manufacturers[COUNT_OF(all_formats)];
  110. void initialize_manufacturers(char** manufacturers) {
  111. // Populate the manufacturers array
  112. for(size_t i = 0; i < COUNT_OF(all_formats); i++) {
  113. manufacturers[i] = all_formats[i].manufacturer;
  114. }
  115. }
  116. static void key_copier_format_change(VariableItem* item) {
  117. KeyCopierApp* app = variable_item_get_context(item);
  118. KeyCopierModel* model = view_get_model(app->view_measure);
  119. if(model->data_loaded) {
  120. variable_item_set_current_value_index(item, model->format_index);
  121. }
  122. uint8_t format_index = variable_item_get_current_value_index(item);
  123. if(format_index != model->format_index) {
  124. model->format_index = format_index;
  125. model->format = all_formats[format_index];
  126. if(model->depth != NULL) {
  127. free(model->depth);
  128. }
  129. model->depth = (uint8_t*)malloc((model->format.pin_num + 1) * sizeof(uint8_t));
  130. for(uint8_t i = 0; i <= model->format.pin_num; i++) {
  131. model->depth[i] = model->format.min_depth_ind;
  132. }
  133. model->pin_slc = 1;
  134. }
  135. model->data_loaded = false;
  136. variable_item_set_current_value_text(item, model->format.format_name);
  137. model->format = all_formats[model->format_index];
  138. }
  139. static const char* format_config_label = "Key Format";
  140. static void key_copier_config_enter_callback(void* context) {
  141. KeyCopierApp* app = (KeyCopierApp*)context;
  142. KeyCopierModel* my_model = view_get_model(app->view_measure);
  143. variable_item_list_reset(app->variable_item_list_config);
  144. // Recreate this view every time we enter it so that it's always updated
  145. app->format_item = variable_item_list_add(
  146. app->variable_item_list_config,
  147. format_config_label,
  148. COUNT_OF(all_formats),
  149. key_copier_format_change,
  150. app);
  151. View* view_config_i = variable_item_list_get_view(app->variable_item_list_config);
  152. variable_item_set_current_value_index(app->format_item, my_model->format_index);
  153. key_copier_format_change(app->format_item);
  154. view_set_previous_callback(view_config_i, key_copier_navigation_submenu_callback);
  155. view_dispatcher_remove_view(
  156. app->view_dispatcher, KeyCopierViewConfigure_i); // delete the last one
  157. view_dispatcher_add_view(app->view_dispatcher, KeyCopierViewConfigure_i, view_config_i);
  158. view_dispatcher_switch_to_view(app->view_dispatcher, KeyCopierViewConfigure_i); // recreate it
  159. }
  160. static const char* key_name_entry_text = "Enter name";
  161. static void key_copier_file_saver(void* context) {
  162. KeyCopierApp* app = (KeyCopierApp*)context;
  163. KeyCopierModel* model = view_get_model(app->view_measure);
  164. bool redraw = true;
  165. with_view_model(
  166. app->view_measure,
  167. KeyCopierModel * model,
  168. { furi_string_set(model->key_name_str, app->temp_buffer); },
  169. redraw);
  170. FuriString* file_path = furi_string_alloc();
  171. furi_string_printf(
  172. file_path,
  173. "%s/%s%s",
  174. STORAGE_APP_DATA_PATH_PREFIX,
  175. furi_string_get_cstr(model->key_name_str),
  176. KEY_COPIER_FILE_EXTENSION);
  177. Storage* storage = furi_record_open(RECORD_STORAGE);
  178. storage_simply_mkdir(storage, STORAGE_APP_DATA_PATH_PREFIX);
  179. FURI_LOG_D(TAG, "mkdir finished");
  180. FlipperFormat* flipper_format = flipper_format_file_alloc(storage);
  181. do {
  182. const uint32_t version = 1;
  183. const uint32_t pin_num_buffer = (uint32_t)model->format.pin_num;
  184. const uint32_t macs_buffer = (uint32_t)model->format.macs;
  185. FuriString* buffer = furi_string_alloc();
  186. if(!flipper_format_file_open_always(flipper_format, furi_string_get_cstr(file_path)))
  187. break;
  188. if(!flipper_format_write_header_cstr(flipper_format, "Flipper Key Copier File", version))
  189. break;
  190. if(!flipper_format_write_string_cstr(
  191. flipper_format, "Manufacturer", model->format.manufacturer))
  192. break;
  193. if(!flipper_format_write_string_cstr(
  194. flipper_format, "Format Name", model->format.format_name))
  195. break;
  196. if(!flipper_format_write_string_cstr(
  197. flipper_format, "Data Sheet", model->format.format_link))
  198. break;
  199. if(!flipper_format_write_uint32(flipper_format, "Number of Pins", &pin_num_buffer, 1))
  200. break;
  201. if(!flipper_format_write_uint32(
  202. flipper_format, "Maximum Adjacent Cut Specification (MACS)", &macs_buffer, 1))
  203. break;
  204. for(int i = 0; i < model->format.pin_num; i++) {
  205. if(i < model->format.pin_num - 1) {
  206. furi_string_cat_printf(buffer, "%d-", model->depth[i]);
  207. } else {
  208. furi_string_cat_printf(buffer, "%d", model->depth[i]);
  209. }
  210. }
  211. if(!flipper_format_write_string(flipper_format, "Bitting Pattern", buffer)) break;
  212. furi_string_free(buffer);
  213. // signal that the file was written successfully
  214. } while(0);
  215. flipper_format_free(flipper_format);
  216. view_dispatcher_switch_to_view(app->view_dispatcher, KeyCopierViewSubmenu);
  217. }
  218. static void key_copier_view_save_callback(void* context) {
  219. KeyCopierApp* app = (KeyCopierApp*)context;
  220. // Header to display on the text input screen.
  221. text_input_set_header_text(app->text_input, key_name_entry_text);
  222. // Copy the current name into the temporary buffer.
  223. bool redraw = false;
  224. with_view_model(
  225. app->view_measure,
  226. KeyCopierModel * model,
  227. {
  228. strncpy(
  229. app->temp_buffer,
  230. furi_string_get_cstr(model->key_name_str),
  231. app->temp_buffer_size);
  232. },
  233. redraw);
  234. // Configure the text input. When user enters text and clicks OK, key_copier_file_saver be called.
  235. bool clear_previous_text = false;
  236. text_input_set_result_callback(
  237. app->text_input,
  238. key_copier_file_saver,
  239. app,
  240. app->temp_buffer,
  241. app->temp_buffer_size,
  242. clear_previous_text);
  243. view_set_previous_callback(
  244. text_input_get_view(app->text_input), key_copier_navigation_submenu_callback);
  245. // Show text input dialog.
  246. view_dispatcher_switch_to_view(app->view_dispatcher, KeyCopierViewTextInput);
  247. }
  248. static void key_copier_view_load_callback(void* context) {
  249. KeyCopierApp* app = (KeyCopierApp*)context;
  250. KeyCopierModel* model = view_get_model(app->view_measure);
  251. DialogsFileBrowserOptions browser_options;
  252. Storage* storage = furi_record_open(RECORD_STORAGE);
  253. storage_simply_mkdir(storage, STORAGE_APP_DATA_PATH_PREFIX);
  254. dialog_file_browser_set_basic_options(&browser_options, KEY_COPIER_FILE_EXTENSION, &I_icon);
  255. browser_options.base_path = STORAGE_APP_DATA_PATH_PREFIX;
  256. furi_string_set(app->file_path, browser_options.base_path);
  257. if(dialog_file_browser_show(app->dialogs, app->file_path, app->file_path, &browser_options)) {
  258. FlipperFormat* flipper_format = flipper_format_file_alloc(storage);
  259. do {
  260. if(!flipper_format_file_open_existing(
  261. flipper_format, furi_string_get_cstr(app->file_path)))
  262. break;
  263. FuriString* format_buffer = furi_string_alloc();
  264. FuriString* depth_buffer = furi_string_alloc();
  265. if(!flipper_format_read_string(flipper_format, "Format Name", format_buffer)) break;
  266. if(!flipper_format_read_string(flipper_format, "Bitting Pattern", depth_buffer)) break;
  267. for(size_t i = 0; i < COUNT_OF(all_formats); i++) {
  268. if(!strcmp(furi_string_get_cstr(format_buffer), all_formats[i].format_name)) {
  269. model->format_index = (uint32_t)i;
  270. model->format = all_formats[model->format_index];
  271. }
  272. }
  273. for(int i = 0; i < model->format.pin_num; i++) {
  274. model->depth[i] = (uint8_t)(furi_string_get_char(depth_buffer, i * 2) - '0');
  275. }
  276. model->data_loaded = true;
  277. // signal that the file was read successfully
  278. } while(0);
  279. flipper_format_free(flipper_format);
  280. furi_record_close(RECORD_STORAGE);
  281. }
  282. view_dispatcher_switch_to_view(app->view_dispatcher, KeyCopierViewSubmenu);
  283. }
  284. static void key_copier_view_measure_draw_callback(Canvas* canvas, void* model) {
  285. static double inches_per_px = (double)INCHES_PER_PX;
  286. canvas_set_bitmap_mode(canvas, true);
  287. KeyCopierModel* my_model = (KeyCopierModel*)model;
  288. KeyFormat my_format = my_model->format;
  289. FuriString* buffer = furi_string_alloc();
  290. int pin_half_width_px = (int)round((my_format.pin_width_inch / inches_per_px) / 2);
  291. int pin_step_px = (int)round(my_format.pin_increment_inch / inches_per_px);
  292. double drill_radians =
  293. (180 - my_format.drill_angle) / 2 / 180 * (double)M_PI; // Convert angle to radians
  294. double tangent = tan(drill_radians);
  295. int top_contour_px = (int)round(63 - my_format.uncut_depth_inch / inches_per_px);
  296. int post_extra_x_px = 0;
  297. int pre_extra_x_px = 0;
  298. for(int current_pin = 1; current_pin <= my_model->format.pin_num; current_pin += 1) {
  299. double current_center_px =
  300. my_format.first_pin_inch + (current_pin - 1) * my_format.pin_increment_inch;
  301. int pin_center_px = (int)round(current_center_px / inches_per_px);
  302. furi_string_printf(buffer, "%d", my_model->depth[current_pin - 1]);
  303. canvas_draw_str_aligned(
  304. canvas,
  305. pin_center_px,
  306. top_contour_px - 12,
  307. AlignCenter,
  308. AlignCenter,
  309. furi_string_get_cstr(buffer));
  310. canvas_draw_line(
  311. canvas,
  312. pin_center_px,
  313. top_contour_px - 5,
  314. pin_center_px,
  315. top_contour_px); // the vertical line to indicate pin center
  316. int current_depth = my_model->depth[current_pin - 1] - my_format.min_depth_ind;
  317. int current_depth_px =
  318. (int)round(current_depth * my_format.depth_step_inch / inches_per_px);
  319. canvas_draw_line(
  320. canvas,
  321. pin_center_px - pin_half_width_px,
  322. top_contour_px + current_depth_px,
  323. pin_center_px + pin_half_width_px,
  324. top_contour_px + current_depth_px); // draw pin width horizontal line
  325. int last_depth = my_model->depth[current_pin - 2] - my_format.min_depth_ind;
  326. int next_depth = my_model->depth[current_pin] - my_format.min_depth_ind;
  327. if(current_pin == 1) {
  328. canvas_draw_line(
  329. canvas,
  330. 0,
  331. top_contour_px,
  332. pin_center_px - pin_half_width_px - current_depth_px,
  333. top_contour_px);
  334. last_depth = 0;
  335. pre_extra_x_px = max(current_depth_px + pin_half_width_px, 0);
  336. }
  337. if(current_pin == my_model->format.pin_num) {
  338. next_depth = 0;
  339. }
  340. if((last_depth + current_depth) > my_format.clearance) { //yes intersection
  341. if(current_pin != 1) {
  342. pre_extra_x_px =
  343. min(max(pin_step_px - post_extra_x_px, pin_half_width_px),
  344. pin_step_px - pin_half_width_px);
  345. }
  346. canvas_draw_line(
  347. canvas,
  348. pin_center_px - pre_extra_x_px,
  349. top_contour_px +
  350. max((int)round(
  351. (current_depth_px - (pre_extra_x_px - pin_half_width_px)) * tangent),
  352. 0),
  353. pin_center_px - pin_half_width_px,
  354. top_contour_px + (int)round(current_depth_px * tangent));
  355. } else {
  356. int last_depth_px = (int)round(last_depth * my_format.depth_step_inch / inches_per_px);
  357. int down_slope_start_x_px = pin_center_px - pin_half_width_px - current_depth_px;
  358. canvas_draw_line(
  359. canvas,
  360. pin_center_px - pin_half_width_px - current_depth_px,
  361. top_contour_px,
  362. pin_center_px - pin_half_width_px,
  363. top_contour_px + (int)round(current_depth_px * tangent));
  364. canvas_draw_line(
  365. canvas,
  366. min(pin_center_px - pin_step_px + pin_half_width_px + last_depth_px,
  367. down_slope_start_x_px),
  368. top_contour_px,
  369. down_slope_start_x_px,
  370. top_contour_px);
  371. }
  372. if((current_depth + next_depth) > my_format.clearance) { //yes intersection
  373. double numerator = (double)current_depth;
  374. double denominator = (double)(current_depth + next_depth);
  375. double product = (numerator / denominator) * pin_step_px;
  376. post_extra_x_px =
  377. (int)min(max(product, pin_half_width_px), pin_step_px - pin_half_width_px);
  378. canvas_draw_line(
  379. canvas,
  380. pin_center_px + pin_half_width_px,
  381. top_contour_px + current_depth_px,
  382. pin_center_px + post_extra_x_px,
  383. top_contour_px +
  384. max(current_depth_px -
  385. (int)round((post_extra_x_px - pin_half_width_px) * tangent),
  386. 0));
  387. } else { // no intersection
  388. canvas_draw_line(
  389. canvas,
  390. pin_center_px + pin_half_width_px,
  391. top_contour_px + (int)round(current_depth_px * tangent),
  392. pin_center_px + pin_half_width_px + current_depth_px,
  393. top_contour_px);
  394. }
  395. }
  396. int level_contour_px =
  397. (int)round((my_format.last_pin_inch + my_format.elbow_inch) / inches_per_px);
  398. int elbow_px = (int)round(my_format.elbow_inch / inches_per_px);
  399. canvas_draw_line(canvas, 0, 62, level_contour_px, 62);
  400. canvas_draw_line(canvas, level_contour_px, 62, level_contour_px + elbow_px, 62 - elbow_px);
  401. int slc_pin_px = (int)round(
  402. (my_format.first_pin_inch + (my_model->pin_slc - 1) * my_format.pin_increment_inch) /
  403. inches_per_px);
  404. canvas_draw_icon(canvas, slc_pin_px - 2, top_contour_px - 25, &I_arrow_down);
  405. furi_string_printf(buffer, "%s", my_format.format_name);
  406. canvas_draw_str(canvas, 110, 10, furi_string_get_cstr(buffer));
  407. furi_string_free(buffer);
  408. }
  409. static bool key_copier_view_measure_input_callback(InputEvent* event, void* context) {
  410. KeyCopierApp* app = (KeyCopierApp*)context;
  411. if(event->type == InputTypeShort) {
  412. switch(event->key) {
  413. case InputKeyLeft: {
  414. bool redraw = true;
  415. with_view_model(
  416. app->view_measure,
  417. KeyCopierModel * model,
  418. {
  419. if(model->pin_slc > 1) {
  420. model->pin_slc--;
  421. }
  422. },
  423. redraw);
  424. break;
  425. }
  426. case InputKeyRight: {
  427. bool redraw = true;
  428. with_view_model(
  429. app->view_measure,
  430. KeyCopierModel * model,
  431. {
  432. if(model->pin_slc < model->format.pin_num) {
  433. model->pin_slc++;
  434. }
  435. },
  436. redraw);
  437. break;
  438. }
  439. case InputKeyUp: {
  440. bool redraw = true;
  441. with_view_model(
  442. app->view_measure,
  443. KeyCopierModel * model,
  444. {
  445. if(model->depth[model->pin_slc - 1] > model->format.min_depth_ind) {
  446. if(model->pin_slc == 1) { //first pin only limited by the next one
  447. if(model->depth[model->pin_slc] - model->depth[model->pin_slc - 1] <
  448. model->format.macs)
  449. model->depth[model->pin_slc - 1]--;
  450. } else if(
  451. model->pin_slc ==
  452. model->format.pin_num) { //last pin only limited by the previous one
  453. if(model->depth[model->pin_slc - 2] -
  454. model->depth[model->pin_slc - 1] <
  455. model->format.macs) {
  456. model->depth[model->pin_slc - 1]--;
  457. }
  458. } else {
  459. if(model->depth[model->pin_slc] - model->depth[model->pin_slc - 1] <
  460. model->format.macs &&
  461. model->depth[model->pin_slc - 2] -
  462. model->depth[model->pin_slc - 1] <
  463. model->format.macs) {
  464. model->depth[model->pin_slc - 1]--;
  465. }
  466. }
  467. }
  468. },
  469. redraw);
  470. break;
  471. }
  472. case InputKeyDown: {
  473. bool redraw = true;
  474. with_view_model(
  475. app->view_measure,
  476. KeyCopierModel * model,
  477. {
  478. if(model->depth[model->pin_slc - 1] < model->format.max_depth_ind) {
  479. if(model->pin_slc == 1) { //first pin only limited by the next one
  480. if(model->depth[model->pin_slc - 1] - model->depth[model->pin_slc] <
  481. model->format.macs)
  482. model->depth[model->pin_slc - 1]++;
  483. } else if(
  484. model->pin_slc ==
  485. model->format.pin_num) { //last pin only limited by the previous one
  486. if(model->depth[model->pin_slc - 1] -
  487. model->depth[model->pin_slc - 2] <
  488. model->format.macs) {
  489. model->depth[model->pin_slc - 1]++;
  490. }
  491. } else {
  492. if(model->depth[model->pin_slc - 1] - model->depth[model->pin_slc] <
  493. model->format.macs &&
  494. model->depth[model->pin_slc - 1] -
  495. model->depth[model->pin_slc - 2] <
  496. model->format.macs) {
  497. model->depth[model->pin_slc - 1]++;
  498. }
  499. }
  500. }
  501. },
  502. redraw);
  503. break;
  504. }
  505. default:
  506. // Handle other keys or do nothing
  507. break;
  508. }
  509. }
  510. return false;
  511. }
  512. static KeyCopierApp* key_copier_app_alloc() {
  513. KeyCopierApp* app = (KeyCopierApp*)malloc(sizeof(KeyCopierApp));
  514. Gui* gui = furi_record_open(RECORD_GUI);
  515. app->view_dispatcher = view_dispatcher_alloc();
  516. view_dispatcher_attach_to_gui(app->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  517. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  518. app->dialogs = furi_record_open(RECORD_DIALOGS);
  519. app->file_path = furi_string_alloc();
  520. app->submenu = submenu_alloc();
  521. submenu_add_item(
  522. app->submenu, "Measure", KeyCopierSubmenuIndexMeasure, key_copier_submenu_callback, app);
  523. submenu_add_item(
  524. app->submenu, "Config", KeyCopierSubmenuIndexConfigure, key_copier_submenu_callback, app);
  525. submenu_add_item(
  526. app->submenu, "Save", KeyCopierSubmenuIndexSave, key_copier_submenu_callback, app);
  527. submenu_add_item(
  528. app->submenu, "Load", KeyCopierSubmenuIndexLoad, key_copier_submenu_callback, app);
  529. submenu_add_item(
  530. app->submenu, "About", KeyCopierSubmenuIndexAbout, key_copier_submenu_callback, app);
  531. view_set_previous_callback(
  532. submenu_get_view(app->submenu), key_copier_navigation_exit_callback);
  533. view_dispatcher_add_view(
  534. app->view_dispatcher, KeyCopierViewSubmenu, submenu_get_view(app->submenu));
  535. view_dispatcher_switch_to_view(app->view_dispatcher, KeyCopierViewSubmenu);
  536. app->text_input = text_input_alloc();
  537. view_dispatcher_add_view(
  538. app->view_dispatcher, KeyCopierViewTextInput, text_input_get_view(app->text_input));
  539. app->temp_buffer_size = 32;
  540. app->temp_buffer = (char*)malloc(app->temp_buffer_size);
  541. app->temp_buffer = "";
  542. app->view_measure = view_alloc();
  543. view_set_draw_callback(app->view_measure, key_copier_view_measure_draw_callback);
  544. view_set_input_callback(app->view_measure, key_copier_view_measure_input_callback);
  545. view_set_previous_callback(app->view_measure, key_copier_navigation_submenu_callback);
  546. view_set_context(app->view_measure, app);
  547. view_allocate_model(app->view_measure, ViewModelTypeLockFree, sizeof(KeyCopierModel));
  548. KeyCopierModel* model = view_get_model(app->view_measure);
  549. initialize_model(model);
  550. view_dispatcher_add_view(app->view_dispatcher, KeyCopierViewMeasure, app->view_measure);
  551. app->variable_item_list_config = variable_item_list_alloc();
  552. app->view_config_e = view_alloc();
  553. view_set_context(app->view_config_e, app);
  554. view_set_previous_callback(app->view_config_e, key_copier_navigation_submenu_callback);
  555. view_set_enter_callback(app->view_config_e, key_copier_config_enter_callback);
  556. view_dispatcher_add_view(app->view_dispatcher, KeyCopierViewConfigure_e, app->view_config_e);
  557. View* view_buffer = view_alloc();
  558. view_dispatcher_add_view(app->view_dispatcher, KeyCopierViewConfigure_i, view_buffer);
  559. app->view_save = view_alloc();
  560. view_set_context(app->view_save, app);
  561. view_set_enter_callback(app->view_save, key_copier_view_save_callback);
  562. view_set_previous_callback(app->view_save, key_copier_navigation_submenu_callback);
  563. view_dispatcher_add_view(app->view_dispatcher, KeyCopierViewSave, app->view_save);
  564. app->view_load = view_alloc();
  565. view_set_context(app->view_load, app);
  566. view_set_enter_callback(app->view_load, key_copier_view_load_callback);
  567. view_set_previous_callback(app->view_load, key_copier_navigation_submenu_callback);
  568. view_dispatcher_add_view(app->view_dispatcher, KeyCopierViewLoad, app->view_load);
  569. app->widget_about = widget_alloc();
  570. widget_add_text_scroll_element(
  571. app->widget_about,
  572. 0,
  573. 0,
  574. 128,
  575. 64,
  576. "Key Maker App 1.0\nAuthor: @Torron\n\nTo measure your key:\n\n1. Place it on top of the screen.\n\n2. Use the contour to align your key.\n\n3. Adjust each pin's depth until they match. It's easier if you look with one eye closed.\n\nGithub: github.com/zinongli/KeyCopier \n\nSpecial thanks to Derek Jamison's Skeleton App Template.");
  577. view_set_previous_callback(
  578. widget_get_view(app->widget_about), key_copier_navigation_submenu_callback);
  579. view_dispatcher_add_view(
  580. app->view_dispatcher, KeyCopierViewAbout, widget_get_view(app->widget_about));
  581. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  582. #ifdef BACKLIGHT_ON
  583. notification_message(app->notifications, &sequence_display_backlight_enforce_on);
  584. #endif
  585. return app;
  586. }
  587. static void key_copier_app_free(KeyCopierApp* app) {
  588. #ifdef BACKLIGHT_ON
  589. notification_message(app->notifications, &sequence_display_backlight_enforce_auto);
  590. #endif
  591. furi_record_close(RECORD_NOTIFICATION);
  592. view_dispatcher_remove_view(app->view_dispatcher, KeyCopierViewTextInput);
  593. text_input_free(app->text_input);
  594. free(app->temp_buffer);
  595. view_dispatcher_remove_view(app->view_dispatcher, KeyCopierViewAbout);
  596. widget_free(app->widget_about);
  597. view_dispatcher_remove_view(app->view_dispatcher, KeyCopierViewMeasure);
  598. with_view_model(
  599. app->view_measure,
  600. KeyCopierModel * model,
  601. {
  602. if(model->depth != NULL) {
  603. free(model->depth);
  604. }
  605. },
  606. false);
  607. view_free(app->view_measure);
  608. view_dispatcher_remove_view(app->view_dispatcher, KeyCopierViewConfigure_e);
  609. view_dispatcher_remove_view(app->view_dispatcher, KeyCopierViewConfigure_i);
  610. view_dispatcher_remove_view(app->view_dispatcher, KeyCopierViewSave);
  611. view_dispatcher_remove_view(app->view_dispatcher, KeyCopierViewLoad);
  612. variable_item_list_free(app->variable_item_list_config);
  613. view_dispatcher_remove_view(app->view_dispatcher, KeyCopierViewSubmenu);
  614. submenu_free(app->submenu);
  615. view_dispatcher_free(app->view_dispatcher);
  616. furi_record_close(RECORD_GUI);
  617. free(app);
  618. }
  619. int32_t main_key_copier_app(void* _p) {
  620. UNUSED(_p);
  621. KeyCopierApp* app = key_copier_app_alloc();
  622. view_dispatcher_run(app->view_dispatcher);
  623. key_copier_app_free(app);
  624. return 0;
  625. }