summaryrefslogtreecommitdiff
path: root/src/tangara/audio/fatfs_source.cpp
diff options
context:
space:
mode:
authorcooljqln <cooljqln@noreply.codeberg.org>2024-05-03 04:48:17 +0000
committercooljqln <cooljqln@noreply.codeberg.org>2024-05-03 04:48:17 +0000
commit3ceb8025ee4330c177101ed30ec17dfb0002f41e (patch)
tree58350210f15df7d00d967cac6f30eeceeb031a3c /src/tangara/audio/fatfs_source.cpp
parent964da15a0b84f8e5f00e8abac2f7dfda0bf60488 (diff)
parent9fafd797a5504f458b5fcae4a1d28a68da936315 (diff)
downloadtangara-fw-3ceb8025ee4330c177101ed30ec17dfb0002f41e.tar.gz
Merge pull request 'Break dependency cycles with our components by merging co-dependent components together' (#68) from jqln/component-merge into main
Reviewed-on: https://codeberg.org/cool-tech-zone/tangara-fw/pulls/68
Diffstat (limited to 'src/tangara/audio/fatfs_source.cpp')
-rw-r--r--src/tangara/audio/fatfs_source.cpp77
1 files changed, 77 insertions, 0 deletions
diff --git a/src/tangara/audio/fatfs_source.cpp b/src/tangara/audio/fatfs_source.cpp
new file mode 100644
index 00000000..fb6a684d
--- /dev/null
+++ b/src/tangara/audio/fatfs_source.cpp
@@ -0,0 +1,77 @@
+/*
+ * Copyright 2023 jacqueline <me@jacqueline.id.au>
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+#include "audio/fatfs_source.hpp"
+#include <sys/_stdint.h>
+
+#include <cstddef>
+#include <cstdint>
+#include <memory>
+
+#include "esp_log.h"
+#include "events/event_queue.hpp"
+#include "ff.h"
+
+#include "audio/audio_source.hpp"
+#include "codec.hpp"
+#include "drivers/spi.hpp"
+#include "system_fsm/system_events.hpp"
+#include "types.hpp"
+
+namespace audio {
+
+[[maybe_unused]] static constexpr char kTag[] = "fatfs_src";
+
+FatfsSource::FatfsSource(codecs::StreamType t, std::unique_ptr<FIL> file)
+ : IStream(t), file_(std::move(file)) {}
+
+FatfsSource::~FatfsSource() {
+ auto lock = drivers::acquire_spi();
+ f_close(file_.get());
+}
+
+auto FatfsSource::Read(std::span<std::byte> dest) -> ssize_t {
+ auto lock = drivers::acquire_spi();
+ if (f_eof(file_.get())) {
+ return 0;
+ }
+ UINT bytes_read = 0;
+ FRESULT res = f_read(file_.get(), dest.data(), dest.size(), &bytes_read);
+ if (res != FR_OK) {
+ events::System().Dispatch(system_fsm::StorageError{.error = res});
+ return -1;
+ }
+ return bytes_read;
+}
+
+auto FatfsSource::CanSeek() -> bool {
+ return true;
+}
+
+auto FatfsSource::SeekTo(int64_t destination, SeekFrom from) -> void {
+ auto lock = drivers::acquire_spi();
+ switch (from) {
+ case SeekFrom::kStartOfStream:
+ f_lseek(file_.get(), destination);
+ break;
+ case SeekFrom::kEndOfStream:
+ f_lseek(file_.get(), f_size(file_.get()) + destination);
+ break;
+ case SeekFrom::kCurrentPosition:
+ f_lseek(file_.get(), f_tell(file_.get()) + destination);
+ break;
+ }
+}
+
+auto FatfsSource::CurrentPosition() -> int64_t {
+ return f_tell(file_.get());
+}
+
+auto FatfsSource::Size() -> std::optional<int64_t> {
+ return f_size(file_.get());
+}
+
+} // namespace audio