key_copier.c 33 KB

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