This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

nRF51822 s110 custom event handlers

Hi guys and gals!

I recently got Evelution kit nRF51822 and I'm working on custom application with SoftDevice s110 on Peripheral side. I was looking in "nAN-36" documentation file for easy start and I understand it, but I'm having truble with writting custom event handler for my application.

Can you guide me or suggest me, how can I write event handler, which will look for change in value of variable and pass it on, and eventually send it to Central (peer) device. I'm trying to send data from which was gathered through ADC, I2C and OneWire protocol.

I think that best way for doing this is to write in a variable, whose change will be picked up by event handler and report it to Central device.

Thanks ahead for any suggestions and advices! :)

  • Since you already started with the nAN-36 led-button example, you can use the button characteristic to send out the notification.

    In the led-button example the ble_lbs_on_button_change() in ble_lbs.c is called by the button_event_handler() handler in main.c. The button event handler is called whenever a change in the buttons registered in the gpiote_init() function changes.

    Likewise, you can call the ble_lbs_on_button_change() from any other interrupt handlers. If you are going to use the ADC, its interrupt handler is ADC_IRQHandler()

    If you use the led-button example as a starting point, and you add the following code to main.c, it will use a timer to sample the variable value, then send a notification using the ble_lbs_on_button_change().

    If you want to send a value from I2C, the I2C creates an event when there is a new variable in the TXD register. Then you can use this event to call ble_lbs_on_button_change(), instead of the timeout event. Similarly, the ADC has its own event when a value has been sampled.

    #define CUSTOM_TIMER_MEAS_INTERVAL     APP_TIMER_TICKS(2000, APP_TIMER_PRESCALER)
    static app_timer_id_t                  m_custom_timer_id;
    
    // The timeout handler that calls the ble_lbs_on_button_change() function 
    // with an arbitrary value
    static void custom_timer_meas_timeout_handler(void * p_context)
    {
    	ble_lbs_on_value_change(&m_lbs, YOUR_VALUE);
    }
    
    // Initialize the timer to call the timeout handler
    static void timers_init(void)
    {
        APP_TIMER_INIT(APP_TIMER_PRESCALER, APP_TIMER_MAX_TIMERS, APP_TIMER_OP_QUEUE_SIZE, true);
        uint32_t err_code;
    	err_code = app_timer_create(&m_custom_timer_id,
                                    APP_TIMER_MODE_REPEATED,
                                    custom_timer_meas_timeout_handler);
        APP_ERROR_CHECK(err_code);
    }
    
    // Start the timer
    static void timers_start(void)
    {
    	uint32_t err_code;
    	err_code = app_timer_start(m_custom_timer_id, CUSTOM_TIMER_MEAS_INTERVAL, NULL);
        APP_ERROR_CHECK(err_code);
    }
    
  • If I want always to send YOUR_VALUE. Where I have to call ble_lbs_on_button_change()?

Related