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.

screen_types.h 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /********************
  2. * screen_types.cpp *
  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: <https://www.gnu.org/licenses/>. *
  19. ****************************************************************************/
  20. #pragma once
  21. typedef enum {
  22. BACKGROUND = 1,
  23. FOREGROUND = 2,
  24. BOTH = 3
  25. } draw_mode_t;
  26. /********************** VIRTUAL DISPATCH DATA TYPE ******************************/
  27. // True virtual classes are extremely expensive on the Arduino
  28. // as the compiler stores the virtual function tables in RAM.
  29. // We invent a data type called ScreenRef that gives us
  30. // polymorphism by mapping an ID to virtual methods on various
  31. // classes. This works by keeping a table in PROGMEM of pointers
  32. // to static methods.
  33. #define DECL_SCREEN(className) { \
  34. className::onStartup, \
  35. className::onEntry, \
  36. className::onExit, \
  37. className::onIdle, \
  38. className::onRefresh, \
  39. className::onRedraw, \
  40. className::onTouchStart, \
  41. className::onTouchHeld, \
  42. className::onTouchEnd \
  43. }
  44. #define GET_METHOD(type, method) reinterpret_cast<method##_func_t*>(pgm_read_ptr_far(&functionTable[type].method##_ptr))
  45. #define SCREEN_TABLE PROGMEM const ScreenRef::table_t ScreenRef::functionTable[] =
  46. #define SCREEN_TABLE_POST size_t ScreenRef::tableSize() { \
  47. constexpr size_t siz = sizeof(functionTable)/sizeof(functionTable[0]); \
  48. static_assert(siz > 0, "The screen table is empty!"); \
  49. return siz; \
  50. }
  51. class ScreenRef {
  52. protected:
  53. typedef void onStartup_func_t();
  54. typedef void onEntry_func_t();
  55. typedef void onExit_func_t();
  56. typedef void onIdle_func_t();
  57. typedef void onRefresh_func_t();
  58. typedef void onRedraw_func_t(draw_mode_t);
  59. typedef bool onTouchStart_func_t(uint8_t);
  60. typedef bool onTouchHeld_func_t(uint8_t);
  61. typedef bool onTouchEnd_func_t(uint8_t);
  62. private:
  63. typedef struct {
  64. onStartup_func_t *onStartup_ptr;
  65. onEntry_func_t *onEntry_ptr;
  66. onExit_func_t *onExit_ptr;
  67. onIdle_func_t *onIdle_ptr;
  68. onRefresh_func_t *onRefresh_ptr;
  69. onRedraw_func_t *onRedraw_ptr;
  70. onTouchStart_func_t *onTouchStart_ptr;
  71. onTouchHeld_func_t *onTouchHeld_ptr;
  72. onTouchEnd_func_t *onTouchEnd_ptr;
  73. } table_t;
  74. uint8_t type = 0;
  75. static PROGMEM const table_t functionTable[];
  76. public:
  77. static size_t tableSize();
  78. uint8_t getType() {return type;}
  79. void setType(uint8_t t) {type = t;}
  80. uint8_t lookupScreen(onRedraw_func_t onRedraw_ptr);
  81. void setScreen(onRedraw_func_t onRedraw_ptr);
  82. void onStartup() {GET_METHOD(type, onStartup)();}
  83. void onEntry() {GET_METHOD(type, onEntry)();}
  84. void onExit() {GET_METHOD(type, onExit)();}
  85. void onIdle() {GET_METHOD(type, onIdle)();}
  86. void onRefresh() {GET_METHOD(type, onRefresh)();}
  87. void onRedraw(draw_mode_t dm) {GET_METHOD(type, onRedraw)(dm);}
  88. bool onTouchStart(uint8_t tag) {return GET_METHOD(type, onTouchStart)(tag);}
  89. bool onTouchHeld(uint8_t tag) {return GET_METHOD(type, onTouchHeld)(tag);}
  90. bool onTouchEnd(uint8_t tag) {return GET_METHOD(type, onTouchEnd)(tag);}
  91. void initializeAll();
  92. };
  93. /********************** SCREEN STACK ******************************/
  94. // To conserve dynamic memory, the screen stack is hard-coded to
  95. // have four values, allowing a menu of up to four levels.
  96. class ScreenStack : public ScreenRef {
  97. private:
  98. uint8_t stack[4];
  99. public:
  100. void start();
  101. void push(onRedraw_func_t);
  102. void push();
  103. void pop();
  104. void forget();
  105. void goTo(onRedraw_func_t);
  106. void goBack();
  107. uint8_t peek() {return stack[0];}
  108. uint8_t getScreen() {return getType();}
  109. };
  110. extern ScreenStack current_screen;
  111. /********************** BASE SCREEN CLASS ******************************/
  112. /* UIScreen is the base class for all user interface screens.
  113. */
  114. class UIScreen {
  115. public:
  116. static void onStartup() {}
  117. static void onEntry() {current_screen.onRefresh();}
  118. static void onExit() {}
  119. static void onIdle() {}
  120. static bool onTouchStart(uint8_t) {return true;}
  121. static bool onTouchHeld(uint8_t) {return false;}
  122. static bool onTouchEnd(uint8_t) {return true;}
  123. };
  124. #define PUSH_SCREEN(screen) current_screen.push(screen::onRedraw)
  125. #define GOTO_SCREEN(screen) current_screen.goTo(screen::onRedraw)
  126. #define GOTO_PREVIOUS() current_screen.goBack();
  127. #define AT_SCREEN(screen) (current_screen.getType() == current_screen.lookupScreen(screen::onRedraw))
  128. #define IS_PARENT_SCREEN(screen) (current_screen.peek() == current_screen.lookupScreen(screen::onRedraw))
  129. /************************** CACHED VS UNCACHED SCREENS ***************************/
  130. class UncachedScreen {
  131. public:
  132. static void onRefresh() {
  133. using namespace FTDI;
  134. CommandProcessor cmd;
  135. cmd.cmd(CMD_DLSTART);
  136. #if ENABLED(TOUCH_UI_USE_UTF8)
  137. load_utf8_bitmaps(cmd);
  138. #endif
  139. current_screen.onRedraw(BOTH);
  140. cmd.cmd(DL::DL_DISPLAY);
  141. cmd.cmd(CMD_SWAP);
  142. cmd.execute();
  143. }
  144. };
  145. template<uint8_t DL_SLOT,uint32_t DL_SIZE = 0>
  146. class CachedScreen {
  147. protected:
  148. static void gfxError() {
  149. using namespace FTDI;
  150. CommandProcessor cmd;
  151. cmd.cmd(CMD_DLSTART)
  152. .cmd(CLEAR(true,true,true))
  153. .font(30)
  154. .text(0, 0, display_width, display_height, F("GFX MEM FULL"));
  155. }
  156. static bool storeBackground() {
  157. DLCache dlcache(DL_SLOT);
  158. if (!dlcache.store(DL_SIZE)) {
  159. SERIAL_ECHO_MSG("CachedScreen::storeBackground() failed: not enough DL cache space");
  160. gfxError(); // Try to cache a shorter error message instead.
  161. dlcache.store(DL_SIZE);
  162. return false;
  163. }
  164. return true;
  165. }
  166. static void repaintBackground() {
  167. using namespace FTDI;
  168. DLCache dlcache(DL_SLOT);
  169. CommandProcessor cmd;
  170. cmd.cmd(CMD_DLSTART);
  171. #if ENABLED(TOUCH_UI_USE_UTF8)
  172. load_utf8_bitmaps(cmd);
  173. #endif
  174. current_screen.onRedraw(BACKGROUND);
  175. dlcache.store(DL_SIZE);
  176. }
  177. public:
  178. static void onRefresh() {
  179. #if ENABLED(TOUCH_UI_DEBUG)
  180. const uint32_t start_time = millis();
  181. #endif
  182. using namespace FTDI;
  183. DLCache dlcache(DL_SLOT);
  184. CommandProcessor cmd;
  185. cmd.cmd(CMD_DLSTART);
  186. if (dlcache.has_data()) {
  187. dlcache.append();
  188. }
  189. else {
  190. #if ENABLED(TOUCH_UI_USE_UTF8)
  191. load_utf8_bitmaps(cmd);
  192. #endif
  193. current_screen.onRedraw(BACKGROUND);
  194. dlcache.store(DL_SIZE);
  195. }
  196. current_screen.onRedraw(FOREGROUND);
  197. cmd.cmd(DL::DL_DISPLAY);
  198. cmd.cmd(CMD_SWAP);
  199. cmd.execute();
  200. #if ENABLED(TOUCH_UI_DEBUG)
  201. SERIAL_ECHOLNPAIR("Time to draw screen (ms): ", millis() - start_time);
  202. #endif
  203. }
  204. };