summaryrefslogtreecommitdiff
path: root/src/drivers
diff options
context:
space:
mode:
Diffstat (limited to 'src/drivers')
-rw-r--r--src/drivers/CMakeLists.txt10
-rw-r--r--src/drivers/bluetooth.cpp55
-rw-r--r--src/drivers/include/bluetooth.hpp10
-rw-r--r--src/drivers/include/bluetooth_types.hpp2
-rw-r--r--src/drivers/include/nvs.hpp2
5 files changed, 62 insertions, 17 deletions
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<bluetooth::Disabled>();
}
+auto Bluetooth::IsConnected() -> bool {
+ return bluetooth::BluetoothState::is_in_state<bluetooth::Connected>();
+}
+
+auto Bluetooth::ConnectedDevice() -> std::optional<bluetooth::Device> {
+ 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<bluetooth::BluetoothState>::dispatch(
+ bluetooth::events::DiscoveryChanged{});
+}
+
+auto Bluetooth::IsDiscovering() -> bool {
+ return bluetooth::BluetoothState::discovery();
+}
+
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 {
@@ -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<bluetooth::BluetoothState>::dispatch(
- bluetooth::events::DiscoveryChanged{});
+auto Bluetooth::PreferredDevice() -> std::optional<bluetooth::mac_addr_t> {
+ return bluetooth::BluetoothState::preferred_device();
}
auto Bluetooth::SetSource(StreamBufferHandle_t src) -> void {
@@ -120,7 +145,7 @@ auto Bluetooth::SetEventHandler(std::function<void(bluetooth::Event)> 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<mac_addr_t, Device> BluetoothState::sDevices_{};
std::optional<mac_addr_t> BluetoothState::sPreferredDevice_{};
-mac_addr_t BluetoothState::sCurrentDevice_{0};
+std::optional<Device> BluetoothState::sCurrentDevice_{};
bool BluetoothState::sIsDiscoveryAllowed_{false};
std::atomic<StreamBufferHandle_t> 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<bluetooth::Device>;
+
/*
* 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<bluetooth::Device>;
+
auto SetPreferredDevice(const bluetooth::mac_addr_t& mac) -> void;
+ auto PreferredDevice() -> std::optional<bluetooth::mac_addr_t>;
auto SetSource(StreamBufferHandle_t) -> void;
auto SetEventHandler(std::function<void(bluetooth::Event)> cb) -> void;
@@ -100,9 +106,11 @@ class BluetoothState : public tinyfsm::Fsm<BluetoothState> {
static auto Init(NvsStorage& storage) -> void;
static auto devices() -> std::vector<Device>;
+
static auto preferred_device() -> std::optional<mac_addr_t>;
static auto preferred_device(std::optional<mac_addr_t>) -> void;
+ static auto scanning() -> bool;
static auto discovery() -> bool;
static auto discovery(bool) -> void;
@@ -135,7 +143,7 @@ class BluetoothState : public tinyfsm::Fsm<BluetoothState> {
static std::mutex sDevicesMutex_;
static std::map<mac_addr_t, Device> sDevices_;
static std::optional<mac_addr_t> sPreferredDevice_;
- static mac_addr_t sCurrentDevice_;
+ static std::optional<Device> sCurrentDevice_;
static bool sIsDiscoveryAllowed_;
static std::atomic<StreamBufferHandle_t> 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