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 18KB

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