Bluetooth: Support multiple devices

This commit is contained in:
Linus Dierheimer
2023-01-24 12:02:20 +01:00
parent 4967d0a222
commit 209d0c98d9
10 changed files with 117 additions and 81 deletions
+40 -14
View File
@@ -4,6 +4,29 @@
#define FF_BLUETOOTH_MODULE_NAME "Bluetooth"
#define FF_BLUETOOTH_NUM_FORMAT_ARGS 4
static void printDevice(FFinstance* instance, const FFBluetoothDevice* device, uint8_t index)
{
if(instance->config.bluetooth.outputFormat.length == 0)
{
ffPrintLogoAndKey(instance, FF_BLUETOOTH_MODULE_NAME, index, &instance->config.bluetooth.key);
ffStrbufWriteTo(&device->name, stdout);
if(device->battery > 0)
printf(" (%d%%)", device->battery);
putchar('\n');
}
else
{
ffPrintFormat(instance, FF_BLUETOOTH_MODULE_NAME, index, &instance->config.bluetooth, FF_BLUETOOTH_NUM_FORMAT_ARGS, (FFformatarg[]) {
{FF_FORMAT_ARG_TYPE_STRBUF, &device->name},
{FF_FORMAT_ARG_TYPE_STRBUF, &device->address},
{FF_FORMAT_ARG_TYPE_STRBUF, &device->type},
{FF_FORMAT_ARG_TYPE_UINT8, &device->battery}
});
}
}
void ffPrintBluetooth(FFinstance* instance)
{
const FFBluetoothResult* bluetooth = ffDetectBluetooth(instance);
@@ -14,23 +37,26 @@ void ffPrintBluetooth(FFinstance* instance)
return;
}
if(instance->config.bluetooth.outputFormat.length == 0)
FFlist filtered;
ffListInit(&filtered, sizeof(FFBluetoothDevice*));
FF_LIST_FOR_EACH(FFBluetoothDevice, device, bluetooth->devices)
{
ffPrintLogoAndKey(instance, FF_BLUETOOTH_MODULE_NAME, 0, &instance->config.bluetooth.key);
ffStrbufWriteTo(&bluetooth->name, stdout);
if(!device->connected && !instance->config.bluetoothShowDisconnected)
continue;
if(bluetooth->battery > 0)
printf(" (%d%%)", bluetooth->battery);
putchar('\n');
*(FFBluetoothDevice**)ffListAdd(&filtered) = device;
}
else
if(filtered.length == 0)
{
ffPrintFormat(instance, FF_BLUETOOTH_MODULE_NAME, 0, &instance->config.bluetooth, FF_BLUETOOTH_NUM_FORMAT_ARGS, (FFformatarg[]) {
{FF_FORMAT_ARG_TYPE_STRBUF, &bluetooth->name},
{FF_FORMAT_ARG_TYPE_STRBUF, &bluetooth->address},
{FF_FORMAT_ARG_TYPE_STRBUF, &bluetooth->type},
{FF_FORMAT_ARG_TYPE_UINT8, &bluetooth->battery}
});
ffPrintError(instance, FF_BLUETOOTH_MODULE_NAME, 0, &instance->config.bluetooth, "No bluetooth devices found");
return;
}
for(uint32_t i = 0; i < filtered.length; i++)
{
uint8_t index = (uint8_t) (filtered.length == 1 ? 0 : i + 1);
printDevice(instance, *(FFBluetoothDevice**)ffListGet(&filtered, i), index);
}
}