Break FatfsStreamFactory's dep on ServiceLocator

custom
jacqueline 10 months ago
parent 7d33f99216
commit a9d2335e1d
  1. 13
      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

@ -237,11 +237,11 @@ void AudioState::react(const system_fsm::BluetoothEvent& ev) {
break;
}
}
if (std::holds_alternative<drivers::bluetooth::RemoteVolumeChanged>(ev.event)) {
auto volume_chg = std::get<drivers::bluetooth::RemoteVolumeChanged>(ev.event).new_vol;
events::Ui().Dispatch(RemoteVolumeChanged{
.value = volume_chg
});
if (std::holds_alternative<drivers::bluetooth::RemoteVolumeChanged>(
ev.event)) {
auto volume_chg =
std::get<drivers::bluetooth::RemoteVolumeChanged>(ev.event).new_vol;
events::Ui().Dispatch(RemoteVolumeChanged{.value = volume_chg});
}
}
@ -356,7 +356,8 @@ void Uninitialised::react(const system_fsm::BootComplete& ev) {
sDrainBuffer = std::make_unique<drivers::PcmBuffer>(kDrainLatencySamples);
sStreamFactory.reset(new FatfsStreamFactory(*sServices));
sStreamFactory.reset(
new FatfsStreamFactory(sServices->database(), sServices->tag_parser()));
sI2SOutput.reset(new I2SAudioOutput(sServices->gpios(), *sDrainBuffer));
sBtOutput.reset(new BluetoothAudioOutput(
sServices->bluetooth(), *sDrainBuffer, sServices->bg_worker()));

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

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

@ -684,6 +684,12 @@ auto Database::countRecords(const SearchKey& c) -> size_t {
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 {
if (key) {
return *key;

@ -128,6 +128,16 @@ class Database {
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
* 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 database() -> std::weak_ptr<database::Database> { return database_; }
auto database() -> database::Handle { return database_; }
auto database(std::unique_ptr<database::Database> i) {
database_ = std::move(i);

Loading…
Cancel
Save