summaryrefslogtreecommitdiff
path: root/src/drivers/include/driver_cache.hpp
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2023-04-26 08:49:02 +1000
committerjacqueline <me@jacqueline.id.au>2023-04-26 08:49:02 +1000
commit7972bd4567a99179338259e9e6ce19168c2c0db3 (patch)
treef46642afd36011d3d064e022232e77744b82c6ae /src/drivers/include/driver_cache.hpp
parent4887f3789817f87bf1272af0b52684e3364270c2 (diff)
parent5575378c1c8171cd716b79d3ab89df1e56ceb9d3 (diff)
downloadtangara-fw-7972bd4567a99179338259e9e6ce19168c2c0db3.tar.gz
Merge branch 'main' into leveldb
Diffstat (limited to 'src/drivers/include/driver_cache.hpp')
-rw-r--r--src/drivers/include/driver_cache.hpp54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/drivers/include/driver_cache.hpp b/src/drivers/include/driver_cache.hpp
new file mode 100644
index 00000000..c56ebc3f
--- /dev/null
+++ b/src/drivers/include/driver_cache.hpp
@@ -0,0 +1,54 @@
+#pragma once
+
+#include <memory>
+#include <mutex>
+
+#include "dac.hpp"
+#include "display.hpp"
+#include "gpio_expander.hpp"
+#include "storage.hpp"
+#include "touchwheel.hpp"
+
+namespace drivers {
+
+class DriverCache {
+ private:
+ std::unique_ptr<GpioExpander> gpios_;
+ std::weak_ptr<AudioDac> dac_;
+ std::weak_ptr<Display> display_;
+ std::weak_ptr<SdStorage> storage_;
+ std::weak_ptr<TouchWheel> touchwheel_;
+ // TODO(jacqueline): Haptics, samd
+
+ std::mutex mutex_;
+
+ template <typename T, typename F>
+ auto Acquire(std::weak_ptr<T> ptr, F factory) -> std::shared_ptr<T> {
+ std::shared_ptr<T> acquired = ptr.lock();
+ if (acquired) {
+ return acquired;
+ }
+
+ std::lock_guard<std::mutex> lock(mutex_);
+
+ acquired = ptr.lock();
+ if (acquired) {
+ return acquired;
+ }
+ acquired.reset(factory());
+ ptr = acquired;
+ return acquired;
+ }
+
+ public:
+ DriverCache();
+ ~DriverCache();
+
+ auto AcquireGpios() -> GpioExpander*;
+ auto AcquireDac() -> std::shared_ptr<AudioDac>;
+ auto AcquireDisplay() -> std::shared_ptr<Display>;
+ auto AcquireStorage() -> std::shared_ptr<SdStorage>;
+ auto AcquireTouchWheel() -> std::shared_ptr<TouchWheel>;
+};
+
+} // namespace drivers