key_copier.c 34 KB

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