From d6b83fcf4a1a3039c06e0b1d1a1f7e2af2351efb Mon Sep 17 00:00:00 2001 From: jacqueline Date: Tue, 15 Aug 2023 13:53:30 +1000 Subject: Flesh out basic bluetooth support No ui yet, and performance isn't great. It kinda works though!! --- src/app_console/app_console.cpp | 43 +++++++++++++++++++++++++++++++++ src/app_console/include/app_console.hpp | 2 ++ 2 files changed, 45 insertions(+) (limited to 'src/app_console') diff --git a/src/app_console/app_console.cpp b/src/app_console/app_console.cpp index 30b7d2dc..7804fd34 100644 --- a/src/app_console/app_console.cpp +++ b/src/app_console/app_console.cpp @@ -37,6 +37,7 @@ namespace console { std::weak_ptr AppConsole::sDatabase; audio::TrackQueue* AppConsole::sTrackQueue; +drivers::Bluetooth* AppConsole::sBluetooth; int CmdListDir(int argc, char** argv) { auto lock = AppConsole::sDatabase.lock(); @@ -439,6 +440,47 @@ void RegisterTaskStates() { esp_console_cmd_register(&cmd); } +int CmdBtList(int argc, char** argv) { + static const std::string usage = "usage: bt_list "; + if (argc > 2) { + std::cout << usage << std::endl; + return 1; + } + + auto devices = AppConsole::sBluetooth->KnownDevices(); + if (argc == 2) { + int index = std::atoi(argv[1]); + if (index < 0 || index >= devices.size()) { + std::cout << "index out of range" << std::endl; + return -1; + } + AppConsole::sBluetooth->SetPreferredDevice(devices[index].address); + } else { + std::cout << "mac\t\trssi\tname" << std::endl; + for (const auto& device : devices) { + for (size_t i = 0; i < device.address.size(); i++) { + std::cout << std::hex << std::setfill('0') << std::setw(2) + << static_cast(device.address[i]); + } + float perc = + (static_cast(device.signal_strength) + 127.0) / 256.0 * 100; + std::cout << "\t" << std::fixed << std::setprecision(0) << perc << "%"; + std::cout << "\t" << device.name << std::endl; + } + } + + return 0; +} + +void RegisterBtList() { + esp_console_cmd_t cmd{.command = "bt_list", + .help = "lists and connects to bluetooth devices", + .hint = NULL, + .func = &CmdBtList, + .argtable = NULL}; + esp_console_cmd_register(&cmd); +} + auto AppConsole::RegisterExtraComponents() -> void { RegisterListDir(); RegisterPlayFile(); @@ -452,6 +494,7 @@ auto AppConsole::RegisterExtraComponents() -> void { RegisterDbIndex(); RegisterDbDump(); RegisterTaskStates(); + RegisterBtList(); } } // namespace console diff --git a/src/app_console/include/app_console.hpp b/src/app_console/include/app_console.hpp index 3cb62b21..667e452f 100644 --- a/src/app_console/include/app_console.hpp +++ b/src/app_console/include/app_console.hpp @@ -8,6 +8,7 @@ #include +#include "bluetooth.hpp" #include "console.hpp" #include "database.hpp" #include "track_queue.hpp" @@ -18,6 +19,7 @@ class AppConsole : public Console { public: static std::weak_ptr sDatabase; static audio::TrackQueue* sTrackQueue; + static drivers::Bluetooth* sBluetooth; protected: virtual auto RegisterExtraComponents() -> void; -- cgit v1.2.3