app.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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_maker_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_depths(KeyMakerGameModel* model) {
  67. if(model->depth != NULL) {
  68. free(model->depth);
  69. }
  70. model->depth = (uint8_t*)malloc((model->total_pin + 1) * sizeof(uint8_t));
  71. for(uint8_t i = 0; i <= model->total_pin; i++) {
  72. model->depth[i] = 0;
  73. }
  74. }
  75. void initialize_format(KeyMakerGameModel* model) {
  76. memcpy(&model->format, &all_formats[0], sizeof(KeyFormat));
  77. }
  78. /**
  79. * @brief Callback for exiting the application.
  80. * @details This function is called when user press back button. We return VIEW_NONE to
  81. * indicate that we want to exit the application.
  82. * @param _context The context - unused
  83. * @return next view id
  84. */
  85. static uint32_t key_maker_navigation_exit_callback(void* _context) {
  86. UNUSED(_context);
  87. return VIEW_NONE;
  88. }
  89. /**
  90. * @brief Callback for returning to submenu.
  91. * @details This function is called when user press back button. We return VIEW_NONE to
  92. * indicate that we want to navigate to the submenu.
  93. * @param _context The context - unused
  94. * @return next view id
  95. */
  96. static uint32_t key_maker_navigation_submenu_callback(void* _context) {
  97. UNUSED(_context);
  98. return KeyMakerViewSubmenu;
  99. }
  100. /**
  101. * @brief Callback for returning to configure screen.
  102. * @details This function is called when user press back button. We return VIEW_NONE to
  103. * indicate that we want to navigate to the configure screen.
  104. * @param _context The context - unused
  105. * @return next view id
  106. */
  107. static uint32_t key_maker_navigation_configure_callback(void* _context) {
  108. UNUSED(_context);
  109. return KeyMakerViewConfigure;
  110. }
  111. /**
  112. * @brief Handle submenu item selection.
  113. * @details This function is called when user selects an item from the submenu.
  114. * @param context The context - KeyMakerApp object.
  115. * @param index The KeyMakerSubmenuIndex item that was clicked.
  116. */
  117. static void key_maker_submenu_callback(void* context, uint32_t index) {
  118. KeyMakerApp* app = (KeyMakerApp*)context;
  119. switch(index) {
  120. case KeyMakerSubmenuIndexConfigure:
  121. view_dispatcher_switch_to_view(app->view_dispatcher, KeyMakerViewConfigure);
  122. break;
  123. case KeyMakerSubmenuIndexGame:
  124. view_dispatcher_switch_to_view(app->view_dispatcher, KeyMakerViewGame);
  125. break;
  126. case KeyMakerSubmenuIndexAbout:
  127. view_dispatcher_switch_to_view(app->view_dispatcher, KeyMakerViewAbout);
  128. break;
  129. default:
  130. break;
  131. }
  132. }
  133. /**
  134. * Our 1st sample setting is a team color. We have 3 options: red, green, and blue.
  135. */
  136. static const char* total_pin_config_label = "Key Format";
  137. static char* format_names[] = {"Kwikset", "Schlage"};
  138. static void key_maker_total_pin_change(VariableItem* item) {
  139. KeyMakerApp* app = variable_item_get_context(item);
  140. uint8_t format_index = variable_item_get_current_value_index(item);
  141. variable_item_set_current_value_text(item, format_names[format_index]);
  142. KeyMakerGameModel* model = view_get_model(app->view_game);
  143. model->format_index = format_index;
  144. model->format = all_formats[format_index];
  145. model->total_pin = model->format.pin_num;
  146. if(model->depth != NULL) {
  147. free(model->depth);
  148. }
  149. model->depth = (uint8_t*)malloc((model->total_pin + 1) * sizeof(uint8_t));
  150. }
  151. /**
  152. * Our 2nd sample setting is a text field. When the user clicks OK on the configuration
  153. * setting we use a text input screen to allow the user to enter a name. This function is
  154. * called when the user clicks OK on the text input screen.
  155. */
  156. static const char* key_name_config_label = "Key Name";
  157. static const char* key_name_entry_text = "Enter name";
  158. static const char* key_name_default_value = "Key 1";
  159. static void key_maker_key_name_text_updated(void* context) {
  160. KeyMakerApp* app = (KeyMakerApp*)context;
  161. bool redraw = true;
  162. with_view_model(
  163. app->view_game,
  164. KeyMakerGameModel * model,
  165. {
  166. furi_string_set(model->key_name_str, app->temp_buffer);
  167. variable_item_set_current_value_text(
  168. app->key_name_item, furi_string_get_cstr(model->key_name_str));
  169. },
  170. redraw);
  171. view_dispatcher_switch_to_view(app->view_dispatcher, KeyMakerViewConfigure);
  172. }
  173. /**
  174. * @brief Callback when item in configuration screen is clicked.
  175. * @details This function is called when user clicks OK on an item in the configuration screen.
  176. * If the item clicked is our text field then we switch to the text input screen.
  177. * @param context The context - KeyMakerApp object.
  178. * @param index - The index of the item that was clicked.
  179. */
  180. static void key_maker_setting_item_clicked(void* context, uint32_t index) {
  181. KeyMakerApp* app = (KeyMakerApp*)context;
  182. index++; // The index starts at zero, but we want to start at 1.
  183. // Our configuration UI has the 2nd item as a text field.
  184. if(index == 2) {
  185. // Header to display on the text input screen.
  186. text_input_set_header_text(app->text_input, key_name_entry_text);
  187. // Copy the current name into the temporary buffer.
  188. bool redraw = false;
  189. with_view_model(
  190. app->view_game,
  191. KeyMakerGameModel * model,
  192. {
  193. strncpy(
  194. app->temp_buffer,
  195. furi_string_get_cstr(model->key_name_str),
  196. app->temp_buffer_size);
  197. },
  198. redraw);
  199. // Configure the text input. When user enters text and clicks OK, key_maker_setting_text_updated be called.
  200. bool clear_previous_text = false;
  201. text_input_set_result_callback(
  202. app->text_input,
  203. key_maker_key_name_text_updated,
  204. app,
  205. app->temp_buffer,
  206. app->temp_buffer_size,
  207. clear_previous_text);
  208. // Pressing the BACK button will reload the configure screen.
  209. view_set_previous_callback(
  210. text_input_get_view(app->text_input), key_maker_navigation_configure_callback);
  211. // Show text input dialog.
  212. view_dispatcher_switch_to_view(app->view_dispatcher, KeyMakerViewTextInput);
  213. }
  214. }
  215. static inline int min(int a, int b) {
  216. return (a < b) ? a : b;
  217. }
  218. /**
  219. * @brief Callback for drawing the game screen.
  220. * @details This function is called when the screen needs to be redrawn, like when the model gets updated.
  221. * @param canvas The canvas to draw on.
  222. * @param model The model - MyModel object.
  223. */
  224. static double inches_per_pixel = (double)INCHES_PER_PIXEL;
  225. static int pin_half_width_pixel;
  226. static int pin_step_pixel;
  227. static void key_maker_view_game_draw_callback(Canvas* canvas, void* model) {
  228. KeyMakerGameModel* my_model = (KeyMakerGameModel*)model;
  229. KeyFormat my_format = my_model->format;
  230. static bool initialized = false;
  231. if (!initialized) {
  232. pin_half_width_pixel = (int)round(my_format.pin_width_inch / inches_per_pixel / 2);
  233. pin_step_pixel = (int)round(my_format.pin_increment_inch / inches_per_pixel);
  234. initialized = true;
  235. }
  236. int pin_step_pixel = (int)round(my_format.pin_increment_inch / inches_per_pixel);
  237. int post_extra_x_pixel = pin_step_pixel;
  238. for (int current_pin = 1; current_pin <= my_model->total_pin; current_pin += 1) {
  239. double current_center_pixel = my_format.first_pin_inch + (current_pin - 1) * my_format.pin_increment_inch;
  240. int pin_center_pixel = (int)round(current_center_pixel / inches_per_pixel);
  241. int top_contour_pixel = (int)round(63 - my_format.uncut_depth_inch / inches_per_pixel);
  242. canvas_draw_line(canvas, pin_center_pixel, 20, pin_center_pixel, 50);
  243. int depth_pixel_i = (int)round(my_model->depth[current_pin - 1] * my_format.depth_step_inch / inches_per_pixel);
  244. canvas_draw_line(canvas, pin_center_pixel - pin_half_width_pixel, top_contour_pixel + depth_pixel_i, pin_center_pixel + pin_half_width_pixel, top_contour_pixel + depth_pixel_i);
  245. int last_depth = my_model->depth[current_pin - 2];
  246. int next_depth = my_model->depth[current_pin];
  247. if(current_pin == 1){
  248. canvas_draw_line(canvas, 0, top_contour_pixel, pin_center_pixel - pin_half_width_pixel - depth_pixel_i, top_contour_pixel);
  249. last_depth = 0;
  250. }
  251. if(current_pin == my_model->total_pin) {
  252. next_depth = my_format.max_depth_ind;
  253. double numerator = (double)my_model->depth[current_pin - 1];
  254. double denominator = (double)(my_model->depth[current_pin - 1] + next_depth);
  255. double product = numerator / denominator * pin_step_pixel;
  256. int extra_x_pixel = (int)round(product);
  257. canvas_draw_line(
  258. canvas,
  259. pin_center_pixel + extra_x_pixel,
  260. top_contour_pixel + depth_pixel_i - (extra_x_pixel - pin_half_width_pixel),
  261. 128,
  262. top_contour_pixel + (128 - (pin_center_pixel + extra_x_pixel))
  263. );
  264. }
  265. if ((last_depth + my_model->depth[current_pin - 1]) > 4) { //yes intersection
  266. int pre_extra_x_pixel = pin_step_pixel - post_extra_x_pixel;
  267. canvas_draw_line(
  268. canvas,
  269. pin_center_pixel - pre_extra_x_pixel,
  270. top_contour_pixel + depth_pixel_i - (pre_extra_x_pixel - pin_half_width_pixel),
  271. pin_center_pixel - pin_half_width_pixel,
  272. top_contour_pixel + depth_pixel_i
  273. );
  274. } else {
  275. canvas_draw_line(
  276. canvas,
  277. pin_center_pixel - pin_half_width_pixel - depth_pixel_i,
  278. top_contour_pixel,
  279. pin_center_pixel - pin_half_width_pixel,
  280. top_contour_pixel + depth_pixel_i
  281. );
  282. }
  283. if ((my_model->depth[current_pin - 1] + next_depth) > 4) { //yes intersection
  284. double numerator = (double)my_model->depth[current_pin - 1];
  285. double denominator = (double)(my_model->depth[current_pin - 1] + next_depth);
  286. double product = numerator / denominator * pin_step_pixel;
  287. post_extra_x_pixel = (int)round(product);
  288. canvas_draw_line(
  289. canvas,
  290. pin_center_pixel + pin_half_width_pixel,
  291. top_contour_pixel + depth_pixel_i,
  292. pin_center_pixel + post_extra_x_pixel,
  293. top_contour_pixel + depth_pixel_i - (post_extra_x_pixel - pin_half_width_pixel)
  294. );
  295. } else { // no intersection
  296. canvas_draw_line(
  297. canvas,
  298. pin_center_pixel + pin_half_width_pixel,
  299. top_contour_pixel + depth_pixel_i,
  300. pin_center_pixel + pin_half_width_pixel + depth_pixel_i,
  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, 63, level_contour_pixel, 63);
  307. int step_pixel = (int)round(my_format.pin_increment_inch / inches_per_pixel);
  308. canvas_draw_line(canvas, level_contour_pixel, 63, level_contour_pixel+step_pixel, 63-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, 18, "*");
  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, "depth: %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_maker_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_maker_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_maker_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_maker_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_maker_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_maker_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.start_depth_ind) {
  436. model->depth[model->pin_slc - 1]--;
  437. }
  438. },
  439. redraw);
  440. break;
  441. }
  442. case InputKeyDown: {
  443. // Right button clicked, increase x coordinate.
  444. bool redraw = true;
  445. with_view_model(
  446. app->view_game,
  447. KeyMakerGameModel * model,
  448. {
  449. if(model->depth[model->pin_slc - 1] <= model->format.max_depth_ind) {
  450. model->depth[model->pin_slc - 1]++;
  451. }
  452. },
  453. redraw);
  454. break;
  455. }
  456. default:
  457. // Handle other keys or do nothing
  458. break;
  459. }
  460. } else if(event->type == InputTypePress) {
  461. if(event->key == InputKeyOk) {
  462. // We choose to send a custom event when user presses OK button. key_maker_custom_event_callback will
  463. // handle our KeyMakerEventIdOkPressed event. We could have just put the code from
  464. // key_maker_custom_event_callback here, it's a matter of preference.
  465. view_dispatcher_send_custom_event(app->view_dispatcher, KeyMakerEventIdOkPressed);
  466. return true;
  467. }
  468. }
  469. return false;
  470. }
  471. /**
  472. * @brief Allocate the key_maker application.
  473. * @details This function allocates the key_maker application resources.
  474. * @return KeyMakerApp object.
  475. */
  476. static KeyMakerApp* key_maker_app_alloc() {
  477. KeyMakerApp* app = (KeyMakerApp*)malloc(sizeof(KeyMakerApp));
  478. Gui* gui = furi_record_open(RECORD_GUI);
  479. app->view_dispatcher = view_dispatcher_alloc();
  480. view_dispatcher_enable_queue(app->view_dispatcher);
  481. view_dispatcher_attach_to_gui(app->view_dispatcher, gui, ViewDispatcherTypeFullscreen);
  482. view_dispatcher_set_event_callback_context(app->view_dispatcher, app);
  483. app->submenu = submenu_alloc();
  484. submenu_add_item(
  485. app->submenu, "Config", KeyMakerSubmenuIndexConfigure, key_maker_submenu_callback, app);
  486. submenu_add_item(
  487. app->submenu, "Play", KeyMakerSubmenuIndexGame, key_maker_submenu_callback, app);
  488. submenu_add_item(
  489. app->submenu, "About", KeyMakerSubmenuIndexAbout, key_maker_submenu_callback, app);
  490. view_set_previous_callback(submenu_get_view(app->submenu), key_maker_navigation_exit_callback);
  491. view_dispatcher_add_view(
  492. app->view_dispatcher, KeyMakerViewSubmenu, submenu_get_view(app->submenu));
  493. view_dispatcher_switch_to_view(app->view_dispatcher, KeyMakerViewSubmenu);
  494. app->text_input = text_input_alloc();
  495. view_dispatcher_add_view(
  496. app->view_dispatcher, KeyMakerViewTextInput, text_input_get_view(app->text_input));
  497. app->temp_buffer_size = 32;
  498. app->temp_buffer = (char*)malloc(app->temp_buffer_size);
  499. app->variable_item_list_config = variable_item_list_alloc();
  500. variable_item_list_reset(app->variable_item_list_config);
  501. VariableItem* item = variable_item_list_add(
  502. app->variable_item_list_config,
  503. total_pin_config_label,
  504. COUNT_OF(format_names),
  505. key_maker_total_pin_change,
  506. app);
  507. uint8_t format_index = 1;
  508. variable_item_set_current_value_index(item, format_index);
  509. variable_item_set_current_value_text(item, format_names[format_index]);
  510. FuriString* key_name_str = furi_string_alloc();
  511. furi_string_set_str(key_name_str, key_name_default_value);
  512. app->key_name_item = variable_item_list_add(
  513. app->variable_item_list_config, key_name_config_label, 1, NULL, NULL);
  514. variable_item_set_current_value_text(
  515. app->key_name_item, furi_string_get_cstr(key_name_str));
  516. variable_item_list_set_enter_callback(
  517. app->variable_item_list_config, key_maker_setting_item_clicked, app);
  518. view_set_previous_callback(
  519. variable_item_list_get_view(app->variable_item_list_config),
  520. key_maker_navigation_submenu_callback);
  521. view_dispatcher_add_view(
  522. app->view_dispatcher,
  523. KeyMakerViewConfigure,
  524. variable_item_list_get_view(app->variable_item_list_config));
  525. app->view_game = view_alloc();
  526. view_set_draw_callback(app->view_game, key_maker_view_game_draw_callback);
  527. view_set_input_callback(app->view_game, key_maker_view_game_input_callback);
  528. view_set_previous_callback(app->view_game, key_maker_navigation_submenu_callback);
  529. view_set_enter_callback(app->view_game, key_maker_view_game_enter_callback);
  530. view_set_exit_callback(app->view_game, key_maker_view_game_exit_callback);
  531. view_set_context(app->view_game, app);
  532. view_set_custom_callback(app->view_game, key_maker_view_game_custom_event_callback);
  533. view_allocate_model(app->view_game, ViewModelTypeLockFree, sizeof(KeyMakerGameModel));
  534. KeyMakerGameModel* model = view_get_model(app->view_game);
  535. initialize_depths(model);
  536. initialize_format(model);
  537. model->format_index = format_index;
  538. model->key_name_str = key_name_str;
  539. model->pin_slc = 1;
  540. model->total_pin = model->format.pin_num;
  541. view_dispatcher_add_view(app->view_dispatcher, KeyMakerViewGame, app->view_game);
  542. app->widget_about = widget_alloc();
  543. widget_add_text_scroll_element(
  544. app->widget_about,
  545. 0,
  546. 0,
  547. 128,
  548. 64,
  549. "Key Maker App 0.1");
  550. view_set_previous_callback(
  551. widget_get_view(app->widget_about), key_maker_navigation_submenu_callback);
  552. view_dispatcher_add_view(
  553. app->view_dispatcher, KeyMakerViewAbout, widget_get_view(app->widget_about));
  554. app->notifications = furi_record_open(RECORD_NOTIFICATION);
  555. #ifdef BACKLIGHT_ON
  556. notification_message(app->notifications, &sequence_display_backlight_enforce_on);
  557. #endif
  558. return app;
  559. }
  560. /**
  561. * @brief Free the key_maker application.
  562. * @details This function frees the key_maker application resources.
  563. * @param app The key_maker application object.
  564. */
  565. static void key_maker_app_free(KeyMakerApp* app) {
  566. #ifdef BACKLIGHT_ON
  567. notification_message(app->notifications, &sequence_display_backlight_enforce_auto);
  568. #endif
  569. furi_record_close(RECORD_NOTIFICATION);
  570. view_dispatcher_remove_view(app->view_dispatcher, KeyMakerViewTextInput);
  571. text_input_free(app->text_input);
  572. free(app->temp_buffer);
  573. view_dispatcher_remove_view(app->view_dispatcher, KeyMakerViewAbout);
  574. widget_free(app->widget_about);
  575. view_dispatcher_remove_view(app->view_dispatcher, KeyMakerViewGame);
  576. with_view_model(
  577. app->view_game,
  578. KeyMakerGameModel * model,
  579. {
  580. if(model->depth != NULL) {
  581. free(model->depth);
  582. }
  583. },
  584. false);
  585. view_free(app->view_game);
  586. view_dispatcher_remove_view(app->view_dispatcher, KeyMakerViewConfigure);
  587. variable_item_list_free(app->variable_item_list_config);
  588. view_dispatcher_remove_view(app->view_dispatcher, KeyMakerViewSubmenu);
  589. submenu_free(app->submenu);
  590. view_dispatcher_free(app->view_dispatcher);
  591. furi_record_close(RECORD_GUI);
  592. free(app);
  593. }
  594. /**
  595. * @brief Main function for key_maker application.
  596. * @details This function is the entry point for the key_maker application. It should be defined in
  597. * application.fam as the entry_point setting.
  598. * @param _p Input parameter - unused
  599. * @return 0 - Success
  600. */
  601. int32_t main_key_maker_app(void* _p) {
  602. UNUSED(_p);
  603. KeyMakerApp* app = key_maker_app_alloc();
  604. view_dispatcher_run(app->view_dispatcher);
  605. key_maker_app_free(app);
  606. return 0;
  607. }