key_copier.c 35 KB

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