Skip to content

Commit

Permalink
samples: nrf_desktop: Control LEDs by pin, not PWM channel
Browse files Browse the repository at this point in the history
nrfx PWM driver API changed and now the pin number is required instead
of the PWM channel. Fix usage of PWM driver in the application.

Jira: DESK-377

Signed-off-by: Filip Kubicz <[email protected]>
  • Loading branch information
Qbicz authored and carlescufi committed Dec 20, 2018
1 parent ea4528a commit adba30a
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 10 deletions.
8 changes: 8 additions & 0 deletions samples/nrf_desktop/configuration/led_state_pca10056.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@

#include "led_state.h"

/* Mapping the PWM channels to pin numbers */
const size_t led_pins[CONFIG_DESKTOP_LED_COUNT]
[CONFIG_DESKTOP_LED_COLOR_COUNT] = {
{
DT_NORDIC_NRF_PWM_PWM_0_CH0_PIN,
}
};

const struct led_config led_config[SYSTEM_STATE_COUNT][CONFIG_DESKTOP_LED_COUNT] = {
/* SYSTEM_STATE_DISCONNECTED */
{
Expand Down
15 changes: 15 additions & 0 deletions samples/nrf_desktop/configuration/led_state_pca20041.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@

#include "led_state.h"

/* Mapping the PWM channels to pin numbers */
const size_t led_pins[CONFIG_DESKTOP_LED_COUNT]
[CONFIG_DESKTOP_LED_COLOR_COUNT] = {
{
DT_NORDIC_NRF_PWM_PWM_0_CH0_PIN,
DT_NORDIC_NRF_PWM_PWM_0_CH1_PIN,
DT_NORDIC_NRF_PWM_PWM_0_CH2_PIN
},
{
DT_NORDIC_NRF_PWM_PWM_1_CH0_PIN,
DT_NORDIC_NRF_PWM_PWM_1_CH1_PIN,
DT_NORDIC_NRF_PWM_PWM_1_CH2_PIN
}
};

const struct led_config led_config[SYSTEM_STATE_COUNT][CONFIG_DESKTOP_LED_COUNT] = {
/* SYSTEM_STATE_DISCONNECTED */
{
Expand Down
8 changes: 8 additions & 0 deletions samples/nrf_desktop/configuration/led_state_pca63519.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,14 @@

#include "led_state.h"

/* Mapping the PWM channels to pin numbers */
const size_t led_pins[CONFIG_DESKTOP_LED_COUNT]
[CONFIG_DESKTOP_LED_COLOR_COUNT] = {
{
DT_NORDIC_NRF_PWM_PWM_0_CH0_PIN,
}
};

const struct led_config led_config[SYSTEM_STATE_COUNT][CONFIG_DESKTOP_LED_COUNT] = {
/* SYSTEM_STATE_DISCONNECTED */
{
Expand Down
24 changes: 14 additions & 10 deletions samples/nrf_desktop/src/hw_interface/leds.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
#include <logging/log.h>
LOG_MODULE_REGISTER(MODULE, CONFIG_DESKTOP_LED_LOG_LEVEL);


struct led {
struct device *pwm_dev;

size_t id;
enum led_mode mode;
struct led_color color;

Expand All @@ -31,30 +31,33 @@ struct led {
struct k_delayed_work work;
};

extern size_t led_pins[CONFIG_DESKTOP_LED_COUNT]
[CONFIG_DESKTOP_LED_COLOR_COUNT];

static struct led leds[CONFIG_DESKTOP_LED_COUNT];

static void pwm_out(struct device *pwm_dev, struct led_color *color)
static void pwm_out(struct led *led, struct led_color *color)
{
for (size_t i = 0; i < ARRAY_SIZE(color->c); i++) {
pwm_pin_set_usec(pwm_dev, i, CONFIG_DESKTOP_LED_BRIGHTNESS_MAX,
pwm_pin_set_usec(led->pwm_dev, led_pins[led->id][i],
CONFIG_DESKTOP_LED_BRIGHTNESS_MAX,
color->c[i]);
}
}

static void pwm_off(struct device *pwm_dev)
static void pwm_off(struct led *led)
{
struct led_color nocolor = {0};

pwm_out(pwm_dev, &nocolor);
pwm_out(led, &nocolor);
}

static void led_blink(struct led *led)
{
if (led->dir) {
pwm_off(led->pwm_dev);
pwm_off(led);
} else {
pwm_out(led->pwm_dev, &led->color);
pwm_out(led, &led->color);
}
led->dir = !led->dir;

Expand Down Expand Up @@ -82,11 +85,11 @@ static void led_mode_update(struct led *led, enum led_mode mode)

switch (mode) {
case LED_MODE_ON:
pwm_out(led->pwm_dev, &led->color);
pwm_out(led, &led->color);
break;

case LED_MODE_OFF:
pwm_off(led->pwm_dev);
pwm_off(led);
break;

case LED_MODE_BLINKING:
Expand All @@ -113,7 +116,7 @@ static int leds_init(void)
CONFIG_PWM_2_NAME,
#endif
#if CONFIG_PWM_3
CONFIG_PWM_4_NAME,
CONFIG_PWM_3_NAME,
#endif
};

Expand Down Expand Up @@ -170,6 +173,7 @@ static bool event_handler(const struct event_header *eh)
/* Record params */
struct led *led = &leds[event->led_id];

led->id = event->led_id;
led->color = event->color;
led->mode = event->mode;
led->period = event->period;
Expand Down

0 comments on commit adba30a

Please sign in to comment.