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

Different actions when Button press and released

I want to detect the button short press and long press.

When short press is "released", trigger LED mode 1.

When detect 3 sec long press, start edit mode.

It seems GPIOE pins can't detect different states at the same time.

I try to use the interrupt to change the configure of the pin, so that it can detect both HITOLO and LOTOHI.

But it didn't work.

Is there any problem with my code? Or any better way to achieve my needs?

void gpio_init(){
	ret_code_t err_code; //hold error value	
	
	err_code = nrf_drv_gpiote_init(); // Initialize the GPIOTE
	APP_ERROR_CHECK(err_code); // check for the errors
	
	// sence button change
	nrf_drv_gpiote_in_config_t in_config_d = GPIOTE_CONFIG_IN_SENSE_HITOLO(true);
	in_config_d.pull = NRF_GPIO_PIN_PULLUP;
	
		
	// initialize the pin of 4 Buttons down
	err_code = nrf_drv_gpiote_in_init(BUTTON_1, &in_config_d, button_down_handler);
	APP_ERROR_CHECK(err_code);
	

	// Enable the interrupt events
	nrf_drv_gpiote_in_event_enable(BUTTON_1, true); 
	
}

void button_down_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action){
		ret_code_t err_code;
		switch(pin)
		{
				case BUTTON_1:
						nrf_gpio_pin_clear(LED_1);
						err_code = app_timer_start(sec_3_B1_timer_id, APP_TIMER_TICKS(3000), NULL);
						APP_ERROR_CHECK(err_code);
						NRF_LOG_INFO("Button 1 push");
						nrf_drv_gpiote_in_uninit(BUTTON_1);
						nrf_drv_gpiote_in_config_t in_config_d = GPIOTE_CONFIG_IN_SENSE_LOTOHI(true);
						err_code = nrf_drv_gpiote_in_init(BUTTON_1, &in_config_d, button_up_handler);
						APP_ERROR_CHECK(err_code);										
						break;
						
				default:
						break;						
		}			
}

void button_up_handler(nrf_drv_gpiote_pin_t pin, nrf_gpiote_polarity_t action){
		ret_code_t err_code;
		NRF_LOG_INFO("Button up handler");
		switch(pin)
		{
				case BUTTON_1:
					{
						nrf_gpio_pin_set(LED_1); //turn off LEDon interrupt
						err_code = app_timer_stop(sec_3_B1_timer_id);
						NRF_LOG_INFO("Button 1 Timer stop");	
						nrf_drv_gpiote_in_uninit(BUTTON_1);
						nrf_drv_gpiote_in_config_t in_config_d = GPIOTE_CONFIG_IN_SENSE_HITOLO(true);
						err_code = nrf_drv_gpiote_in_init(BUTTON_1, &in_config_d, button_down_handler);
						APP_ERROR_CHECK(err_code);
					}													
					break;
				
				default:
						break;
				
		}
		
}

Related