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

A switch instead of a button

we use peripheral: exemple ble_app_blinky central: exemple_ble_app_bliky-c

Instead of a button we have a Switch. When we set the Switch to "on" the led go on. That's ok. But when we let the position of the Switch "on" and then reseat the bords, the led don't go on.

What do we have to change on the code?

Thank you very much for the help!

  • Hello mc

    The ble_app_blinky example uses the app_buttons module, which configures the pin for GPIOTE port events, see page 70 of the nRF51 series reference manual. Port events triggers on the rising edge of the GPIO DETECT signal, see page 56 of the reference manual. When the button is pressed a GPIOTE event is triggered, and it ends up at the app_buttons module. The app_buttons module compares the current pin state with the pins active state (in the example active state is set to 0). If the pin is high a 0 is transmitted, if the pin is low a 1 is transmitted. When you flip the switch and reset the boards there isn't a rising transition of the DETECT signal after the GPIOTE has been configured, and the port event is never triggered. To get the initial state of the led correct you would need to do a pin read on startup, and then transmit the initial state state.

    ==EDIT==

    I made a quick fix. It's not optimal, but it will update the initial value of the button when the client enables notifications. I modified the ble_lbs.c file for the peripheral in the following way

    I added

    #include "boards.h" 
    

    to the top of the file, I added the function

    void init_state_update(ble_lbs_t * p_lbs)
    {
    
                ret_code_t err_code;
                ble_gatts_hvx_params_t params;
                uint8_t button_state = 0;
                if(nrf_gpio_pin_read(BSP_BUTTON_0)==0)
                {
                button_state=1;
                }
                uint16_t len = sizeof(button_state);
            
                memset(&params, 0, sizeof(params));
                params.type = BLE_GATT_HVX_NOTIFICATION;
                params.handle = p_lbs->button_char_handles.value_handle;
                params.p_data = &button_state;
                params.p_len = &len;
                err_code = sd_ble_gatts_hvx(p_lbs->conn_handle, &params);
                nrf_gpio_pin_read(BSP_BOARD_LED_3);
                APP_ERROR_CHECK(err_code);
    
    }
    

    close to the top of the file, and I modified the on_write function

    /**@brief Function for handling the Write event.
     *
     * @param[in] p_lbs      LED Button Service structure.
     * @param[in] p_ble_evt  Event received from the BLE stack.
     */
    static void on_write(ble_lbs_t * p_lbs, ble_evt_t * p_ble_evt)
    {
        ble_gatts_evt_write_t * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
    
        if ((p_evt_write->handle == p_lbs->led_char_handles.value_handle) &&
            (p_evt_write->len == 1) &&
            (p_lbs->led_write_handler != NULL))
        {
            p_lbs->led_write_handler(p_lbs, p_evt_write->data[0]);
        }
    		
        else if (p_evt_write->handle == p_lbs->button_char_handles.cccd_handle)
        {
            if(p_ble_evt->evt.gatts_evt.params.write.len==2)
            {
    					init_state_update(p_lbs);
            }
        }    
    }
    

    This checks if the write is a cccd change, and sends a notification of the initial button value. DO NOTE the button value updated here is entered separately from the one defined in main.c, so if you change the pin you will need to change it both in main.c and in ble_lbs.c. You could move the pin defines to a common header file to avoid this.

    Best regards

    Jørn Frøysa

  • Hello Jorn Froysa

    Thank you very much for the answer. Since we are new not so good in programing, its possible that you please can send us the code and where we must write it in the "peripheral": exemple ble_app_blinky / and in the "central":exemple_ble_app_bliky-c. We would also pay for your work.

    Best regards MC

  • Hello John Froysa Super it works fine!! Thank you very much! As said we would pay your work. How can we do it. It is possible to ask you directly for further questions?

  • Happy to help! I appreciate the offer, but there is not need for payment. Nordic Semiconductor already pays me :) We prefer that our customers use our official support channels for the best efficiency so feel free to ask additional questions here on DevZone, or if your questions are confidential you can make a MyPage case over at nordicsemi.com.

Related