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.

FileNavigator.cpp 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /**
  2. * Marlin 3D Printer Firmware
  3. * Copyright (c) 2021 MarlinFirmware [https://github.com/MarlinFirmware/Marlin]
  4. *
  5. * Based on Sprinter and grbl.
  6. * Copyright (c) 2011 Camiel Gubbels / Erik van der Zalm
  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. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /* ****************************************
  23. * lcd/extui/lib/nextion/FileNavigator.cpp
  24. * ****************************************
  25. * Extensible_UI implementation for Nextion
  26. * https://github.com/Skorpi08
  27. * ***************************************/
  28. #include "../../../../inc/MarlinConfigPre.h"
  29. #if ENABLED(NEXTION_TFT)
  30. #include "FileNavigator.h"
  31. #include "nextion_tft.h"
  32. using namespace ExtUI;
  33. #define DEBUG_OUT NEXDEBUGLEVEL
  34. #include "../../../../core/debug_out.h"
  35. FileList FileNavigator::filelist; // Instance of the Marlin file API
  36. char FileNavigator::currentfoldername[MAX_PATH_LEN]; // Current folder path
  37. uint16_t FileNavigator::lastindex;
  38. uint8_t FileNavigator::folderdepth;
  39. uint16_t FileNavigator::currentindex; // override the panel request
  40. FileNavigator filenavigator;
  41. FileNavigator::FileNavigator() { reset(); }
  42. void FileNavigator::reset() {
  43. currentfoldername[0] = '\0';
  44. folderdepth = 0;
  45. currentindex = 0;
  46. lastindex = 0;
  47. // Start at root folder
  48. while (!filelist.isAtRootDir()) filelist.upDir();
  49. refresh();
  50. }
  51. void FileNavigator::refresh() { filelist.refresh(); }
  52. void FileNavigator::getFiles(uint16_t index) {
  53. uint16_t files = 7, fseek = 0, fcnt = 0;
  54. if (index == 0)
  55. currentindex = 0;
  56. else {
  57. // Each time we change folder we reset the file index to 0 and keep track
  58. // of the current position as the TFT panel isn't aware of folder trees.
  59. --currentindex; // go back a file to take account of the .. added to the root.
  60. if (index > lastindex)
  61. currentindex += files + 1;
  62. else if (currentindex >= files)
  63. currentindex -= files - 1;
  64. else
  65. currentindex = 0;
  66. }
  67. lastindex = index;
  68. #if NEXDEBUG(AC_FILE)
  69. DEBUG_ECHOLNPAIR("index=", index, " currentindex=", currentindex);
  70. #endif
  71. if (currentindex == 0 && folderdepth > 0) { // Add a link to go up a folder
  72. nextion.SendtoTFT(PSTR("vis p0,1"));
  73. nextion.SendtoTFT(PSTR("\xFF\xFF\xFF"));
  74. SEND_VAL("tmpUP", "0");
  75. files--;
  76. }
  77. else {
  78. nextion.SendtoTFT(PSTR("vis p0,0"));
  79. nextion.SendtoTFT(PSTR("\xFF\xFF\xFF"));
  80. }
  81. for (uint16_t seek = currentindex; seek < currentindex + files; seek++) {
  82. if (filelist.seek(seek)) {
  83. nextion.SendtoTFT(PSTR("s"));
  84. LCD_SERIAL.print(fcnt);
  85. nextion.SendtoTFT(PSTR(".txt=\""));
  86. if (filelist.isDir()) {
  87. LCD_SERIAL.print(filelist.shortFilename());
  88. nextion.SendtoTFT(PSTR("/\""));
  89. nextion.SendtoTFT(PSTR("\xFF\xFF\xFF"));
  90. nextion.SendtoTFT(PSTR("l"));
  91. LCD_SERIAL.print(fcnt);
  92. nextion.SendtoTFT(PSTR(".txt=\""));
  93. LCD_SERIAL.print(filelist.filename());
  94. nextion.SendtoTFT(PSTR("\""));
  95. nextion.SendtoTFT(PSTR("\xFF\xFF\xFF"));
  96. SEND_PCO2("l", fcnt, "1055");
  97. }
  98. else {
  99. LCD_SERIAL.print(currentfoldername);
  100. LCD_SERIAL.print(filelist.shortFilename());
  101. nextion.SendtoTFT(PSTR("\""));
  102. nextion.SendtoTFT(PSTR("\xFF\xFF\xFF"));
  103. nextion.SendtoTFT(PSTR("l"));
  104. LCD_SERIAL.print(fcnt);
  105. nextion.SendtoTFT(PSTR(".txt=\""));
  106. LCD_SERIAL.print(filelist.longFilename());
  107. nextion.SendtoTFT(PSTR("\""));
  108. nextion.SendtoTFT(PSTR("\xFF\xFF\xFF"));
  109. }
  110. fcnt++;
  111. fseek = seek;
  112. #if NEXDEBUG(AC_FILE)
  113. DEBUG_ECHOLNPAIR("-", seek, " '", filelist.longFilename(), "' '", currentfoldername, "", filelist.shortFilename(), "'\n");
  114. #endif
  115. }
  116. }
  117. SEND_VAL("n0", filelist.count());
  118. SEND_VAL("n1", fseek + 1);
  119. }
  120. void FileNavigator::changeDIR(char *folder) {
  121. #if NEXDEBUG(AC_FILE)
  122. DEBUG_ECHOLNPAIR("currentfolder: ", currentfoldername, " New: ", folder);
  123. #endif
  124. if (folderdepth >= MAX_FOLDER_DEPTH) return; // limit the folder depth
  125. strcat(currentfoldername, folder);
  126. strcat(currentfoldername, "/");
  127. filelist.changeDir(folder);
  128. refresh();
  129. folderdepth++;
  130. currentindex = 0;
  131. }
  132. void FileNavigator::upDIR() {
  133. filelist.upDir();
  134. refresh();
  135. folderdepth--;
  136. currentindex = 0;
  137. // Remove the last child folder from the stored path
  138. if (folderdepth == 0) {
  139. currentfoldername[0] = '\0';
  140. reset();
  141. }
  142. else {
  143. char *pos = nullptr;
  144. for (uint8_t f = 0; f < folderdepth; f++)
  145. pos = strchr(currentfoldername, '/');
  146. pos[1] = '\0';
  147. }
  148. #if NEXDEBUG(AC_FILE)
  149. DEBUG_ECHOLNPAIR("depth: ", folderdepth, " currentfoldername: ", currentfoldername);
  150. #endif
  151. }
  152. char* FileNavigator::getCurrentFolderName() { return currentfoldername; }
  153. #endif // NEXTION_TFT