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.3KB

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