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.

draw_keyboard.cpp 9.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. #include "../../../inc/MarlinConfigPre.h"
  23. #if HAS_TFT_LVGL_UI
  24. #include "draw_ui.h"
  25. #include <lv_conf.h>
  26. #include "../../../inc/MarlinConfig.h"
  27. #include "../../../gcode/queue.h"
  28. extern lv_group_t *g;
  29. static lv_obj_t *scr;
  30. #define LV_KB_CTRL_BTN_FLAGS (LV_BTNM_CTRL_NO_REPEAT | LV_BTNM_CTRL_CLICK_TRIG)
  31. static const char * kb_map_lc[] = {"1#", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", LV_SYMBOL_BACKSPACE, "\n",
  32. "ABC", "a", "s", "d", "f", "g", "h", "j", "k", "l", LV_SYMBOL_NEW_LINE, "\n",
  33. "_", "-", "z", "x", "c", "v", "b", "n", "m", ".", ",", ":", "\n",
  34. LV_SYMBOL_CLOSE, LV_SYMBOL_LEFT, " ", LV_SYMBOL_RIGHT, LV_SYMBOL_OK, ""};
  35. static const lv_btnm_ctrl_t kb_ctrl_lc_map[] = {
  36. LV_KB_CTRL_BTN_FLAGS | 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 7,
  37. LV_KB_CTRL_BTN_FLAGS | 6, 3, 3, 3, 3, 3, 3, 3, 3, 3, 7,
  38. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  39. LV_KB_CTRL_BTN_FLAGS | 2, 2, 6, 2, LV_KB_CTRL_BTN_FLAGS | 2};
  40. static const char * kb_map_uc[] = {"1#", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", LV_SYMBOL_BACKSPACE, "\n",
  41. "abc", "A", "S", "D", "F", "G", "H", "J", "K", "L", LV_SYMBOL_NEW_LINE, "\n",
  42. "_", "-", "Z", "X", "C", "V", "B", "N", "M", ".", ",", ":", "\n",
  43. LV_SYMBOL_CLOSE, LV_SYMBOL_LEFT, " ", LV_SYMBOL_RIGHT, LV_SYMBOL_OK, ""};
  44. static const lv_btnm_ctrl_t kb_ctrl_uc_map[] = {
  45. LV_KB_CTRL_BTN_FLAGS | 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 7,
  46. LV_KB_CTRL_BTN_FLAGS | 6, 3, 3, 3, 3, 3, 3, 3, 3, 3, 7,
  47. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  48. LV_KB_CTRL_BTN_FLAGS | 2, 2, 6, 2, LV_KB_CTRL_BTN_FLAGS | 2};
  49. static const char * kb_map_spec[] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ".", LV_SYMBOL_BACKSPACE, "\n",
  50. "abc", "+", "-", "/", "*", "=", "%", "!", "?", "#", "<", ">", "\n",
  51. "\\", "@", "$", "(", ")", "{", "}", "[", "]", ";", "\"", "'", "\n",
  52. LV_SYMBOL_CLOSE, LV_SYMBOL_LEFT, " ", LV_SYMBOL_RIGHT, LV_SYMBOL_OK, ""};
  53. static const lv_btnm_ctrl_t kb_ctrl_spec_map[] = {
  54. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, LV_KB_CTRL_BTN_FLAGS | 2,
  55. LV_KB_CTRL_BTN_FLAGS | 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  56. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  57. LV_KB_CTRL_BTN_FLAGS | 2, 2, 6, 2, LV_KB_CTRL_BTN_FLAGS | 2};
  58. static const lv_btnm_ctrl_t kb_ctrl_num_map[] = {
  59. 1, 1, 1, LV_KB_CTRL_BTN_FLAGS | 2,
  60. 1, 1, 1, LV_KB_CTRL_BTN_FLAGS | 2,
  61. 1, 1, 1, 2,
  62. 1, 1, 1, 1, 1
  63. };
  64. static void lv_kb_event_cb(lv_obj_t *kb, lv_event_t event) {
  65. if (event != LV_EVENT_VALUE_CHANGED) return;
  66. lv_kb_ext_t *ext = (lv_kb_ext_t*)lv_obj_get_ext_attr(kb);
  67. const uint16_t btn_id = lv_btnm_get_active_btn(kb);
  68. if (btn_id == LV_BTNM_BTN_NONE) return;
  69. if (lv_btnm_get_btn_ctrl(kb, btn_id, LV_BTNM_CTRL_HIDDEN | LV_BTNM_CTRL_INACTIVE)) return;
  70. if (lv_btnm_get_btn_ctrl(kb, btn_id, LV_BTNM_CTRL_NO_REPEAT) && event == LV_EVENT_LONG_PRESSED_REPEAT) return;
  71. const char * txt = lv_btnm_get_active_btn_text(kb);
  72. if (!txt) return;
  73. // Do the corresponding action according to the text of the button
  74. if (strcmp_P(txt, PSTR("abc")) == 0) {
  75. lv_btnm_set_map(kb, kb_map_lc);
  76. lv_btnm_set_ctrl_map(kb, kb_ctrl_lc_map);
  77. return;
  78. }
  79. else if (strcmp_P(txt, PSTR("ABC")) == 0) {
  80. lv_btnm_set_map(kb, kb_map_uc);
  81. lv_btnm_set_ctrl_map(kb, kb_ctrl_uc_map);
  82. return;
  83. }
  84. else if (strcmp_P(txt, PSTR("1#")) == 0) {
  85. lv_btnm_set_map(kb, kb_map_spec);
  86. lv_btnm_set_ctrl_map(kb, kb_ctrl_spec_map);
  87. return;
  88. }
  89. else if (strcmp_P(txt, PSTR(LV_SYMBOL_CLOSE)) == 0) {
  90. if (kb->event_cb != lv_kb_def_event_cb) {
  91. goto_previous_ui();
  92. }
  93. else {
  94. lv_kb_set_ta(kb, nullptr); // De-assign the text area to hide its cursor if needed
  95. lv_obj_del(kb);
  96. return;
  97. }
  98. return;
  99. }
  100. else if (strcmp_P(txt, PSTR(LV_SYMBOL_OK)) == 0) {
  101. if (kb->event_cb != lv_kb_def_event_cb) {
  102. const char * ret_ta_txt = lv_ta_get_text(ext->ta);
  103. switch (keyboard_value) {
  104. #if ENABLED(MKS_WIFI_MODULE)
  105. case wifiName:
  106. memcpy(uiCfg.wifi_name, ret_ta_txt, sizeof(uiCfg.wifi_name));
  107. goto_previous_ui();
  108. break;
  109. case wifiPassWord:
  110. memcpy(uiCfg.wifi_key, ret_ta_txt, sizeof(uiCfg.wifi_name));
  111. goto_previous_ui();
  112. break;
  113. case wifiConfig:
  114. ZERO(uiCfg.wifi_name);
  115. memcpy((void *)uiCfg.wifi_name, wifi_list.wifiName[wifi_list.nameIndex], 32);
  116. ZERO(uiCfg.wifi_key);
  117. memcpy((void *)uiCfg.wifi_key, ret_ta_txt, sizeof(uiCfg.wifi_key));
  118. gCfgItems.wifi_mode_sel = STA_MODEL;
  119. package_to_wifi(WIFI_PARA_SET, nullptr, 0);
  120. public_buf_l[0] = 0xA5;
  121. public_buf_l[1] = 0x09;
  122. public_buf_l[2] = 0x01;
  123. public_buf_l[3] = 0x00;
  124. public_buf_l[4] = 0x01;
  125. public_buf_l[5] = 0xFC;
  126. public_buf_l[6] = 0x00;
  127. raw_send_to_wifi((uint8_t*)public_buf_l, 6);
  128. last_disp_state = KEYBOARD_UI;
  129. lv_clear_keyboard();
  130. wifi_tips_type = TIPS_TYPE_JOINING;
  131. lv_draw_wifi_tips();
  132. break;
  133. #endif // MKS_WIFI_MODULE
  134. case autoLevelGcodeCommand:
  135. uint8_t buf[100];
  136. strncpy((char *)buf, ret_ta_txt, sizeof(buf));
  137. update_gcode_command(AUTO_LEVELING_COMMAND_ADDR, buf);
  138. goto_previous_ui();
  139. break;
  140. case GCodeCommand:
  141. if (ret_ta_txt[0] && !queue.ring_buffer.full(3)) {
  142. // Hook for the next bytes to arrive from the serial port
  143. MYSERIAL1.setHook(lv_serial_capt_hook, lv_eom_hook, 0);
  144. // Run the command as soon as possible
  145. queue.inject(ret_ta_txt);
  146. }
  147. goto_previous_ui();
  148. break;
  149. default: break;
  150. }
  151. }
  152. else
  153. lv_kb_set_ta(kb, nullptr); // De-assign the text area to hide it cursor if needed
  154. return;
  155. }
  156. // Add the characters to the text area if set
  157. if (!ext->ta) return;
  158. if (strcmp_P(txt, PSTR("Enter")) == 0 || strcmp_P(txt, PSTR(LV_SYMBOL_NEW_LINE)) == 0)
  159. lv_ta_add_char(ext->ta, '\n');
  160. else if (strcmp_P(txt, PSTR(LV_SYMBOL_LEFT)) == 0)
  161. lv_ta_cursor_left(ext->ta);
  162. else if (strcmp_P(txt, PSTR(LV_SYMBOL_RIGHT)) == 0)
  163. lv_ta_cursor_right(ext->ta);
  164. else if (strcmp_P(txt, PSTR(LV_SYMBOL_BACKSPACE)) == 0)
  165. lv_ta_del_char(ext->ta);
  166. else if (strcmp_P(txt, PSTR("+/-")) == 0) {
  167. uint16_t cur = lv_ta_get_cursor_pos(ext->ta);
  168. const char * ta_txt = lv_ta_get_text(ext->ta);
  169. if (ta_txt[0] == '-') {
  170. lv_ta_set_cursor_pos(ext->ta, 1);
  171. lv_ta_del_char(ext->ta);
  172. lv_ta_add_char(ext->ta, '+');
  173. lv_ta_set_cursor_pos(ext->ta, cur);
  174. }
  175. else if (ta_txt[0] == '+') {
  176. lv_ta_set_cursor_pos(ext->ta, 1);
  177. lv_ta_del_char(ext->ta);
  178. lv_ta_add_char(ext->ta, '-');
  179. lv_ta_set_cursor_pos(ext->ta, cur);
  180. }
  181. else {
  182. lv_ta_set_cursor_pos(ext->ta, 0);
  183. lv_ta_add_char(ext->ta, '-');
  184. lv_ta_set_cursor_pos(ext->ta, cur + 1);
  185. }
  186. }
  187. else {
  188. lv_ta_add_text(ext->ta, txt);
  189. }
  190. }
  191. void lv_draw_keyboard() {
  192. scr = lv_screen_create(KEYBOARD_UI, "");
  193. // Create styles for the keyboard
  194. static lv_style_t rel_style, pr_style;
  195. lv_style_copy(&rel_style, &lv_style_btn_rel);
  196. rel_style.body.radius = 0;
  197. rel_style.body.border.width = 1;
  198. rel_style.body.main_color = lv_color_make(0xA9, 0x62, 0x1D);
  199. rel_style.body.grad_color = lv_color_make(0xA7, 0x59, 0x0E);
  200. lv_style_copy(&pr_style, &lv_style_btn_pr);
  201. pr_style.body.radius = 0;
  202. pr_style.body.border.width = 1;
  203. pr_style.body.main_color = lv_color_make(0x72, 0x42, 0x15);
  204. pr_style.body.grad_color = lv_color_make(0x6A, 0x3A, 0x0C);
  205. // Create a keyboard and apply the styles
  206. lv_obj_t *kb = lv_kb_create(scr, nullptr);
  207. lv_obj_set_event_cb(kb, lv_kb_event_cb);
  208. lv_kb_set_cursor_manage(kb, true);
  209. lv_kb_set_style(kb, LV_KB_STYLE_BG, &lv_style_transp_tight);
  210. lv_kb_set_style(kb, LV_KB_STYLE_BTN_REL, &rel_style);
  211. lv_kb_set_style(kb, LV_KB_STYLE_BTN_PR, &pr_style);
  212. #if HAS_ROTARY_ENCODER
  213. if (gCfgItems.encoder_enable) {
  214. }
  215. #endif
  216. // Create a text area. The keyboard will write here
  217. lv_obj_t *ta = lv_ta_create(scr, nullptr);
  218. lv_obj_align(ta, nullptr, LV_ALIGN_IN_TOP_MID, 0, 10);
  219. switch (keyboard_value) {
  220. case autoLevelGcodeCommand:
  221. get_gcode_command(AUTO_LEVELING_COMMAND_ADDR, (uint8_t *)public_buf_m);
  222. public_buf_m[sizeof(public_buf_m) - 1] = '\0';
  223. lv_ta_set_text(ta, public_buf_m);
  224. break;
  225. case GCodeCommand:
  226. // Start with uppercase by default
  227. lv_btnm_set_map(kb, kb_map_uc);
  228. lv_btnm_set_ctrl_map(kb, kb_ctrl_uc_map);
  229. // Fallthrough
  230. default:
  231. lv_ta_set_text(ta, "");
  232. }
  233. // Assign the text area to the keyboard
  234. lv_kb_set_ta(kb, ta);
  235. }
  236. void lv_clear_keyboard() {
  237. lv_obj_del(scr);
  238. }
  239. #endif // HAS_TFT_LVGL_UI