From 0bb12912bc185d78114ccebb5d6e7aae67cb1728 Mon Sep 17 00:00:00 2001 From: jacqueline Date: Tue, 6 Feb 2024 13:36:37 +1100 Subject: Implement basic volume control for bt outputs --- src/drivers/bluetooth.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'src/drivers/bluetooth.cpp') 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::dispatch( + bluetooth::events::ChangeVolume{.volume = vol}); +} + auto Bluetooth::SetEventHandler(std::function 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(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) { -- cgit v1.2.3