app.c 28 KB

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