Access the led_state value in the blinky app example, for using it in the main function !

hi ,

new to Nordic developmen. struggling to access the characteristic data to be able to use it in the main function !

i have started with the blinky app example as it covers my needs for the ble setting (need one service + 2 characteristics ) . for my project i need to use the characteristic data (the  led_state  in the example ) in my main function .

but the led_state data seems to be not accessible at this function !  have tried the memcpy() , can't copy it .

please kindly guide .

static void led_write_handler(uint16_t conn_handle, ble_lbs_t * p_lbs, uint8_t led_state)
{
          
if(led_state){
        bsp_board_led_on(BSP_BOARD_LED_0);
        NRF_LOG_INFO("Received LED ON!");
    }
    else
    {
        bsp_board_led_off(BSP_BOARD_LED_0);
        NRF_LOG_INFO("Received LED OFF!");
    }

}
  • Hi,

    "led_state" is a variable that only exists inside led_write_handler() and you cannot use it outside of this function.

    What you can do is to create a static variable outside of the function to store the LED status information and then use this variable in main().

    For example:

    static uint8_t m_led_state; 
    
    static void led_write_handler(uint16_t conn_handle, ble_lbs_t * p_lbs, uint8_t led_state)
    {
        m_led_state = led_state;
        if (led_state)
        {
            bsp_board_led_on(LEDBUTTON_LED);
            NRF_LOG_INFO("Received LED ON!");
        }
        else
        {
            bsp_board_led_off(LEDBUTTON_LED);
            NRF_LOG_INFO("Received LED OFF!");
        } 

  • hi,

    thanks for your reply !

    i have merged the pwm driver example with the blinky example to be able to control the PWM duty cycle via an application on my phone . 

    i can control the pwm's duty cycle from the app , but the control seems to be dellayed ! when the ducty cycle goes up , it is kind on working , but when it goes down the control seems to take lot of time .

    i use a slider in the app for controlling the duty cycle , the app will keep writing new values to the led characteristic when ever the slider value updated .

    this is the pwm example i used . so basicaly i just copy the led_sate variable from the led_write_handler() an passe it to the pwm_update_duty_cycle() function . 

    i have enabled the required settings in the sdk_config file for the PWM , it is working fine . the example work as expected !

    the issue where just when we add the ble control .

    #include <stdio.h>
    #include <string.h>
    #include "nrf_drv_pwm.h"
    #include "app_util_platform.h"
    #include "app_error.h"
    #include "boards.h"
    #include "bsp.h"
    #include "nrf_drv_clock.h"
    #include "nrf_delay.h"
    
    
    #define OUTPUT_PIN LED_1
    
    static nrf_drv_pwm_t m_pwm0 = NRF_DRV_PWM_INSTANCE(0);
    
    // Declare variables holding PWM sequence values. In this example only one channel is used 
    nrf_pwm_values_individual_t seq_values[] = {0, 0, 0, 0};
    nrf_pwm_sequence_t const seq =
    {
        .values.p_individual = seq_values,
        .length          = NRF_PWM_VALUES_LENGTH(seq_values),
        .repeats         = 0,
        .end_delay       = 0
    };
    
    
    // Set duty cycle between 0 and 100%
    void pwm_update_duty_cycle(uint8_t duty_cycle)
    {
        
        // Check if value is outside of range. If so, set to 100%
        if(duty_cycle >= 100)
        {
            seq_values->channel_0 = 100;
        }
        else
        {
            seq_values->channel_0 = duty_cycle;
        }
        
        nrf_drv_pwm_simple_playback(&m_pwm0, &seq, 1, NRF_DRV_PWM_FLAG_LOOP);
    }
    
    static void pwm_init(void)
    {
        nrf_drv_pwm_config_t const config0 =
        {
            .output_pins =
            {
                OUTPUT_PIN, // channel 0
                NRF_DRV_PWM_PIN_NOT_USED,             // channel 1
                NRF_DRV_PWM_PIN_NOT_USED,             // channel 2
                NRF_DRV_PWM_PIN_NOT_USED,             // channel 3
            },
            .irq_priority = APP_IRQ_PRIORITY_LOWEST,
            .base_clock   = NRF_PWM_CLK_1MHz,
            .count_mode   = NRF_PWM_MODE_UP,
            .top_value    = 100,
            .load_mode    = NRF_PWM_LOAD_INDIVIDUAL,
            .step_mode    = NRF_PWM_STEP_AUTO
        };
        // Init PWM without error handler
        APP_ERROR_CHECK(nrf_drv_pwm_init(&m_pwm0, &config0, NULL));
        
    }
    
    
    int main(void)
    {
    
        // Start clock for accurate frequencies
        NRF_CLOCK->TASKS_HFCLKSTART = 1; 
        // Wait for clock to start
        while(NRF_CLOCK->EVENTS_HFCLKSTARTED == 0) 
            ;
        
        pwm_init();
    
        for (;;)
        {
            for(int i = 0; i <= 100; i++)
            {
                pwm_update_duty_cycle(i);
            }
        }
    }