app.c 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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_px 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_px = (double)INCHES_PER_px;
  233. static void key_copier_view_game_draw_callback(Canvas* canvas, void* model) {
  234. KeyMakerGameModel* my_model = (KeyMakerGameModel*)model;
  235. KeyFormat my_format = my_model->format;
  236. int pin_half_width_px = (int)round((my_format.pin_width_inch / inches_per_px) / 2);
  237. int pin_step_px = (int)round(my_format.pin_increment_inch / inches_per_px);
  238. double drill_radians = (180 - my_format.drill_angle) / 2 / 180 * (double)M_PI; // Convert angle to radians
  239. double tangent = tan(drill_radians);
  240. int post_extra_x_px = 0;
  241. int pre_extra_x_px = 0;
  242. for (int current_pin = 1; current_pin <= my_model->total_pin; current_pin += 1) {
  243. double current_center_px = my_format.first_pin_inch + (current_pin - 1) * my_format.pin_increment_inch;
  244. int pin_center_px = (int)round(current_center_px / inches_per_px);
  245. int top_contour_px = (int)round(63 - my_format.uncut_depth_inch / inches_per_px);
  246. canvas_draw_line(canvas, pin_center_px, 25, pin_center_px, 50);
  247. int current_depth = my_model->depth[current_pin - 1] - my_format.min_depth_ind;
  248. int current_depth_px = (int)round(current_depth * my_format.depth_step_inch / inches_per_px);
  249. 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);
  250. int last_depth = my_model->depth[current_pin - 2] - my_format.min_depth_ind;
  251. int next_depth = my_model->depth[current_pin] - my_format.min_depth_ind;
  252. if(current_pin == 1){
  253. canvas_draw_line(canvas, 0, top_contour_px, pin_center_px - pin_half_width_px - current_depth_px, top_contour_px);
  254. last_depth = 0;
  255. pre_extra_x_px = max(current_depth_px + pin_half_width_px, 0);
  256. }
  257. if(current_pin == my_model->total_pin) {
  258. next_depth = my_format.min_depth_ind;
  259. }
  260. if ((last_depth + current_depth) > my_format.clearance && current_depth != my_format.min_depth_ind) { //yes intersection
  261. 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);}
  262. canvas_draw_line(
  263. canvas,
  264. pin_center_px - pre_extra_x_px,
  265. top_contour_px + max((int)round((current_depth_px - (pre_extra_x_px - pin_half_width_px)) * tangent),0),
  266. pin_center_px - pin_half_width_px,
  267. top_contour_px + (int)round(current_depth_px * tangent)
  268. );
  269. } else {
  270. int last_depth_px = (int)round(last_depth * my_format.depth_step_inch / inches_per_px);
  271. int down_slope_start_x_px = pin_center_px - pin_half_width_px - current_depth_px;
  272. canvas_draw_line(
  273. canvas,
  274. pin_center_px - pin_half_width_px - current_depth_px,
  275. top_contour_px,
  276. pin_center_px - pin_half_width_px,
  277. top_contour_px + (int)round(current_depth_px * tangent)
  278. );
  279. canvas_draw_line(
  280. canvas,
  281. min(pin_center_px - pin_step_px + pin_half_width_px + last_depth_px, down_slope_start_x_px),
  282. top_contour_px,
  283. down_slope_start_x_px,
  284. top_contour_px
  285. );
  286. }
  287. if ((current_depth + next_depth) > my_format.clearance && current_depth != my_format.min_depth_ind) { //yes intersection
  288. double numerator = (double)current_depth;
  289. double denominator = (double)(current_depth + next_depth);
  290. double product = (numerator / denominator) * pin_step_px;
  291. post_extra_x_px = (int)min(max(product,pin_half_width_px),pin_step_px - pin_half_width_px);
  292. canvas_draw_line(
  293. canvas,
  294. pin_center_px + pin_half_width_px,
  295. top_contour_px + current_depth_px,
  296. pin_center_px + post_extra_x_px,
  297. top_contour_px + max(current_depth_px - (int)round((post_extra_x_px - pin_half_width_px) * tangent),0)
  298. );
  299. } else { // no intersection
  300. canvas_draw_line(
  301. canvas,
  302. pin_center_px + pin_half_width_px,
  303. top_contour_px + (int)round(current_depth_px * tangent),
  304. pin_center_px + pin_half_width_px + current_depth_px,
  305. top_contour_px
  306. );
  307. }
  308. }
  309. int level_contour_px = (int)round((my_format.last_pin_inch + my_format.pin_increment_inch) / inches_per_px - 4);
  310. canvas_draw_line(canvas, 0, 62, level_contour_px, 62);
  311. int step_px = (int)round(my_format.pin_increment_inch / inches_per_px);
  312. canvas_draw_line(canvas, level_contour_px, 62, level_contour_px+step_px, 62-step_px);
  313. int slc_pin_px = (int)round((my_format.first_pin_inch + (my_model->pin_slc - 1) * my_format.pin_increment_inch)/ inches_per_px);
  314. canvas_draw_str(canvas, slc_pin_px-2, 23, "*");
  315. FuriString* xstr = furi_string_alloc();
  316. int buffer_size = my_model->total_pin + 1;
  317. char depth_str[buffer_size];
  318. depth_str[0] = '\0'; // Initialize the string
  319. // Manual string concatenation
  320. char* pos = depth_str;
  321. for (int i = 0; i < my_model->total_pin; i++) {
  322. int written = snprintf(pos, buffer_size - (pos - depth_str), "%u", my_model->depth[i]);
  323. if (written < 0 || written >= buffer_size - (pos - depth_str)) {
  324. // Handle error
  325. break;
  326. }
  327. pos += written;
  328. }
  329. furi_string_printf(xstr, "bitting: %s", depth_str);
  330. canvas_draw_str(canvas, 0, 10, furi_string_get_cstr(xstr));
  331. //furi_string_printf(xstr, "Num of Pins: %s", format_names[my_model->format_index]);
  332. //canvas_draw_str(canvas, 44, 24, furi_string_get_cstr(xstr));
  333. furi_string_free(xstr);
  334. }
  335. /**
  336. * @brief Callback for timer elapsed.
  337. * @details This function is called when the timer is elapsed. We use this to queue a redraw event.
  338. * @param context The context - KeyMakerApp object.
  339. */
  340. static void key_copier_view_game_timer_callback(void* context) {
  341. KeyMakerApp* app = (KeyMakerApp*)context;
  342. view_dispatcher_send_custom_event(app->view_dispatcher, KeyMakerEventIdRedrawScreen);
  343. }
  344. /**
  345. * @brief Callback when the user starts the game screen.
  346. * @details This function is called when the user enters the game screen. We start a timer to
  347. * redraw the screen periodically (so the random number is refreshed).
  348. * @param context The context - KeyMakerApp object.
  349. */
  350. static void key_copier_view_game_enter_callback(void* context) {
  351. uint32_t period = furi_ms_to_ticks(200);
  352. KeyMakerApp* app = (KeyMakerApp*)context;
  353. furi_assert(app->timer == NULL);
  354. app->timer =
  355. furi_timer_alloc(key_copier_view_game_timer_callback, FuriTimerTypePeriodic, context);
  356. furi_timer_start(app->timer, period);
  357. }
  358. /**
  359. * @brief Callback when the user exits the game screen.
  360. * @details This function is called when the user exits the game screen. We stop the timer.
  361. * @param context The context - KeyMakerApp object.
  362. */
  363. static void key_copier_view_game_exit_callback(void* context) {
  364. KeyMakerApp* app = (KeyMakerApp*)context;
  365. furi_timer_stop(app->timer);
  366. furi_timer_free(app->timer);
  367. app->timer = NULL;
  368. }
  369. /**
  370. * @brief Callback for custom events.
  371. * @details This function is called when a custom event is sent to the view dispatcher.
  372. * @param event The event id - KeyMakerEventId value.
  373. * @param context The context - KeyMakerApp object.
  374. */
  375. static bool key_copier_view_game_custom_event_callback(uint32_t event, void* context) {
  376. KeyMakerApp* app = (KeyMakerApp*)context;
  377. switch(event) {
  378. case KeyMakerEventIdRedrawScreen:
  379. // Redraw screen by passing true to last parameter of with_view_model.
  380. {
  381. bool redraw = true;
  382. with_view_model(
  383. app->view_game, KeyMakerGameModel * _model, { UNUSED(_model); }, redraw);
  384. return true;
  385. }
  386. case KeyMakerEventIdOkPressed:
  387. // Process the OK button. We play a tone based on the x coordinate.
  388. return true;
  389. default:
  390. return false;
  391. }
  392. }
  393. /**
  394. * @brief Callback for game screen input.
  395. * @details This function is called when the user presses a button while on the game screen.
  396. * @param event The event - InputEvent object.
  397. * @param context The context - KeyMakerApp object.
  398. * @return true if the event was handled, false otherwise.
  399. */
  400. static bool key_copier_view_game_input_callback(InputEvent* event, void* context) {
  401. KeyMakerApp* app = (KeyMakerApp*)context;
  402. if(event->type == InputTypeShort) {
  403. switch(event->key) {
  404. case InputKeyLeft: {
  405. // Left button clicked, reduce x coordinate.
  406. bool redraw = true;
  407. with_view_model(
  408. app->view_game,
  409. KeyMakerGameModel * model,
  410. {
  411. if(model->pin_slc > 1) {
  412. model->pin_slc--;
  413. }
  414. },
  415. redraw);
  416. break;
  417. }
  418. case InputKeyRight: {
  419. // Left button clicked, reduce x coordinate.
  420. bool redraw = true;
  421. with_view_model(
  422. app->view_game,
  423. KeyMakerGameModel * model,
  424. {
  425. if(model->pin_slc < model->format.pin_num) {
  426. model->pin_slc++;
  427. }
  428. },
  429. redraw);
  430. break;
  431. }
  432. case InputKeyUp: {
  433. // Left button clicked, reduce x coordinate.
  434. bool redraw = true;
  435. with_view_model(
  436. app->view_game,
  437. KeyMakerGameModel * model,
  438. {
  439. if(model->depth[model->pin_slc - 1] > model->format.min_depth_ind) {
  440. if (model->pin_slc == 1) { //first pin only limited by the next one
  441. if (model->depth[model->pin_slc] - model->depth[model->pin_slc - 1] < model->format.macs - 1)
  442. model->depth[model->pin_slc - 1]--;
  443. } else if (model->pin_slc == model->format.pin_num) { //last pin only limited by the previous one
  444. if (model->depth[model->pin_slc - 2] - model->depth[model->pin_slc - 1] < model->format.macs - 1) {
  445. model->depth[model->pin_slc - 1]--;
  446. }
  447. } else{
  448. if (model->depth[model->pin_slc] - model->depth[model->pin_slc - 1] < model->format.macs - 1 &&
  449. model->depth[model->pin_slc - 2] - model->depth[model->pin_slc - 1] < model->format.macs - 1) {
  450. model->depth[model->pin_slc - 1]--;
  451. }
  452. }
  453. }
  454. },
  455. redraw);
  456. break;
  457. }
  458. case InputKeyDown: {
  459. // Right button clicked, increase x coordinate.
  460. bool redraw = true;
  461. with_view_model(
  462. app->view_game,
  463. KeyMakerGameModel * model,
  464. {
  465. if(model->depth[model->pin_slc - 1] < model->format.max_depth_ind) {
  466. if (model->pin_slc == 1) { //first pin only limited by the next one
  467. if (model->depth[model->pin_slc - 1] - model->depth[model->pin_slc] < model->format.macs - 1)
  468. model->depth[model->pin_slc - 1]++;
  469. } else if (model->pin_slc == model->format.pin_num) { //last pin only limited by the previous one
  470. if (model->depth[model->pin_slc - 1] - model->depth[model->pin_slc - 2] < model->format.macs - 1) {
  471. model->depth[model->pin_slc - 1]++;
  472. }
  473. } else{
  474. if (model->depth[model->pin_slc - 1] - model->depth[model->pin_slc] < model->format.macs - 1 &&
  475. model->depth[model->pin_slc - 1] - model->depth[model->pin_slc - 2] < model->format.macs - 1) {
  476. model->depth[model->pin_slc - 1]++;
  477. }
  478. }
  479. }
  480. },
  481. redraw);
  482. break;
  483. }
  484. default:
  485. // Handle other keys or do nothing
  486. break;
  487. }
  488. } else if(event->type == InputTypePress) {
  489. if(event->key == InputKeyOk) {
  490. // We choose to send a custom event when user presses OK button. key_copier_custom_event_callback will
  491. // handle our KeyMakerEventIdOkPressed event. We could have just put the code from
  492. // key_copier_custom_event_callback here, it's a matter of preference.
  493. view_dispatcher_send_custom_event(app->view_dispatcher, KeyMakerEventIdOkPressed);
  494. return true;
  495. }
  496. }
  497. return false;
  498. }
  499. /**
  500. * @brief Allocate the key_copier application.
  501. * @details This function allocates the key_copier application resources.
  502. * @return KeyMakerApp object.
  503. */
  504. static KeyMakerApp* key_copier_app_alloc() {
  505. KeyMakerApp* app = (KeyMakerApp*)malloc(sizeof(KeyMakerApp));
  506. Gui* gui = furi_record_open(RECORD_GUI);
  507. app->view_dispatcher = view_dispatcher_alloc();
  508. view_dispatcher_enable_queue(app->view_dispatcher);
  509. view_dispatcher_attach_to_gui(app->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  510. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  511. app->submenu = submenu_alloc();
  512. submenu_add_item(
  513. app->submenu, "Measure", KeyMakerSubmenuIndexGame, key_copier_submenu_callback, app);
  514. submenu_add_item(
  515. app->submenu, "Config", KeyMakerSubmenuIndexConfigure, key_copier_submenu_callback, app);
  516. submenu_add_item(
  517. app->submenu, "About", KeyMakerSubmenuIndexAbout, key_copier_submenu_callback, app);
  518. view_set_previous_callback(submenu_get_view(app->submenu), key_copier_navigation_exit_callback);
  519. view_dispatcher_add_view(
  520. app->view_dispatcher, KeyMakerViewSubmenu, submenu_get_view(app->submenu));
  521. view_dispatcher_switch_to_view(app->view_dispatcher, KeyMakerViewSubmenu);
  522. app->text_input = text_input_alloc();
  523. view_dispatcher_add_view(
  524. app->view_dispatcher, KeyMakerViewTextInput, text_input_get_view(app->text_input));
  525. app->temp_buffer_size = 32;
  526. app->temp_buffer = (char*)malloc(app->temp_buffer_size);
  527. app->variable_item_list_config = variable_item_list_alloc();
  528. variable_item_list_reset(app->variable_item_list_config);
  529. VariableItem* item = variable_item_list_add(
  530. app->variable_item_list_config,
  531. total_pin_config_label,
  532. COUNT_OF(format_names),
  533. key_copier_total_pin_change,
  534. app);
  535. FuriString* key_name_str = furi_string_alloc();
  536. furi_string_set_str(key_name_str, key_name_default_value);
  537. app->key_name_item = variable_item_list_add(
  538. app->variable_item_list_config, key_name_config_label, 1, NULL, NULL);
  539. variable_item_set_current_value_text(
  540. app->key_name_item, furi_string_get_cstr(key_name_str));
  541. variable_item_list_set_enter_callback(
  542. app->variable_item_list_config, key_copier_setting_item_clicked, app);
  543. view_set_previous_callback(
  544. variable_item_list_get_view(app->variable_item_list_config),
  545. key_copier_navigation_submenu_callback);
  546. view_dispatcher_add_view(
  547. app->view_dispatcher,
  548. KeyMakerViewConfigure,
  549. variable_item_list_get_view(app->variable_item_list_config));
  550. app->view_game = view_alloc();
  551. view_set_draw_callback(app->view_game, key_copier_view_game_draw_callback);
  552. view_set_input_callback(app->view_game, key_copier_view_game_input_callback);
  553. view_set_previous_callback(app->view_game, key_copier_navigation_submenu_callback);
  554. view_set_enter_callback(app->view_game, key_copier_view_game_enter_callback);
  555. view_set_exit_callback(app->view_game, key_copier_view_game_exit_callback);
  556. view_set_context(app->view_game, app);
  557. view_set_custom_callback(app->view_game, key_copier_view_game_custom_event_callback);
  558. view_allocate_model(app->view_game, ViewModelTypeLockFree, sizeof(KeyMakerGameModel));
  559. KeyMakerGameModel* model = view_get_model(app->view_game);
  560. initialize_model(model);
  561. model->key_name_str = key_name_str;
  562. model->pin_slc = 1;
  563. model->total_pin = model->format.pin_num;
  564. variable_item_set_current_value_index(item, model->format_index);
  565. variable_item_set_current_value_text(item, format_names[model->format_index]);
  566. view_dispatcher_add_view(app->view_dispatcher, KeyMakerViewGame, app->view_game);
  567. app->widget_about = widget_alloc();
  568. widget_add_text_scroll_element(
  569. app->widget_about,
  570. 0,
  571. 0,
  572. 128,
  573. 64,
  574. "Key Maker App 0.1\nGithub: https://github.com/zinongli/KeyCopier \nBased on Derak Jamison's \nSkeleton App\nProject channel: \nhttps://discord.gg/BwNar4pAQ9");
  575. view_set_previous_callback(
  576. widget_get_view(app->widget_about), key_copier_navigation_submenu_callback);
  577. view_dispatcher_add_view(
  578. app->view_dispatcher, KeyMakerViewAbout, widget_get_view(app->widget_about));
  579. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  580. #ifdef BACKLIGHT_ON
  581. notification_message(app->notifications, &sequence_display_backlight_enforce_on);
  582. #endif
  583. return app;
  584. }
  585. /**
  586. * @brief Free the key_copier application.
  587. * @details This function frees the key_copier application resources.
  588. * @param app The key_copier application object.
  589. */
  590. static void key_copier_app_free(KeyMakerApp* app) {
  591. #ifdef BACKLIGHT_ON
  592. notification_message(app->notifications, &sequence_display_backlight_enforce_auto);
  593. #endif
  594. furi_record_close(RECORD_NOTIFICATION);
  595. view_dispatcher_remove_view(app->view_dispatcher, KeyMakerViewTextInput);
  596. text_input_free(app->text_input);
  597. free(app->temp_buffer);
  598. view_dispatcher_remove_view(app->view_dispatcher, KeyMakerViewAbout);
  599. widget_free(app->widget_about);
  600. view_dispatcher_remove_view(app->view_dispatcher, KeyMakerViewGame);
  601. with_view_model(
  602. app->view_game,
  603. KeyMakerGameModel * model,
  604. {
  605. if(model->depth != NULL) {
  606. free(model->depth);
  607. }
  608. },
  609. false);
  610. view_free(app->view_game);
  611. view_dispatcher_remove_view(app->view_dispatcher, KeyMakerViewConfigure);
  612. variable_item_list_free(app->variable_item_list_config);
  613. view_dispatcher_remove_view(app->view_dispatcher, KeyMakerViewSubmenu);
  614. submenu_free(app->submenu);
  615. view_dispatcher_free(app->view_dispatcher);
  616. furi_record_close(RECORD_GUI);
  617. free(app);
  618. }
  619. /**
  620. * @brief Main function for key_copier application.
  621. * @details This function is the entry point for the key_copier application. It should be defined in
  622. * application.fam as the entry_point setting.
  623. * @param _p Input parameter - unused
  624. * @return 0 - Success
  625. */
  626. int32_t main_key_copier_app(void* _p) {
  627. UNUSED(_p);
  628. KeyMakerApp* app = key_copier_app_alloc();
  629. view_dispatcher_run(app->view_dispatcher);
  630. key_copier_app_free(app);
  631. return 0;
  632. }