diff options
| author | jacqueline <me@jacqueline.id.au> | 2023-08-15 13:53:30 +1000 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2023-08-15 13:53:30 +1000 |
| commit | d6b83fcf4a1a3039c06e0b1d1a1f7e2af2351efb (patch) | |
| tree | 03c6a534931736a2755aacef86e271ecc5b8e87c /src/app_console/app_console.cpp | |
| parent | 205e3053506191fab69d01e7523e733dccc09d77 (diff) | |
| download | tangara-fw-d6b83fcf4a1a3039c06e0b1d1a1f7e2af2351efb.tar.gz | |
Flesh out basic bluetooth support
No ui yet, and performance isn't great. It kinda works though!!
Diffstat (limited to 'src/app_console/app_console.cpp')
| -rw-r--r-- | src/app_console/app_console.cpp | 43 |
1 files changed, 43 insertions, 0 deletions
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<database::Database> 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 <index>"; + 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<int>(device.address[i]); + } + float perc = + (static_cast<double>(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 |
