diff options
| author | jacqueline <me@jacqueline.id.au> | 2023-09-22 15:48:41 +1000 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2023-09-22 21:19:19 +1000 |
| commit | b192975cb1eeb4e6b7c7bf53cdf42701c55f034a (patch) | |
| tree | 39755851543d596f2630704be9efb56be1f39bfc /src/drivers | |
| parent | cbd99b2134c6c471708deb409a4b0fcc4c31516d (diff) | |
| download | tangara-fw-b192975cb1eeb4e6b7c7bf53cdf42701c55f034a.tar.gz | |
make bluetooth pairing ui functional
Diffstat (limited to 'src/drivers')
| -rw-r--r-- | src/drivers/bluetooth.cpp | 34 | ||||
| -rw-r--r-- | src/drivers/include/bluetooth.hpp | 9 | ||||
| -rw-r--r-- | src/drivers/include/bluetooth_types.hpp | 5 | ||||
| -rw-r--r-- | src/drivers/nvs.cpp | 5 |
4 files changed, 42 insertions, 11 deletions
diff --git a/src/drivers/bluetooth.cpp b/src/drivers/bluetooth.cpp index f3373849..924cdf42 100644 --- a/src/drivers/bluetooth.cpp +++ b/src/drivers/bluetooth.cpp @@ -8,6 +8,7 @@ #include <ostream> #include <sstream> +#include "bluetooth_types.hpp" #include "esp_a2dp_api.h" #include "esp_avrc_api.h" #include "esp_bt.h" @@ -56,7 +57,7 @@ auto a2dp_data_cb(uint8_t* buf, int32_t buf_size) -> int32_t { return xStreamBufferReceive(stream, buf, buf_size, 0); } -Bluetooth::Bluetooth(NvsStorage* storage) { +Bluetooth::Bluetooth(NvsStorage& storage) { bluetooth::BluetoothState::Init(storage); } @@ -72,6 +73,10 @@ auto Bluetooth::Disable() -> void { bluetooth::events::Disable{}); } +auto Bluetooth::IsEnabled() -> bool { + return !bluetooth::BluetoothState::is_in_state<bluetooth::Disabled>(); +} + auto Bluetooth::KnownDevices() -> std::vector<bluetooth::Device> { std::vector<bluetooth::Device> out = bluetooth::BluetoothState::devices(); std::sort(out.begin(), out.end(), [](const auto& a, const auto& b) -> bool { @@ -98,6 +103,11 @@ auto Bluetooth::SetSource(StreamBufferHandle_t src) -> void { bluetooth::events::SourceChanged{}); } +auto Bluetooth::SetEventHandler(std::function<void(bluetooth::Event)> cb) + -> void { + bluetooth::BluetoothState::event_handler(cb); +} + auto DeviceName() -> std::string { uint8_t mac[8]{0}; esp_efuse_mac_get_default(mac); @@ -111,16 +121,17 @@ namespace bluetooth { NvsStorage* BluetoothState::sStorage_; -std::mutex BluetoothState::sDevicesMutex_; -std::map<mac_addr_t, Device> BluetoothState::sDevices_; -std::optional<mac_addr_t> BluetoothState::sPreferredDevice_; +std::mutex BluetoothState::sDevicesMutex_{}; +std::map<mac_addr_t, Device> BluetoothState::sDevices_{}; +std::optional<mac_addr_t> BluetoothState::sPreferredDevice_{}; mac_addr_t BluetoothState::sCurrentDevice_; std::atomic<StreamBufferHandle_t> BluetoothState::sSource_; +std::function<void(Event)> BluetoothState::sEventHandler_; -auto BluetoothState::Init(NvsStorage* storage) -> void { - sStorage_ = storage; - sPreferredDevice_ = storage->PreferredBluetoothDevice().get(); +auto BluetoothState::Init(NvsStorage& storage) -> void { + sStorage_ = &storage; + sPreferredDevice_ = storage.PreferredBluetoothDevice().get(); tinyfsm::FsmList<bluetooth::BluetoothState>::start(); } @@ -153,6 +164,11 @@ auto BluetoothState::source(StreamBufferHandle_t src) -> void { sSource_.store(src); } +auto BluetoothState::event_handler(std::function<void(Event)> cb) -> void { + std::lock_guard lock{sDevicesMutex_}; + sEventHandler_ = cb; +} + static bool sIsFirstEntry = true; void Disabled::entry() { @@ -303,6 +319,10 @@ auto Scanning::OnDeviceDiscovered(esp_bt_gap_cb_param_t* param) -> void { sCurrentDevice_ = device.address; is_preferred = true; } + + if (sEventHandler_) { + std::invoke(sEventHandler_, Event::kKnownDevicesChanged); + } } if (is_preferred) { diff --git a/src/drivers/include/bluetooth.hpp b/src/drivers/include/bluetooth.hpp index fc72d393..1489b790 100644 --- a/src/drivers/include/bluetooth.hpp +++ b/src/drivers/include/bluetooth.hpp @@ -26,15 +26,17 @@ namespace drivers { */ class Bluetooth { public: - Bluetooth(NvsStorage* storage); + Bluetooth(NvsStorage& storage); auto Enable() -> bool; auto Disable() -> void; + auto IsEnabled() -> bool; auto KnownDevices() -> std::vector<bluetooth::Device>; auto SetPreferredDevice(const bluetooth::mac_addr_t& mac) -> void; auto SetSource(StreamBufferHandle_t) -> void; + auto SetEventHandler(std::function<void(bluetooth::Event)> cb) -> void; }; namespace bluetooth { @@ -64,7 +66,7 @@ struct Avrc : public tinyfsm::Event { class BluetoothState : public tinyfsm::Fsm<BluetoothState> { public: - static auto Init(NvsStorage* storage) -> void; + static auto Init(NvsStorage& storage) -> void; static auto devices() -> std::vector<Device>; static auto preferred_device() -> std::optional<mac_addr_t>; @@ -73,6 +75,8 @@ class BluetoothState : public tinyfsm::Fsm<BluetoothState> { static auto source() -> StreamBufferHandle_t; static auto source(StreamBufferHandle_t) -> void; + static auto event_handler(std::function<void(Event)>) -> void; + virtual ~BluetoothState(){}; virtual void entry() {} @@ -96,6 +100,7 @@ class BluetoothState : public tinyfsm::Fsm<BluetoothState> { static mac_addr_t sCurrentDevice_; static std::atomic<StreamBufferHandle_t> sSource_; + static std::function<void(Event)> sEventHandler_; }; class Disabled : public BluetoothState { diff --git a/src/drivers/include/bluetooth_types.hpp b/src/drivers/include/bluetooth_types.hpp index 03100651..12ed5cb3 100644 --- a/src/drivers/include/bluetooth_types.hpp +++ b/src/drivers/include/bluetooth_types.hpp @@ -16,5 +16,10 @@ struct Device { int8_t signal_strength; }; +enum class Event { + kKnownDevicesChanged, + kConnectionStateChanged, +}; + } // namespace bluetooth } // namespace drivers diff --git a/src/drivers/nvs.cpp b/src/drivers/nvs.cpp index 11dde08c..67867c07 100644 --- a/src/drivers/nvs.cpp +++ b/src/drivers/nvs.cpp @@ -128,7 +128,7 @@ auto NvsStorage::OutputMode() -> std::future<Output> { nvs_get_u8(handle_, kKeyOutput, &out); switch (out) { case static_cast<uint8_t>(Output::kBluetooth): - return Output::kHeadphones; + return Output::kBluetooth; case static_cast<uint8_t>(Output::kHeadphones): default: return Output::kHeadphones; @@ -138,7 +138,8 @@ auto NvsStorage::OutputMode() -> std::future<Output> { auto NvsStorage::OutputMode(Output out) -> std::future<bool> { return writer_->Dispatch<bool>([&]() { - nvs_set_u8(handle_, kKeyOutput, static_cast<uint8_t>(out)); + uint8_t as_int = static_cast<uint8_t>(out); + nvs_set_u8(handle_, kKeyOutput, as_int); return nvs_commit(handle_) == ESP_OK; }); } |
