Skip to content

Commit

Permalink
fw: use led_dt_spec
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiobaltieri committed Nov 16, 2024
1 parent e66be38 commit a1eabcc
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 40 deletions.
8 changes: 3 additions & 5 deletions firmware/boards/balto/darfon/leds.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,15 @@
#include <zephyr/kernel.h>
#include <hid_kbd.h>

#define LED_CAPS_NODE DT_NODELABEL(led_capslock)
#define LED_CAPS DT_NODE_CHILD_IDX(LED_CAPS_NODE)
static const struct device *leds = DEVICE_DT_GET(DT_PARENT(LED_CAPS_NODE));
static const struct led_dt_spec led_caps = LED_DT_SPEC_GET(DT_NODELABEL(led_capslock));
static const struct device *hid_kbd = DEVICE_DT_GET_ONE(hid_kbd);

static void caps_led_cb(const struct device *dev, uint8_t state)
{
if (state & BIT(1)) {
led_on(leds, LED_CAPS);
led_on_dt(&led_caps);
} else {
led_off(leds, LED_CAPS);
led_off_dt(&led_caps);
}
}

Expand Down
19 changes: 8 additions & 11 deletions firmware/boards/balto/zalpakka/touch_sense_led.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,16 @@
#include <zephyr/input/input.h>
#include <zephyr/kernel.h>

#define TOUCH_LED_NODE DT_NODELABEL(led_touch)
#define TOUCH_LED_IDX DT_NODE_CHILD_IDX(TOUCH_LED_NODE)

static void input_cb(struct input_event *evt, void *user_data)
{
static const struct device *dev = DEVICE_DT_GET(DT_PARENT(TOUCH_LED_NODE));
static const struct led_dt_spec touch_led = LED_DT_SPEC_GET(DT_NODELABEL(led_touch));

if (evt->code == INPUT_BTN_TOUCH) {
if (evt->value) {
led_on(dev, TOUCH_LED_IDX);
} else {
led_off(dev, TOUCH_LED_IDX);
}
}
if (evt->code == INPUT_BTN_TOUCH) {
if (evt->value) {
led_on_dt(&touch_led);
} else {
led_off_dt(&touch_led);
}
}
}
INPUT_CALLBACK_DEFINE(DEVICE_DT_GET(DT_NODELABEL(touch_sense)), input_cb, NULL);
35 changes: 11 additions & 24 deletions firmware/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,8 @@ static struct k_sem blink_sem = Z_SEM_INITIALIZER(blink_sem, 0, 4);
#define BLINKER_NODE DT_NODELABEL(led_input_activity)
#define STATUS_NODE DT_NODELABEL(led_status)

static const struct {
const struct device *activity_leds;
const uint32_t activity_idx;
const struct device *status_leds;
const uint32_t status_idx;
} cfg = {
#if DT_NODE_EXISTS(BLINKER_NODE)
.activity_leds = DEVICE_DT_GET_OR_NULL(DT_PARENT(BLINKER_NODE)),
.activity_idx = DT_NODE_CHILD_IDX(BLINKER_NODE),
#endif
#if DT_NODE_EXISTS(STATUS_NODE)
.status_leds = DEVICE_DT_GET(DT_PARENT(STATUS_NODE)),
.status_idx = DT_NODE_CHILD_IDX(STATUS_NODE),
#endif
};
const struct led_dt_spec activity_led = LED_DT_SPEC_GET_OR(BLINKER_NODE, {0});
const struct led_dt_spec status_led = LED_DT_SPEC_GET_OR(STATUS_NODE, {0});

#define BLINKER_QUEUE_SZ 10

Expand All @@ -36,30 +23,30 @@ static bool led_invert;

static void led_status(bool on)
{
if (cfg.status_leds == NULL) {
if (!led_is_ready_dt(&status_led)) {
return;
}

if (on) {
led_set_brightness(cfg.status_leds, cfg.status_idx,
(cfg.activity_leds == NULL && led_invert) ? 0 : 100);
led_set_brightness_dt(&status_led,
(activity_led.dev == NULL && led_invert) ? 0 : 100);
} else {
led_set_brightness(cfg.status_leds, cfg.status_idx,
(cfg.activity_leds == NULL && led_invert) ? 100 : 0);
led_set_brightness_dt(&status_led,
(activity_led.dev == NULL && led_invert) ? 100 : 0);
}
}

static void led_activity(bool on)
{
if (cfg.activity_leds == NULL) {
if (!led_is_ready_dt(&activity_led)) {
led_status(on);
return;
}

if (on) {
led_set_brightness(cfg.activity_leds, cfg.activity_idx, led_invert ? 0 : 100);
led_set_brightness_dt(&activity_led, led_invert ? 0 : 100);
} else {
led_set_brightness(cfg.activity_leds, cfg.activity_idx, led_invert ? 100 : 0);
led_set_brightness_dt(&activity_led, led_invert ? 100 : 0);
}
}

Expand All @@ -77,7 +64,7 @@ int main(void)
continue;
}

if (cfg.status_leds == NULL && cfg.activity_leds == NULL) {
if (!led_is_ready_dt(&status_led) && !led_is_ready_dt(&activity_led)) {
continue;
}

Expand Down

0 comments on commit a1eabcc

Please sign in to comment.