This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

function LED_EFFECT_LED_ON() make which led on?

I used NRF5 SKD before, it is my first time to use nrf connect sdk. I had been spent few days to learn how to use it to develop ,but encountered many problems.

LED_EFFECT_LED_ON() function is locate in  ncs\nrf\applications\nrf_desktop\configuration\nrf52840_pca20041\led_state_def.h

In led_state_def.h ,There are several function to control LED in different status ,but which LED it is control??

like LED_EFFECT_LED_ON(LED_COLOR(255, 0, 20)) ,I know I can change the led color ,but how can I use this function make other led on?

This is led_state_def.h

/*
 * Copyright (c) 2018 Nordic Semiconductor ASA
 *
 * SPDX-License-Identifier: LicenseRef-BSD-5-Clause-Nordic
 */

#include "led_state.h"
#include "led_effect.h"

/* This configuration file is included only once from led_state module and holds
 * information about LED effect associated with each state.
 */

/* This structure enforces the header file is included only once in the build.
 * Violating this requirement triggers a multiple definition error at link time.
 */
const struct {} led_state_def_include_once;


/* Map function to LED ID */
static const u8_t led_map[LED_ID_COUNT] = {
	[LED_ID_SYSTEM_STATE] = 0,
	[LED_ID_PEER_STATE] = 1
};

static const struct led_effect led_system_state_effect[LED_SYSTEM_STATE_COUNT] = {
	[LED_SYSTEM_STATE_IDLE]     = LED_EFFECT_LED_ON(LED_COLOR(255, 0, 20)),
	[LED_SYSTEM_STATE_CHARGING] = LED_EFFECT_LED_ON(LED_COLOR(250, 250, 250)),
	[LED_SYSTEM_STATE_ERROR]    = LED_EFFECT_LED_BLINK(200, LED_COLOR(255, 0, 0)),
};

static const struct led_effect led_peer_state_effect[LED_PEER_COUNT][LED_PEER_STATE_COUNT] = {
	{
		[LED_PEER_STATE_DISCONNECTED]   = LED_EFFECT_LED_OFF(),
		[LED_PEER_STATE_CONNECTED]      = LED_EFFECT_LED_ON(LED_COLOR(150, 0, 0)),
		[LED_PEER_STATE_PEER_SEARCH]    = LED_EFFECT_LED_BREATH(500, LED_COLOR(150, 0, 0)),
		[LED_PEER_STATE_CONFIRM_SELECT] = LED_EFFECT_LED_BLINK(50, LED_COLOR(150, 0, 0)),
		[LED_PEER_STATE_CONFIRM_ERASE]  = LED_EFFECT_LED_BLINK(25, LED_COLOR(150, 0, 0)),
		[LED_PEER_STATE_ERASE_ADV]	= LED_EFFECT_LED_BREATH(100, LED_COLOR(150, 0, 0)),
	},
	{
		[LED_PEER_STATE_DISCONNECTED]   = LED_EFFECT_LED_OFF(),
		[LED_PEER_STATE_CONNECTED]      = LED_EFFECT_LED_ON(LED_COLOR(0, 150, 0)),
		[LED_PEER_STATE_PEER_SEARCH]    = LED_EFFECT_LED_BREATH(500, LED_COLOR(0, 150, 0)),
		[LED_PEER_STATE_CONFIRM_SELECT] = LED_EFFECT_LED_BLINK(50, LED_COLOR(0, 150, 0)),
		[LED_PEER_STATE_CONFIRM_ERASE]  = LED_EFFECT_LED_BLINK(25, LED_COLOR(0, 150, 0)),
		[LED_PEER_STATE_ERASE_ADV]	= LED_EFFECT_LED_BREATH(100, LED_COLOR(0, 150, 0)),
	},
	{
		[LED_PEER_STATE_DISCONNECTED]   = LED_EFFECT_LED_OFF(),
		[LED_PEER_STATE_CONNECTED]      = LED_EFFECT_LED_ON(LED_COLOR(0, 0, 150)),
		[LED_PEER_STATE_PEER_SEARCH]    = LED_EFFECT_LED_BREATH(500, LED_COLOR(0, 0, 150)),
		[LED_PEER_STATE_CONFIRM_SELECT] = LED_EFFECT_LED_BLINK(50, LED_COLOR(0, 0, 150)),
		[LED_PEER_STATE_CONFIRM_ERASE]  = LED_EFFECT_LED_BLINK(25, LED_COLOR(0, 0, 150)),
		[LED_PEER_STATE_ERASE_ADV]	= LED_EFFECT_LED_BREATH(100, LED_COLOR(0, 0, 150)),
	},
	{
		[LED_PEER_STATE_DISCONNECTED]   = LED_EFFECT_LED_OFF(),
		[LED_PEER_STATE_CONNECTED]      = LED_EFFECT_LED_ON(LED_COLOR(150, 150, 150)),
		[LED_PEER_STATE_PEER_SEARCH]    = LED_EFFECT_LED_BREATH(500, LED_COLOR(150, 150, 150)),
		[LED_PEER_STATE_CONFIRM_SELECT] = LED_EFFECT_LED_BLINK(50, LED_COLOR(150, 150, 150)),
		[LED_PEER_STATE_CONFIRM_ERASE]  = LED_EFFECT_LED_BLINK(25, LED_COLOR(150, 150, 150)),
		[LED_PEER_STATE_ERASE_ADV]	= LED_EFFECT_LED_BREATH(100, LED_COLOR(150, 150, 150)),
	},
};

  • What is your end goal? Is it to run the nrf_desktop application with nRF52840_pca20041 (for a gaming mouse)? Are you planning on making a gaming mouse? If this is the case, please tell me and I will assist you.

    Is your goal to just blink a specific LED? Then I would not recommend you to start with the nrf_desktop example, as it is quite complicated and contains much code and many layers of abstraction. Instead, try to run the samples ncs\zephyr\samples\basic\blink_led and ncs\zephyr\samples\basic\blinky. This thread is also worth taking a look at.

    If you're new to NCS, I would recommend you to take a look at the NCS tutorial series.

    Best regards,

    simon

  • Thank for your respone!

    I want to make a keyboard with a touchpad . And the LED as signal to show battery information. Thereforce I want to know  LED_EFFECT_LED_ON() how to relate  a specify LED?

    My ncs version: 1.2.0

    In addition,I want to know what different between nrf52840_pca10056 , nrf52840_pca10059  and  nrf52840_pca20041 file , I  can't distinguish which is mouse and which is keyboard.

  • Warden_cn said:
    I want to make a keyboard with a touchpad . And the LED as signal to show battery information. Thereforce I want to know  LED_EFFECT_LED_ON() how to relate  a specify LED?

    The nRF desktop application uses the Event Manager library, and I would recommend you to read about it in order to understand the application better. Workqueue Threads are also used, and I recommend you to read about that as well.

    I've looked at the nrf_desktop code and think I somewhat understand what is happening:

    • First, you should look at leds_init() inside ncs\nrf\applications\nrf_desktop\src\hw_interface\leds.c, where the pin option of the specific LEDs is set. They depend on the board used and the DTS and configuration definition created (pins used by the PWM's). The size of the leds[] array depends on the configuration CONFIG_DESKTOP_LED_COUNT

    • Inside ncs\nrf\applications\nrf_desktop\src\modules\led_state.c there is an event listener at the bottom: EVENT_LISTENER(MODULE, event_handler);
    • Let's say it receives an event from somewhere else (EVENT_SUBMIT(..)), and the function event_handler() in led_state.c is called
    • This function will then call load_peer_state_led() (defined in led_state.c as well) which will check what the peer_op is equal to
    • Depending on the peer operation (peer_op), it will set the state accordingly, e.g. if the operation is 
      PEER_OPERATION_ERASE, it will set the state to LED_PEER_STATE_CONFIRM_ERASE
      • If you look inside ncs\nrf\applications\nrf_desktop\configuration\nrf52kbd_nrf52832\led_state_def.h, you can see that this event is associated with the led effect LED_EFFECT_LED_BLINK().
    • Eventually, at the bottom of load_peer_state_led(), it will set the led_id of the event through
      event->led_id = led_map[LED_ID_PEER_STATE] (=1). Which decides which pin that will be used, then it will submit the event through EVENT_SUBMIT(event)
    • This event will then be received by event_handler() in ncs\nrf\applications\nrf_desktop\src\hw_interface\leds.c
      • It is registered as a listener at the bottom of the file through EVENT_LISTENER(MODULE, event_handler)
    • The handler event_handler() will then get the specific led from leds[], through struct led *led = &leds[event->led_id], and as described above, the pin used for this led is set by leds_init() inside ncs\nrf\applications\nrf_desktop\src\hw_interface\leds.c
    • Then led_update() will be called, which submits the event through k_delayed_work_submit()
    • In leds.c the function work_handler() created through k_delayed_work_init(&leds[i].work, work_handler) will then get called and blink the specific LED accordig to LED_EFFECT_LED_BLINK(). However this could just as well be LED_EFFECT_LED_ON() in another case

    If you want to blink an LED to show battery information, you can accomplish this by creating an LED event and simply submit it through EVENT_SUBMIT(). You can also create a new state in ncs\nrf\applications\nrf_desktop\configuration\common\led_state.h and a new custom led effect in ncs\nrf\applications\nrf_desktop\configuration\common\led_effect.h, then you can associate that state with the led effect in ncs\nrf\applications\nrf_desktop\configuration\nrf52kbd_nrf52832\led_state_def.h.

    Warden_cn said:
    In addition,I want to know what different between nrf52840_pca10056 , nrf52840_pca10059  and  nrf52840_pca20041 file , I  can't distinguish which is mouse and which is keyboard.

    Check out the master branch or the release candidate of NCS v1.3.0 and look at ncs\nrf\boards\arm, where the board names are changed, which gives a more descriptive understanding. E.g. the keyboard board name is changed to nrf52kbd_nrf52832.

    associate that state 

    Best regards,

    Simon

Related