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.

files_screen.cpp 8.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /********************
  2. * files_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: <http://www.gnu.org/licenses/>. *
  20. ****************************************************************************/
  21. #include "../config.h"
  22. #if ENABLED(LULZBOT_TOUCH_UI)
  23. #include "screens.h"
  24. #include "screen_data.h"
  25. using namespace FTDI;
  26. using namespace ExtUI;
  27. using namespace Theme;
  28. void FilesScreen::onEntry() {
  29. screen_data.FilesScreen.cur_page = 0;
  30. screen_data.FilesScreen.selected_tag = 0xFF;
  31. #if ENABLED(SCROLL_LONG_FILENAMES) && (FTDI_API_LEVEL >= 810)
  32. CLCD::mem_write_32(CLCD::REG::MACRO_0,DL::NOP);
  33. #endif
  34. gotoPage(0);
  35. BaseScreen::onEntry();
  36. }
  37. const char *FilesScreen::getSelectedShortFilename() {
  38. FileList files;
  39. files.seek(getFileForTag(screen_data.FilesScreen.selected_tag), true);
  40. return files.shortFilename();
  41. }
  42. const char *FilesScreen::getSelectedLongFilename() {
  43. FileList files;
  44. files.seek(getFileForTag(screen_data.FilesScreen.selected_tag), true);
  45. return files.longFilename();
  46. }
  47. void FilesScreen::drawSelectedFile() {
  48. FileList files;
  49. files.seek(getFileForTag(screen_data.FilesScreen.selected_tag), true);
  50. screen_data.FilesScreen.flags.is_dir = files.isDir();
  51. drawFileButton(
  52. files.filename(),
  53. screen_data.FilesScreen.selected_tag,
  54. screen_data.FilesScreen.flags.is_dir,
  55. true
  56. );
  57. }
  58. uint16_t FilesScreen::getFileForTag(uint8_t tag) {
  59. return screen_data.FilesScreen.cur_page * files_per_page + tag - 2;
  60. }
  61. #ifdef TOUCH_UI_PORTRAIT
  62. #define GRID_COLS 6
  63. #define GRID_ROWS (files_per_page + header_h + footer_h)
  64. #else
  65. #define GRID_COLS 6
  66. #define GRID_ROWS (files_per_page + header_h + footer_h)
  67. #endif
  68. void FilesScreen::drawFileButton(const char* filename, uint8_t tag, bool is_dir, bool is_highlighted) {
  69. const uint8_t line = getLineForTag(tag)+1;
  70. CommandProcessor cmd;
  71. cmd.tag(tag);
  72. cmd.cmd(COLOR_RGB(is_highlighted ? fg_action : bg_color));
  73. cmd.font(font_medium)
  74. .rectangle( 0, BTN_Y(header_h+line), display_width, BTN_H(1));
  75. cmd.cmd(COLOR_RGB(is_highlighted ? normal_btn.rgb : bg_text_enabled));
  76. #if ENABLED(SCROLL_LONG_FILENAMES)
  77. if (is_highlighted) {
  78. cmd.cmd(SAVE_CONTEXT());
  79. cmd.cmd(MACRO(0));
  80. }
  81. #endif
  82. cmd.text (BTN_POS(1,header_h+line), BTN_SIZE(6,1), filename, OPT_CENTERY);
  83. if (is_dir) {
  84. cmd.text(BTN_POS(1,header_h+line), BTN_SIZE(6,1), F("> "), OPT_CENTERY | OPT_RIGHTX);
  85. }
  86. #if ENABLED(SCROLL_LONG_FILENAMES)
  87. if (is_highlighted) {
  88. cmd.cmd(RESTORE_CONTEXT());
  89. }
  90. #endif
  91. }
  92. void FilesScreen::drawFileList() {
  93. FileList files;
  94. screen_data.FilesScreen.num_page = max(1,(ceil)(float(files.count()) / files_per_page));
  95. screen_data.FilesScreen.cur_page = min(screen_data.FilesScreen.cur_page, screen_data.FilesScreen.num_page-1);
  96. screen_data.FilesScreen.flags.is_root = files.isAtRootDir();
  97. #undef MARGIN_T
  98. #undef MARGIN_B
  99. #define MARGIN_T 0
  100. #define MARGIN_B 0
  101. uint16_t fileIndex = screen_data.FilesScreen.cur_page * files_per_page;
  102. for(uint8_t i = 0; i < files_per_page; i++, fileIndex++) {
  103. if (files.seek(fileIndex)) {
  104. drawFileButton(files.filename(), getTagForLine(i), files.isDir(), false);
  105. } else {
  106. break;
  107. }
  108. }
  109. }
  110. void FilesScreen::drawHeader() {
  111. const bool prev_enabled = screen_data.FilesScreen.cur_page > 0;
  112. const bool next_enabled = screen_data.FilesScreen.cur_page < (screen_data.FilesScreen.num_page - 1);
  113. #undef MARGIN_T
  114. #undef MARGIN_B
  115. #define MARGIN_T 0
  116. #define MARGIN_B 2
  117. char str[16];
  118. sprintf_P(str, PSTR("Page %d of %d"),
  119. screen_data.FilesScreen.cur_page + 1, screen_data.FilesScreen.num_page);
  120. CommandProcessor cmd;
  121. cmd.colors(normal_btn)
  122. .font(font_small)
  123. .tag(0).button( BTN_POS(2,1), BTN_SIZE(4,header_h), str, OPT_CENTER | OPT_FLAT)
  124. .font(font_medium)
  125. .colors(action_btn)
  126. .tag(241).enabled(prev_enabled).button( BTN_POS(1,1), BTN_SIZE(1,header_h), F("<"))
  127. .tag(242).enabled(next_enabled).button( BTN_POS(6,1), BTN_SIZE(1,header_h), F(">"));
  128. }
  129. void FilesScreen::drawFooter() {
  130. #undef MARGIN_T
  131. #undef MARGIN_B
  132. #ifdef TOUCH_UI_PORTRAIT
  133. #define MARGIN_T 15
  134. #define MARGIN_B 5
  135. #else
  136. #define MARGIN_T 5
  137. #define MARGIN_B 5
  138. #endif
  139. const bool has_selection = screen_data.FilesScreen.selected_tag != 0xFF;
  140. const uint8_t back_tag = screen_data.FilesScreen.flags.is_root ? 240 : 245;
  141. const uint8_t y = GRID_ROWS - footer_h + 1;
  142. const uint8_t h = footer_h;
  143. CommandProcessor cmd;
  144. cmd.colors(normal_btn)
  145. .font(font_medium)
  146. .colors(has_selection ? normal_btn : action_btn)
  147. .tag(back_tag).button( BTN_POS(4,y), BTN_SIZE(3,h), GET_TEXTF(BACK))
  148. .enabled(has_selection)
  149. .colors(has_selection ? action_btn : normal_btn);
  150. if (screen_data.FilesScreen.flags.is_dir) {
  151. cmd.tag(244).button( BTN_POS(1, y), BTN_SIZE(3,h), GET_TEXTF(OPEN_DIR));
  152. } else {
  153. cmd.tag(243).button( BTN_POS(1, y), BTN_SIZE(3,h), GET_TEXTF(PRINT_FILE));
  154. }
  155. }
  156. void FilesScreen::onRedraw(draw_mode_t what) {
  157. if (what & FOREGROUND) {
  158. drawHeader();
  159. drawSelectedFile();
  160. drawFooter();
  161. }
  162. }
  163. void FilesScreen::gotoPage(uint8_t page) {
  164. screen_data.FilesScreen.selected_tag = 0xFF;
  165. screen_data.FilesScreen.cur_page = page;
  166. CommandProcessor cmd;
  167. cmd.cmd(CMD_DLSTART)
  168. .cmd(CLEAR_COLOR_RGB(bg_color))
  169. .cmd(CLEAR(true,true,true))
  170. .colors(normal_btn);
  171. drawFileList();
  172. storeBackground();
  173. }
  174. bool FilesScreen::onTouchEnd(uint8_t tag) {
  175. switch (tag) {
  176. case 240: GOTO_PREVIOUS(); return true;
  177. case 241:
  178. if (screen_data.FilesScreen.cur_page > 0) {
  179. gotoPage(screen_data.FilesScreen.cur_page-1);
  180. }
  181. break;
  182. case 242:
  183. if (screen_data.FilesScreen.cur_page < (screen_data.FilesScreen.num_page-1)) {
  184. gotoPage(screen_data.FilesScreen.cur_page+1);
  185. }
  186. break;
  187. case 243:
  188. printFile(getSelectedShortFilename());
  189. StatusScreen::setStatusMessage(GET_TEXTF(PRINT_STARTING));
  190. GOTO_SCREEN(StatusScreen);
  191. return true;
  192. case 244:
  193. {
  194. FileList files;
  195. files.changeDir(getSelectedShortFilename());
  196. gotoPage(0);
  197. }
  198. break;
  199. case 245:
  200. {
  201. FileList files;
  202. files.upDir();
  203. gotoPage(0);
  204. }
  205. break;
  206. default:
  207. if (tag < 240) {
  208. screen_data.FilesScreen.selected_tag = tag;
  209. #if ENABLED(SCROLL_LONG_FILENAMES) && (FTDI_API_LEVEL >= 810)
  210. if (FTDI::ftdi_chip >= 810) {
  211. const char *longFilename = getSelectedLongFilename();
  212. if (longFilename[0]) {
  213. CLCD::FontMetrics fm(font_medium);
  214. uint16_t text_width = fm.get_text_width(longFilename);
  215. screen_data.FilesScreen.scroll_pos = 0;
  216. if (text_width > display_width)
  217. screen_data.FilesScreen.scroll_max = text_width - display_width + MARGIN_L + MARGIN_R;
  218. else
  219. screen_data.FilesScreen.scroll_max = 0;
  220. }
  221. }
  222. #endif
  223. }
  224. break;
  225. }
  226. return true;
  227. }
  228. void FilesScreen::onIdle() {
  229. #if ENABLED(SCROLL_LONG_FILENAMES) && (FTDI_API_LEVEL >= 810)
  230. if (FTDI::ftdi_chip >= 810) {
  231. CLCD::mem_write_32(CLCD::REG::MACRO_0,
  232. VERTEX_TRANSLATE_X(-int32_t(screen_data.FilesScreen.scroll_pos)));
  233. if (screen_data.FilesScreen.scroll_pos < screen_data.FilesScreen.scroll_max * 16)
  234. screen_data.FilesScreen.scroll_pos++;
  235. }
  236. #endif
  237. }
  238. #endif // LULZBOT_TOUCH_UI