app.c 26 KB

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