diff options
| author | ailurux <ailuruxx@gmail.com> | 2024-05-06 12:36:16 +1000 |
|---|---|---|
| committer | ailurux <ailuruxx@gmail.com> | 2024-05-06 12:36:16 +1000 |
| commit | eeb3f2d406a951b423b83f559fe749df0b4f745a (patch) | |
| tree | 8c4174f5fefcfa7aceb5afea70f0064894126aa7 /src | |
| parent | fb3d6a7b86991fe38da9a2741db8801785aa4c1b (diff) | |
| download | tangara-fw-eeb3f2d406a951b423b83f559fe749df0b4f745a.tar.gz | |
WIP: File browser, needs bug fixes
Diffstat (limited to 'src')
| -rw-r--r-- | src/lua/file_iterator.cpp | 31 | ||||
| -rw-r--r-- | src/lua/include/file_iterator.hpp | 1 | ||||
| -rw-r--r-- | src/lua/lua_filesystem.cpp | 36 |
3 files changed, 49 insertions, 19 deletions
diff --git a/src/lua/file_iterator.cpp b/src/lua/file_iterator.cpp index 8de1a923..58b256b2 100644 --- a/src/lua/file_iterator.cpp +++ b/src/lua/file_iterator.cpp @@ -17,12 +17,13 @@ namespace database { FileIterator::FileIterator(std::string filepath) : original_path_(filepath), - current_() + current_(), + offset_(-1) { auto lock = drivers::acquire_spi(); - const TCHAR* next_path = static_cast<const TCHAR*>(filepath.c_str()); - FRESULT res = f_opendir(&dir_, next_path); + const TCHAR* path = static_cast<const TCHAR*>(filepath.c_str()); + FRESULT res = f_opendir(&dir_, path); if (res != FR_OK) { ESP_LOGE(kTag, "Error opening directory: %s", filepath.c_str()); } @@ -42,36 +43,40 @@ auto FileIterator::next() -> void { } auto FileIterator::prev() -> void { - iterate(true); + if (offset_ == 0) { + current_.reset(); + return; + } + f_rewinddir(&dir_); + auto new_offset = offset_-1; + offset_ = -1; + for (int i = 0; i < new_offset; i++) { + iterate(false); + } } auto FileIterator::iterate(bool reverse) -> bool { FILINFO info; - if (reverse) { - f_rewinddir(&dir_); - } { auto lock = drivers::acquire_spi(); auto res = f_readdir(&dir_, &info); if (res != FR_OK) { - ESP_LOGI(kTag, "AAAAAAAAAAAAAAAAAAA"); - ESP_LOGI(kTag, "%d", res); + ESP_LOGE(kTag, "Error reading directory. Error: %d", res); return false; } } if (info.fname[0] == 0) { // End of directory + // Set value to nil current_.reset(); - ESP_LOGI(kTag, "End of dir"); - } else { // Update current value - ESP_LOGI(kTag, "File: %s", info.fname); + offset_++; current_ = FileEntry{ .isHidden = (info.fattrib & AM_HID) > 0, .isDirectory = (info.fattrib & AM_DIR) > 0, .isTrack = false, // TODO - .filepath = original_path_ + info.fname, + .filepath = original_path_ + (original_path_.size()>0?"/":"") + info.fname, }; } diff --git a/src/lua/include/file_iterator.hpp b/src/lua/include/file_iterator.hpp index 1632949e..da1a6eeb 100644 --- a/src/lua/include/file_iterator.hpp +++ b/src/lua/include/file_iterator.hpp @@ -36,6 +36,7 @@ class FileIterator { std::string original_path_; std::optional<FileEntry> current_; + int offset_; auto iterate(bool reverse = false) -> bool; }; diff --git a/src/lua/lua_filesystem.cpp b/src/lua/lua_filesystem.cpp index 5c690c16..f0dbaf9a 100644 --- a/src/lua/lua_filesystem.cpp +++ b/src/lua/lua_filesystem.cpp @@ -88,11 +88,11 @@ static auto fs_iterate(lua_State* state) -> int { return 1; } -// static auto db_iterator_clone(lua_State* state) -> int { -// database::Iterator* it = db_check_iterator(state, 1); -// push_iterator(state, *it); -// return 1; -// } +static auto fs_iterator_clone(lua_State* state) -> int { + database::FileIterator* it = check_file_iterator(state, 1); + push_iterator(state, *it); + return 1; +} static auto fs_iterator_gc(lua_State* state) -> int { database::FileIterator* it = check_file_iterator(state, 1); @@ -102,7 +102,7 @@ static auto fs_iterator_gc(lua_State* state) -> int { static const struct luaL_Reg kFileIteratorFuncs[] = {{"next", fs_iterate}, {"prev", fs_iterate_prev}, - // {"clone", db_iterator_clone}, + {"clone", fs_iterator_clone}, {"__call", fs_iterate}, {"__gc", fs_iterator_gc}, {NULL, NULL}}; @@ -114,7 +114,31 @@ static auto file_entry_path(lua_State* state) -> int { return 1; } +static auto file_entry_is_dir(lua_State* state) -> int { + LuaFileEntry* data = reinterpret_cast<LuaFileEntry*>( + luaL_checkudata(state, 1, kFileEntryMetatable)); + lua_pushboolean(state, data->isDirectory); + return 1; +} + +static auto file_entry_is_hidden(lua_State* state) -> int { + LuaFileEntry* data = reinterpret_cast<LuaFileEntry*>( + luaL_checkudata(state, 1, kFileEntryMetatable)); + lua_pushboolean(state, data->isHidden); + return 1; +} + +static auto file_entry_is_track(lua_State* state) -> int { + LuaFileEntry* data = reinterpret_cast<LuaFileEntry*>( + luaL_checkudata(state, 1, kFileEntryMetatable)); + lua_pushboolean(state, data->isTrack); + return 1; +} + static const struct luaL_Reg kFileEntryFuncs[] = {{"filepath", file_entry_path}, + {"is_directory", file_entry_is_dir}, + {"is_hidden", file_entry_is_hidden}, + {"is_track", file_entry_is_track}, {"__tostring", file_entry_path}, {NULL, NULL}}; |
