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

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