diff options
Diffstat (limited to 'src/drivers')
| -rw-r--r-- | src/drivers/bluetooth.cpp | 21 | ||||
| -rw-r--r-- | src/drivers/include/bluetooth.hpp | 9 |
2 files changed, 30 insertions, 0 deletions
diff --git a/src/drivers/bluetooth.cpp b/src/drivers/bluetooth.cpp index 4ea56ab0..b6cd40a9 100644 --- a/src/drivers/bluetooth.cpp +++ b/src/drivers/bluetooth.cpp @@ -17,6 +17,7 @@ #include "esp_bt_defs.h" #include "esp_bt_device.h" #include "esp_bt_main.h" +#include "esp_err.h" #include "esp_gap_bt_api.h" #include "esp_log.h" #include "esp_mac.h" @@ -149,6 +150,11 @@ auto Bluetooth::SetSource(StreamBufferHandle_t src) -> void { bluetooth::events::SourceChanged{}); } +auto Bluetooth::SetVolume(uint8_t vol) -> void { + tinyfsm::FsmList<bluetooth::BluetoothState>::dispatch( + bluetooth::events::ChangeVolume{.volume = vol}); +} + auto Bluetooth::SetEventHandler(std::function<void(bluetooth::Event)> cb) -> void { bluetooth::BluetoothState::event_handler(cb); @@ -401,6 +407,8 @@ void Disabled::entry() { sScanner_->StopScanningNow(); + esp_a2d_source_deinit(); + esp_avrc_ct_deinit(); esp_bluedroid_disable(); esp_bluedroid_deinit(); esp_bt_controller_disable(); @@ -603,6 +611,7 @@ void Connecting::react(const events::internal::A2dp& ev) { void Connected::entry() { ESP_LOGI(kTag, "entering connected state"); + transaction_num_ = 0; connected_to_ = sConnectingDevice_->mac; sPreferredDevice_ = sConnectingDevice_; sConnectingDevice_ = {}; @@ -639,6 +648,18 @@ void Connected::react(const events::SourceChanged& ev) { } } +void Connected::react(const events::ChangeVolume& ev) { + ESP_LOGI(kTag, "send vol %u", ev.volume); + esp_err_t err = esp_avrc_ct_send_set_absolute_volume_cmd( + transaction_num_++, std::clamp<uint8_t>(ev.volume, 0, 0x7f)); + if (err != ESP_OK) { + ESP_LOGW(kTag, "send vol failed %u", err); + } + if (transaction_num_ > ESP_AVRC_TRANS_LABEL_MAX) { + transaction_num_ = 0; + } +} + void Connected::react(const events::internal::Gap& ev) { sScanner_->HandleGapEvent(ev); switch (ev.type) { diff --git a/src/drivers/include/bluetooth.hpp b/src/drivers/include/bluetooth.hpp index 291d049d..00ddb0c0 100644 --- a/src/drivers/include/bluetooth.hpp +++ b/src/drivers/include/bluetooth.hpp @@ -11,6 +11,7 @@ #include <freertos/FreeRTOS.h> #include <freertos/stream_buffer.h> +#include <stdint.h> #include "bluetooth_types.hpp" #include "esp_a2dp_api.h" #include "esp_avrc_api.h" @@ -48,6 +49,8 @@ class Bluetooth { auto PreferredDevice() -> std::optional<bluetooth::MacAndName>; auto SetSource(StreamBufferHandle_t) -> void; + auto SetVolume(uint8_t) -> void; + auto SetEventHandler(std::function<void(bluetooth::Event)> cb) -> void; }; @@ -63,6 +66,9 @@ struct DiscoveryChanged : public tinyfsm::Event {}; struct DeviceDiscovered : public tinyfsm::Event { const Device& device; }; +struct ChangeVolume : public tinyfsm::Event { + const uint8_t volume; +}; namespace internal { struct Gap : public tinyfsm::Event { @@ -129,6 +135,7 @@ class BluetoothState : public tinyfsm::Fsm<BluetoothState> { virtual void react(const events::PreferredDeviceChanged& ev){}; virtual void react(const events::SourceChanged& ev){}; virtual void react(const events::DiscoveryChanged&); + virtual void react(const events::ChangeVolume&) {} virtual void react(const events::DeviceDiscovered&); @@ -198,6 +205,7 @@ class Connected : public BluetoothState { void react(const events::PreferredDeviceChanged& ev) override; void react(const events::SourceChanged& ev) override; + void react(const events::ChangeVolume&) override; void react(const events::Disable& ev) override; void react(const events::internal::Gap& ev) override; @@ -207,6 +215,7 @@ class Connected : public BluetoothState { using BluetoothState::react; private: + uint8_t transaction_num_; mac_addr_t connected_to_; }; |
