fbd-dev-leds: Move led creation into fbd-dev-led

This will allow us to separate the different led types
This commit is contained in:
Guido Günther
2023-02-10 15:15:12 +00:00
committed by Guido Gunther
parent 5fc0372b45
commit fd852c6f69
3 changed files with 75 additions and 58 deletions
+69
View File
@@ -9,9 +9,15 @@
#define G_LOG_DOMAIN "fbd-dev-led"
#include "fbd-dev-led.h"
#include "fbd-enums.h"
#include "fbd-udev.h"
#define LED_BRIGHTNESS_ATTR "brightness"
#define LED_MAX_BRIGHTNESS_ATTR "max_brightness"
#define LED_MULTI_INDEX_ATTR "multi_index"
#define LED_MULTI_INDEX_RED "red"
#define LED_MULTI_INDEX_GREEN "green"
#define LED_MULTI_INDEX_BLUE "blue"
void
fbd_dev_led_free (FbdDevLed *led)
@@ -20,6 +26,69 @@ fbd_dev_led_free (FbdDevLed *led)
g_free (led);
}
FbdDevLed *
fbd_dev_led_new (GUdevDevice *dev)
{
FbdDevLed *led = NULL;
const gchar *name, *path;
name = g_udev_device_get_name (dev);
/* We don't know anything about diffusors that can combine different
color LEDSs so go with fixed colors until the kernel gives us
enough information */
for (int i = 0; i <= FBD_FEEDBACK_LED_COLOR_LAST; i++) {
g_autofree char *color = NULL;
g_autofree char *enum_name = NULL;
const gchar * const *index;
guint counter = 0;
gchar *c;
enum_name = g_enum_to_string (FBD_TYPE_FEEDBACK_LED_COLOR, i);
c = strrchr (enum_name, '_');
color = g_ascii_strdown (c+1, -1);
if (g_strstr_len (name, -1, color)) {
g_autoptr (GError) err = NULL;
guint brightness = g_udev_device_get_sysfs_attr_as_int (dev, LED_MAX_BRIGHTNESS_ATTR);
index = g_udev_device_get_sysfs_attr_as_strv (dev, LED_MULTI_INDEX_ATTR);
if (!brightness)
continue;
led = g_malloc0 (sizeof(FbdDevLed));
led->dev = g_object_ref (dev);
led->color = i;
led->max_brightness = brightness;
if (index) {
for (int j = 0; j < g_strv_length ((gchar **) index); j++) {
g_debug ("Index: %s", index[j]);
if (g_strcmp0 (index[j], LED_MULTI_INDEX_RED) == 0) {
led->red_index = counter;
counter++;
} else if (g_strcmp0 (index[j], LED_MULTI_INDEX_GREEN) == 0) {
led->green_index = counter;
counter++;
} else if (g_strcmp0 (index[j], LED_MULTI_INDEX_BLUE) == 0) {
led->blue_index = counter;
counter++;
} else {
g_warning ("Unsupport LED color index: %d %s", counter, index[j]);
}
}
}
path = g_udev_device_get_sysfs_path (dev);
g_debug ("LED at '%s' usable", path);
break;
}
}
return led;
}
gboolean
fbd_dev_led_set_brightness (FbdDevLed *led, guint brightness)
{