Toggle LED with GPIOTE interrupt

Hi, I am using Mesh v4.2.0 and nRF52840 light_lightness example but instead of button_event_handler, I am using GPIOTE to control the lights.

I want to toggle one LED with one button, say when I press button 1, LED 1 will turn on, and when I press button 1 again, LED 1 will turn off. How do I do that? 

This is a part of my code:

//Button 1 Interrupt Handler
void button1_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action)
{
        nrf_drv_gpiote_out_toggle(BSP_LED_1);
}

int main(void)
{
    /*Setup GPIOTE*/

    //Call nrf_drv_gpiote_init() to initialize the GPIOTE.
    nrf_drv_gpiote_init();

    //Create a variable of type nrf_drv_gpiote_out_config_t, and-
    //configure a pin to use the GPIOTE TASK_OUT to to toggle the pin state.
    nrf_drv_gpiote_out_config_t out_config = GPIOTE_CONFIG_OUT_TASK_TOGGLE(false);

    //Call nrf_drv_gpiote_out_init(output pin) to initialize pin13 to be controlled using GPIOTE
    nrf_drv_gpiote_out_init(BSP_LED_1, &out_config); //BSP_LED_0 == LED1 == pin13 defined in pca10056.h
    
    //Set a GPIOTE output pin (Turn Off LED1 - Active Low)
    nrf_drv_gpiote_out_set(BSP_LED_1);

    //Create a variable of type nrf_drv_gpiote_in_config_t, and-
    //configure a pin to use GPIOTE EVENT_IN to detect any change on the pin. 
    nrf_drv_gpiote_in_config_t in_config = GPIOTE_CONFIG_IN_SENSE_TOGGLE(true);
    in_config.pull = NRF_GPIO_PIN_PULLUP; //configure pull up resister. 
    
    nrf_drv_gpiote_in_init(BUTTON_1, &in_config, button1_handler);
    nrf_drv_gpiote_in_init(BUTTON_2, &in_config, button2_handler);
    nrf_drv_gpiote_in_init(BUTTON_3, &in_config, button3_handler);

    //Enable events and its interrupts. 
    nrf_drv_gpiote_in_event_enable(BUTTON_1, true);
    nrf_drv_gpiote_in_event_enable(BUTTON_2, true);
    nrf_drv_gpiote_in_event_enable(BUTTON_3, true);

    initialize();
    start();

    for (;;)
    {
        (void)sd_app_evt_wait();
    }
}

*Don't mind button2 and button3.

Parents Reply
  • Like I mentioned in my previuos reply, you can see how this is done in the Light switch server example. You don't have to add all the files, just include the seimple_hal module to handle the buttons. Like Hung mentions in the post I referred to:

    "You need to include simple_hal.c to your project just like in the light switch example. 
    Then you need to call ERROR_CHECK(hal_buttons_init(button_event_handler)); inside intialize() function. "

Children
Related