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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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. extern lv_group_t *g;
  28. static lv_obj_t *scr;
  29. #define LV_KB_CTRL_BTN_FLAGS (LV_BTNM_CTRL_NO_REPEAT | LV_BTNM_CTRL_CLICK_TRIG)
  30. static const char * kb_map_lc[] = {"1#", "q", "w", "e", "r", "t", "y", "u", "i", "o", "p", LV_SYMBOL_BACKSPACE, "\n",
  31. "ABC", "a", "s", "d", "f", "g", "h", "j", "k", "l", LV_SYMBOL_NEW_LINE, "\n",
  32. "_", "-", "z", "x", "c", "v", "b", "n", "m", ".", ",", ":", "\n",
  33. LV_SYMBOL_CLOSE, LV_SYMBOL_LEFT, " ", LV_SYMBOL_RIGHT, LV_SYMBOL_OK, ""};
  34. static const lv_btnm_ctrl_t kb_ctrl_lc_map[] = {
  35. LV_KB_CTRL_BTN_FLAGS | 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 7,
  36. LV_KB_CTRL_BTN_FLAGS | 6, 3, 3, 3, 3, 3, 3, 3, 3, 3, 7,
  37. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  38. LV_KB_CTRL_BTN_FLAGS | 2, 2, 6, 2, LV_KB_CTRL_BTN_FLAGS | 2};
  39. static const char * kb_map_uc[] = {"1#", "Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P", LV_SYMBOL_BACKSPACE, "\n",
  40. "abc", "A", "S", "D", "F", "G", "H", "J", "K", "L", LV_SYMBOL_NEW_LINE, "\n",
  41. "_", "-", "Z", "X", "C", "V", "B", "N", "M", ".", ",", ":", "\n",
  42. LV_SYMBOL_CLOSE, LV_SYMBOL_LEFT, " ", LV_SYMBOL_RIGHT, LV_SYMBOL_OK, ""};
  43. static const lv_btnm_ctrl_t kb_ctrl_uc_map[] = {
  44. LV_KB_CTRL_BTN_FLAGS | 5, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 7,
  45. LV_KB_CTRL_BTN_FLAGS | 6, 3, 3, 3, 3, 3, 3, 3, 3, 3, 7,
  46. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  47. LV_KB_CTRL_BTN_FLAGS | 2, 2, 6, 2, LV_KB_CTRL_BTN_FLAGS | 2};
  48. static const char * kb_map_spec[] = {"0", "1", "2", "3", "4" ,"5", "6", "7", "8", "9", LV_SYMBOL_BACKSPACE, "\n",
  49. "abc", "+", "-", "/", "*", "=", "%", "!", "?", "#", "<", ">", "\n",
  50. "\\", "@", "$", "(", ")", "{", "}", "[", "]", ";", "\"", "'", "\n",
  51. LV_SYMBOL_CLOSE, LV_SYMBOL_LEFT, " ", LV_SYMBOL_RIGHT, LV_SYMBOL_OK, ""};
  52. static const lv_btnm_ctrl_t kb_ctrl_spec_map[] = {
  53. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, LV_KB_CTRL_BTN_FLAGS | 2,
  54. LV_KB_CTRL_BTN_FLAGS | 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  55. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  56. LV_KB_CTRL_BTN_FLAGS | 2, 2, 6, 2, LV_KB_CTRL_BTN_FLAGS | 2};
  57. static const lv_btnm_ctrl_t kb_ctrl_num_map[] = {
  58. 1, 1, 1, LV_KB_CTRL_BTN_FLAGS | 2,
  59. 1, 1, 1, LV_KB_CTRL_BTN_FLAGS | 2,
  60. 1, 1, 1, 2,
  61. 1, 1, 1, 1, 1};
  62. static void lv_kb_event_cb(lv_obj_t *kb, lv_event_t event) {
  63. //LV_ASSERT_OBJ(kb, LV_OBJX_NAME);
  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_res_t res = lv_event_send(kb, LV_EVENT_CANCEL, nullptr);
  91. //if (res != LV_RES_OK) return;
  92. lv_clear_keyboard();
  93. draw_return_ui();
  94. }
  95. else {
  96. lv_kb_set_ta(kb, nullptr); // De-assign the text area to hide it cursor if needed
  97. lv_obj_del(kb);
  98. return;
  99. }
  100. return;
  101. }
  102. else if (strcmp(txt, LV_SYMBOL_OK) == 0) {
  103. if (kb->event_cb != lv_kb_def_event_cb) {
  104. //lv_res_t res = lv_event_send(kb, LV_EVENT_APPLY, nullptr);
  105. //if (res != LV_RES_OK) return;
  106. const char * ret_ta_txt = lv_ta_get_text(ext->ta);
  107. switch (keyboard_value) {
  108. #if ENABLED(USE_WIFI_FUNCTION)
  109. case wifiName:
  110. memcpy(uiCfg.wifi_name,ret_ta_txt,sizeof(uiCfg.wifi_name));
  111. lv_clear_keyboard();
  112. draw_return_ui();
  113. break;
  114. case wifiPassWord:
  115. memcpy(uiCfg.wifi_key,ret_ta_txt,sizeof(uiCfg.wifi_name));
  116. lv_clear_keyboard();
  117. draw_return_ui();
  118. break;
  119. case wifiConfig:
  120. ZERO(uiCfg.wifi_name);
  121. memcpy((void *)uiCfg.wifi_name, wifi_list.wifiName[wifi_list.nameIndex], 32);
  122. ZERO(uiCfg.wifi_key);
  123. memcpy((void *)uiCfg.wifi_key, ret_ta_txt, sizeof(uiCfg.wifi_key));
  124. gCfgItems.wifi_mode_sel = STA_MODEL;
  125. package_to_wifi(WIFI_PARA_SET, (char *)0, 0);
  126. public_buf_l[0] = 0xA5;
  127. public_buf_l[1] = 0x09;
  128. public_buf_l[2] = 0x01;
  129. public_buf_l[3] = 0x00;
  130. public_buf_l[4] = 0x01;
  131. public_buf_l[5] = 0xFC;
  132. public_buf_l[6] = 0x00;
  133. raw_send_to_wifi(public_buf_l, 6);
  134. last_disp_state = KEY_BOARD_UI;
  135. lv_clear_keyboard();
  136. wifi_tips_type = TIPS_TYPE_JOINING;
  137. lv_draw_wifi_tips();
  138. break;
  139. #endif // USE_WIFI_FUNCTION
  140. case gcodeCommand:
  141. uint8_t buf[100];
  142. strncpy((char *)buf,ret_ta_txt,sizeof(buf));
  143. update_gcode_command(AUTO_LEVELING_COMMAND_ADDR,buf);
  144. lv_clear_keyboard();
  145. draw_return_ui();
  146. break;
  147. default: break;
  148. }
  149. }
  150. else
  151. lv_kb_set_ta(kb, nullptr); // De-assign the text area to hide it cursor if needed
  152. return;
  153. }
  154. // Add the characters to the text area if set
  155. if (!ext->ta) return;
  156. if (strcmp(txt, "Enter") == 0 || strcmp(txt, LV_SYMBOL_NEW_LINE) == 0)
  157. lv_ta_add_char(ext->ta, '\n');
  158. else if (strcmp(txt, LV_SYMBOL_LEFT) == 0)
  159. lv_ta_cursor_left(ext->ta);
  160. else if (strcmp(txt, LV_SYMBOL_RIGHT) == 0)
  161. lv_ta_cursor_right(ext->ta);
  162. else if (strcmp(txt, LV_SYMBOL_BACKSPACE) == 0)
  163. lv_ta_del_char(ext->ta);
  164. else if (strcmp(txt, "+/-") == 0) {
  165. uint16_t cur = lv_ta_get_cursor_pos(ext->ta);
  166. const char * ta_txt = lv_ta_get_text(ext->ta);
  167. if (ta_txt[0] == '-') {
  168. lv_ta_set_cursor_pos(ext->ta, 1);
  169. lv_ta_del_char(ext->ta);
  170. lv_ta_add_char(ext->ta, '+');
  171. lv_ta_set_cursor_pos(ext->ta, cur);
  172. }
  173. else if (ta_txt[0] == '+') {
  174. lv_ta_set_cursor_pos(ext->ta, 1);
  175. lv_ta_del_char(ext->ta);
  176. lv_ta_add_char(ext->ta, '-');
  177. lv_ta_set_cursor_pos(ext->ta, cur);
  178. }
  179. else {
  180. lv_ta_set_cursor_pos(ext->ta, 0);
  181. lv_ta_add_char(ext->ta, '-');
  182. lv_ta_set_cursor_pos(ext->ta, cur + 1);
  183. }
  184. }
  185. else {
  186. lv_ta_add_text(ext->ta, txt);
  187. }
  188. }
  189. void lv_draw_keyboard() {
  190. scr = lv_screen_create(KEY_BOARD_UI, "");
  191. // Create styles for the keyboard
  192. static lv_style_t rel_style, pr_style;
  193. lv_style_copy(&rel_style, &lv_style_btn_rel);
  194. rel_style.body.radius = 0;
  195. rel_style.body.border.width = 1;
  196. rel_style.body.main_color = lv_color_make(0xA9, 0x62, 0x1D);
  197. rel_style.body.grad_color = lv_color_make(0xA7, 0x59, 0x0E);
  198. lv_style_copy(&pr_style, &lv_style_btn_pr);
  199. pr_style.body.radius = 0;
  200. pr_style.body.border.width = 1;
  201. pr_style.body.main_color = lv_color_make(0x72, 0x42, 0x15);
  202. pr_style.body.grad_color = lv_color_make(0x6A, 0x3A, 0x0C);
  203. // Create a keyboard and apply the styles
  204. lv_obj_t *kb = lv_kb_create(scr, nullptr);
  205. lv_obj_set_event_cb(kb, lv_kb_event_cb);
  206. lv_kb_set_cursor_manage(kb, true);
  207. lv_kb_set_style(kb, LV_KB_STYLE_BG, &lv_style_transp_tight);
  208. lv_kb_set_style(kb, LV_KB_STYLE_BTN_REL, &rel_style);
  209. lv_kb_set_style(kb, LV_KB_STYLE_BTN_PR, &pr_style);
  210. #if HAS_ROTARY_ENCODER
  211. if (gCfgItems.encoder_enable) {
  212. //lv_group_add_obj(g, kb);
  213. //lv_group_set_editing(g, true);
  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. if (keyboard_value == gcodeCommand) {
  220. get_gcode_command(AUTO_LEVELING_COMMAND_ADDR,(uint8_t *)public_buf_m);
  221. public_buf_m[sizeof(public_buf_m)-1] = 0;
  222. lv_ta_set_text(ta, public_buf_m);
  223. }
  224. else {
  225. lv_ta_set_text(ta, "");
  226. }
  227. // Assign the text area to the keyboard
  228. lv_kb_set_ta(kb, ta);
  229. }
  230. void lv_clear_keyboard() {
  231. #if HAS_ROTARY_ENCODER
  232. if (gCfgItems.encoder_enable) { /* lv_group_remove_all_objs(g); */ }
  233. #endif
  234. lv_obj_del(scr);
  235. }
  236. #endif // HAS_TFT_LVGL_UI