app.c 29 KB

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