key_copier.c 28 KB

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