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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. static void lv_kb_event_cb(lv_obj_t *kb, lv_event_t event) {
  64. if (event != LV_EVENT_VALUE_CHANGED) return;
  65. lv_kb_ext_t * ext = (lv_kb_ext_t * )lv_obj_get_ext_attr(kb);
  66. const uint16_t btn_id = lv_btnm_get_active_btn(kb);
  67. if (btn_id == LV_BTNM_BTN_NONE) return;
  68. if (lv_btnm_get_btn_ctrl(kb, btn_id, LV_BTNM_CTRL_HIDDEN | LV_BTNM_CTRL_INACTIVE)) return;
  69. if (lv_btnm_get_btn_ctrl(kb, btn_id, LV_BTNM_CTRL_NO_REPEAT) && event == LV_EVENT_LONG_PRESSED_REPEAT) return;
  70. const char * txt = lv_btnm_get_active_btn_text(kb);
  71. if (!txt) return;
  72. // Do the corresponding action according to the text of the button
  73. if (strcmp(txt, "abc") == 0) {
  74. lv_btnm_set_map(kb, kb_map_lc);
  75. lv_btnm_set_ctrl_map(kb, kb_ctrl_lc_map);
  76. return;
  77. }
  78. else if (strcmp(txt, "ABC") == 0) {
  79. lv_btnm_set_map(kb, kb_map_uc);
  80. lv_btnm_set_ctrl_map(kb, kb_ctrl_uc_map);
  81. return;
  82. }
  83. else if (strcmp(txt, "1#") == 0) {
  84. lv_btnm_set_map(kb, kb_map_spec);
  85. lv_btnm_set_ctrl_map(kb, kb_ctrl_spec_map);
  86. return;
  87. }
  88. else if (strcmp(txt, LV_SYMBOL_CLOSE) == 0) {
  89. if (kb->event_cb != lv_kb_def_event_cb) {
  90. lv_clear_keyboard();
  91. draw_return_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(txt, 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. lv_clear_keyboard();
  108. draw_return_ui();
  109. break;
  110. case wifiPassWord:
  111. memcpy(uiCfg.wifi_key,ret_ta_txt,sizeof(uiCfg.wifi_name));
  112. lv_clear_keyboard();
  113. draw_return_ui();
  114. break;
  115. case wifiConfig:
  116. ZERO(uiCfg.wifi_name);
  117. memcpy((void *)uiCfg.wifi_name, wifi_list.wifiName[wifi_list.nameIndex], 32);
  118. ZERO(uiCfg.wifi_key);
  119. memcpy((void *)uiCfg.wifi_key, ret_ta_txt, sizeof(uiCfg.wifi_key));
  120. gCfgItems.wifi_mode_sel = STA_MODEL;
  121. package_to_wifi(WIFI_PARA_SET, (uint8_t *)0, 0);
  122. public_buf_l[0] = 0xA5;
  123. public_buf_l[1] = 0x09;
  124. public_buf_l[2] = 0x01;
  125. public_buf_l[3] = 0x00;
  126. public_buf_l[4] = 0x01;
  127. public_buf_l[5] = 0xFC;
  128. public_buf_l[6] = 0x00;
  129. raw_send_to_wifi((uint8_t*)public_buf_l, 6);
  130. last_disp_state = KEY_BOARD_UI;
  131. lv_clear_keyboard();
  132. wifi_tips_type = TIPS_TYPE_JOINING;
  133. lv_draw_wifi_tips();
  134. break;
  135. #endif // MKS_WIFI_MODULE
  136. case autoLevelGcodeCommand:
  137. uint8_t buf[100];
  138. strncpy((char *)buf,ret_ta_txt,sizeof(buf));
  139. update_gcode_command(AUTO_LEVELING_COMMAND_ADDR,buf);
  140. lv_clear_keyboard();
  141. draw_return_ui();
  142. break;
  143. case GCodeCommand:
  144. if (!queue.ring_buffer.full(3)) {
  145. // Hook anything that goes to the serial port
  146. MYSERIAL0.setHook(lv_serial_capt_hook, lv_eom_hook, 0);
  147. queue.enqueue_one_now(ret_ta_txt);
  148. }
  149. lv_clear_keyboard();
  150. // draw_return_ui is called in the end of message hook
  151. break;
  152. default: break;
  153. }
  154. }
  155. else
  156. lv_kb_set_ta(kb, nullptr); // De-assign the text area to hide it cursor if needed
  157. return;
  158. }
  159. // Add the characters to the text area if set
  160. if (!ext->ta) return;
  161. if (strcmp(txt, "Enter") == 0 || strcmp(txt, LV_SYMBOL_NEW_LINE) == 0)
  162. lv_ta_add_char(ext->ta, '\n');
  163. else if (strcmp(txt, LV_SYMBOL_LEFT) == 0)
  164. lv_ta_cursor_left(ext->ta);
  165. else if (strcmp(txt, LV_SYMBOL_RIGHT) == 0)
  166. lv_ta_cursor_right(ext->ta);
  167. else if (strcmp(txt, LV_SYMBOL_BACKSPACE) == 0)
  168. lv_ta_del_char(ext->ta);
  169. else if (strcmp(txt, "+/-") == 0) {
  170. uint16_t cur = lv_ta_get_cursor_pos(ext->ta);
  171. const char * ta_txt = lv_ta_get_text(ext->ta);
  172. if (ta_txt[0] == '-') {
  173. lv_ta_set_cursor_pos(ext->ta, 1);
  174. lv_ta_del_char(ext->ta);
  175. lv_ta_add_char(ext->ta, '+');
  176. lv_ta_set_cursor_pos(ext->ta, cur);
  177. }
  178. else if (ta_txt[0] == '+') {
  179. lv_ta_set_cursor_pos(ext->ta, 1);
  180. lv_ta_del_char(ext->ta);
  181. lv_ta_add_char(ext->ta, '-');
  182. lv_ta_set_cursor_pos(ext->ta, cur);
  183. }
  184. else {
  185. lv_ta_set_cursor_pos(ext->ta, 0);
  186. lv_ta_add_char(ext->ta, '-');
  187. lv_ta_set_cursor_pos(ext->ta, cur + 1);
  188. }
  189. }
  190. else {
  191. lv_ta_add_text(ext->ta, txt);
  192. }
  193. }
  194. void lv_draw_keyboard() {
  195. scr = lv_screen_create(KEY_BOARD_UI, "");
  196. // Create styles for the keyboard
  197. static lv_style_t rel_style, pr_style;
  198. lv_style_copy(&rel_style, &lv_style_btn_rel);
  199. rel_style.body.radius = 0;
  200. rel_style.body.border.width = 1;
  201. rel_style.body.main_color = lv_color_make(0xA9, 0x62, 0x1D);
  202. rel_style.body.grad_color = lv_color_make(0xA7, 0x59, 0x0E);
  203. lv_style_copy(&pr_style, &lv_style_btn_pr);
  204. pr_style.body.radius = 0;
  205. pr_style.body.border.width = 1;
  206. pr_style.body.main_color = lv_color_make(0x72, 0x42, 0x15);
  207. pr_style.body.grad_color = lv_color_make(0x6A, 0x3A, 0x0C);
  208. // Create a keyboard and apply the styles
  209. lv_obj_t *kb = lv_kb_create(scr, nullptr);
  210. lv_obj_set_event_cb(kb, lv_kb_event_cb);
  211. lv_kb_set_cursor_manage(kb, true);
  212. lv_kb_set_style(kb, LV_KB_STYLE_BG, &lv_style_transp_tight);
  213. lv_kb_set_style(kb, LV_KB_STYLE_BTN_REL, &rel_style);
  214. lv_kb_set_style(kb, LV_KB_STYLE_BTN_PR, &pr_style);
  215. #if HAS_ROTARY_ENCODER
  216. if (gCfgItems.encoder_enable) {
  217. }
  218. #endif
  219. // Create a text area. The keyboard will write here
  220. lv_obj_t *ta = lv_ta_create(scr, nullptr);
  221. lv_obj_align(ta, nullptr, LV_ALIGN_IN_TOP_MID, 0, 10);
  222. switch (keyboard_value) {
  223. case autoLevelGcodeCommand:
  224. get_gcode_command(AUTO_LEVELING_COMMAND_ADDR,(uint8_t *)public_buf_m);
  225. public_buf_m[sizeof(public_buf_m)-1] = 0;
  226. lv_ta_set_text(ta, public_buf_m);
  227. break;
  228. case GCodeCommand:
  229. // Start with uppercase by default
  230. lv_btnm_set_map(kb, kb_map_uc);
  231. lv_btnm_set_ctrl_map(kb, kb_ctrl_uc_map);
  232. // Fallthrough
  233. default:
  234. lv_ta_set_text(ta, "");
  235. }
  236. // Assign the text area to the keyboard
  237. lv_kb_set_ta(kb, ta);
  238. }
  239. void lv_clear_keyboard() {
  240. lv_obj_del(scr);
  241. }
  242. #endif // HAS_TFT_LVGL_UI