|
| 1 | +/** |
| 2 | + * Marlin 3D Printer Firmware |
| 3 | + * Copyright (c) 2022 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 | +/* **************************************** |
| 24 | + * lcd/extui/ia_creality/FileNavigator.cpp |
| 25 | + * **************************************** |
| 26 | + * Extensible_UI implementation for Creality DWIN |
| 27 | + * 10SPro, Max, CR10V2 |
| 28 | + * Based on implementations for Anycubic Chiron and Nextion by Nick Wells and Skorpi08 |
| 29 | + * Written by Insanity Automation |
| 30 | + * ***************************************/ |
| 31 | + |
| 32 | +#include "../../../inc/MarlinConfigPre.h" |
| 33 | + |
| 34 | +#if ENABLED(DGUS_LCD_UI_IA_CREALITY) |
| 35 | + |
| 36 | +#include "FileNavigator.h" |
| 37 | + |
| 38 | +using namespace ExtUI; |
| 39 | + |
| 40 | +#define DEBUG_OUT ENABLED(DEBUG_DWIN) |
| 41 | +#include "../../../core/debug_out.h" |
| 42 | + |
| 43 | +FileList FileNavigator::filelist; // Instance of the Marlin file API |
| 44 | +char FileNavigator::currentfoldername[MAX_PATH_LEN]; // Current folder path |
| 45 | +uint16_t FileNavigator::lastindex; |
| 46 | +uint8_t FileNavigator::folderdepth; |
| 47 | +uint16_t FileNavigator::currentindex; // override the panel request |
| 48 | + |
| 49 | +FileNavigator filenavigator; |
| 50 | + |
| 51 | +FileNavigator::FileNavigator() { reset(); } |
| 52 | + |
| 53 | +void FileNavigator::reset() { |
| 54 | + currentfoldername[0] = '\0'; |
| 55 | + folderdepth = 0; |
| 56 | + currentindex = 0; |
| 57 | + lastindex = 0; |
| 58 | + // Start at root folder |
| 59 | + while (!filelist.isAtRootDir()) filelist.upDir(); |
| 60 | + refresh(); |
| 61 | +} |
| 62 | + |
| 63 | +void FileNavigator::refresh() { filelist.refresh(); } |
| 64 | + |
| 65 | +bool FileNavigator::getIndexisDir(uint16_t index){ |
| 66 | + filelist.seek(index); |
| 67 | + return filelist.isDir(); |
| 68 | +} |
| 69 | + |
| 70 | +const char *FileNavigator::getIndexName(uint16_t index){ |
| 71 | + filelist.seek(index); |
| 72 | + return filelist.shortFilename(); |
| 73 | +} |
| 74 | + |
| 75 | +uint16_t FileNavigator::maxFiles() { |
| 76 | + return filelist.count(); |
| 77 | +} |
| 78 | + |
| 79 | +void FileNavigator::getFiles(uint16_t index) { |
| 80 | + uint16_t files = DISPLAY_FILES, fcnt = 0; |
| 81 | + if (index == 0) |
| 82 | + currentindex = 0; |
| 83 | + else { |
| 84 | + // Each time we change folder we reset the file index to 0 and keep track |
| 85 | + // of the current position as the TFT panel isn't aware of folder trees. |
| 86 | + --currentindex; // go back a file to take account of the .. added to the root. |
| 87 | + if (index > lastindex) |
| 88 | + currentindex += files + 1; |
| 89 | + else if (currentindex >= files) |
| 90 | + currentindex -= files - 1; |
| 91 | + else |
| 92 | + currentindex = 0; |
| 93 | + } |
| 94 | + lastindex = index; |
| 95 | + |
| 96 | + |
| 97 | + // Clear currently drawn screen |
| 98 | + for (int i = 0; i < DISPLAY_FILES; i++) { |
| 99 | + for (int j = 0; j < 20; j++) |
| 100 | + rtscheck.RTS_SndData(0, SDFILE_ADDR + (i * 20) + j); |
| 101 | + } |
| 102 | + |
| 103 | + for (int j = 0; j < 10; j++) { |
| 104 | + rtscheck.RTS_SndData(0, Printfilename + j); // clear screen. |
| 105 | + rtscheck.RTS_SndData(0, Choosefilename + j); // clear filename |
| 106 | + } |
| 107 | + for (int j = 0; j < 8; j++) |
| 108 | + rtscheck.RTS_SndData(0, FilenameCount + j); |
| 109 | + for (int j = 1; j <= DISPLAY_FILES; j++) { |
| 110 | + rtscheck.RTS_SndData(10, FilenameIcon + j); |
| 111 | + rtscheck.RTS_SndData(10, FilenameIcon1 + j); |
| 112 | + } |
| 113 | + |
| 114 | + DEBUG_ECHOLNPGM("index=", index, " currentindex=", currentindex, "folderdepth=", folderdepth); |
| 115 | + |
| 116 | + if (currentindex == 0 && folderdepth > 0) { // Add a link to go up a folder |
| 117 | + files--; |
| 118 | + rtscheck.RTS_SndData("Up Directory", SDFILE_ADDR); |
| 119 | + fcnt++; |
| 120 | + } |
| 121 | + else if (currentindex == DISPLAY_FILES && folderdepth > 0) |
| 122 | + currentindex--; |
| 123 | + |
| 124 | + for (uint16_t seek = currentindex; seek < currentindex + files; seek++) { |
| 125 | + if (filelist.seek(seek)) { |
| 126 | + const int filelen = strlen(filelist.filename()); |
| 127 | + if (filelen > 20) { |
| 128 | + char *buf = (char *)filelist.filename(); |
| 129 | + //char buf[filelen]; |
| 130 | + //strcpy(&buf[filelen], filelist.filename()); |
| 131 | + buf[18] = '\0'; // cutoff at screen edge |
| 132 | + rtscheck.RTS_SndData(buf, (SDFILE_ADDR + (fcnt * 20))); |
| 133 | + } |
| 134 | + else |
| 135 | + rtscheck.RTS_SndData(filelist.filename(), (SDFILE_ADDR + (fcnt * 20))); |
| 136 | + |
| 137 | + if (filelist.isDir()) { |
| 138 | + rtscheck.RTS_SndData((uint8_t)4, FilenameIcon + (fcnt+1)); |
| 139 | + rtscheck.RTS_SndData((unsigned long)0x041F, (FilenameNature + ((1+fcnt) * 16))); // Change BG of selected line to Blue |
| 140 | + } |
| 141 | + else { |
| 142 | + rtscheck.RTS_SndData((uint8_t)0, FilenameIcon + (fcnt+1)); |
| 143 | + rtscheck.RTS_SndData((unsigned long)0xFFFF, (FilenameNature + ((1+fcnt) * 16))); // white |
| 144 | + } |
| 145 | + SERIAL_ECHOLNPGM("-", seek, " '", filelist.filename(), "' '", currentfoldername, "", filelist.shortFilename(), "'\n"); |
| 146 | + fcnt++; |
| 147 | + } |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +void FileNavigator::changeDIR(char *folder) { |
| 152 | + DEBUG_ECHOLNPGM("currentfolder: ", currentfoldername, " New: ", folder); |
| 153 | + if (folderdepth >= MAX_FOLDER_DEPTH) return; // limit the folder depth |
| 154 | + strcat(currentfoldername, folder); |
| 155 | + strcat(currentfoldername, "/"); |
| 156 | + filelist.changeDir(folder); |
| 157 | + refresh(); |
| 158 | + folderdepth++; |
| 159 | + currentindex = 0; |
| 160 | +} |
| 161 | + |
| 162 | +void FileNavigator::upDIR() { |
| 163 | + filelist.upDir(); |
| 164 | + refresh(); |
| 165 | + folderdepth--; |
| 166 | + currentindex = 0; |
| 167 | + // Remove the last child folder from the stored path |
| 168 | + if (folderdepth == 0) { |
| 169 | + currentfoldername[0] = '\0'; |
| 170 | + reset(); |
| 171 | + } |
| 172 | + else { |
| 173 | + char *pos = nullptr; |
| 174 | + for (uint8_t f = 0; f < folderdepth; f++) |
| 175 | + pos = strchr(currentfoldername, '/'); |
| 176 | + pos[1] = '\0'; |
| 177 | + } |
| 178 | + DEBUG_ECHOLNPGM("depth: ", folderdepth, " currentfoldername: ", currentfoldername); |
| 179 | +} |
| 180 | + |
| 181 | +char* FileNavigator::getCurrentFolderName() { return currentfoldername; } |
| 182 | + |
| 183 | +#endif // DGUS_LCD_UI_IA_CREALITY |
0 commit comments