key_copier.c 33 KB

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