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

Controlling multiple buttons and LEDs from Central or Peripheral

I have done the ble_app_blinky example, for both central and peripheral. I press button at central and LED glow at peripheral and vice-versa.

I am struggling in using the code for multiple buttons and multiple LED's. Like Button1, button2, button3 at central to control LED1,LED2,LED3 of peripheral respectively and vice-versa.

//------------------------

static void buttons_init(void)
{
    uint32_t err_code;

    //The array must be static because a pointer to it will be saved in the button handler module.
    static app_button_cfg_t buttons[3] =
    {
        {LEDBUTTON_BUTTON_PIN_1, false, BUTTON_PULL, button_event_handler},

        {LEDBUTTON_BUTTON_PIN_2, false, BUTTON_PULL, button_event_handler},

        {LEDBUTTON_BUTTON_PIN_3, false, BUTTON_PULL, button_event_handler}
    };

    err_code = app_button_init(buttons, sizeof(buttons) / sizeof(buttons[0]),
                               BUTTON_DETECTION_DELAY);
    APP_ERROR_CHECK(err_code);
}

If I do this for 3 buttons to transmit status, I think it will transmit the status of three buttons one by one. But then how am I going to differentiate at the receiver end which data for which LED. Also, do I need to make changes in the LED and Button services.

Following that code I go down the rabbit hole to board.c, bsp.c, pca10040.h, ble_lbs.c, ble_lbs.h etc. but didn't understood what changes to do.

What I actually want is a BCD counter, if I press a button at tx, corresponding led should glow at rx,

Parents
  • Hi shubham,

    The button status is transferred between devices to control the led state. You can use it to represent multiple buttons status, for example, 0bxxx can be used to refer to three buttons' status.

    Based on this simple definition, you can modify ble_app_blinky_c as:

    ...
    #define LEDBUTTON_LED_1                  BSP_BOARD_LED_2                         /**< LED to be toggled with the help of the LED Button Service. */
    #define LEDBUTTON_LED_2                  BSP_BOARD_LED_3                         /**< LED to be toggled with the help of the LED Button Service. */
    #define LEDBUTTON_LED_3                  BSP_BOARD_LED_0                         /**< LED to be toggled with the help of the LED Button Service. */
    #define LEDBUTTON_BUTTON_1                BSP_BUTTON_0                            /**< Button that will trigger the notification event with the LED Button Service */
    #define LEDBUTTON_BUTTON_2                BSP_BUTTON_1                            /**< Button that will trigger the notification event with the LED Button Service */
    #define LEDBUTTON_BUTTON_3                BSP_BUTTON_2                            /**< Button that will trigger the notification event with the LED Button Service */
    ...
    
           case BLE_LBS_C_EVT_BUTTON_NOTIFICATION:
            {
                NRF_LOG_INFO("Button state changed on peer to 0x%x.", p_lbs_c_evt->params.button.button_state);
                //if (p_lbs_c_evt->params.button.button_state)
                //{
                //    bsp_board_led_on(LEDBUTTON_LED);
                //}
                //else
                //{
                //    bsp_board_led_off(LEDBUTTON_LED);
                //}
                uint8_t led_state = p_lbs_c_evt->params.button.button_state;
                if ((led_state&0b001)==1)
                {
                    bsp_board_led_on(LEDBUTTON_LED_1);
                    NRF_LOG_INFO("Received LED ON!");
                }
                else
                {
                    bsp_board_led_off(LEDBUTTON_LED_1);
                    NRF_LOG_INFO("Received LED OFF!");
                }
    
                if (((led_state>>1)&0b001)==1)
                {
                    bsp_board_led_on(LEDBUTTON_LED_2);
                    NRF_LOG_INFO("Received LED ON!");
                }
                else
                {
                    bsp_board_led_off(LEDBUTTON_LED_2);
                    NRF_LOG_INFO("Received LED OFF!");
                }
    
                if (((led_state>>2)&0b001)==1)
                {
                    bsp_board_led_on(LEDBUTTON_LED_3);
                    NRF_LOG_INFO("Received LED ON!");
                }
                else
                {
                    bsp_board_led_off(LEDBUTTON_LED_3);
                    NRF_LOG_INFO("Received LED OFF!");
                }
    
            } break; // BLE_LBS_C_EVT_BUTTON_NOTIFICATION
            
    ...
    
    static void button_event_handler(uint8_t pin_no, uint8_t button_action)
    {
        ret_code_t err_code;
        static uint8_t p_button_status=0b000;
        uint8_t c_button_status;
        switch (pin_no)
        {
            case LEDBUTTON_BUTTON_1:
                NRF_LOG_INFO("Send button state change.");
                c_button_status = (p_button_status|0b001)&(button_action|0b110);
                break;
            case LEDBUTTON_BUTTON_2:
                NRF_LOG_INFO("Send button state change.");
                c_button_status = (p_button_status|0b010)&((button_action<<1)|0b101);
                break;
            case LEDBUTTON_BUTTON_3:
                NRF_LOG_INFO("Send button state change.");
                c_button_status = (p_button_status|0b100)&((button_action<<2)|0b011);
                break;
            default:
                APP_ERROR_HANDLER(pin_no);
                break;
        }
    
        err_code =  ble_lbs_led_status_send(&m_ble_lbs_c,  c_button_status);
        if (err_code != NRF_SUCCESS &&
            err_code != BLE_ERROR_INVALID_CONN_HANDLE &&
            err_code != NRF_ERROR_INVALID_STATE)
        {
            APP_ERROR_CHECK(err_code);
        }
        if (err_code == NRF_SUCCESS)
        {
            NRF_LOG_INFO("LBS write LED state %d", button_action);
        }
        p_button_status=c_button_status;
    }

    ble_app_blinky as:

    ...
    #define LEDBUTTON_LED_1                  BSP_BOARD_LED_2                         /**< LED to be toggled with the help of the LED Button Service. */
    #define LEDBUTTON_LED_2                  BSP_BOARD_LED_3                         /**< LED to be toggled with the help of the LED Button Service. */
    #define LEDBUTTON_LED_3                  BSP_BOARD_LED_0                         /**< LED to be toggled with the help of the LED Button Service. */
    #define LEDBUTTON_BUTTON_1                BSP_BUTTON_0                            /**< Button that will trigger the notification event with the LED Button Service */
    #define LEDBUTTON_BUTTON_2                BSP_BUTTON_1                            /**< Button that will trigger the notification event with the LED Button Service */
    #define LEDBUTTON_BUTTON_3                BSP_BUTTON_2                            /**< Button that will trigger the notification event with the LED Button Service */
    ...
    static void led_write_handler(uint16_t conn_handle, ble_lbs_t * p_lbs, uint8_t led_state)
    {   
       
        if ((led_state&0b001)==1)
        {
            bsp_board_led_on(LEDBUTTON_LED_1);
            NRF_LOG_INFO("Received LED ON!");
        }
        else
        {
            bsp_board_led_off(LEDBUTTON_LED_1);
            NRF_LOG_INFO("Received LED OFF!");
        }
    
        if (((led_state>>1)&0b001)==1)
        {
            bsp_board_led_on(LEDBUTTON_LED_2);
            NRF_LOG_INFO("Received LED ON!");
        }
        else
        {
            bsp_board_led_off(LEDBUTTON_LED_2);
            NRF_LOG_INFO("Received LED OFF!");
        }
    
        if (((led_state>>2)&0b001)==1)
        {
            bsp_board_led_on(LEDBUTTON_LED_3);
            NRF_LOG_INFO("Received LED ON!");
        }
        else
        {
            bsp_board_led_off(LEDBUTTON_LED_3);
            NRF_LOG_INFO("Received LED OFF!");
        }
    }
    ...
    static void button_event_handler(uint8_t pin_no, uint8_t button_action)
    {
        ret_code_t err_code;
        static uint8_t p_button_status=0b000;
        uint8_t c_button_status;
        switch (pin_no)
        {
            case LEDBUTTON_BUTTON_1:
                NRF_LOG_INFO("Send button state change.");
                c_button_status = (p_button_status|0b001)&(button_action|0b110);
                break;
            case LEDBUTTON_BUTTON_2:
                NRF_LOG_INFO("Send button state change.");
                c_button_status = (p_button_status|0b010)&((button_action<<1)|0b101);
                break;
            case LEDBUTTON_BUTTON_3:
                NRF_LOG_INFO("Send button state change.");
                c_button_status = (p_button_status|0b100)&((button_action<<2)|0b011);
                break;
            default:
                APP_ERROR_HANDLER(pin_no);
                break;
        }
    
        err_code = ble_lbs_on_button_change(m_conn_handle, &m_lbs, c_button_status);
        if (err_code != NRF_SUCCESS &&
            err_code != BLE_ERROR_INVALID_CONN_HANDLE &&
            err_code != NRF_ERROR_INVALID_STATE &&
            err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
        {
            APP_ERROR_CHECK(err_code);
        }
        p_button_status=c_button_status;
    }

Reply
  • Hi shubham,

    The button status is transferred between devices to control the led state. You can use it to represent multiple buttons status, for example, 0bxxx can be used to refer to three buttons' status.

    Based on this simple definition, you can modify ble_app_blinky_c as:

    ...
    #define LEDBUTTON_LED_1                  BSP_BOARD_LED_2                         /**< LED to be toggled with the help of the LED Button Service. */
    #define LEDBUTTON_LED_2                  BSP_BOARD_LED_3                         /**< LED to be toggled with the help of the LED Button Service. */
    #define LEDBUTTON_LED_3                  BSP_BOARD_LED_0                         /**< LED to be toggled with the help of the LED Button Service. */
    #define LEDBUTTON_BUTTON_1                BSP_BUTTON_0                            /**< Button that will trigger the notification event with the LED Button Service */
    #define LEDBUTTON_BUTTON_2                BSP_BUTTON_1                            /**< Button that will trigger the notification event with the LED Button Service */
    #define LEDBUTTON_BUTTON_3                BSP_BUTTON_2                            /**< Button that will trigger the notification event with the LED Button Service */
    ...
    
           case BLE_LBS_C_EVT_BUTTON_NOTIFICATION:
            {
                NRF_LOG_INFO("Button state changed on peer to 0x%x.", p_lbs_c_evt->params.button.button_state);
                //if (p_lbs_c_evt->params.button.button_state)
                //{
                //    bsp_board_led_on(LEDBUTTON_LED);
                //}
                //else
                //{
                //    bsp_board_led_off(LEDBUTTON_LED);
                //}
                uint8_t led_state = p_lbs_c_evt->params.button.button_state;
                if ((led_state&0b001)==1)
                {
                    bsp_board_led_on(LEDBUTTON_LED_1);
                    NRF_LOG_INFO("Received LED ON!");
                }
                else
                {
                    bsp_board_led_off(LEDBUTTON_LED_1);
                    NRF_LOG_INFO("Received LED OFF!");
                }
    
                if (((led_state>>1)&0b001)==1)
                {
                    bsp_board_led_on(LEDBUTTON_LED_2);
                    NRF_LOG_INFO("Received LED ON!");
                }
                else
                {
                    bsp_board_led_off(LEDBUTTON_LED_2);
                    NRF_LOG_INFO("Received LED OFF!");
                }
    
                if (((led_state>>2)&0b001)==1)
                {
                    bsp_board_led_on(LEDBUTTON_LED_3);
                    NRF_LOG_INFO("Received LED ON!");
                }
                else
                {
                    bsp_board_led_off(LEDBUTTON_LED_3);
                    NRF_LOG_INFO("Received LED OFF!");
                }
    
            } break; // BLE_LBS_C_EVT_BUTTON_NOTIFICATION
            
    ...
    
    static void button_event_handler(uint8_t pin_no, uint8_t button_action)
    {
        ret_code_t err_code;
        static uint8_t p_button_status=0b000;
        uint8_t c_button_status;
        switch (pin_no)
        {
            case LEDBUTTON_BUTTON_1:
                NRF_LOG_INFO("Send button state change.");
                c_button_status = (p_button_status|0b001)&(button_action|0b110);
                break;
            case LEDBUTTON_BUTTON_2:
                NRF_LOG_INFO("Send button state change.");
                c_button_status = (p_button_status|0b010)&((button_action<<1)|0b101);
                break;
            case LEDBUTTON_BUTTON_3:
                NRF_LOG_INFO("Send button state change.");
                c_button_status = (p_button_status|0b100)&((button_action<<2)|0b011);
                break;
            default:
                APP_ERROR_HANDLER(pin_no);
                break;
        }
    
        err_code =  ble_lbs_led_status_send(&m_ble_lbs_c,  c_button_status);
        if (err_code != NRF_SUCCESS &&
            err_code != BLE_ERROR_INVALID_CONN_HANDLE &&
            err_code != NRF_ERROR_INVALID_STATE)
        {
            APP_ERROR_CHECK(err_code);
        }
        if (err_code == NRF_SUCCESS)
        {
            NRF_LOG_INFO("LBS write LED state %d", button_action);
        }
        p_button_status=c_button_status;
    }

    ble_app_blinky as:

    ...
    #define LEDBUTTON_LED_1                  BSP_BOARD_LED_2                         /**< LED to be toggled with the help of the LED Button Service. */
    #define LEDBUTTON_LED_2                  BSP_BOARD_LED_3                         /**< LED to be toggled with the help of the LED Button Service. */
    #define LEDBUTTON_LED_3                  BSP_BOARD_LED_0                         /**< LED to be toggled with the help of the LED Button Service. */
    #define LEDBUTTON_BUTTON_1                BSP_BUTTON_0                            /**< Button that will trigger the notification event with the LED Button Service */
    #define LEDBUTTON_BUTTON_2                BSP_BUTTON_1                            /**< Button that will trigger the notification event with the LED Button Service */
    #define LEDBUTTON_BUTTON_3                BSP_BUTTON_2                            /**< Button that will trigger the notification event with the LED Button Service */
    ...
    static void led_write_handler(uint16_t conn_handle, ble_lbs_t * p_lbs, uint8_t led_state)
    {   
       
        if ((led_state&0b001)==1)
        {
            bsp_board_led_on(LEDBUTTON_LED_1);
            NRF_LOG_INFO("Received LED ON!");
        }
        else
        {
            bsp_board_led_off(LEDBUTTON_LED_1);
            NRF_LOG_INFO("Received LED OFF!");
        }
    
        if (((led_state>>1)&0b001)==1)
        {
            bsp_board_led_on(LEDBUTTON_LED_2);
            NRF_LOG_INFO("Received LED ON!");
        }
        else
        {
            bsp_board_led_off(LEDBUTTON_LED_2);
            NRF_LOG_INFO("Received LED OFF!");
        }
    
        if (((led_state>>2)&0b001)==1)
        {
            bsp_board_led_on(LEDBUTTON_LED_3);
            NRF_LOG_INFO("Received LED ON!");
        }
        else
        {
            bsp_board_led_off(LEDBUTTON_LED_3);
            NRF_LOG_INFO("Received LED OFF!");
        }
    }
    ...
    static void button_event_handler(uint8_t pin_no, uint8_t button_action)
    {
        ret_code_t err_code;
        static uint8_t p_button_status=0b000;
        uint8_t c_button_status;
        switch (pin_no)
        {
            case LEDBUTTON_BUTTON_1:
                NRF_LOG_INFO("Send button state change.");
                c_button_status = (p_button_status|0b001)&(button_action|0b110);
                break;
            case LEDBUTTON_BUTTON_2:
                NRF_LOG_INFO("Send button state change.");
                c_button_status = (p_button_status|0b010)&((button_action<<1)|0b101);
                break;
            case LEDBUTTON_BUTTON_3:
                NRF_LOG_INFO("Send button state change.");
                c_button_status = (p_button_status|0b100)&((button_action<<2)|0b011);
                break;
            default:
                APP_ERROR_HANDLER(pin_no);
                break;
        }
    
        err_code = ble_lbs_on_button_change(m_conn_handle, &m_lbs, c_button_status);
        if (err_code != NRF_SUCCESS &&
            err_code != BLE_ERROR_INVALID_CONN_HANDLE &&
            err_code != NRF_ERROR_INVALID_STATE &&
            err_code != BLE_ERROR_GATTS_SYS_ATTR_MISSING)
        {
            APP_ERROR_CHECK(err_code);
        }
        p_button_status=c_button_status;
    }

Children
No Data
Related