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.

lock_screen.cpp 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*******************
  2. * lock_screen.cpp *
  3. *******************/
  4. /****************************************************************************
  5. * Written By Mark Pelletier 2017 - Aleph Objects, Inc. *
  6. * Written By Marcio Teixeira 2018 - Aleph Objects, Inc. *
  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. * To view a copy of the GNU General Public License, go to the following *
  19. * location: <https://www.gnu.org/licenses/>. *
  20. ****************************************************************************/
  21. #include "../config.h"
  22. #include "screens.h"
  23. #include "screen_data.h"
  24. #ifdef FTDI_LOCK_SCREEN
  25. using namespace FTDI;
  26. using namespace Theme;
  27. uint16_t LockScreen::passcode = 0;
  28. constexpr static LockScreenData &mydata = screen_data.LockScreen;
  29. void LockScreen::onEntry() {
  30. const uint8_t siz = sizeof(mydata.passcode);
  31. memset(mydata.passcode, '_', siz-1);
  32. mydata.passcode[siz-1] = '\0';
  33. BaseScreen::onEntry();
  34. }
  35. void LockScreen::onRedraw(draw_mode_t what) {
  36. CommandProcessor cmd;
  37. if (what & BACKGROUND) {
  38. cmd.cmd(CLEAR_COLOR_RGB(bg_color))
  39. .cmd(CLEAR(true,true,true))
  40. .cmd(COLOR_RGB(bg_text_enabled))
  41. .tag(0);
  42. }
  43. if (what & FOREGROUND) {
  44. #if ENABLED(TOUCH_UI_PORTRAIT)
  45. #define GRID_COLS 1
  46. #define GRID_ROWS 10
  47. #else
  48. #define GRID_COLS 1
  49. #define GRID_ROWS 7
  50. #endif
  51. #undef MARGIN_T
  52. #undef MARGIN_B
  53. #define MARGIN_T 3
  54. #define MARGIN_B 3
  55. progmem_str message;
  56. switch (message_style()) {
  57. case 'w':
  58. message = GET_TEXT_F(MSG_PASSCODE_REJECTED);
  59. break;
  60. case 'g':
  61. message = GET_TEXT_F(MSG_PASSCODE_ACCEPTED);
  62. break;
  63. default:
  64. message = passcode ? GET_TEXT_F(MSG_PASSCODE_REQUEST) : GET_TEXT_F(MSG_PASSCODE_SELECT);
  65. }
  66. message_style() = '\0'; // Terminate the string.
  67. constexpr uint8_t l = TERN(TOUCH_UI_PORTRAIT, 6, 3);
  68. const uint8_t pressed = EventLoop::get_pressed_tag();
  69. cmd.font(font_large)
  70. #if ENABLED(TOUCH_UI_PORTRAIT)
  71. .text(BTN_POS(1,2), BTN_SIZE(1,1), message)
  72. .font(font_xlarge)
  73. .text(BTN_POS(1,4), BTN_SIZE(1,1), mydata.passcode)
  74. #else
  75. .text(BTN_POS(1,1), BTN_SIZE(1,1), message)
  76. .font(font_xlarge)
  77. .text(BTN_POS(1,2), BTN_SIZE(1,1), mydata.passcode)
  78. #endif
  79. .font(font_large)
  80. .colors(normal_btn)
  81. #ifdef TOUCH_UI_PASSCODE
  82. .keys(BTN_POS(1,l+1), BTN_SIZE(1,1), F("123"), pressed)
  83. .keys(BTN_POS(1,l+2), BTN_SIZE(1,1), F("456"), pressed)
  84. .keys(BTN_POS(1,l+3), BTN_SIZE(1,1), F("789"), pressed)
  85. .keys(BTN_POS(1,l+4), BTN_SIZE(1,1), F("0.<"), pressed);
  86. #else
  87. .keys(BTN_POS(1,l+1), BTN_SIZE(1,1), F("1234567890"), pressed)
  88. .keys(BTN_POS(1,l+2), BTN_SIZE(1,1), F("qwertyuiop"), pressed)
  89. .keys(BTN_POS(1,l+3), BTN_SIZE(1,1), F("asdfghjkl "), pressed)
  90. .keys(BTN_POS(1,l+4), BTN_SIZE(1,1), F("zxcvbnm!?<"), pressed);
  91. #endif
  92. #undef MARGIN_T
  93. #undef MARGIN_B
  94. #define MARGIN_T MARGIN_DEFAULT
  95. #define MARGIN_B MARGIN_DEFAULT
  96. #undef GRID_COLS
  97. #undef GRID_ROWS
  98. }
  99. }
  100. char &LockScreen::message_style() {
  101. // We use the last byte of the passcode string as a flag to indicate,
  102. // which message to show.
  103. constexpr uint8_t last_char = sizeof(mydata.passcode)-1;
  104. return mydata.passcode[last_char];
  105. }
  106. void LockScreen::onPasscodeEntered() {
  107. if (passcode == 0) { // We are defining a passcode
  108. message_style() = 0;
  109. onRefresh();
  110. sound.play(twinkle, PLAY_SYNCHRONOUS);
  111. passcode = compute_checksum();
  112. GOTO_PREVIOUS();
  113. }
  114. else if (passcode == compute_checksum()) { // We are verifying a passcode
  115. message_style() = 'g';
  116. onRefresh();
  117. sound.play(twinkle, PLAY_SYNCHRONOUS);
  118. GOTO_PREVIOUS();
  119. }
  120. else {
  121. message_style() = 'w';
  122. onRefresh();
  123. sound.play(sad_trombone, PLAY_SYNCHRONOUS);
  124. current_screen.forget(); // Discard the screen the user was trying to go to.
  125. GOTO_PREVIOUS();
  126. }
  127. }
  128. bool LockScreen::onTouchEnd(uint8_t tag) {
  129. char *c = strchr(mydata.passcode,'_');
  130. if (c) {
  131. if (tag == '<') {
  132. if (c != mydata.passcode) {
  133. // Backspace deletes previous entered characters.
  134. *--c = '_';
  135. }
  136. }
  137. else {
  138. // Append character to passcode
  139. *c++ = tag;
  140. if (*c == '\0') {
  141. // If at last character, then process the code.
  142. onPasscodeEntered();
  143. }
  144. }
  145. }
  146. return true;
  147. }
  148. uint16_t LockScreen::compute_checksum() {
  149. uint16_t checksum = 0;
  150. const char* c = mydata.passcode;
  151. while (*c) {
  152. checksum = (checksum << 2) ^ *c++;
  153. }
  154. if (checksum == 0) checksum = 0xFFFF; // Prevent a zero checksum
  155. return checksum;
  156. }
  157. // This function should be called *after* calling GOTO_SCREEN
  158. // to move to new screen. If a passcode is enabled, it will
  159. // immediately jump to the keypad screen, pushing the previous
  160. // screen onto the stack. If the code is entered correctly,
  161. // the stack will be popped, allowing the user to proceed to
  162. // the new screen. Otherwise it will be popped twice, taking
  163. // the user back to where they were before.
  164. void LockScreen::check_passcode() {
  165. if (passcode == 0) return;
  166. message_style() = 0;
  167. GOTO_SCREEN(LockScreen);
  168. }
  169. bool LockScreen::is_enabled() {
  170. return passcode != 0;
  171. }
  172. void LockScreen::disable() {
  173. passcode = 0;
  174. }
  175. void LockScreen::enable() {
  176. message_style() = 0;
  177. passcode = 0;
  178. GOTO_SCREEN(LockScreen);
  179. }
  180. #endif // FTDI_LOCK_SCREEN