key_copier.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800
  1. #include "key_copier.h"
  2. #include "key_copier_icons.h"
  3. #include "key_formats.h"
  4. #include <applications/services/dialogs/dialogs.h>
  5. #include <applications/services/storage/storage.h>
  6. #include <flipper_format.h>
  7. #include <furi.h>
  8. #include <furi_hal.h>
  9. #include <gui/gui.h>
  10. #include <gui/modules/submenu.h>
  11. #include <gui/modules/text_input.h>
  12. #include <gui/modules/variable_item_list.h>
  13. #include <gui/modules/widget.h>
  14. #include <gui/view.h>
  15. #include <gui/view_dispatcher.h>
  16. #include <math.h>
  17. #include <notification/notification.h>
  18. #include <notification/notification_messages.h>
  19. #include <stdbool.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,
  157. KeyCopierViewConfigure_i); // delete the last one
  158. view_dispatcher_add_view(app->view_dispatcher, KeyCopierViewConfigure_i, view_config_i);
  159. view_dispatcher_switch_to_view(app->view_dispatcher,
  160. KeyCopierViewConfigure_i); // recreate it
  161. }
  162. static const char* key_name_entry_text = "Enter name";
  163. static void key_copier_file_saver(void* context) {
  164. KeyCopierApp* app = (KeyCopierApp*)context;
  165. KeyCopierModel* model = view_get_model(app->view_measure);
  166. bool redraw = true;
  167. with_view_model(
  168. app->view_measure,
  169. KeyCopierModel * model,
  170. { furi_string_set(model->key_name_str, app->temp_buffer); },
  171. redraw);
  172. FuriString* file_path = furi_string_alloc();
  173. furi_string_printf(
  174. file_path,
  175. "%s/%s%s",
  176. STORAGE_APP_DATA_PATH_PREFIX,
  177. furi_string_get_cstr(model->key_name_str),
  178. KEY_COPIER_FILE_EXTENSION);
  179. Storage* storage = furi_record_open(RECORD_STORAGE);
  180. storage_simply_mkdir(storage, STORAGE_APP_DATA_PATH_PREFIX);
  181. FURI_LOG_D(TAG, "mkdir finished");
  182. FlipperFormat* flipper_format = flipper_format_file_alloc(storage);
  183. do {
  184. const uint32_t version = 1;
  185. const uint32_t pin_num_buffer = (uint32_t)model->format.pin_num;
  186. const uint32_t macs_buffer = (uint32_t)model->format.macs;
  187. FuriString* buffer = furi_string_alloc();
  188. if(!flipper_format_file_open_always(flipper_format, furi_string_get_cstr(file_path)))
  189. break;
  190. if(!flipper_format_write_header_cstr(flipper_format, "Flipper Key Copier File", version))
  191. break;
  192. if(!flipper_format_write_string_cstr(
  193. flipper_format, "Manufacturer", model->format.manufacturer))
  194. break;
  195. if(!flipper_format_write_string_cstr(
  196. flipper_format, "Format Name", model->format.format_name))
  197. break;
  198. if(!flipper_format_write_string_cstr(
  199. flipper_format, "Data Sheet", model->format.format_link))
  200. break;
  201. if(!flipper_format_write_uint32(flipper_format, "Number of Pins", &pin_num_buffer, 1))
  202. break;
  203. if(!flipper_format_write_uint32(
  204. flipper_format, "Maximum Adjacent Cut Specification (MACS)", &macs_buffer, 1))
  205. break;
  206. for(int i = 0; i < model->format.pin_num; i++) {
  207. if(i < model->format.pin_num - 1) {
  208. furi_string_cat_printf(buffer, "%d-", model->depth[i]);
  209. } else {
  210. furi_string_cat_printf(buffer, "%d", model->depth[i]);
  211. }
  212. }
  213. if(!flipper_format_write_string(flipper_format, "Bitting Pattern", buffer)) break;
  214. furi_string_free(buffer);
  215. // signal that the file was written successfully
  216. } while(0);
  217. flipper_format_free(flipper_format);
  218. view_dispatcher_switch_to_view(app->view_dispatcher, KeyCopierViewSubmenu);
  219. }
  220. static void key_copier_view_save_callback(void* context) {
  221. KeyCopierApp* app = (KeyCopierApp*)context;
  222. // Header to display on the text input screen.
  223. text_input_set_header_text(app->text_input, key_name_entry_text);
  224. // Copy the current name into the temporary buffer.
  225. bool redraw = false;
  226. with_view_model(
  227. app->view_measure,
  228. KeyCopierModel * model,
  229. {
  230. strncpy(
  231. app->temp_buffer,
  232. furi_string_get_cstr(model->key_name_str),
  233. app->temp_buffer_size);
  234. },
  235. redraw);
  236. // Configure the text input. When user enters text and clicks OK,
  237. // key_copier_file_saver be called.
  238. bool clear_previous_text = false;
  239. text_input_set_result_callback(
  240. app->text_input,
  241. key_copier_file_saver,
  242. app,
  243. app->temp_buffer,
  244. app->temp_buffer_size,
  245. clear_previous_text);
  246. view_set_previous_callback(
  247. text_input_get_view(app->text_input), key_copier_navigation_submenu_callback);
  248. // Show text input dialog.
  249. view_dispatcher_switch_to_view(app->view_dispatcher, KeyCopierViewTextInput);
  250. }
  251. static void key_copier_view_load_callback(void* context) {
  252. KeyCopierApp* app = (KeyCopierApp*)context;
  253. KeyCopierModel* model = view_get_model(app->view_measure);
  254. DialogsFileBrowserOptions browser_options;
  255. Storage* storage = furi_record_open(RECORD_STORAGE);
  256. storage_simply_mkdir(storage, STORAGE_APP_DATA_PATH_PREFIX);
  257. dialog_file_browser_set_basic_options(&browser_options, KEY_COPIER_FILE_EXTENSION, &I_icon);
  258. browser_options.base_path = STORAGE_APP_DATA_PATH_PREFIX;
  259. furi_string_set(app->file_path, browser_options.base_path);
  260. if(dialog_file_browser_show(app->dialogs, app->file_path, app->file_path, &browser_options)) {
  261. FlipperFormat* flipper_format = flipper_format_file_alloc(storage);
  262. do {
  263. if(!flipper_format_file_open_existing(
  264. flipper_format, furi_string_get_cstr(app->file_path)))
  265. break;
  266. FuriString* format_buffer = furi_string_alloc();
  267. FuriString* depth_buffer = furi_string_alloc();
  268. if(!flipper_format_read_string(flipper_format, "Format Name", format_buffer)) break;
  269. if(!flipper_format_read_string(flipper_format, "Bitting Pattern", depth_buffer)) break;
  270. for(size_t i = 0; i < COUNT_OF(all_formats); i++) {
  271. if(!strcmp(furi_string_get_cstr(format_buffer), all_formats[i].format_name)) {
  272. model->format_index = (uint32_t)i;
  273. model->format = all_formats[model->format_index];
  274. }
  275. }
  276. for(int i = 0; i < model->format.pin_num; i++) {
  277. model->depth[i] = (uint8_t)(furi_string_get_char(depth_buffer, i * 2) - '0');
  278. }
  279. model->data_loaded = true;
  280. // signal that the file was read successfully
  281. } while(0);
  282. flipper_format_free(flipper_format);
  283. furi_record_close(RECORD_STORAGE);
  284. }
  285. view_dispatcher_switch_to_view(app->view_dispatcher, KeyCopierViewSubmenu);
  286. }
  287. static void key_copier_view_measure_draw_callback(Canvas* canvas, void* model) {
  288. static double inches_per_px = (double)INCHES_PER_PX;
  289. canvas_set_bitmap_mode(canvas, true);
  290. KeyCopierModel* my_model = (KeyCopierModel*)model;
  291. KeyFormat my_format = my_model->format;
  292. FuriString* buffer = furi_string_alloc();
  293. int pin_half_width_px = (int)round((my_format.pin_width_inch / inches_per_px) / 2);
  294. int pin_step_px = (int)round(my_format.pin_increment_inch / inches_per_px);
  295. double drill_radians =
  296. (180 - my_format.drill_angle) / 2 / 180 * (double)M_PI; // Convert angle to radians
  297. double tangent = tan(drill_radians);
  298. int top_contour_px = (int)round(62 - my_format.uncut_depth_inch / inches_per_px);
  299. int bottom_contour_px = 0;
  300. if(my_format.sides == 2)
  301. bottom_contour_px =
  302. top_contour_px + (int)round(my_format.uncut_depth_inch / inches_per_px);
  303. int post_extra_x_px = 0;
  304. int pre_extra_x_px = 0;
  305. int bottom_post_extra_x_px = 0; // new
  306. int bottom_pre_extra_x_px = 0; // new
  307. int level_contour_px =
  308. (int)round((my_format.last_pin_inch + my_format.elbow_inch) / inches_per_px);
  309. for(int current_pin = 1; current_pin <= my_model->format.pin_num; current_pin += 1) {
  310. double current_center_px =
  311. my_format.first_pin_inch + (current_pin - 1) * my_format.pin_increment_inch;
  312. int pin_center_px = (int)round(current_center_px / inches_per_px);
  313. furi_string_printf(buffer, "%d", my_model->depth[current_pin - 1]);
  314. canvas_draw_str_aligned(
  315. canvas,
  316. pin_center_px,
  317. top_contour_px - 12,
  318. AlignCenter,
  319. AlignCenter,
  320. furi_string_get_cstr(buffer));
  321. canvas_draw_line(
  322. canvas,
  323. pin_center_px,
  324. top_contour_px - 5,
  325. pin_center_px,
  326. top_contour_px); // the vertical line to indicate pin center
  327. int current_depth = my_model->depth[current_pin - 1] - my_format.min_depth_ind;
  328. int current_depth_px =
  329. (int)round(current_depth * my_format.depth_step_inch / inches_per_px);
  330. canvas_draw_line(
  331. canvas,
  332. pin_center_px - pin_half_width_px,
  333. top_contour_px + current_depth_px,
  334. pin_center_px + pin_half_width_px,
  335. top_contour_px + current_depth_px); // draw top pin width horizontal line
  336. if(my_format.sides == 2) { // new
  337. int last_depth = my_model->depth[current_pin - 2] - my_format.min_depth_ind;
  338. int next_depth = my_model->depth[current_pin] - my_format.min_depth_ind;
  339. int current_depth = my_model->depth[current_pin - 1] - my_format.min_depth_ind;
  340. int current_depth_px =
  341. (int)round(current_depth * my_format.depth_step_inch / inches_per_px);
  342. // Draw horizontal line for bottom pin
  343. canvas_draw_line(
  344. canvas,
  345. pin_center_px - pin_half_width_px,
  346. bottom_contour_px - current_depth_px,
  347. pin_center_px + pin_half_width_px,
  348. bottom_contour_px - current_depth_px);
  349. // Handle first pin for bottom
  350. if(current_pin == 1) {
  351. canvas_draw_line(
  352. canvas,
  353. 0,
  354. bottom_contour_px,
  355. pin_center_px - pin_half_width_px - current_depth_px,
  356. bottom_contour_px);
  357. last_depth = 0;
  358. bottom_pre_extra_x_px = max(current_depth_px + pin_half_width_px, 0);
  359. }
  360. // Handle left side intersection for bottom
  361. if((last_depth + current_depth) > my_format.clearance) {
  362. if(current_pin != 1) {
  363. bottom_pre_extra_x_px =
  364. min(max(pin_step_px - bottom_post_extra_x_px, pin_half_width_px),
  365. pin_step_px - pin_half_width_px);
  366. }
  367. canvas_draw_line(
  368. canvas,
  369. pin_center_px - bottom_pre_extra_x_px,
  370. bottom_contour_px -
  371. max((int)round(
  372. (current_depth_px - (bottom_pre_extra_x_px - pin_half_width_px)) *
  373. tangent),
  374. 0),
  375. pin_center_px - pin_half_width_px,
  376. bottom_contour_px - (int)round(current_depth_px * tangent));
  377. } else {
  378. int last_depth_px =
  379. (int)round(last_depth * my_format.depth_step_inch / inches_per_px);
  380. int up_slope_start_x_px = pin_center_px - pin_half_width_px - current_depth_px;
  381. canvas_draw_line(
  382. canvas,
  383. pin_center_px - pin_half_width_px - current_depth_px,
  384. bottom_contour_px,
  385. pin_center_px - pin_half_width_px,
  386. bottom_contour_px - (int)round(current_depth_px * tangent));
  387. canvas_draw_line(
  388. canvas,
  389. min(pin_center_px - pin_step_px + pin_half_width_px + last_depth_px,
  390. up_slope_start_x_px),
  391. bottom_contour_px,
  392. up_slope_start_x_px,
  393. bottom_contour_px);
  394. }
  395. // Handle right side intersection for bottom
  396. if((current_depth + next_depth) > my_format.clearance) {
  397. double numerator = (double)current_depth;
  398. double denominator = (double)(current_depth + next_depth);
  399. double product = (numerator / denominator) * pin_step_px;
  400. bottom_post_extra_x_px =
  401. (int)min(max(product, pin_half_width_px), pin_step_px - pin_half_width_px);
  402. canvas_draw_line(
  403. canvas,
  404. pin_center_px + pin_half_width_px,
  405. bottom_contour_px - current_depth_px,
  406. pin_center_px + bottom_post_extra_x_px,
  407. bottom_contour_px -
  408. max(current_depth_px -
  409. (int)round((bottom_post_extra_x_px - pin_half_width_px) * tangent),
  410. 0));
  411. } else {
  412. canvas_draw_line(
  413. canvas,
  414. pin_center_px + pin_half_width_px,
  415. bottom_contour_px - (int)round(current_depth_px * tangent),
  416. pin_center_px + pin_half_width_px + current_depth_px,
  417. bottom_contour_px);
  418. }
  419. }
  420. // new end
  421. int last_depth = my_model->depth[current_pin - 2] - my_format.min_depth_ind;
  422. int next_depth = my_model->depth[current_pin] - my_format.min_depth_ind;
  423. if(current_pin == 1) {
  424. canvas_draw_line(
  425. canvas,
  426. 0,
  427. top_contour_px,
  428. pin_center_px - pin_half_width_px - current_depth_px,
  429. top_contour_px); // draw top shoulder
  430. last_depth = 0;
  431. pre_extra_x_px = max(current_depth_px + pin_half_width_px, 0);
  432. if(my_format.sides == 2) {
  433. canvas_draw_line(
  434. canvas,
  435. 0,
  436. bottom_contour_px,
  437. pin_center_px - pin_half_width_px - current_depth_px,
  438. bottom_contour_px); // draw bottom shoulder (hidden by level contour)
  439. } else {
  440. canvas_draw_line(canvas, 0, 62, level_contour_px, 62);
  441. }
  442. }
  443. if(current_pin == my_model->format.pin_num) {
  444. next_depth = 0;
  445. }
  446. if((last_depth + current_depth) > my_format.clearance) { // yes
  447. // intersection
  448. if(current_pin != 1) {
  449. pre_extra_x_px =
  450. min(max(pin_step_px - post_extra_x_px, pin_half_width_px),
  451. pin_step_px - pin_half_width_px);
  452. }
  453. canvas_draw_line(
  454. canvas,
  455. pin_center_px - pre_extra_x_px,
  456. top_contour_px +
  457. max((int)round(
  458. (current_depth_px - (pre_extra_x_px - pin_half_width_px)) * tangent),
  459. 0),
  460. pin_center_px - pin_half_width_px,
  461. top_contour_px + (int)round(current_depth_px * tangent));
  462. } else {
  463. int last_depth_px = (int)round(last_depth * my_format.depth_step_inch / inches_per_px);
  464. int down_slope_start_x_px = pin_center_px - pin_half_width_px - current_depth_px;
  465. canvas_draw_line(
  466. canvas,
  467. pin_center_px - pin_half_width_px - current_depth_px,
  468. top_contour_px,
  469. pin_center_px - pin_half_width_px,
  470. top_contour_px + (int)round(current_depth_px * tangent));
  471. canvas_draw_line(
  472. canvas,
  473. min(pin_center_px - pin_step_px + pin_half_width_px + last_depth_px,
  474. down_slope_start_x_px),
  475. top_contour_px,
  476. down_slope_start_x_px,
  477. top_contour_px);
  478. }
  479. if((current_depth + next_depth) > my_format.clearance) { // yes
  480. // intersection
  481. double numerator = (double)current_depth;
  482. double denominator = (double)(current_depth + next_depth);
  483. double product = (numerator / denominator) * pin_step_px;
  484. post_extra_x_px =
  485. (int)min(max(product, pin_half_width_px), pin_step_px - pin_half_width_px);
  486. canvas_draw_line(
  487. canvas,
  488. pin_center_px + pin_half_width_px,
  489. top_contour_px + current_depth_px,
  490. pin_center_px + post_extra_x_px,
  491. top_contour_px +
  492. max(current_depth_px -
  493. (int)round((post_extra_x_px - pin_half_width_px) * tangent),
  494. 0));
  495. } else { // no intersection
  496. canvas_draw_line(
  497. canvas,
  498. pin_center_px + pin_half_width_px,
  499. top_contour_px + (int)round(current_depth_px * tangent),
  500. pin_center_px + pin_half_width_px + current_depth_px,
  501. top_contour_px);
  502. }
  503. }
  504. int elbow_px = (int)round(my_format.elbow_inch / inches_per_px);
  505. canvas_draw_line(canvas, level_contour_px, 62, level_contour_px + elbow_px, 62 - elbow_px);
  506. canvas_draw_line(canvas, 0, top_contour_px - 6, 0, top_contour_px);
  507. if(my_format.stop == 2) {
  508. // Draw a line using level_contour_px if stop equals 2 elbow must be firt pin inch
  509. canvas_draw_line(canvas, level_contour_px, top_contour_px, level_contour_px, 63);
  510. // } else {
  511. // Otherwise, draw a default line
  512. // canvas_draw_line(canvas, 0, top_contour_px, 0, 63); // too confusing but may want later
  513. }
  514. int slc_pin_px = (int)round(
  515. (my_format.first_pin_inch + (my_model->pin_slc - 1) * my_format.pin_increment_inch) /
  516. inches_per_px);
  517. canvas_draw_icon(canvas, slc_pin_px - 2, top_contour_px - 25, &I_arrow_down);
  518. furi_string_printf(buffer, "%s", my_format.format_name);
  519. canvas_draw_str(canvas, 100, 10, furi_string_get_cstr(buffer));
  520. furi_string_free(buffer);
  521. }
  522. static bool key_copier_view_measure_input_callback(InputEvent* event, void* context) {
  523. KeyCopierApp* app = (KeyCopierApp*)context;
  524. if(event->type == InputTypeShort) {
  525. switch(event->key) {
  526. case InputKeyLeft: {
  527. bool redraw = true;
  528. with_view_model(
  529. app->view_measure,
  530. KeyCopierModel * model,
  531. {
  532. if(model->pin_slc > 1) {
  533. model->pin_slc--;
  534. }
  535. },
  536. redraw);
  537. break;
  538. }
  539. case InputKeyRight: {
  540. bool redraw = true;
  541. with_view_model(
  542. app->view_measure,
  543. KeyCopierModel * model,
  544. {
  545. if(model->pin_slc < model->format.pin_num) {
  546. model->pin_slc++;
  547. }
  548. },
  549. redraw);
  550. break;
  551. }
  552. case InputKeyUp: {
  553. bool redraw = true;
  554. with_view_model(
  555. app->view_measure,
  556. KeyCopierModel * model,
  557. {
  558. if(model->depth[model->pin_slc - 1] > model->format.min_depth_ind) {
  559. if(model->pin_slc == 1) { // first pin only limited by the next one
  560. if(model->depth[model->pin_slc] - model->depth[model->pin_slc - 1] <
  561. model->format.macs)
  562. model->depth[model->pin_slc - 1]--;
  563. } else if(model->pin_slc == model->format.pin_num) { // last pin only limited by
  564. // the previous one
  565. if(model->depth[model->pin_slc - 2] -
  566. model->depth[model->pin_slc - 1] <
  567. model->format.macs) {
  568. model->depth[model->pin_slc - 1]--;
  569. }
  570. } else {
  571. if(model->depth[model->pin_slc] - model->depth[model->pin_slc - 1] <
  572. model->format.macs &&
  573. model->depth[model->pin_slc - 2] -
  574. model->depth[model->pin_slc - 1] <
  575. model->format.macs) {
  576. model->depth[model->pin_slc - 1]--;
  577. }
  578. }
  579. }
  580. },
  581. redraw);
  582. break;
  583. }
  584. case InputKeyDown: {
  585. bool redraw = true;
  586. with_view_model(
  587. app->view_measure,
  588. KeyCopierModel * model,
  589. {
  590. if(model->depth[model->pin_slc - 1] < model->format.max_depth_ind) {
  591. if(model->pin_slc == 1) { // first pin only limited by the next one
  592. if(model->depth[model->pin_slc - 1] - model->depth[model->pin_slc] <
  593. model->format.macs)
  594. model->depth[model->pin_slc - 1]++;
  595. } else if(model->pin_slc == model->format.pin_num) { // last pin only limited by
  596. // the previous one
  597. if(model->depth[model->pin_slc - 1] -
  598. model->depth[model->pin_slc - 2] <
  599. model->format.macs) {
  600. model->depth[model->pin_slc - 1]++;
  601. }
  602. } else {
  603. if(model->depth[model->pin_slc - 1] - model->depth[model->pin_slc] <
  604. model->format.macs &&
  605. model->depth[model->pin_slc - 1] -
  606. model->depth[model->pin_slc - 2] <
  607. model->format.macs) {
  608. model->depth[model->pin_slc - 1]++;
  609. }
  610. }
  611. }
  612. },
  613. redraw);
  614. break;
  615. }
  616. default:
  617. // Handle other keys or do nothing
  618. break;
  619. }
  620. }
  621. return false;
  622. }
  623. static KeyCopierApp* key_copier_app_alloc() {
  624. KeyCopierApp* app = (KeyCopierApp*)malloc(sizeof(KeyCopierApp));
  625. Gui* gui = furi_record_open(RECORD_GUI);
  626. app->view_dispatcher = view_dispatcher_alloc();
  627. view_dispatcher_attach_to_gui(app->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  628. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  629. app->dialogs = furi_record_open(RECORD_DIALOGS);
  630. app->file_path = furi_string_alloc();
  631. app->submenu = submenu_alloc();
  632. submenu_add_item(
  633. app->submenu, "Measure", KeyCopierSubmenuIndexMeasure, key_copier_submenu_callback, app);
  634. submenu_add_item(
  635. app->submenu, "Config", KeyCopierSubmenuIndexConfigure, key_copier_submenu_callback, app);
  636. submenu_add_item(
  637. app->submenu, "Save", KeyCopierSubmenuIndexSave, key_copier_submenu_callback, app);
  638. submenu_add_item(
  639. app->submenu, "Load", KeyCopierSubmenuIndexLoad, key_copier_submenu_callback, app);
  640. submenu_add_item(
  641. app->submenu, "About", KeyCopierSubmenuIndexAbout, key_copier_submenu_callback, app);
  642. view_set_previous_callback(
  643. submenu_get_view(app->submenu), key_copier_navigation_exit_callback);
  644. view_dispatcher_add_view(
  645. app->view_dispatcher, KeyCopierViewSubmenu, submenu_get_view(app->submenu));
  646. view_dispatcher_switch_to_view(app->view_dispatcher, KeyCopierViewSubmenu);
  647. app->text_input = text_input_alloc();
  648. view_dispatcher_add_view(
  649. app->view_dispatcher, KeyCopierViewTextInput, text_input_get_view(app->text_input));
  650. app->temp_buffer_size = 32;
  651. app->temp_buffer = (char*)malloc(app->temp_buffer_size);
  652. app->temp_buffer = "";
  653. app->view_measure = view_alloc();
  654. view_set_draw_callback(app->view_measure, key_copier_view_measure_draw_callback);
  655. view_set_input_callback(app->view_measure, key_copier_view_measure_input_callback);
  656. view_set_previous_callback(app->view_measure, key_copier_navigation_submenu_callback);
  657. view_set_context(app->view_measure, app);
  658. view_allocate_model(app->view_measure, ViewModelTypeLockFree, sizeof(KeyCopierModel));
  659. KeyCopierModel* model = view_get_model(app->view_measure);
  660. initialize_model(model);
  661. view_dispatcher_add_view(app->view_dispatcher, KeyCopierViewMeasure, app->view_measure);
  662. app->variable_item_list_config = variable_item_list_alloc();
  663. app->view_config_e = view_alloc();
  664. view_set_context(app->view_config_e, app);
  665. view_set_previous_callback(app->view_config_e, key_copier_navigation_submenu_callback);
  666. view_set_enter_callback(app->view_config_e, key_copier_config_enter_callback);
  667. view_dispatcher_add_view(app->view_dispatcher, KeyCopierViewConfigure_e, app->view_config_e);
  668. View* view_buffer = view_alloc();
  669. view_dispatcher_add_view(app->view_dispatcher, KeyCopierViewConfigure_i, view_buffer);
  670. app->view_save = view_alloc();
  671. view_set_context(app->view_save, app);
  672. view_set_enter_callback(app->view_save, key_copier_view_save_callback);
  673. view_set_previous_callback(app->view_save, key_copier_navigation_submenu_callback);
  674. view_dispatcher_add_view(app->view_dispatcher, KeyCopierViewSave, app->view_save);
  675. app->view_load = view_alloc();
  676. view_set_context(app->view_load, app);
  677. view_set_enter_callback(app->view_load, key_copier_view_load_callback);
  678. view_set_previous_callback(app->view_load, key_copier_navigation_submenu_callback);
  679. view_dispatcher_add_view(app->view_dispatcher, KeyCopierViewLoad, app->view_load);
  680. app->widget_about = widget_alloc();
  681. widget_add_text_scroll_element(
  682. app->widget_about,
  683. 0,
  684. 0,
  685. 128,
  686. 64,
  687. "Key Maker App 1.1\nAuthor: @Torron\n\nTo measure your key:\n\n1. Place "
  688. "it on top of the screen.\n\n2. Use the contour to align your key.\n\n3. "
  689. "Adjust each pin's depth until they match. It's easier if you look with "
  690. "one eye closed.\n\nGithub: github.com/zinongli/KeyCopier \n\nSpecial "
  691. "thanks to Derek Jamison's Skeleton App Template.");
  692. view_set_previous_callback(
  693. widget_get_view(app->widget_about), key_copier_navigation_submenu_callback);
  694. view_dispatcher_add_view(
  695. app->view_dispatcher, KeyCopierViewAbout, widget_get_view(app->widget_about));
  696. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  697. #ifdef BACKLIGHT_ON
  698. notification_message(app->notifications, &sequence_display_backlight_enforce_on);
  699. #endif
  700. return app;
  701. }
  702. static void key_copier_app_free(KeyCopierApp* app) {
  703. #ifdef BACKLIGHT_ON
  704. notification_message(app->notifications, &sequence_display_backlight_enforce_auto);
  705. #endif
  706. furi_record_close(RECORD_NOTIFICATION);
  707. view_dispatcher_remove_view(app->view_dispatcher, KeyCopierViewTextInput);
  708. text_input_free(app->text_input);
  709. free(app->temp_buffer);
  710. view_dispatcher_remove_view(app->view_dispatcher, KeyCopierViewAbout);
  711. widget_free(app->widget_about);
  712. view_dispatcher_remove_view(app->view_dispatcher, KeyCopierViewMeasure);
  713. with_view_model(
  714. app->view_measure,
  715. KeyCopierModel * model,
  716. {
  717. if(model->depth != NULL) {
  718. free(model->depth);
  719. }
  720. },
  721. false);
  722. view_free(app->view_measure);
  723. view_dispatcher_remove_view(app->view_dispatcher, KeyCopierViewConfigure_e);
  724. view_dispatcher_remove_view(app->view_dispatcher, KeyCopierViewConfigure_i);
  725. view_dispatcher_remove_view(app->view_dispatcher, KeyCopierViewSave);
  726. view_dispatcher_remove_view(app->view_dispatcher, KeyCopierViewLoad);
  727. variable_item_list_free(app->variable_item_list_config);
  728. view_dispatcher_remove_view(app->view_dispatcher, KeyCopierViewSubmenu);
  729. submenu_free(app->submenu);
  730. view_dispatcher_free(app->view_dispatcher);
  731. furi_record_close(RECORD_GUI);
  732. free(app);
  733. }
  734. int32_t main_key_copier_app(void* _p) {
  735. UNUSED(_p);
  736. KeyCopierApp* app = key_copier_app_alloc();
  737. view_dispatcher_run(app->view_dispatcher);
  738. key_copier_app_free(app);
  739. return 0;
  740. }