key_copier.c 35 KB

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