app.c 28 KB

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