app.c 28 KB

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