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.

base_numeric_adjustment_screen.cpp 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /**************************************
  2. * base_numeric_adjustment_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_BASE_NUMERIC_ADJ_SCREEN
  25. using namespace FTDI;
  26. using namespace Theme;
  27. constexpr static BaseNumericAdjustmentScreenData &mydata = screen_data.BaseNumericAdjustmentScreen;
  28. #if ENABLED(TOUCH_UI_PORTRAIT)
  29. #define GRID_COLS 13
  30. #define GRID_ROWS 10
  31. #define LAYOUT_FONT font_small
  32. #else
  33. #define GRID_COLS 18
  34. #define GRID_ROWS 7
  35. #define LAYOUT_FONT font_medium
  36. #endif
  37. BaseNumericAdjustmentScreen::widgets_t::widgets_t(draw_mode_t what) : _what(what) {
  38. CommandProcessor cmd;
  39. if (what & BACKGROUND) {
  40. cmd.cmd(CLEAR_COLOR_RGB(bg_color))
  41. .cmd(CLEAR(true,true,true))
  42. .colors(normal_btn)
  43. .cmd(COLOR_RGB(bg_text_enabled))
  44. .tag(0);
  45. }
  46. else
  47. cmd.colors(normal_btn);
  48. cmd.font(font_medium);
  49. _button(cmd, 1,
  50. #if ENABLED(TOUCH_UI_PORTRAIT)
  51. BTN_POS(1,10), BTN_SIZE(13,1),
  52. #else
  53. BTN_POS(15,7), BTN_SIZE(4,1),
  54. #endif
  55. GET_TEXT_F(MSG_BUTTON_DONE), true, true
  56. );
  57. _line = 1;
  58. _units = F("");
  59. }
  60. /**
  61. * Speed optimization for changing button style.
  62. */
  63. void BaseNumericAdjustmentScreen::widgets_t::_button_style(CommandProcessor &cmd, BaseNumericAdjustmentScreen::widgets_t::style_t style) {
  64. if (_style != style) {
  65. const btn_colors *old_colors = &normal_btn;
  66. const btn_colors *new_colors = &normal_btn;
  67. switch (_style) {
  68. case BTN_ACTION: old_colors = &action_btn; break;
  69. case BTN_TOGGLE: old_colors = &ui_toggle; break;
  70. case BTN_DISABLED: old_colors = &disabled_btn; break;
  71. default: break;
  72. }
  73. switch (style) {
  74. case BTN_ACTION: new_colors = &action_btn; break;
  75. case BTN_TOGGLE: new_colors = &ui_toggle; break;
  76. case BTN_DISABLED: new_colors = &disabled_btn; break;
  77. default: break;
  78. }
  79. const bool rgb_changed = (old_colors->rgb != new_colors->rgb) ||
  80. (_style == TEXT_LABEL && style != TEXT_LABEL) ||
  81. (_style != TEXT_LABEL && style == TEXT_LABEL);
  82. const bool grad_changed = old_colors->grad != new_colors->grad;
  83. const bool fg_changed = (old_colors->fg != new_colors->fg) || (_style == TEXT_AREA);
  84. const bool bg_changed = old_colors->bg != new_colors->bg;
  85. if (rgb_changed) cmd.cmd(COLOR_RGB(style == TEXT_LABEL ? bg_text_enabled : new_colors->rgb));
  86. if (grad_changed) cmd.gradcolor(new_colors->grad);
  87. if (fg_changed) cmd.fgcolor(new_colors->fg);
  88. if (bg_changed) cmd.bgcolor(new_colors->bg);
  89. _style = style;
  90. }
  91. }
  92. /**
  93. * Speed optimization for drawing buttons. Draw all unpressed buttons in the
  94. * background layer and draw only the pressed button in the foreground layer.
  95. */
  96. void BaseNumericAdjustmentScreen::widgets_t::_button(CommandProcessor &cmd, uint8_t tag, int16_t x, int16_t y, int16_t w, int16_t h, FSTR_P text, bool enabled, bool highlight) {
  97. if (_what & BACKGROUND) enabled = true;
  98. if ((_what & BACKGROUND) || buttonIsPressed(tag) || highlight || !enabled) {
  99. _button_style(cmd, (!enabled) ? BTN_DISABLED : (highlight ? BTN_ACTION : BTN_NORMAL));
  100. cmd.tag(enabled ? tag : 0).button(x, y, w, h, text);
  101. }
  102. }
  103. BaseNumericAdjustmentScreen::widgets_t &BaseNumericAdjustmentScreen::widgets_t::precision(uint8_t decimals, precision_default_t initial) {
  104. _decimals = decimals;
  105. if (mydata.increment == 0) {
  106. mydata.increment = 243 + (initial - DEFAULT_LOWEST) - _decimals;
  107. }
  108. return *this;
  109. }
  110. void BaseNumericAdjustmentScreen::widgets_t::heading(FSTR_P label) {
  111. if (_what & BACKGROUND) {
  112. CommandProcessor cmd;
  113. _button_style(cmd, TEXT_LABEL);
  114. cmd.font(font_medium)
  115. .tag(0)
  116. .text(
  117. #if ENABLED(TOUCH_UI_PORTRAIT)
  118. BTN_POS(1, _line), BTN_SIZE(12,1),
  119. #else
  120. BTN_POS(5, _line), BTN_SIZE(8,1),
  121. #endif
  122. label
  123. );
  124. }
  125. _line++;
  126. }
  127. #if ENABLED(TOUCH_UI_PORTRAIT)
  128. #ifdef TOUCH_UI_800x480
  129. #undef EDGE_R
  130. #define EDGE_R 20
  131. #else
  132. #undef EDGE_R
  133. #define EDGE_R 10
  134. #endif
  135. #endif
  136. void BaseNumericAdjustmentScreen::widgets_t::_draw_increment_btn(CommandProcessor &cmd, uint8_t, const uint8_t tag) {
  137. const char *label = PSTR("?");
  138. uint8_t pos;
  139. uint8_t & increment = mydata.increment;
  140. if (increment == 0) {
  141. increment = tag; // Set the default value to be the first.
  142. }
  143. switch (tag) {
  144. case 240: label = PSTR( ".001"); pos = _decimals - 3; break;
  145. case 241: label = PSTR( ".01" ); pos = _decimals - 2; break;
  146. case 242: label = PSTR( "0.1" ); pos = _decimals - 1; break;
  147. case 243: label = PSTR( "1" ); pos = _decimals + 0; break;
  148. case 244: label = PSTR( "10" ); pos = _decimals + 1; break;
  149. default: label = PSTR("100" ); pos = _decimals + 2; break;
  150. }
  151. const bool highlight = (_what & FOREGROUND) && (increment == tag);
  152. switch (pos) {
  153. #if ENABLED(TOUCH_UI_PORTRAIT)
  154. case 0: _button(cmd, tag, BTN_POS(5,_line), BTN_SIZE(2,1), FPSTR(label), true, highlight); break;
  155. case 1: _button(cmd, tag, BTN_POS(7,_line), BTN_SIZE(2,1), FPSTR(label), true, highlight); break;
  156. case 2: _button(cmd, tag, BTN_POS(9,_line), BTN_SIZE(2,1), FPSTR(label), true, highlight); break;
  157. #else
  158. case 0: _button(cmd, tag, BTN_POS(15,2), BTN_SIZE(4,1), FPSTR(label), true, highlight); break;
  159. case 1: _button(cmd, tag, BTN_POS(15,3), BTN_SIZE(4,1), FPSTR(label), true, highlight); break;
  160. case 2: _button(cmd, tag, BTN_POS(15,4), BTN_SIZE(4,1), FPSTR(label), true, highlight); break;
  161. #endif
  162. }
  163. }
  164. void BaseNumericAdjustmentScreen::widgets_t::increments() {
  165. CommandProcessor cmd;
  166. cmd.font(LAYOUT_FONT);
  167. if (_what & BACKGROUND) {
  168. _button_style(cmd, TEXT_LABEL);
  169. cmd.tag(0).text(
  170. #if ENABLED(TOUCH_UI_PORTRAIT)
  171. BTN_POS(1, _line), BTN_SIZE(4,1),
  172. #else
  173. BTN_POS(15, 1), BTN_SIZE(4,1),
  174. #endif
  175. GET_TEXT_F(MSG_INCREMENT)
  176. );
  177. }
  178. _draw_increment_btn(cmd, _line+1, 245 - _decimals);
  179. _draw_increment_btn(cmd, _line+1, 244 - _decimals);
  180. _draw_increment_btn(cmd, _line+1, 243 - _decimals);
  181. #if ENABLED(TOUCH_UI_PORTRAIT)
  182. _line++;
  183. #endif
  184. }
  185. void BaseNumericAdjustmentScreen::widgets_t::adjuster_sram_val(uint8_t tag, FSTR_P label, const char *value, bool is_enabled) {
  186. CommandProcessor cmd;
  187. if (_what & BACKGROUND) {
  188. _button_style(cmd, TEXT_LABEL);
  189. cmd.tag(0)
  190. .font(font_small)
  191. .text( BTN_POS(1,_line), BTN_SIZE(4,1), label);
  192. _button_style(cmd, TEXT_AREA);
  193. cmd.fgcolor(_color).button(BTN_POS(5,_line), BTN_SIZE(5,1), F(""), OPT_FLAT);
  194. }
  195. cmd.font(font_medium);
  196. _button(cmd, tag, BTN_POS(10,_line), BTN_SIZE(2,1), F("-"), is_enabled);
  197. _button(cmd, tag + 1, BTN_POS(12,_line), BTN_SIZE(2,1), F("+"), is_enabled);
  198. if ((_what & FOREGROUND) && is_enabled) {
  199. _button_style(cmd, BTN_NORMAL);
  200. cmd.tag(0)
  201. .font(font_small)
  202. .text(BTN_POS(5,_line), BTN_SIZE(5,1), value);
  203. }
  204. _line++;
  205. }
  206. void BaseNumericAdjustmentScreen::widgets_t::adjuster(uint8_t tag, FSTR_P label, const char *value, bool is_enabled) {
  207. if (_what & BACKGROUND) {
  208. adjuster_sram_val(tag, label, nullptr);
  209. }
  210. if (_what & FOREGROUND) {
  211. char b[strlen(value) + 1];
  212. strcpy(b, value);
  213. adjuster_sram_val(tag, label, b, is_enabled);
  214. }
  215. }
  216. void BaseNumericAdjustmentScreen::widgets_t::adjuster(uint8_t tag, FSTR_P label, float value, bool is_enabled) {
  217. if (_what & BACKGROUND) {
  218. adjuster_sram_val(tag, label, nullptr);
  219. }
  220. if (_what & FOREGROUND) {
  221. char b[32];
  222. dtostrf(value, 5, _decimals, b);
  223. strcat_P(b, PSTR(" "));
  224. strcat_P(b, (const char*) _units);
  225. adjuster_sram_val(tag, label, b, is_enabled);
  226. }
  227. }
  228. void BaseNumericAdjustmentScreen::widgets_t::button(uint8_t tag, FSTR_P label, bool is_enabled) {
  229. CommandProcessor cmd;
  230. cmd.font(LAYOUT_FONT);
  231. _button(cmd, tag, BTN_POS(5,_line), BTN_SIZE(9,1), label, is_enabled);
  232. _line++;
  233. }
  234. void BaseNumericAdjustmentScreen::widgets_t::text_field(uint8_t tag, FSTR_P label, const char *value, bool is_enabled) {
  235. CommandProcessor cmd;
  236. if (_what & BACKGROUND) {
  237. _button_style(cmd, TEXT_LABEL);
  238. cmd.enabled(1)
  239. .tag(0)
  240. .font(font_small)
  241. .text( BTN_POS(1,_line), BTN_SIZE(4,1), label);
  242. _button_style(cmd, TEXT_AREA);
  243. cmd.fgcolor(_color)
  244. .tag(tag)
  245. .button(BTN_POS(5,_line), BTN_SIZE(9,1), F(""), OPT_FLAT);
  246. }
  247. if (_what & FOREGROUND) {
  248. cmd.font(font_small).text( BTN_POS(5,_line), BTN_SIZE(9,1), is_enabled ? value : "-");
  249. }
  250. _line++;
  251. }
  252. void BaseNumericAdjustmentScreen::widgets_t::two_buttons(uint8_t tag1, FSTR_P label1, uint8_t tag2, FSTR_P label2, bool is_enabled) {
  253. CommandProcessor cmd;
  254. cmd.font(LAYOUT_FONT);
  255. _button(cmd, tag1, BTN_POS(5,_line), BTN_SIZE(4.5,1), label1, is_enabled);
  256. _button(cmd, tag2, BTN_POS(9.5,_line), BTN_SIZE(4.5,1), label2, is_enabled);
  257. _line++;
  258. }
  259. void BaseNumericAdjustmentScreen::widgets_t::toggle(uint8_t tag, FSTR_P label, bool value, bool is_enabled) {
  260. CommandProcessor cmd;
  261. if (_what & BACKGROUND) {
  262. _button_style(cmd, TEXT_LABEL);
  263. cmd.font(font_small)
  264. .text(
  265. #if ENABLED(TOUCH_UI_PORTRAIT)
  266. BTN_POS(1, _line), BTN_SIZE( 8,1),
  267. #else
  268. BTN_POS(1, _line), BTN_SIZE(10,1),
  269. #endif
  270. label
  271. );
  272. }
  273. if (_what & FOREGROUND) {
  274. _button_style(cmd, is_enabled ? BTN_TOGGLE : BTN_DISABLED);
  275. cmd.tag(is_enabled ? tag : 0)
  276. .enabled(is_enabled)
  277. .font(font_small)
  278. .toggle2(
  279. #if ENABLED(TOUCH_UI_PORTRAIT)
  280. BTN_POS( 9,_line), BTN_SIZE(5,1),
  281. #else
  282. BTN_POS(10,_line), BTN_SIZE(4,1),
  283. #endif
  284. GET_TEXT_F(MSG_NO), GET_TEXT_F(MSG_YES), value
  285. );
  286. }
  287. _line++;
  288. }
  289. void BaseNumericAdjustmentScreen::widgets_t::home_buttons(uint8_t tag) {
  290. CommandProcessor cmd;
  291. if (_what & BACKGROUND) {
  292. _button_style(cmd, TEXT_LABEL);
  293. cmd.font(font_small)
  294. .text(BTN_POS(1, _line), BTN_SIZE(4,1), GET_TEXT_F(MSG_HOME));
  295. }
  296. cmd.font(LAYOUT_FONT);
  297. _button(cmd, tag+0, BTN_POS(5,_line), BTN_SIZE(2,1), GET_TEXT_F(MSG_AXIS_X));
  298. _button(cmd, tag+1, BTN_POS(7,_line), BTN_SIZE(2,1), GET_TEXT_F(MSG_AXIS_Y));
  299. #if DISABLED(Z_SAFE_HOMING)
  300. _button(cmd, tag+2, BTN_POS(9,_line), BTN_SIZE(2,1), GET_TEXT_F(MSG_AXIS_Z));
  301. _button(cmd, tag+3, BTN_POS(11,_line), BTN_SIZE(3,1), GET_TEXT_F(MSG_AXIS_ALL));
  302. #else
  303. _button(cmd, tag+3, BTN_POS(9,_line), BTN_SIZE(3,1), GET_TEXT_F(MSG_AXIS_ALL));
  304. #endif
  305. _line++;
  306. }
  307. void BaseNumericAdjustmentScreen::onEntry() {
  308. mydata.increment = 0; // This will force the increment to be picked while drawing.
  309. BaseScreen::onEntry();
  310. CommandProcessor cmd;
  311. cmd.set_button_style_callback(nullptr);
  312. }
  313. bool BaseNumericAdjustmentScreen::onTouchEnd(uint8_t tag) {
  314. switch (tag) {
  315. case 1: GOTO_PREVIOUS(); return true;
  316. case 240 ... 245: mydata.increment = tag; break;
  317. default: return current_screen.onTouchHeld(tag);
  318. }
  319. return true;
  320. }
  321. float BaseNumericAdjustmentScreen::getIncrement() {
  322. switch (mydata.increment) {
  323. case 240: return 0.001;
  324. case 241: return 0.01;
  325. case 242: return 0.1;
  326. case 243: return 1.0;
  327. case 244: return 10.0;
  328. case 245: return 100.0;
  329. default: return 0.0;
  330. }
  331. }
  332. #endif // FTDI_BASE_NUMERIC_ADJ_SCREEN