Break FatfsStreamFactory's dep on ServiceLocator

custom
jacqueline 10 months ago
parent 41e0605f17
commit 370d1853b5
  1. 3
      src/tangara/audio/audio_fsm.cpp
  2. 12
      src/tangara/audio/fatfs_stream_factory.cpp
  3. 11
      src/tangara/audio/fatfs_stream_factory.hpp
  4. 6
      src/tangara/database/database.cpp
  5. 10
      src/tangara/database/database.hpp
  6. 2
      src/tangara/system_fsm/service_locator.hpp

@ -363,7 +363,8 @@ void Uninitialised::react(const system_fsm::BootComplete& ev) {
sDrainBuffers = std::make_unique<drivers::OutputBuffers>( sDrainBuffers = std::make_unique<drivers::OutputBuffers>(
kTrackDrainLatencySamples, kSystemDrainLatencySamples); kTrackDrainLatencySamples, kSystemDrainLatencySamples);
sStreamFactory.reset(new FatfsStreamFactory(*sServices)); sStreamFactory.reset(
new FatfsStreamFactory(sServices->database(), sServices->tag_parser()));
sI2SOutput.reset(new I2SAudioOutput(sServices->gpios(), *sDrainBuffers)); sI2SOutput.reset(new I2SAudioOutput(sServices->gpios(), *sDrainBuffers));
sBtOutput.reset(new BluetoothAudioOutput( sBtOutput.reset(new BluetoothAudioOutput(
sServices->bluetooth(), *sDrainBuffers, sServices->bg_worker())); sServices->bluetooth(), *sDrainBuffers, sServices->bg_worker()));

@ -10,7 +10,6 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include "database/database.hpp"
#include "esp_log.h" #include "esp_log.h"
#include "ff.h" #include "ff.h"
#include "freertos/portmacro.h" #include "freertos/portmacro.h"
@ -19,10 +18,10 @@
#include "audio/audio_source.hpp" #include "audio/audio_source.hpp"
#include "audio/fatfs_source.hpp" #include "audio/fatfs_source.hpp"
#include "codec.hpp" #include "codec.hpp"
#include "database/database.hpp"
#include "database/tag_parser.hpp" #include "database/tag_parser.hpp"
#include "database/track.hpp" #include "database/track.hpp"
#include "drivers/spi.hpp" #include "drivers/spi.hpp"
#include "system_fsm/service_locator.hpp"
#include "tasks.hpp" #include "tasks.hpp"
#include "types.hpp" #include "types.hpp"
@ -30,12 +29,13 @@
namespace audio { namespace audio {
FatfsStreamFactory::FatfsStreamFactory(system_fsm::ServiceLocator& services) FatfsStreamFactory::FatfsStreamFactory(database::Handle&& handle,
: services_(services) {} database::ITagParser& parser)
: db_(handle), tag_parser_(parser) {}
auto FatfsStreamFactory::create(database::TrackId id, uint32_t offset) auto FatfsStreamFactory::create(database::TrackId id, uint32_t offset)
-> std::shared_ptr<TaggedStream> { -> std::shared_ptr<TaggedStream> {
auto db = services_.database().lock(); auto db = db_.lock();
if (!db) { if (!db) {
return {}; return {};
} }
@ -48,7 +48,7 @@ auto FatfsStreamFactory::create(database::TrackId id, uint32_t offset)
auto FatfsStreamFactory::create(std::string path, uint32_t offset) auto FatfsStreamFactory::create(std::string path, uint32_t offset)
-> std::shared_ptr<TaggedStream> { -> std::shared_ptr<TaggedStream> {
auto tags = services_.tag_parser().ReadAndParseTags(path); auto tags = tag_parser_.ReadAndParseTags(path);
if (!tags) { if (!tags) {
ESP_LOGE(kTag, "failed to read tags"); ESP_LOGE(kTag, "failed to read tags");
return {}; return {};

@ -6,23 +6,21 @@
#pragma once #pragma once
#include <stdint.h>
#include <cstddef> #include <cstddef>
#include <cstdint> #include <cstdint>
#include <future> #include <future>
#include <memory> #include <memory>
#include <string> #include <string>
#include "database/database.hpp"
#include "database/track.hpp"
#include "ff.h" #include "ff.h"
#include "freertos/portmacro.h" #include "freertos/portmacro.h"
#include "audio/audio_source.hpp" #include "audio/audio_source.hpp"
#include "codec.hpp" #include "codec.hpp"
#include "database/database.hpp"
#include "database/future_fetcher.hpp" #include "database/future_fetcher.hpp"
#include "database/tag_parser.hpp" #include "database/tag_parser.hpp"
#include "system_fsm/service_locator.hpp" #include "database/track.hpp"
#include "tasks.hpp" #include "tasks.hpp"
#include "types.hpp" #include "types.hpp"
@ -33,7 +31,7 @@ namespace audio {
*/ */
class FatfsStreamFactory { class FatfsStreamFactory {
public: public:
explicit FatfsStreamFactory(system_fsm::ServiceLocator&); explicit FatfsStreamFactory(database::Handle&&, database::ITagParser&);
auto create(database::TrackId, uint32_t offset = 0) auto create(database::TrackId, uint32_t offset = 0)
-> std::shared_ptr<TaggedStream>; -> std::shared_ptr<TaggedStream>;
@ -47,7 +45,8 @@ class FatfsStreamFactory {
auto ContainerToStreamType(database::Container) auto ContainerToStreamType(database::Container)
-> std::optional<codecs::StreamType>; -> std::optional<codecs::StreamType>;
system_fsm::ServiceLocator& services_; database::Handle db_;
database::ITagParser& tag_parser_;
}; };
} // namespace audio } // namespace audio

@ -684,6 +684,12 @@ auto Database::countRecords(const SearchKey& c) -> size_t {
return count; return count;
} }
Handle::Handle(std::shared_ptr<Database>& db) : db_(db) {}
auto Handle::lock() -> std::shared_ptr<Database> {
return db_;
}
auto SearchKey::startKey() const -> std::string_view { auto SearchKey::startKey() const -> std::string_view {
if (key) { if (key) {
return *key; return *key;

@ -128,6 +128,16 @@ class Database {
auto countRecords(const SearchKey& c) -> size_t; auto countRecords(const SearchKey& c) -> size_t;
}; };
class Handle {
public:
Handle(std::shared_ptr<Database>& db);
auto lock() -> std::shared_ptr<Database>;
private:
std::shared_ptr<Database>& db_;
};
/* /*
* Container for the data needed to iterate through database records. This is a * Container for the data needed to iterate through database records. This is a
* lower-level type that the higher-level iterators are built from; most users * lower-level type that the higher-level iterators are built from; most users

@ -92,7 +92,7 @@ class ServiceLocator {
auto haptics(std::unique_ptr<drivers::Haptics> i) { haptics_ = std::move(i); } auto haptics(std::unique_ptr<drivers::Haptics> i) { haptics_ = std::move(i); }
auto database() -> std::weak_ptr<database::Database> { return database_; } auto database() -> database::Handle { return database_; }
auto database(std::unique_ptr<database::Database> i) { auto database(std::unique_ptr<database::Database> i) {
database_ = std::move(i); database_ = std::move(i);

Loading…
Cancel
Save