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.

command_processor.h 19KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. /***********************
  2. * command_processor.h *
  3. ***********************/
  4. /****************************************************************************
  5. * Written By Marcio Teixeira 2018 - Aleph Objects, Inc. *
  6. * *
  7. * This program is free software: you can redistribute it and/or modify *
  8. * it under the terms of the GNU General Public License as published by *
  9. * the Free Software Foundation, either version 3 of the License, or *
  10. * (at your option) any later version. *
  11. * *
  12. * This program is distributed in the hope that it will be useful, *
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
  15. * GNU General Public License for more details. *
  16. * *
  17. * To view a copy of the GNU General Public License, go to the following *
  18. * location: <http://www.gnu.org/licenses/>. *
  19. ****************************************************************************/
  20. #pragma once
  21. typedef struct {
  22. uint32_t bg;
  23. uint32_t grad;
  24. uint32_t fg;
  25. uint32_t rgb;
  26. } btn_colors;
  27. // Disable TOUCH_UI_FIT_TEXT on a case-by-case basis
  28. namespace FTDI {
  29. constexpr uint16_t OPT_NOFIT = OPT_NOTICKS;
  30. }
  31. /**************************** Enhanced Command Processor **************************/
  32. /* The CommandProcessor class wraps the CommandFifo with several features to make
  33. * defining user interfaces much easier.
  34. *
  35. * - Implements chaining on all methods
  36. * - Automatically adds text to button, toggle, text and keys.
  37. * - Constrains all widgets to fit inside a box for ease of layout.
  38. * - Font size is specified using a chained modifier.
  39. * - Option argument is given the default OPT_3D value.
  40. */
  41. class CommandProcessor : public CLCD::CommandFifo {
  42. public:
  43. static constexpr uint8_t STYLE_DISABLED = 0x80;
  44. private:
  45. static bool default_button_style_func(CommandProcessor &, uint8_t tag, uint8_t & /*style*/, uint16_t &options, bool) {
  46. if (tag != 0 && FTDI::EventLoop::get_pressed_tag() == tag) {
  47. options = FTDI::OPT_FLAT;
  48. }
  49. return false;
  50. }
  51. typedef bool btn_style_func_t(CommandProcessor &cmd, uint8_t tag, uint8_t &style, uint16_t &options, bool post);
  52. static btn_style_func_t *_btn_style_callback;
  53. static bool is_tracking;
  54. int8_t _font = 26, _tag = 0;
  55. uint8_t _style = 0;
  56. protected:
  57. // Returns the cannonical thickness of a widget (i.e. the height of a toggle element)
  58. uint16_t widget_thickness() {
  59. CLCD::FontMetrics fm(_font);
  60. return fm.height * 20.0/16;
  61. }
  62. FORCEDINLINE void linear_widget_box(int16_t &x, int16_t &y, int16_t &w, int16_t &h, bool tracker = false) {
  63. const uint16_t th = widget_thickness() / 2;
  64. if (w > h) {
  65. x += tracker ? th * 2.5 : th;
  66. y += (h - th) / 2;
  67. w -= tracker ? th * 5.0 : th * 2;
  68. h = th;
  69. } else {
  70. x += (w - th) / 2;
  71. y += tracker ? th * 2.5 : th;
  72. w = th;
  73. h -= tracker ? th * 5.0 : th * 2;
  74. }
  75. }
  76. FORCEDINLINE uint16_t circular_widget_box(int16_t &x, int16_t &y, int16_t &w, int16_t &h) {
  77. const uint16_t r = min(w,h) / 2;
  78. x += w / 2;
  79. y += h / 2;
  80. w = 1;
  81. h = 1;
  82. return r;
  83. }
  84. public:
  85. // Helper method for setting all colors at once
  86. inline CommandProcessor& colors(const btn_colors &colors) {
  87. cmd(FTDI::COLOR_RGB(colors.rgb))
  88. .gradcolor(colors.grad)
  89. .fgcolor(colors.fg)
  90. .bgcolor(colors.bg);
  91. return *this;
  92. }
  93. inline CommandProcessor& bitmap_size(uint8_t filter, uint8_t wrapx, uint8_t wrapy, uint16_t width, uint16_t height) {
  94. cmd(FTDI::BITMAP_SIZE(filter, wrapx, wrapy, width, height));
  95. #if FTDI_API_LEVEL >= 810
  96. if (FTDI::ftdi_chip >= 810)
  97. cmd(FTDI::BITMAP_SIZE_H(width >> 9, height >> 9));
  98. #endif
  99. return *this;
  100. }
  101. inline CommandProcessor& bitmap_layout(uint8_t format, uint16_t linestride, uint16_t height) {
  102. cmd(FTDI::BITMAP_LAYOUT(format, linestride, height));
  103. #if FTDI_API_LEVEL >= 810
  104. if (FTDI::ftdi_chip >= 810)
  105. cmd(FTDI::BITMAP_LAYOUT_H(linestride >> 10, height >> 9));
  106. #endif
  107. return *this;
  108. }
  109. inline CommandProcessor& set_button_style_callback(const btn_style_func_t *func) {
  110. _btn_style_callback = func ? func : default_button_style_func;
  111. return *this;
  112. }
  113. inline CommandProcessor& tag (uint8_t tag) {_tag = tag; cmd(FTDI::TAG(tag)); return *this;}
  114. inline CommandProcessor& font (int16_t font) {_font = font; return *this;}
  115. inline CommandProcessor& enabled (bool enabled=false) {
  116. if (enabled)
  117. _style &= ~STYLE_DISABLED;
  118. else
  119. _style |= STYLE_DISABLED;
  120. return *this;
  121. }
  122. inline CommandProcessor& style (uint8_t style) {
  123. _style = (_style & STYLE_DISABLED) | style;
  124. return *this;
  125. }
  126. // Wrap all the CommandFifo routines to allow method chaining
  127. inline CommandProcessor& cmd (uint32_t cmd32) {CLCD::CommandFifo::cmd(cmd32); return *this;}
  128. inline CommandProcessor& cmd (void* data, uint16_t len) {CLCD::CommandFifo::cmd(data, len); return *this;}
  129. inline CommandProcessor& execute() {CLCD::CommandFifo::execute(); return *this;}
  130. inline CommandProcessor& fgcolor (uint32_t rgb) {CLCD::CommandFifo::fgcolor(rgb); return *this;}
  131. inline CommandProcessor& bgcolor (uint32_t rgb) {CLCD::CommandFifo::bgcolor(rgb); return *this;}
  132. inline CommandProcessor& gradcolor(uint32_t rgb) {CLCD::CommandFifo::gradcolor(rgb); return *this;}
  133. inline CommandProcessor& snapshot (uint32_t ptr) {CLCD::CommandFifo::snapshot(ptr); return *this;}
  134. inline CommandProcessor& loadimage(uint32_t ptr, uint32_t options)
  135. {CLCD::CommandFifo::loadimage(ptr, options); return *this;}
  136. inline CommandProcessor& sketch (int16_t x, int16_t y, uint16_t w, uint16_t h, uint32_t ptr, uint16_t format)
  137. {CLCD::CommandFifo::sketch(x, y, w, h, ptr, format); return *this;}
  138. inline CommandProcessor& screensaver () {CLCD::CommandFifo::screensaver(); return *this;}
  139. #if FTDI_API_LEVEL >= 810
  140. inline CommandProcessor& setbase (uint8_t base) {CLCD::CommandFifo::setbase(base); return *this;}
  141. #endif
  142. inline CommandProcessor& loadidentity () {CLCD::CommandFifo::loadidentity(); return *this;}
  143. inline CommandProcessor& scale (int32_t sx, int32_t sy) {CLCD::CommandFifo::scale(sx,sy); return *this;}
  144. inline CommandProcessor& rotate (int32_t a) {CLCD::CommandFifo::rotate(a); return *this;}
  145. inline CommandProcessor& translate(int32_t tx, int32_t ty) {CLCD::CommandFifo::translate(tx,ty); return *this;}
  146. inline CommandProcessor& setmatrix () {CLCD::CommandFifo::setmatrix(); return *this;}
  147. inline CommandProcessor& stop () {CLCD::CommandFifo::stop(); return *this;}
  148. inline CommandProcessor& memzero (uint32_t ptr, uint32_t size)
  149. {CLCD::CommandFifo::memzero(ptr, size); return *this;}
  150. inline CommandProcessor& memset (uint32_t ptr, uint32_t val, uint32_t size)
  151. {CLCD::CommandFifo::memset(ptr, val, size); return *this;}
  152. inline CommandProcessor& memcpy (uint32_t src, uint32_t dst, uint32_t size)
  153. {CLCD::CommandFifo::memcpy(src, dst, size); return *this;}
  154. inline CommandProcessor& memcrc (uint32_t ptr, uint32_t num, uint32_t result)
  155. {CLCD::CommandFifo::memcrc(ptr, num, result); return *this;}
  156. inline CommandProcessor& memwrite (uint32_t ptr, uint32_t value)
  157. {CLCD::CommandFifo::memwrite(ptr, value); return *this;}
  158. inline CommandProcessor& inflate (uint32_t ptr)
  159. {CLCD::CommandFifo::inflate(ptr); return *this;}
  160. inline CommandProcessor& getptr (uint32_t result)
  161. {CLCD::CommandFifo::getptr(result); return *this;}
  162. inline CommandProcessor& getprops (uint32_t ptr, uint32_t width, uint32_t height)
  163. {CLCD::CommandFifo::getprops(ptr, width, height); return *this;}
  164. #if FTDI_API_LEVEL >= 810
  165. inline CommandProcessor& setbitmap (uint32_t ptr, uint16_t fmt, uint16_t w, uint16_t h)
  166. {CLCD::CommandFifo::setbitmap(ptr,fmt,w,h); return *this;}
  167. inline CommandProcessor& snapshot2 (uint32_t fmt, uint32_t ptr, int16_t x, int16_t y, uint16_t w, uint16_t h)
  168. {CLCD::CommandFifo::snapshot2(fmt,ptr,x,y,w,h); return *this;}
  169. inline CommandProcessor& mediafifo (uint32_t p, uint32_t s) {CLCD::CommandFifo::mediafifo(p, s); return *this;}
  170. inline CommandProcessor& playvideo(uint32_t options) {CLCD::CommandFifo::playvideo(options); return *this;}
  171. inline CommandProcessor& romfont(uint8_t font, uint8_t slot) {CLCD::CommandFifo::romfont(font, slot); return *this;}
  172. #endif
  173. inline CommandProcessor& gradient(int16_t x0, int16_t y0, uint32_t rgb0, int16_t x1, int16_t y1, uint32_t rgb1)
  174. {CLCD::CommandFifo::gradient(x0,y0,rgb0,x1,y1,rgb1); return *this;}
  175. inline CommandProcessor& rectangle(int16_t x, int16_t y, int16_t w, int16_t h) {
  176. using namespace FTDI;
  177. CLCD::CommandFifo::cmd(BEGIN(RECTS));
  178. CLCD::CommandFifo::cmd(VERTEX2F(x * 16, y * 16));
  179. CLCD::CommandFifo::cmd(VERTEX2F((x + w) * 16, (y + h) * 16));
  180. return *this;
  181. }
  182. template<typename T>
  183. FORCEDINLINE CommandProcessor& toggle(int16_t x, int16_t y, int16_t w, int16_t h, T text, bool state, uint16_t options = FTDI::OPT_3D) {
  184. CLCD::FontMetrics fm(_font);
  185. const int16_t widget_h = fm.height * 20.0 / 16;
  186. //const int16_t outer_bar_r = widget_h / 2;
  187. //const int16_t knob_r = outer_bar_r - 1.5;
  188. // The y coordinate of the toggle is the baseline of the text,
  189. // so we must introduce a fudge factor based on the line height to
  190. // actually center the control.
  191. const int16_t fudge_y = fm.height * 5 / 16;
  192. CLCD::CommandFifo::toggle(x + h / 2, y + (h - widget_h) / 2 + fudge_y, w - h, _font, options, state);
  193. CLCD::CommandFifo::str(text);
  194. return *this;
  195. }
  196. CommandProcessor& toggle2(int16_t x, int16_t y, int16_t w, int16_t h, progmem_str no, progmem_str yes, bool state, uint16_t options = FTDI::OPT_3D) {
  197. char text[strlen_P((const char *)no) + strlen_P((const char *)yes) + 2];
  198. strcpy_P(text, (const char *)no);
  199. strcat(text, "\xFF");
  200. strcat_P(text, (const char *)yes);
  201. return toggle(x, y, w, h, text, state, options);
  202. }
  203. // Contrained drawing routines. These constrain the widget inside a box for easier layout.
  204. // The FORCEDINLINE ensures that the code is inlined so that all the math is done at compile time.
  205. FORCEDINLINE CommandProcessor& track_linear(int16_t x, int16_t y, int16_t w, int16_t h, int16_t tag) {
  206. linear_widget_box(x, y, w, h, true);
  207. CLCD::CommandFifo::track(x, y, w, h, tag);
  208. is_tracking = true;
  209. return *this;
  210. }
  211. FORCEDINLINE CommandProcessor& track_circular(int16_t x, int16_t y, int16_t w, int16_t h, int16_t tag) {
  212. circular_widget_box(x,y, w, h);
  213. CLCD::CommandFifo::track(x, y, w, h, tag);
  214. is_tracking = true;
  215. return *this;
  216. }
  217. uint8_t track_tag (uint16_t &value) {
  218. if (is_tracking) {
  219. if (FTDI::EventLoop::is_touch_held()) {
  220. return CLCD::get_tracker(value);
  221. } else {
  222. CLCD::CommandFifo::track(0, 0, 0, 0, 0);
  223. CLCD::CommandFifo::execute();
  224. is_tracking = false;
  225. }
  226. }
  227. return 0;
  228. }
  229. FORCEDINLINE CommandProcessor& clock(int16_t x, int16_t y, int16_t w, int16_t h, int16_t hr, int16_t m, int16_t s, int16_t ms, uint16_t options = FTDI::OPT_3D) {
  230. const uint16_t r = circular_widget_box(x, y, w, h);
  231. CLCD::CommandFifo::clock(x, y, r, options, hr, m, s, ms);
  232. return *this;
  233. }
  234. FORCEDINLINE CommandProcessor& gauge(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t major, uint16_t minor, uint16_t val, uint16_t range, uint16_t options = FTDI::OPT_3D) {
  235. const uint16_t r = circular_widget_box(x, y, w, h);
  236. CLCD::CommandFifo::gauge(x, y, r, options, major, minor, val, range);
  237. return *this;
  238. }
  239. FORCEDINLINE CommandProcessor& dial(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t val, uint16_t options = FTDI::OPT_3D) {
  240. const uint16_t r = circular_widget_box(x, y, w, h);
  241. CLCD::CommandFifo::dial(x, y, r, options, val);
  242. return *this;
  243. }
  244. FORCEDINLINE CommandProcessor& slider(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t val, uint16_t range, uint16_t options = FTDI::OPT_3D) {
  245. linear_widget_box(x, y, w, h);
  246. CLCD::CommandFifo::slider(x, y, w, h, options, val, range);
  247. return *this;
  248. }
  249. FORCEDINLINE CommandProcessor& progress(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t val, uint16_t range, uint16_t options = FTDI::OPT_3D) {
  250. linear_widget_box(x, y, w, h);
  251. CLCD::CommandFifo::progress(x, y, w, h, options, val, range);
  252. return *this;
  253. }
  254. FORCEDINLINE CommandProcessor& scrollbar(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t val, uint16_t size, uint16_t range, uint16_t options = 0) {
  255. linear_widget_box(x, y, w, h);
  256. CLCD::CommandFifo::scrollbar(x, y, w, h, options, val, size, range);
  257. return *this;
  258. }
  259. void apply_text_alignment(int16_t &x, int16_t &y, int16_t w, int16_t h, uint16_t options) {
  260. using namespace FTDI;
  261. x += ((options & OPT_CENTERX) ? w/2 : ((options & OPT_RIGHTX) ? w : 0));
  262. y += ((options & OPT_CENTERY) ? h/2 : h);
  263. }
  264. // Reduce font size until text fits the enclosing box.
  265. template<typename T>
  266. int8_t apply_fit_text(int16_t w, int16_t h, T text) {
  267. using namespace FTDI;
  268. int8_t font = _font;
  269. #ifdef TOUCH_UI_USE_UTF8
  270. const bool is_utf8 = has_utf8_chars(text);
  271. #endif
  272. for (;font >= 26;) {
  273. int16_t width, height;
  274. #ifdef TOUCH_UI_USE_UTF8
  275. if (is_utf8) {
  276. width = get_utf8_text_width(text, font_size_t::from_romfont(font));
  277. height = font_size_t::from_romfont(font).get_height();
  278. } else
  279. #endif
  280. {
  281. CLCD::FontMetrics fm(font);
  282. width = fm.get_text_width(text);
  283. height = fm.height;
  284. }
  285. if (width < w && height < h) break;
  286. font--;
  287. }
  288. return font;
  289. }
  290. CommandProcessor& number(int16_t x, int16_t y, int16_t w, int16_t h, int32_t n, uint16_t options = FTDI::OPT_CENTER) {
  291. using namespace FTDI;
  292. apply_text_alignment(x, y, w, h, options);
  293. CLCD::CommandFifo::number(x, y, _font, options, n);
  294. return *this;
  295. }
  296. template<typename T>
  297. uint16_t text_width(T text) {
  298. using namespace FTDI;
  299. #ifdef TOUCH_UI_USE_UTF8
  300. if (has_utf8_chars(text))
  301. return get_utf8_text_width(text, font_size_t::from_romfont(_font));
  302. #endif
  303. CLCD::FontMetrics fm(_font);
  304. return fm.get_text_width(text);
  305. }
  306. template<typename T>
  307. CommandProcessor& text(int16_t x, int16_t y, int16_t w, int16_t h, T text, uint16_t options = FTDI::OPT_CENTER) {
  308. using namespace FTDI;
  309. apply_text_alignment(x, y, w, h, options);
  310. #ifdef TOUCH_UI_FIT_TEXT
  311. const int8_t font = (options & OPT_NOFIT) ? _font : apply_fit_text(w, h, text);
  312. #else
  313. const int8_t font = _font;
  314. #endif
  315. #ifdef TOUCH_UI_USE_UTF8
  316. if (has_utf8_chars(text))
  317. draw_utf8_text(*this, x, y, text, font_size_t::from_romfont(font), options);
  318. else
  319. #endif
  320. {
  321. CLCD::CommandFifo::text(x, y, font, options);
  322. CLCD::CommandFifo::str(text);
  323. }
  324. return *this;
  325. }
  326. FORCEDINLINE CommandProcessor& icon(int16_t x, int16_t y, int16_t w, int16_t h, const FTDI::bitmap_info_t& info, const float scale = 1) {
  327. using namespace FTDI;
  328. cmd(BEGIN(BITMAPS));
  329. if (scale != 1) {
  330. cmd(BITMAP_TRANSFORM_A(uint32_t(float(256)/scale)));
  331. cmd(BITMAP_TRANSFORM_E(uint32_t(float(256)/scale)));
  332. }
  333. cmd(BITMAP_SIZE(info.filter, info.wrapx, info.wrapy, info.width*scale, info.height*scale));
  334. cmd(VERTEX2F((x + w/2 - info.width*scale/2)*16, (y + h/2 - info.height*scale/2)*16));
  335. if (scale != 1) {
  336. cmd(BITMAP_TRANSFORM_A(256));
  337. cmd(BITMAP_TRANSFORM_E(256));
  338. }
  339. return *this;
  340. }
  341. template<typename T>
  342. CommandProcessor& button(int16_t x, int16_t y, int16_t w, int16_t h, T text, uint16_t options = FTDI::OPT_3D) {
  343. using namespace FTDI;
  344. bool styleModified = false;
  345. if (_btn_style_callback) styleModified = _btn_style_callback(*this, _tag, _style, options, false);
  346. #ifdef TOUCH_UI_FIT_TEXT
  347. const int8_t font = (options & OPT_NOFIT) ? _font : apply_fit_text(w, h, text);
  348. #else
  349. const int8_t font = _font;
  350. #endif
  351. CLCD::CommandFifo::button(x, y, w, h, font, options);
  352. #ifdef TOUCH_UI_USE_UTF8
  353. if (has_utf8_chars(text)) {
  354. CLCD::CommandFifo::str(F(""));
  355. apply_text_alignment(x, y, w, h, OPT_CENTER);
  356. if (!(options & FTDI::OPT_FLAT)) {
  357. // Reproduce the black "shadow" the FTDI adds to the button label
  358. CLCD::CommandFifo::cmd(SAVE_CONTEXT());
  359. CLCD::CommandFifo::cmd(COLOR_RGB(0x00000));
  360. draw_utf8_text(*this, x-1, y-1, text, font_size_t::from_romfont(font), OPT_CENTER);
  361. CLCD::CommandFifo::cmd(RESTORE_CONTEXT());
  362. }
  363. // Draw the button label
  364. draw_utf8_text(*this, x, y, text, font_size_t::from_romfont(font), OPT_CENTER);
  365. }
  366. else
  367. #endif
  368. CLCD::CommandFifo::str(text);
  369. if (_btn_style_callback && styleModified) _btn_style_callback(*this, _tag, _style, options, true);
  370. return *this;
  371. }
  372. template<typename T>
  373. CommandProcessor& keys(int16_t x, int16_t y, int16_t w, int16_t h, T keys, uint16_t options = FTDI::OPT_3D) {
  374. CLCD::CommandFifo::keys(x, y, w, h, _font, options);
  375. CLCD::CommandFifo::str(keys);
  376. return *this;
  377. }
  378. FORCEDINLINE CommandProcessor& spinner(int16_t x, int16_t y, int16_t w, int16_t h, uint16_t style = 0, uint16_t scale = 0) {
  379. circular_widget_box(x, y, w, h);
  380. CLCD::CommandFifo::spinner(x, y, style, scale);
  381. return *this;
  382. }
  383. };