My Marlin configs for Fabrikator Mini and CTC i3 Pro B
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

menu_password.cpp 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2020 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. //
  23. // Advanced Settings Menus
  24. //
  25. #include "../../inc/MarlinConfigPre.h"
  26. #if BOTH(HAS_MARLINUI_MENU, PASSWORD_FEATURE)
  27. #include "../../feature/password/password.h"
  28. #include "menu_item.h"
  29. #include "menu_addon.h"
  30. void menu_advanced_settings();
  31. screenFunc_t success_screen, fail_screen;
  32. bool authenticating; // = false
  33. char string[(PASSWORD_LENGTH) + 1];
  34. static uint8_t digit_no;
  35. //
  36. // Screen for both editing and setting the password
  37. //
  38. void Password::menu_password_entry() {
  39. ui.defer_status_screen(!did_first_run); // No timeout to status before first auth
  40. START_MENU();
  41. // "Login" or "New Code"
  42. STATIC_ITEM_F(authenticating ? GET_TEXT_F(MSG_LOGIN_REQUIRED) : GET_TEXT_F(MSG_EDIT_PASSWORD), SS_CENTER|SS_INVERT);
  43. STATIC_ITEM_F(FPSTR(NUL_STR), SS_CENTER, string);
  44. #if HAS_MARLINUI_U8GLIB
  45. STATIC_ITEM_F(FPSTR(NUL_STR), SS_CENTER, "");
  46. #endif
  47. // Make the digit edit item look like a sub-menu
  48. FSTR_P const label = GET_TEXT_F(MSG_ENTER_DIGIT);
  49. EDIT_ITEM_F(uint8, label, &editable.uint8, 0, 9, digit_entered);
  50. MENU_ITEM_ADDON_START(utf8_strlen(label) + 1);
  51. lcd_put_lchar(' ');
  52. lcd_put_lchar('1' + digit_no);
  53. SETCURSOR_X(LCD_WIDTH - 2);
  54. lcd_put_lchar('>');
  55. MENU_ITEM_ADDON_END();
  56. ACTION_ITEM(MSG_START_OVER, start_over);
  57. if (!authenticating) BACK_ITEM(MSG_BUTTON_CANCEL);
  58. END_MENU();
  59. }
  60. //
  61. // Authentication check
  62. //
  63. void Password::authentication_done() {
  64. ui.goto_screen(is_locked ? fail_screen : success_screen);
  65. ui.completion_feedback(!is_locked);
  66. }
  67. // A single digit was completed
  68. void Password::digit_entered() {
  69. uint32_t multiplier = CAT(1e, PASSWORD_LENGTH); // 1e5 = 100000
  70. LOOP_LE_N(i, digit_no) multiplier /= 10;
  71. value_entry += editable.uint8 * multiplier;
  72. string[digit_no++] = '0' + editable.uint8;
  73. // Exit edit screen menu and go to another screen
  74. ui.goto_previous_screen();
  75. ui.use_click();
  76. ui.goto_screen(menu_password_entry);
  77. // After password has been keyed in
  78. if (digit_no == PASSWORD_LENGTH) {
  79. if (authenticating)
  80. authentication_check();
  81. else
  82. set_password_done();
  83. }
  84. }
  85. //
  86. // Set/Change Password
  87. //
  88. void Password::screen_password_entry() {
  89. value_entry = 0;
  90. digit_no = 0;
  91. editable.uint8 = 0;
  92. memset(string, '_', PASSWORD_LENGTH);
  93. string[PASSWORD_LENGTH] = '\0';
  94. menu_password_entry();
  95. }
  96. void Password::screen_set_password() {
  97. authenticating = false;
  98. screen_password_entry();
  99. }
  100. void Password::authenticate_user(const screenFunc_t in_succ_scr, const screenFunc_t in_fail_scr) {
  101. success_screen = in_succ_scr;
  102. fail_screen = in_fail_scr;
  103. if (is_set) {
  104. authenticating = true;
  105. ui.goto_screen(screen_password_entry);
  106. ui.update();
  107. }
  108. else {
  109. ui.goto_screen(in_succ_scr);
  110. is_locked = false;
  111. }
  112. }
  113. void Password::access_menu_password() {
  114. authenticate_user(menu_password, menu_advanced_settings);
  115. }
  116. #if ENABLED(PASSWORD_ON_SD_PRINT_MENU)
  117. void Password::media_gatekeeper() {
  118. authenticate_user(menu_media, menu_main);
  119. }
  120. #endif
  121. void Password::start_over() {
  122. ui.goto_previous_screen(); // Goto previous screen, if any
  123. ui.goto_screen(screen_password_entry);
  124. }
  125. void Password::menu_password_report() {
  126. START_SCREEN();
  127. BACK_ITEM(MSG_PASSWORD_SETTINGS);
  128. STATIC_ITEM(MSG_PASSWORD_SET, SS_LEFT, string);
  129. STATIC_ITEM(MSG_REMINDER_SAVE_SETTINGS, SS_LEFT);
  130. END_SCREEN();
  131. }
  132. void Password::set_password_done(const bool with_set/*=true*/) {
  133. is_set = with_set;
  134. value = value_entry;
  135. ui.completion_feedback(true);
  136. ui.goto_screen(menu_password_report);
  137. }
  138. void Password::remove_password() {
  139. string[0] = '0';
  140. string[1] = '\0';
  141. set_password_done(false);
  142. }
  143. //
  144. // Password Menu
  145. //
  146. void Password::menu_password() {
  147. START_MENU();
  148. BACK_ITEM(MSG_ADVANCED_SETTINGS);
  149. SUBMENU(MSG_CHANGE_PASSWORD, screen_set_password);
  150. ACTION_ITEM(MSG_REMOVE_PASSWORD, []{ ui.push_current_screen(); remove_password(); } );
  151. #if ENABLED(EEPROM_SETTINGS)
  152. ACTION_ITEM(MSG_STORE_EEPROM, ui.store_settings);
  153. #endif
  154. END_MENU();
  155. }
  156. #endif // HAS_MARLINUI_MENU && PASSWORD_FEATURE