|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Marcel Licence |
| 3 | + * |
| 4 | + * This program is free software: you can redistribute it and/or modify |
| 5 | + * it under the terms of the GNU General Public License as published by |
| 6 | + * the Free Software Foundation, either version 3 of the License, or |
| 7 | + * (at your option) any later version. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | + * GNU General Public License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the GNU General Public License |
| 15 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + * |
| 17 | + * Dieses Programm ist Freie Software: Sie können es unter den Bedingungen |
| 18 | + * der GNU General Public License, wie von der Free Software Foundation, |
| 19 | + * Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren |
| 20 | + * veröffentlichten Version, weiter verteilen und/oder modifizieren. |
| 21 | + * |
| 22 | + * Dieses Programm wird in der Hoffnung bereitgestellt, dass es nützlich sein wird, jedoch |
| 23 | + * OHNE JEDE GEWÄHR,; sogar ohne die implizite |
| 24 | + * Gewähr der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK. |
| 25 | + * Siehe die GNU General Public License für weitere Einzelheiten. |
| 26 | + * |
| 27 | + * Sie sollten eine Kopie der GNU General Public License zusammen mit diesem |
| 28 | + * Programm erhalten haben. Wenn nicht, siehe <https://www.gnu.org/licenses/>. |
| 29 | + */ |
| 30 | + |
| 31 | +/** |
| 32 | + * @file fs_teensy.cpp |
| 33 | + * @author Marcel Licence |
| 34 | + * @data 15.11.2024 |
| 35 | + * |
| 36 | + * @brief This file contains Teensy specific file system implementation |
| 37 | + */ |
| 38 | + |
| 39 | + |
| 40 | +#ifdef __CDT_PARSER__ |
| 41 | +#include <cdt.h> |
| 42 | +#endif |
| 43 | + |
| 44 | + |
| 45 | +#ifdef TEENSYDUINO |
| 46 | + |
| 47 | +#ifdef ML_SYNTH_INLINE_DEFINITION |
| 48 | + |
| 49 | + |
| 50 | +/* |
| 51 | + * include files |
| 52 | + */ |
| 53 | +#include <fs/fs_access.h> |
| 54 | +#include <ml_status.h> |
| 55 | +#include <string_utils.h> |
| 56 | + |
| 57 | +#include <SD.h> |
| 58 | + |
| 59 | +/* |
| 60 | + * definitions |
| 61 | + */ |
| 62 | +#define FST fs::FS |
| 63 | + |
| 64 | +/* |
| 65 | + * static function declarations |
| 66 | + */ |
| 67 | +static void printDirectory(File dir, int numTabs); |
| 68 | + |
| 69 | +/* |
| 70 | + * local variables |
| 71 | + */ |
| 72 | +static File f; |
| 73 | +static File *g_file = NULL; |
| 74 | +static File *t_file = NULL; |
| 75 | +const int SD_CS = BUILTIN_SDCARD; |
| 76 | + |
| 77 | +/* |
| 78 | + * static function definitions |
| 79 | + */ |
| 80 | +static void printDirectory(File dir, int numTabs) |
| 81 | +{ |
| 82 | + while (true) |
| 83 | + { |
| 84 | + |
| 85 | + File entry = dir.openNextFile(); |
| 86 | + if (! entry) |
| 87 | + { |
| 88 | + break; |
| 89 | + } |
| 90 | + for (uint8_t i = 0; i < numTabs; i++) |
| 91 | + { |
| 92 | + Serial.print('\t'); |
| 93 | + } |
| 94 | + Serial.print(entry.name()); |
| 95 | + if (entry.isDirectory()) |
| 96 | + { |
| 97 | + Serial.println("/"); |
| 98 | + printDirectory(entry, numTabs + 1); |
| 99 | + } |
| 100 | + else |
| 101 | + { |
| 102 | + Serial.print("\t\t"); |
| 103 | + Serial.println(entry.size(), DEC); |
| 104 | + } |
| 105 | + entry.close(); |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +uint32_t getFileFromIdx(File dir, int numTabs, uint32_t idx, char *filename, char *filter) |
| 110 | +{ |
| 111 | + while (true) |
| 112 | + { |
| 113 | + |
| 114 | + File entry = dir.openNextFile(); |
| 115 | + if (! entry) |
| 116 | + { |
| 117 | + break; |
| 118 | + } |
| 119 | + |
| 120 | + if (entry.isDirectory()) |
| 121 | + { |
| 122 | + idx = getFileFromIdx(entry, numTabs + 1, idx, filename, filter); |
| 123 | + } |
| 124 | + else |
| 125 | + { |
| 126 | + if (str_ends_with_ul(entry.name(), filter)) |
| 127 | + { |
| 128 | + if (idx == 0) |
| 129 | + { |
| 130 | + strcpy(filename, entry.name()); |
| 131 | + return idx; |
| 132 | + } |
| 133 | + idx -= 1; |
| 134 | + } |
| 135 | + |
| 136 | + } |
| 137 | + entry.close(); |
| 138 | + } |
| 139 | + return idx; |
| 140 | +} |
| 141 | + |
| 142 | +bool getFileFromIdx(uint32_t idx, char *filename, char *filter) |
| 143 | +{ |
| 144 | + File dir = SD.open("/", "r"); |
| 145 | + char filename_clean[64]; |
| 146 | + |
| 147 | + bool result = getFileFromIdx(dir, 0, idx, filename_clean, filter) == 0 ? true : false; |
| 148 | + sprintf(filename, "/%s", filename_clean); |
| 149 | + dir.close(); |
| 150 | + return result; |
| 151 | +} |
| 152 | + |
| 153 | +/* |
| 154 | + * function definitions |
| 155 | + */ |
| 156 | +void FS_Setup(void) |
| 157 | +{ |
| 158 | + if (!SD.begin(SD_CS)) |
| 159 | + { |
| 160 | + Serial.println("SD init failed!"); |
| 161 | + } |
| 162 | + else |
| 163 | + { |
| 164 | + Serial.println("SD init passed."); |
| 165 | + File root; |
| 166 | + root = SD.open("/"); |
| 167 | + while (true) |
| 168 | + { |
| 169 | + File entry = root.openNextFile(); |
| 170 | + if (!entry) |
| 171 | + { |
| 172 | + break; |
| 173 | + } |
| 174 | + else |
| 175 | + { |
| 176 | + Serial.println(entry.name()); |
| 177 | + } |
| 178 | + } |
| 179 | + } |
| 180 | +} |
| 181 | + |
| 182 | +bool FS_OpenFile(int fs, const char *filename, const char *mode) |
| 183 | +{ |
| 184 | + f = SD.open(filename, mode); |
| 185 | + if (f) |
| 186 | + { |
| 187 | + g_file = &f; |
| 188 | + return true; |
| 189 | + } |
| 190 | + else |
| 191 | + { |
| 192 | + Serial.printf("Error opening file: %s\n", filename); |
| 193 | + return false; |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +void FS_LittleFS_CloseFile(void) |
| 198 | +{ |
| 199 | + g_file->close(); |
| 200 | + g_file = NULL; |
| 201 | +} |
| 202 | + |
| 203 | +void FS_CloseFile(void) |
| 204 | +{ |
| 205 | + g_file->close(); |
| 206 | + g_file = NULL; |
| 207 | +} |
| 208 | + |
| 209 | +void FS_UseTempFile(void) |
| 210 | +{ |
| 211 | + g_file = t_file; |
| 212 | +} |
| 213 | + |
| 214 | +uint32_t readBytes(uint8_t *buffer, uint32_t len) |
| 215 | +{ |
| 216 | + return g_file->read(buffer, len); |
| 217 | +} |
| 218 | + |
| 219 | +uint32_t writeBytes(uint8_t *buffer, uint32_t len) |
| 220 | +{ |
| 221 | + return g_file->write(buffer, len); |
| 222 | +} |
| 223 | + |
| 224 | +uint32_t availableBytes(void) |
| 225 | +{ |
| 226 | + return g_file->available(); |
| 227 | +} |
| 228 | + |
| 229 | +void fileSeekTo(uint32_t pos) |
| 230 | +{ |
| 231 | + g_file->seek(0, SeekSet); |
| 232 | + g_file->seek(pos, SeekSet); |
| 233 | +} |
| 234 | + |
| 235 | +uint32_t getStaticPos(void) |
| 236 | +{ |
| 237 | + return g_file->position(); |
| 238 | +} |
| 239 | + |
| 240 | +uint32_t getCurrentOffset(void) |
| 241 | +{ |
| 242 | + return g_file->position(); |
| 243 | +} |
| 244 | + |
| 245 | +uint32_t readBytesFromAddr(uint8_t *buffer, uint32_t addr, uint32_t len) |
| 246 | +{ |
| 247 | + fileSeekTo(addr); |
| 248 | + return readBytes(buffer, len); |
| 249 | +} |
| 250 | + |
| 251 | +#endif /* #ifdef ML_SYNTH_INLINE_DEFINITION */ |
| 252 | + |
| 253 | +#endif /* TEENSYDUINO */ |
| 254 | + |
0 commit comments