From 71b46730394979ea528d152dbe884cc35c368759 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Wed, 17 Jan 2024 11:48:40 +1100 Subject: all screens basically working, but bluetooth is rough --- src/drivers/CMakeLists.txt | 10 +++--- src/drivers/bluetooth.cpp | 55 ++++++++++++++++++++++++++------- src/drivers/include/bluetooth.hpp | 10 +++++- src/drivers/include/bluetooth_types.hpp | 2 ++ src/drivers/include/nvs.hpp | 2 +- 5 files changed, 62 insertions(+), 17 deletions(-) (limited to 'src/drivers') diff --git a/src/drivers/CMakeLists.txt b/src/drivers/CMakeLists.txt index 7d1e048d..4155ed66 100644 --- a/src/drivers/CMakeLists.txt +++ b/src/drivers/CMakeLists.txt @@ -3,9 +3,11 @@ # SPDX-License-Identifier: GPL-3.0-only idf_component_register( - SRCS "touchwheel.cpp" "i2s_dac.cpp" "gpios.cpp" "adc.cpp" "storage.cpp" "i2c.cpp" - "spi.cpp" "display.cpp" "display_init.cpp" "samd.cpp" "relative_wheel.cpp" "wm8523.cpp" - "nvs.cpp" "bluetooth.cpp" "haptics.cpp" "spiffs.cpp" + SRCS "touchwheel.cpp" "i2s_dac.cpp" "gpios.cpp" "adc.cpp" "storage.cpp" + "i2c.cpp" "bluetooth.cpp" "spi.cpp" "display.cpp" "display_init.cpp" + "samd.cpp" "relative_wheel.cpp" "wm8523.cpp" "nvs.cpp" "haptics.cpp" + "spiffs.cpp" INCLUDE_DIRS "include" - REQUIRES "esp_adc" "fatfs" "result" "lvgl" "span" "tasks" "nvs_flash" "bt" "tinyfsm" "spiffs") + REQUIRES "esp_adc" "fatfs" "result" "lvgl" "span" "tasks" "nvs_flash" "spiffs" + "bt" "tinyfsm") target_compile_options(${COMPONENT_LIB} PRIVATE ${EXTRA_WARNINGS}) diff --git a/src/drivers/bluetooth.cpp b/src/drivers/bluetooth.cpp index a962a280..f58c98a3 100644 --- a/src/drivers/bluetooth.cpp +++ b/src/drivers/bluetooth.cpp @@ -80,6 +80,36 @@ auto Bluetooth::IsEnabled() -> bool { return !bluetooth::BluetoothState::is_in_state(); } +auto Bluetooth::IsConnected() -> bool { + return bluetooth::BluetoothState::is_in_state(); +} + +auto Bluetooth::ConnectedDevice() -> std::optional { + if (!IsConnected()) { + return {}; + } + auto looking_for = bluetooth::BluetoothState::preferred_device(); + for (const auto& dev : bluetooth::BluetoothState::devices()) { + if (dev.address == looking_for) { + return dev; + } + } + return {}; +} + +auto Bluetooth::SetDeviceDiscovery(bool allowed) -> void { + if (allowed == bluetooth::BluetoothState::discovery()) { + return; + } + bluetooth::BluetoothState::discovery(allowed); + tinyfsm::FsmList::dispatch( + bluetooth::events::DiscoveryChanged{}); +} + +auto Bluetooth::IsDiscovering() -> bool { + return bluetooth::BluetoothState::discovery(); +} + auto Bluetooth::KnownDevices() -> std::vector { std::vector out = bluetooth::BluetoothState::devices(); std::sort(out.begin(), out.end(), [](const auto& a, const auto& b) -> bool { @@ -97,13 +127,8 @@ auto Bluetooth::SetPreferredDevice(const bluetooth::mac_addr_t& mac) -> void { bluetooth::events::PreferredDeviceChanged{}); } -auto Bluetooth::SetDeviceDiscovery(bool allowed) -> void { - if (allowed == bluetooth::BluetoothState::discovery()) { - return; - } - bluetooth::BluetoothState::discovery(allowed); - tinyfsm::FsmList::dispatch( - bluetooth::events::DiscoveryChanged{}); +auto Bluetooth::PreferredDevice() -> std::optional { + return bluetooth::BluetoothState::preferred_device(); } auto Bluetooth::SetSource(StreamBufferHandle_t src) -> void { @@ -120,7 +145,7 @@ auto Bluetooth::SetEventHandler(std::function cb) bluetooth::BluetoothState::event_handler(cb); } -auto DeviceName() -> std::pmr::string { +static auto DeviceName() -> std::pmr::string { uint8_t mac[8]{0}; esp_efuse_mac_get_default(mac); std::ostringstream name; @@ -267,7 +292,7 @@ Scanner* BluetoothState::sScanner_; std::mutex BluetoothState::sDevicesMutex_{}; std::map BluetoothState::sDevices_{}; std::optional BluetoothState::sPreferredDevice_{}; -mac_addr_t BluetoothState::sCurrentDevice_{0}; +std::optional BluetoothState::sCurrentDevice_{}; bool BluetoothState::sIsDiscoveryAllowed_{false}; std::atomic BluetoothState::sSource_; @@ -331,7 +356,7 @@ auto BluetoothState::react(const events::DeviceDiscovered& ev) -> void { sDevices_[ev.device.address] = ev.device; if (ev.device.address == sPreferredDevice_) { - sCurrentDevice_ = ev.device.address; + sCurrentDevice_ = ev.device; is_preferred = true; } @@ -466,9 +491,17 @@ void Idle::react(const events::internal::Gap& ev) { void Connecting::entry() { ESP_LOGI(kTag, "connecting to device"); esp_a2d_source_connect(sPreferredDevice_.value().data()); + + if (sEventHandler_) { + std::invoke(sEventHandler_, Event::kConnectionStateChanged); + } } -void Connecting::exit() {} +void Connecting::exit() { + if (sEventHandler_) { + std::invoke(sEventHandler_, Event::kConnectionStateChanged); + } +} void Connecting::react(const events::Disable& ev) { // TODO: disconnect gracefully diff --git a/src/drivers/include/bluetooth.hpp b/src/drivers/include/bluetooth.hpp index f3623fb8..4aefbc42 100644 --- a/src/drivers/include/bluetooth.hpp +++ b/src/drivers/include/bluetooth.hpp @@ -32,14 +32,20 @@ class Bluetooth { auto Disable() -> void; auto IsEnabled() -> bool; + auto IsConnected() -> bool; + auto ConnectedDevice() -> std::optional; + /* * Sets whether or not the bluetooth stack is allowed to actively scan for * new devices. */ auto SetDeviceDiscovery(bool) -> void; + auto IsDiscovering() -> bool; auto KnownDevices() -> std::vector; + auto SetPreferredDevice(const bluetooth::mac_addr_t& mac) -> void; + auto PreferredDevice() -> std::optional; auto SetSource(StreamBufferHandle_t) -> void; auto SetEventHandler(std::function cb) -> void; @@ -100,9 +106,11 @@ class BluetoothState : public tinyfsm::Fsm { static auto Init(NvsStorage& storage) -> void; static auto devices() -> std::vector; + static auto preferred_device() -> std::optional; static auto preferred_device(std::optional) -> void; + static auto scanning() -> bool; static auto discovery() -> bool; static auto discovery(bool) -> void; @@ -135,7 +143,7 @@ class BluetoothState : public tinyfsm::Fsm { static std::mutex sDevicesMutex_; static std::map sDevices_; static std::optional sPreferredDevice_; - static mac_addr_t sCurrentDevice_; + static std::optional sCurrentDevice_; static bool sIsDiscoveryAllowed_; static std::atomic sSource_; diff --git a/src/drivers/include/bluetooth_types.hpp b/src/drivers/include/bluetooth_types.hpp index 7e26f0d7..518771e5 100644 --- a/src/drivers/include/bluetooth_types.hpp +++ b/src/drivers/include/bluetooth_types.hpp @@ -21,6 +21,8 @@ struct Device { enum class Event { kKnownDevicesChanged, kConnectionStateChanged, + kPreferredDeviceChanged, + kDiscoveryChanged, }; } // namespace bluetooth diff --git a/src/drivers/include/nvs.hpp b/src/drivers/include/nvs.hpp index b82013b5..bf0bebab 100644 --- a/src/drivers/include/nvs.hpp +++ b/src/drivers/include/nvs.hpp @@ -67,4 +67,4 @@ class NvsStorage { nvs_handle_t handle_; }; -} // namespace drivers \ No newline at end of file +} // namespace drivers -- cgit v1.2.3