From 4aed95a3cdf6eb39e158cb2333d09b354afe3614 Mon Sep 17 00:00:00 2001 From: ailurux Date: Thu, 2 May 2024 17:02:58 +1000 Subject: WIP: Lua filesystem starting point --- src/lua/file_iterator.cpp | 76 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 src/lua/file_iterator.cpp (limited to 'src/lua/file_iterator.cpp') diff --git a/src/lua/file_iterator.cpp b/src/lua/file_iterator.cpp new file mode 100644 index 00000000..7f2929ba --- /dev/null +++ b/src/lua/file_iterator.cpp @@ -0,0 +1,76 @@ +/* + * Copyright 2023 ailurux + * + * SPDX-License-Identifier: GPL-3.0-only + */ +#include "file_iterator.hpp" +#include "esp_log.h" + +#include + +#include "ff.h" +#include "spi.hpp" + +namespace database { + +[[maybe_unused]] static const char* kTag = "FileIterator"; + +FileIterator::FileIterator(std::string filepath) +: original_path_(filepath), + current_() + { + auto lock = drivers::acquire_spi(); + + const TCHAR* next_path = static_cast(filepath.c_str()); + FRESULT res = f_opendir(&dir_, next_path); + if (res != FR_OK) { + ESP_LOGE(kTag, "Error opening directory: %s", filepath.c_str()); + } +} + +auto FileIterator::value() const -> const std::optional& { + return current_; +} + +auto FileIterator::next() -> void { + iterate(false); +} + +auto FileIterator::prev() -> void { + iterate(true); +} + +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); + return false; + } + } + if (info.fname[0] == 0) { + // End of directory + current_.reset(); + ESP_LOGI(kTag, "End of dir"); + + } else { + // Update current value + ESP_LOGI(kTag, "File: %s", info.fname); + current_ = FileEntry{ + .isHidden = (info.fattrib & AM_HID) > 0, + .isDirectory = (info.fattrib & AM_DIR) > 0, + .isTrack = false, // TODO + .filepath = original_path_ + info.fname, + + }; + } + return true; +} + +} // namespace database \ No newline at end of file -- cgit v1.2.3