key_copier.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. #include <furi.h>
  2. #include <furi_hal.h>
  3. #include <gui/gui.h>
  4. #include <gui/view.h>
  5. #include <gui/view_dispatcher.h>
  6. #include <gui/modules/popup.h>
  7. #include <gui/modules/submenu.h>
  8. #include <gui/modules/text_input.h>
  9. #include <gui/modules/widget.h>
  10. #include <gui/modules/variable_item_list.h>
  11. #include <notification/notification.h>
  12. #include <notification/notification_messages.h>
  13. #include "key_copier_icons.h"
  14. #include "key_formats.h"
  15. #include "key_copier.h"
  16. #include <applications/services/storage/storage.h>
  17. #include <stdbool.h>
  18. #define TAG "KeyCopier"
  19. #define INCHES_PER_PX 0.00978
  20. #define FIRST_PIN_INCH 0.247
  21. #define LAST_PIN_INCH 0.997
  22. #define PIN_INCREMENT_INCH 0.15
  23. #define UNCUT_DEPTH_INCH 0.329
  24. #define DEEPEST_DEPTH_INCH 0.191
  25. #define DEPTH_STEP_INCH 0.023
  26. #define MAX_DEPTH_IND ((UNCUT_DEPTH_INCH - DEEPEST_DEPTH_INCH) / DEPTH_STEP_INCH)
  27. #define PIN_WIDTH_INCH 0.084
  28. #define PIN_NUM 6
  29. // Change this to BACKLIGHT_AUTO if you don't want the backlight to be continuously on.
  30. #define BACKLIGHT_ON 1
  31. // Our application menu has 3 items. You can add more items if you want.
  32. typedef enum {
  33. KeyCopierSubmenuIndexConfigure,
  34. KeyCopierSubmenuIndexGame,
  35. KeyCopierSubmenuIndexAbout,
  36. } KeyCopierSubmenuIndex;
  37. // Each view is a screen we show the user.
  38. typedef enum {
  39. KeyCopierViewSubmenu, // The menu when the app starts
  40. KeyCopierViewTextInput, // Input for configuring text settings
  41. KeyCopierViewConfigure, // The configuration screen
  42. KeyCopierViewGame, // The main screen
  43. KeyCopierViewAbout, // The about screen with directions, link to social channel, etc.
  44. } KeyCopierView;
  45. typedef enum {
  46. KeyCopierEventIdRedrawScreen = 0, // Custom event to redraw the screen
  47. KeyCopierEventIdOkPressed = 42, // Custom event to process OK button getting pressed down
  48. } KeyCopierEventId;
  49. typedef struct {
  50. ViewDispatcher* view_dispatcher; // Switches between our views
  51. NotificationApp* notifications; // Used for controlling the backlight
  52. Submenu* submenu; // The application menu
  53. TextInput* text_input; // The text input screen
  54. VariableItemList* variable_item_list_config; // The configuration screen
  55. View* view_game; // The main screen
  56. Widget* widget_about; // The about screen
  57. VariableItem* key_name_item; // The name setting item (so we can update the text)
  58. char* temp_buffer; // Temporary buffer for text input
  59. uint32_t temp_buffer_size; // Size of temporary buffer
  60. FuriTimer* timer; // Timer for redrawing the screen
  61. } KeyCopierApp;
  62. typedef struct {
  63. uint32_t format_index; // The index for total number of pins
  64. FuriString* key_name_str; // The name setting
  65. uint8_t pin_slc; // The pin that is being adjusted
  66. uint8_t total_pin; // The total number of pins we are adjusting
  67. uint8_t* depth; // The cutting depth
  68. KeyFormat format;
  69. } KeyCopierGameModel;
  70. void initialize_format(KeyCopierGameModel* model) {
  71. model->format_index = 0;
  72. memcpy(&model->format, &all_formats[model->format_index], sizeof(KeyFormat));
  73. }
  74. void initialize_model(KeyCopierGameModel* model) {
  75. if(model->depth != NULL) {
  76. free(model->depth);
  77. }
  78. initialize_format(model);
  79. model->depth = (uint8_t*)malloc((model->format.pin_num + 1) * sizeof(uint8_t));
  80. for(uint8_t i = 0; i <= model->format.pin_num; i++) {
  81. model->depth[i] = model->format.min_depth_ind;
  82. }
  83. }
  84. /**
  85. * @brief Callback for exiting the application.
  86. * @details This function is called when user press back button. We return VIEW_NONE to
  87. * indicate that we want to exit the application.
  88. * @param _context The context - unused
  89. * @return next view id
  90. */
  91. static uint32_t key_copier_navigation_exit_callback(void* _context) {
  92. UNUSED(_context);
  93. return VIEW_NONE;
  94. }
  95. /**
  96. * @brief Callback for returning to submenu.
  97. * @details This function is called when user press back button. We return VIEW_NONE to
  98. * indicate that we want to navigate to the submenu.
  99. * @param _context The context - unused
  100. * @return next view id
  101. */
  102. static uint32_t key_copier_navigation_submenu_callback(void* _context) {
  103. UNUSED(_context);
  104. return KeyCopierViewSubmenu;
  105. }
  106. /**
  107. * @brief Callback for returning to configure screen.
  108. * @details This function is called when user press back button. We return VIEW_NONE to
  109. * indicate that we want to navigate to the configure screen.
  110. * @param _context The context - unused
  111. * @return next view id
  112. */
  113. static uint32_t key_copier_navigation_configure_callback(void* _context) {
  114. UNUSED(_context);
  115. return KeyCopierViewConfigure;
  116. }
  117. /**
  118. * @brief Handle submenu item selection.
  119. * @details This function is called when user selects an item from the submenu.
  120. * @param context The context - KeyCopierApp object.
  121. * @param index The KeyCopierSubmenuIndex item that was clicked.
  122. */
  123. static void key_copier_submenu_callback(void* context, uint32_t index) {
  124. KeyCopierApp* app = (KeyCopierApp*)context;
  125. switch(index) {
  126. case KeyCopierSubmenuIndexConfigure:
  127. view_dispatcher_switch_to_view(app->view_dispatcher, KeyCopierViewConfigure);
  128. break;
  129. case KeyCopierSubmenuIndexGame:
  130. view_dispatcher_switch_to_view(app->view_dispatcher, KeyCopierViewGame);
  131. break;
  132. case KeyCopierSubmenuIndexAbout:
  133. view_dispatcher_switch_to_view(app->view_dispatcher, KeyCopierViewAbout);
  134. break;
  135. default:
  136. break;
  137. }
  138. }
  139. /**
  140. * Our 1st sample setting is a team color. We have 3 options: red, green, and blue.
  141. */
  142. static const char* total_pin_config_label = "Key Format";
  143. static char* format_names[] = {"Kwikset", "Schlage"};
  144. static void key_copier_total_pin_change(VariableItem* item) {
  145. KeyCopierApp* app = variable_item_get_context(item);
  146. uint8_t format_index = variable_item_get_current_value_index(item);
  147. variable_item_set_current_value_text(item, format_names[format_index]);
  148. KeyCopierGameModel* model = view_get_model(app->view_game);
  149. model->format_index = format_index;
  150. model->format = all_formats[format_index];
  151. model->total_pin = model->format.pin_num;
  152. if(model->depth != NULL) {
  153. free(model->depth);
  154. }
  155. model->depth = (uint8_t*)malloc((model->format.pin_num + 1) * sizeof(uint8_t));
  156. for(uint8_t i = 0; i <= model->format.pin_num; i++) {
  157. model->depth[i] = model->format.min_depth_ind;
  158. }
  159. }
  160. void ensure_dir_exists(Storage *storage)
  161. {
  162. // If apps_data directory doesn't exist, create it.
  163. if (!storage_dir_exists(storage, KEY_COPIER_APPS_DATA_FOLDER))
  164. {
  165. FURI_LOG_I(TAG, "Creating directory: %s", KEY_COPIER_APPS_DATA_FOLDER);
  166. storage_simply_mkdir(storage, KEY_COPIER_APPS_DATA_FOLDER);
  167. }
  168. else
  169. {
  170. FURI_LOG_I(TAG, "Directory exists: %s", KEY_COPIER_APPS_DATA_FOLDER);
  171. }
  172. // If wiegand directory doesn't exist, create it.
  173. if (!storage_dir_exists(storage, KEY_COPIER_SAVE_FOLDER))
  174. {
  175. FURI_LOG_I(TAG, "Creating directory: %s", KEY_COPIER_SAVE_FOLDER);
  176. storage_simply_mkdir(storage, KEY_COPIER_SAVE_FOLDER);
  177. }
  178. else
  179. {
  180. FURI_LOG_I(TAG, "Directory exists: %s", KEY_COPIER_SAVE_FOLDER);
  181. }
  182. }
  183. /**
  184. * Our 2nd sample setting is a text field. When the user clicks OK on the configuration
  185. * setting we use a text input screen to allow the user to enter a name. This function is
  186. * called when the user clicks OK on the text input screen.
  187. */
  188. static const char* key_name_config_label = "Save Key";
  189. static const char* key_name_entry_text = "Enter name";
  190. static const char* key_name_default_value = "Key 1";
  191. static void key_copier_key_name_text_updated(void* context) {
  192. KeyCopierApp* app = (KeyCopierApp*)context;
  193. KeyCopierGameModel* model = view_get_model(app->view_game);
  194. bool redraw = true;
  195. with_view_model(
  196. app->view_game,
  197. KeyCopierGameModel * model,
  198. {
  199. furi_string_set(model->key_name_str, app->temp_buffer);
  200. variable_item_set_current_value_text(
  201. app->key_name_item, furi_string_get_cstr(model->key_name_str));
  202. },
  203. redraw);
  204. FuriString *buffer = furi_string_alloc();
  205. FuriString *file_path = furi_string_alloc();
  206. furi_string_printf(
  207. file_path, "%s/%s%s", KEY_COPIER_SAVE_FOLDER, furi_string_get_cstr(model->key_name_str), KEY_COPIER_SAVE_EXTENSION);
  208. Storage *storage = furi_record_open(RECORD_STORAGE);
  209. ensure_dir_exists(storage);
  210. File *data_file = storage_file_alloc(storage);
  211. if (storage_file_open(
  212. data_file, furi_string_get_cstr(file_path), FSAM_WRITE, FSOM_OPEN_ALWAYS))
  213. {
  214. furi_string_printf(buffer, "Filetype: Flipper Key Copier File\n");
  215. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  216. furi_string_printf(buffer, "Version: 1.0\n");
  217. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  218. furi_string_printf(buffer, "Key Format: %s\n", model->format.format_name);
  219. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  220. furi_string_printf(buffer, "Pin Number: %d\n", model->format.pin_num);
  221. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  222. furi_string_printf(buffer, "Maximum Adjacent Cut Specification: %d\n", model->format.macs);
  223. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  224. furi_string_printf(buffer, "Bitting Pattern: ");
  225. for (int i = 0; i < model->format.pin_num; i++)
  226. {
  227. furi_string_cat_printf(
  228. buffer,
  229. "%d ",
  230. model->depth[i]);
  231. }
  232. furi_string_push_back(buffer, '\n');
  233. storage_file_write(data_file, furi_string_get_cstr(buffer), furi_string_size(buffer));
  234. storage_file_close(data_file);
  235. view_dispatcher_switch_to_view(app->view_dispatcher, KeyCopierViewConfigure);
  236. }
  237. }
  238. /**
  239. * @brief Callback when item in configuration screen is clicked.
  240. * @details This function is called when user clicks OK on an item in the configuration screen.
  241. * If the item clicked is our text field then we switch to the text input screen.
  242. * @param context The context - KeyCopierApp object.
  243. * @param index - The index of the item that was clicked.
  244. */
  245. static void key_copier_setting_item_clicked(void* context, uint32_t index) {
  246. KeyCopierApp* app = (KeyCopierApp*)context;
  247. index++; // The index starts at zero, but we want to start at 1.
  248. // Our configuration UI has the 2nd item as a text field.
  249. if(index == 2) {
  250. // Header to display on the text input screen.
  251. text_input_set_header_text(app->text_input, key_name_entry_text);
  252. // Copy the current name into the temporary buffer.
  253. bool redraw = false;
  254. with_view_model(
  255. app->view_game,
  256. KeyCopierGameModel * model,
  257. {
  258. strncpy(
  259. app->temp_buffer,
  260. furi_string_get_cstr(model->key_name_str),
  261. app->temp_buffer_size);
  262. },
  263. redraw);
  264. // Configure the text input. When user enters text and clicks OK, key_copier_setting_text_updated be called.
  265. bool clear_previous_text = false;
  266. text_input_set_result_callback(
  267. app->text_input,
  268. key_copier_key_name_text_updated,
  269. app,
  270. app->temp_buffer,
  271. app->temp_buffer_size,
  272. clear_previous_text);
  273. // Pressing the BACK button will reload the configure screen.
  274. view_set_previous_callback(
  275. text_input_get_view(app->text_input), key_copier_navigation_configure_callback);
  276. // Show text input dialog.
  277. view_dispatcher_switch_to_view(app->view_dispatcher, KeyCopierViewTextInput);
  278. }
  279. }
  280. static inline int min(int a, int b) {
  281. return (a < b) ? a : b;
  282. }
  283. static inline int max(int a, int b) {
  284. return (a > b) ? a : b;
  285. }
  286. /**
  287. * @brief Callback for drawing the game screen.
  288. * @details This function is called when the screen needs to be redrawn, like when the model gets updated.
  289. * @param canvas The canvas to draw on.
  290. * @param model The model - MyModel object.
  291. */
  292. static double inches_per_px = (double)INCHES_PER_PX;
  293. static void key_copier_view_game_draw_callback(Canvas* canvas, void* model) {
  294. KeyCopierGameModel* my_model = (KeyCopierGameModel*)model;
  295. KeyFormat my_format = my_model->format;
  296. int pin_half_width_px = (int)round((my_format.pin_width_inch / inches_per_px) / 2);
  297. int pin_step_px = (int)round(my_format.pin_increment_inch / inches_per_px);
  298. double drill_radians = (180 - my_format.drill_angle) / 2 / 180 * (double)M_PI; // Convert angle to radians
  299. double tangent = tan(drill_radians);
  300. int post_extra_x_px = 0;
  301. int pre_extra_x_px = 0;
  302. for (int current_pin = 1; current_pin <= my_model->total_pin; current_pin += 1) {
  303. double current_center_px = my_format.first_pin_inch + (current_pin - 1) * my_format.pin_increment_inch;
  304. int pin_center_px = (int)round(current_center_px / inches_per_px);
  305. int top_contour_px = (int)round(63 - my_format.uncut_depth_inch / inches_per_px);
  306. canvas_draw_line(canvas, pin_center_px, 25, pin_center_px, 50);
  307. int current_depth = my_model->depth[current_pin - 1] - my_format.min_depth_ind;
  308. int current_depth_px = (int)round(current_depth * my_format.depth_step_inch / inches_per_px);
  309. canvas_draw_line(canvas, pin_center_px - pin_half_width_px, top_contour_px + current_depth_px, pin_center_px + pin_half_width_px, top_contour_px + current_depth_px);
  310. int last_depth = my_model->depth[current_pin - 2] - my_format.min_depth_ind;
  311. int next_depth = my_model->depth[current_pin] - my_format.min_depth_ind;
  312. if(current_pin == 1){
  313. canvas_draw_line(canvas, 0, top_contour_px, pin_center_px - pin_half_width_px - current_depth_px, top_contour_px);
  314. last_depth = 0;
  315. pre_extra_x_px = max(current_depth_px + pin_half_width_px, 0);
  316. }
  317. if(current_pin == my_model->total_pin) {
  318. next_depth = my_format.min_depth_ind;
  319. }
  320. if ((last_depth + current_depth) > my_format.clearance && current_depth != my_format.min_depth_ind) { //yes intersection
  321. if (current_pin != 1) {pre_extra_x_px = min(max(pin_step_px - post_extra_x_px,pin_half_width_px),pin_step_px - pin_half_width_px);}
  322. canvas_draw_line(
  323. canvas,
  324. pin_center_px - pre_extra_x_px,
  325. top_contour_px + max((int)round((current_depth_px - (pre_extra_x_px - pin_half_width_px)) * tangent),0),
  326. pin_center_px - pin_half_width_px,
  327. top_contour_px + (int)round(current_depth_px * tangent)
  328. );
  329. } else {
  330. int last_depth_px = (int)round(last_depth * my_format.depth_step_inch / inches_per_px);
  331. int down_slope_start_x_px = pin_center_px - pin_half_width_px - current_depth_px;
  332. canvas_draw_line(
  333. canvas,
  334. pin_center_px - pin_half_width_px - current_depth_px,
  335. top_contour_px,
  336. pin_center_px - pin_half_width_px,
  337. top_contour_px + (int)round(current_depth_px * tangent)
  338. );
  339. canvas_draw_line(
  340. canvas,
  341. min(pin_center_px - pin_step_px + pin_half_width_px + last_depth_px, down_slope_start_x_px),
  342. top_contour_px,
  343. down_slope_start_x_px,
  344. top_contour_px
  345. );
  346. }
  347. if ((current_depth + next_depth) > my_format.clearance && current_depth != my_format.min_depth_ind) { //yes intersection
  348. double numerator = (double)current_depth;
  349. double denominator = (double)(current_depth + next_depth);
  350. double product = (numerator / denominator) * pin_step_px;
  351. post_extra_x_px = (int)min(max(product,pin_half_width_px),pin_step_px - pin_half_width_px);
  352. canvas_draw_line(
  353. canvas,
  354. pin_center_px + pin_half_width_px,
  355. top_contour_px + current_depth_px,
  356. pin_center_px + post_extra_x_px,
  357. top_contour_px + max(current_depth_px - (int)round((post_extra_x_px - pin_half_width_px) * tangent),0)
  358. );
  359. } else { // no intersection
  360. canvas_draw_line(
  361. canvas,
  362. pin_center_px + pin_half_width_px,
  363. top_contour_px + (int)round(current_depth_px * tangent),
  364. pin_center_px + pin_half_width_px + current_depth_px,
  365. top_contour_px
  366. );
  367. }
  368. }
  369. int level_contour_px = (int)round((my_format.last_pin_inch + my_format.pin_increment_inch) / inches_per_px - 4);
  370. canvas_draw_line(canvas, 0, 62, level_contour_px, 62);
  371. int step_px = (int)round(my_format.pin_increment_inch / inches_per_px);
  372. canvas_draw_line(canvas, level_contour_px, 62, level_contour_px+step_px, 62-step_px);
  373. int slc_pin_px = (int)round((my_format.first_pin_inch + (my_model->pin_slc - 1) * my_format.pin_increment_inch)/ inches_per_px);
  374. canvas_draw_str(canvas, slc_pin_px-2, 23, "*");
  375. FuriString* xstr = furi_string_alloc();
  376. int buffer_size = my_model->total_pin + 1;
  377. char depth_str[buffer_size];
  378. depth_str[0] = '\0'; // Initialize the string
  379. // Manual string concatenation
  380. char* pos = depth_str;
  381. for (int i = 0; i < my_model->total_pin; i++) {
  382. int written = snprintf(pos, buffer_size - (pos - depth_str), "%u", my_model->depth[i]);
  383. if (written < 0 || written >= buffer_size - (pos - depth_str)) {
  384. // Handle error
  385. break;
  386. }
  387. pos += written;
  388. }
  389. furi_string_printf(xstr, "bitting: %s", depth_str);
  390. canvas_draw_str(canvas, 0, 10, furi_string_get_cstr(xstr));
  391. //furi_string_printf(xstr, "Num of Pins: %s", format_names[my_model->format_index]);
  392. //canvas_draw_str(canvas, 44, 24, furi_string_get_cstr(xstr));
  393. furi_string_free(xstr);
  394. }
  395. /**
  396. * @brief Callback for timer elapsed.
  397. * @details This function is called when the timer is elapsed. We use this to queue a redraw event.
  398. * @param context The context - KeyCopierApp object.
  399. */
  400. static void key_copier_view_game_timer_callback(void* context) {
  401. KeyCopierApp* app = (KeyCopierApp*)context;
  402. view_dispatcher_send_custom_event(app->view_dispatcher, KeyCopierEventIdRedrawScreen);
  403. }
  404. /**
  405. * @brief Callback when the user starts the game screen.
  406. * @details This function is called when the user enters the game screen. We start a timer to
  407. * redraw the screen periodically (so the random number is refreshed).
  408. * @param context The context - KeyCopierApp object.
  409. */
  410. static void key_copier_view_game_enter_callback(void* context) {
  411. uint32_t period = furi_ms_to_ticks(500);
  412. KeyCopierApp* app = (KeyCopierApp*)context;
  413. furi_assert(app->timer == NULL);
  414. app->timer =
  415. furi_timer_alloc(key_copier_view_game_timer_callback, FuriTimerTypePeriodic, context);
  416. furi_timer_start(app->timer, period);
  417. }
  418. /**
  419. * @brief Callback when the user exits the game screen.
  420. * @details This function is called when the user exits the game screen. We stop the timer.
  421. * @param context The context - KeyCopierApp object.
  422. */
  423. static void key_copier_view_game_exit_callback(void* context) {
  424. KeyCopierApp* app = (KeyCopierApp*)context;
  425. furi_timer_stop(app->timer);
  426. furi_timer_free(app->timer);
  427. app->timer = NULL;
  428. }
  429. /**
  430. * @brief Callback for custom events.
  431. * @details This function is called when a custom event is sent to the view dispatcher.
  432. * @param event The event id - KeyCopierEventId value.
  433. * @param context The context - KeyCopierApp object.
  434. */
  435. static bool key_copier_view_game_custom_event_callback(uint32_t event, void* context) {
  436. KeyCopierApp* app = (KeyCopierApp*)context;
  437. switch(event) {
  438. case KeyCopierEventIdRedrawScreen:
  439. // Redraw screen by passing true to last parameter of with_view_model.
  440. {
  441. bool redraw = true;
  442. with_view_model(
  443. app->view_game, KeyCopierGameModel * _model, { UNUSED(_model); }, redraw);
  444. return true;
  445. }
  446. case KeyCopierEventIdOkPressed:
  447. // Process the OK button. We play a tone based on the x coordinate.
  448. return true;
  449. default:
  450. return false;
  451. }
  452. }
  453. /**
  454. * @brief Callback for game screen input.
  455. * @details This function is called when the user presses a button while on the game screen.
  456. * @param event The event - InputEvent object.
  457. * @param context The context - KeyCopierApp object.
  458. * @return true if the event was handled, false otherwise.
  459. */
  460. static bool key_copier_view_game_input_callback(InputEvent* event, void* context) {
  461. KeyCopierApp* app = (KeyCopierApp*)context;
  462. if(event->type == InputTypeShort) {
  463. switch(event->key) {
  464. case InputKeyLeft: {
  465. // Left button clicked, reduce x coordinate.
  466. bool redraw = true;
  467. with_view_model(
  468. app->view_game,
  469. KeyCopierGameModel * model,
  470. {
  471. if(model->pin_slc > 1) {
  472. model->pin_slc--;
  473. }
  474. },
  475. redraw);
  476. break;
  477. }
  478. case InputKeyRight: {
  479. // Left button clicked, reduce x coordinate.
  480. bool redraw = true;
  481. with_view_model(
  482. app->view_game,
  483. KeyCopierGameModel * model,
  484. {
  485. if(model->pin_slc < model->format.pin_num) {
  486. model->pin_slc++;
  487. }
  488. },
  489. redraw);
  490. break;
  491. }
  492. case InputKeyUp: {
  493. // Left button clicked, reduce x coordinate.
  494. bool redraw = true;
  495. with_view_model(
  496. app->view_game,
  497. KeyCopierGameModel * model,
  498. {
  499. if(model->depth[model->pin_slc - 1] > model->format.min_depth_ind) {
  500. if (model->pin_slc == 1) { //first pin only limited by the next one
  501. if (model->depth[model->pin_slc] - model->depth[model->pin_slc - 1] < model->format.macs - 1)
  502. model->depth[model->pin_slc - 1]--;
  503. } else if (model->pin_slc == model->format.pin_num) { //last pin only limited by the previous one
  504. if (model->depth[model->pin_slc - 2] - model->depth[model->pin_slc - 1] < model->format.macs - 1) {
  505. model->depth[model->pin_slc - 1]--;
  506. }
  507. } else{
  508. if (model->depth[model->pin_slc] - model->depth[model->pin_slc - 1] < model->format.macs - 1 &&
  509. model->depth[model->pin_slc - 2] - model->depth[model->pin_slc - 1] < model->format.macs - 1) {
  510. model->depth[model->pin_slc - 1]--;
  511. }
  512. }
  513. }
  514. },
  515. redraw);
  516. break;
  517. }
  518. case InputKeyDown: {
  519. // Right button clicked, increase x coordinate.
  520. bool redraw = true;
  521. with_view_model(
  522. app->view_game,
  523. KeyCopierGameModel * model,
  524. {
  525. if(model->depth[model->pin_slc - 1] < model->format.max_depth_ind) {
  526. if (model->pin_slc == 1) { //first pin only limited by the next one
  527. if (model->depth[model->pin_slc - 1] - model->depth[model->pin_slc] < model->format.macs - 1)
  528. model->depth[model->pin_slc - 1]++;
  529. } else if (model->pin_slc == model->format.pin_num) { //last pin only limited by the previous one
  530. if (model->depth[model->pin_slc - 1] - model->depth[model->pin_slc - 2] < model->format.macs - 1) {
  531. model->depth[model->pin_slc - 1]++;
  532. }
  533. } else{
  534. if (model->depth[model->pin_slc - 1] - model->depth[model->pin_slc] < model->format.macs - 1 &&
  535. model->depth[model->pin_slc - 1] - model->depth[model->pin_slc - 2] < model->format.macs - 1) {
  536. model->depth[model->pin_slc - 1]++;
  537. }
  538. }
  539. }
  540. },
  541. redraw);
  542. break;
  543. }
  544. default:
  545. // Handle other keys or do nothing
  546. break;
  547. }
  548. } else if(event->key == InputKeyOk) {
  549. // We choose to send a custom event when user presses OK button. key_copier_custom_event_callback will
  550. // handle our KeyCopierEventIdOkPressed event. We could have just put the code from
  551. // key_copier_custom_event_callback here, it's a matter of preference.
  552. view_dispatcher_send_custom_event(app->view_dispatcher, KeyCopierEventIdOkPressed);
  553. return true;
  554. }
  555. return false;
  556. }
  557. /**
  558. * @brief Allocate the key_copier application.
  559. * @details This function allocates the key_copier application resources.
  560. * @return KeyCopierApp object.
  561. */
  562. static KeyCopierApp* key_copier_app_alloc() {
  563. KeyCopierApp* app = (KeyCopierApp*)malloc(sizeof(KeyCopierApp));
  564. Gui* gui = furi_record_open(RECORD_GUI);
  565. app->view_dispatcher = view_dispatcher_alloc();
  566. view_dispatcher_enable_queue(app->view_dispatcher);
  567. view_dispatcher_attach_to_gui(app->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  568. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  569. app->submenu = submenu_alloc();
  570. submenu_add_item(
  571. app->submenu, "Measure", KeyCopierSubmenuIndexGame, key_copier_submenu_callback, app);
  572. submenu_add_item(
  573. app->submenu, "Config", KeyCopierSubmenuIndexConfigure, key_copier_submenu_callback, app);
  574. submenu_add_item(
  575. app->submenu, "About", KeyCopierSubmenuIndexAbout, key_copier_submenu_callback, app);
  576. view_set_previous_callback(submenu_get_view(app->submenu), key_copier_navigation_exit_callback);
  577. view_dispatcher_add_view(
  578. app->view_dispatcher, KeyCopierViewSubmenu, submenu_get_view(app->submenu));
  579. view_dispatcher_switch_to_view(app->view_dispatcher, KeyCopierViewSubmenu);
  580. app->text_input = text_input_alloc();
  581. view_dispatcher_add_view(
  582. app->view_dispatcher, KeyCopierViewTextInput, text_input_get_view(app->text_input));
  583. app->temp_buffer_size = 32;
  584. app->temp_buffer = (char*)malloc(app->temp_buffer_size);
  585. app->variable_item_list_config = variable_item_list_alloc();
  586. variable_item_list_reset(app->variable_item_list_config);
  587. VariableItem* item = variable_item_list_add(
  588. app->variable_item_list_config,
  589. total_pin_config_label,
  590. COUNT_OF(format_names),
  591. key_copier_total_pin_change,
  592. app);
  593. FuriString* key_name_str = furi_string_alloc();
  594. furi_string_set_str(key_name_str, key_name_default_value);
  595. app->key_name_item = variable_item_list_add(
  596. app->variable_item_list_config, key_name_config_label, 1, NULL, NULL);
  597. variable_item_set_current_value_text(
  598. app->key_name_item, furi_string_get_cstr(key_name_str));
  599. variable_item_list_set_enter_callback(
  600. app->variable_item_list_config, key_copier_setting_item_clicked, app);
  601. view_set_previous_callback(
  602. variable_item_list_get_view(app->variable_item_list_config),
  603. key_copier_navigation_submenu_callback);
  604. view_dispatcher_add_view(
  605. app->view_dispatcher,
  606. KeyCopierViewConfigure,
  607. variable_item_list_get_view(app->variable_item_list_config));
  608. app->view_game = view_alloc();
  609. view_set_draw_callback(app->view_game, key_copier_view_game_draw_callback);
  610. view_set_input_callback(app->view_game, key_copier_view_game_input_callback);
  611. view_set_previous_callback(app->view_game, key_copier_navigation_submenu_callback);
  612. view_set_enter_callback(app->view_game, key_copier_view_game_enter_callback);
  613. view_set_exit_callback(app->view_game, key_copier_view_game_exit_callback);
  614. view_set_context(app->view_game, app);
  615. view_set_custom_callback(app->view_game, key_copier_view_game_custom_event_callback);
  616. view_allocate_model(app->view_game, ViewModelTypeLockFree, sizeof(KeyCopierGameModel));
  617. KeyCopierGameModel* model = view_get_model(app->view_game);
  618. initialize_model(model);
  619. model->key_name_str = key_name_str;
  620. model->pin_slc = 1;
  621. model->total_pin = model->format.pin_num;
  622. variable_item_set_current_value_index(item, model->format_index);
  623. variable_item_set_current_value_text(item, format_names[model->format_index]);
  624. view_dispatcher_add_view(app->view_dispatcher, KeyCopierViewGame, app->view_game);
  625. app->widget_about = widget_alloc();
  626. widget_add_text_scroll_element(
  627. app->widget_about,
  628. 0,
  629. 0,
  630. 128,
  631. 64,
  632. "Key Maker App 0.1\nGithub: https://github.com/zinongli/KeyCopier \nBased on Derak Jamison's \nSkeleton App\nProject channel: \nhttps://discord.gg/BwNar4pAQ9");
  633. view_set_previous_callback(
  634. widget_get_view(app->widget_about), key_copier_navigation_submenu_callback);
  635. view_dispatcher_add_view(
  636. app->view_dispatcher, KeyCopierViewAbout, widget_get_view(app->widget_about));
  637. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  638. #ifdef BACKLIGHT_ON
  639. notification_message(app->notifications, &sequence_display_backlight_enforce_on);
  640. #endif
  641. return app;
  642. }
  643. /**
  644. * @brief Free the key_copier application.
  645. * @details This function frees the key_copier application resources.
  646. * @param app The key_copier application object.
  647. */
  648. static void key_copier_app_free(KeyCopierApp* app) {
  649. #ifdef BACKLIGHT_ON
  650. notification_message(app->notifications, &sequence_display_backlight_enforce_auto);
  651. #endif
  652. furi_record_close(RECORD_NOTIFICATION);
  653. view_dispatcher_remove_view(app->view_dispatcher, KeyCopierViewTextInput);
  654. text_input_free(app->text_input);
  655. free(app->temp_buffer);
  656. view_dispatcher_remove_view(app->view_dispatcher, KeyCopierViewAbout);
  657. widget_free(app->widget_about);
  658. view_dispatcher_remove_view(app->view_dispatcher, KeyCopierViewGame);
  659. with_view_model(
  660. app->view_game,
  661. KeyCopierGameModel * model,
  662. {
  663. if(model->depth != NULL) {
  664. free(model->depth);
  665. }
  666. },
  667. false);
  668. view_free(app->view_game);
  669. view_dispatcher_remove_view(app->view_dispatcher, KeyCopierViewConfigure);
  670. variable_item_list_free(app->variable_item_list_config);
  671. view_dispatcher_remove_view(app->view_dispatcher, KeyCopierViewSubmenu);
  672. submenu_free(app->submenu);
  673. view_dispatcher_free(app->view_dispatcher);
  674. furi_record_close(RECORD_GUI);
  675. free(app);
  676. }
  677. /**
  678. * @brief Main function for key_copier application.
  679. * @details This function is the entry point for the key_copier application. It should be defined in
  680. * application.fam as the entry_point setting.
  681. * @param _p Input parameter - unused
  682. * @return 0 - Success
  683. */
  684. int32_t main_key_copier_app(void* _p) {
  685. UNUSED(_p);
  686. KeyCopierApp* app = key_copier_app_alloc();
  687. view_dispatcher_run(app->view_dispatcher);
  688. key_copier_app_free(app);
  689. return 0;
  690. }