Start stop button ?

Dear Members,

How can I make a start/stop button with 1 button ?

Here's my handler :

void bsp_event_handler(bsp_event_t event)
{
    switch (event)
    {
        case BSP_EVENT_SLEEP:
            nrf_pwr_mgmt_shutdown(NRF_PWR_MGMT_SHUTDOWN_GOTO_SYSOFF);
            break;
				
				 case BSP_EVENT_KEY_0:
					 NRF_LOG_INFO("BSP_EVENT_KEY 0 PUSH ON\n");
            if (actual_state != BSP_INDICATE_FIRST)
                actual_state--;
						    //NRF_LOG_INFO("BSP_EVENT_KEY 0 PUSH ON\n"); //2Mar22 Rixtronix LAB
            else
                actual_state = BSP_INDICATE_LAST;
						    NRF_LOG_INFO("BSP_EVENT_KEY 0 PUSH OFF\n"); //2Mar22 Rixtronix LAB
            break;
            ............

Any clues ?

Kind regards,

Rixtronix LAB

Parents
  • Hi Karl,

    How can I make power on and off in 1 button ?

    Regards,

    Rixtronix

  • Hi Karl,

    from ble_peripheral directory examples above,

    which example is using button with SYSTEM_OFF,

    and which example is using "button long push" for event ?

    Best regards,

    Rixtronix

  • Hello,

    I am not sure that anyone of them uses a button to initiate SYSTEM_OFF, not long button push (because they all are using the btn_ble_bsp variant), but you can easily assign a new event to button action using the bsp function I mentioned earlier, and then initiate SYSTEM_OFF in the same manner as demonstrated in the sleep_mode_enter function.

    Best regards,
    Karl

  • Hi Karl,

    Thanks for the reply,

    I have :

    /*************Rixtronix LAB 17 Mar 22*/

    err_code = bsp_event_to_button_action_assign(BSP_BUTTON_3,
                                                                BSP_BUTTON_ACTION_LONG_PUSH,
                                                                BSP_EVENT_KEY_3);

    and :

     case BSP_EVENT_KEY_3:
                //NRF_LOG_INFO("BSP_EVENT_KEY 3 PUSH OFF\n");
                            
                                                                   
                         if(button3_state)
                                            {  
                                                            
                                                                        
                                                    //ON_function();
                                                  NRF_LOG_INFO("BSP_EVENT_KEY 3 PUSH ON==> LONG PUSH >200ms\n");
                                                 //beep_v11();
                                                
                                                    button3_state = STATE_OFF;
                                            }
                                            else
                                            {
                                                    //OFF_function();
                                                  NRF_LOG_INFO("BSP_EVENT_KEY 3 PUSH OFF\n");
                                                  //beep_v12();
                                                
                                                    button3_state = STATE_ON;
                                            }
                                                         
                if (actual_state != BSP_INDICATE_LAST)
                    actual_state++;
                else
                    actual_state = BSP_INDICATE_FIRST;
                break;       

    on bsp.h

    #define BSP_BUTTON_ACTION_LONG_PUSH (2)                  /**< Represents pushing and holding a button for @ref BSP_LONG_PUSH_TIMEOUT_MS milliseconds. See also @ref bsp_button_action_t. */

    but it's not responding to 2 seconds long push,

    What am I missing here ?

    Regards,

    Rixtronix

  • Hello Rixtronix,

    I see from your logging that it refers to 200 ms, while it sounds like you intend for long push to be 2000 ms. What is your configuration of BSP_LONG_PUSH_TIMEOUT_MS?
    Are you getting any other event in place of the long push event, or is the bsp module otherwise functioning as expected?

    For future reference, please use the Insert -> Code option when sharing code here on DevZone.

    Best regards,
    Karl

  • Hi Karl,

    Thanks for the reply,

    #define BSP_LONG_PUSH_TIMEOUT_MS (1000) /**< The time to hold for a long push (in milliseconds). */

    Here's the complete event :

    void bsp_event_handler(bsp_event_t event)
    {
      ret_code_t err_code;
    	int32_t cnt;
        switch (event)
        {
            case BSP_EVENT_SLEEP:
                nrf_pwr_mgmt_shutdown(NRF_PWR_MGMT_SHUTDOWN_GOTO_SYSOFF);
                break;
    				
    				 case BSP_EVENT_KEY_0:
    					 /************START STOP BUTTON 3Mar22 Rixtronix LAB**************/
    					 
    									//NRF_LOG_INFO("BSP_EVENT_KEY 0 PUSH START\n");
    						
    								   if(button0_state)
    										{
    												//START_function();
    											  //NRF_LOG_INFO("BSP_EVENT_KEY 0 PUSH START\n");
    											
    											 NRF_LOG_INFO("BSP_EVENT_KEY 0 PUSH STOP\n");
    											  //beep_v9();
    											  app_timer_stop(m_repeated_timer_id);
    												button0_state = STATE_OFF;  
    											  
    										}
    										else
    										{
    												//STOP_function();
    											  NRF_LOG_INFO("BSP_EVENT_KEY 0 PUSH START\n");
    											  //NRF_LOG_INFO("BSP_EVENT_KEY 0 PUSH STOP\n");
    											  //beep_v10();
    											   app_timer_start(m_repeated_timer_id, APP_TIMER_TICKS(1000), NULL);  //3Mar22 Time of Ride Rixtronix LAB
    												button0_state = STATE_ON;
    											
    										}
    						
    				  /************START STOP BUTTON 3Mar22 Rixtronix LAB**************/
    					 
                if (actual_state != BSP_INDICATE_FIRST)
    						   
                    actual_state--;
    						   
                else
    						
                    actual_state = BSP_INDICATE_LAST;
    						
                break;
    
            case BSP_EVENT_KEY_1:
                NRF_LOG_INFO("BSP_EVENT_KEY 1 PUSH STOP\n");
    				    
                if (actual_state != BSP_INDICATE_LAST)
                    actual_state++;
                else
                    actual_state = BSP_INDICATE_FIRST;
                break;
    						
    				 case BSP_EVENT_KEY_2:
    					 NRF_LOG_INFO("BSP_EVENT_KEY 2 PUSH ON\n");
    				   if(button2_state)
    										{  
    																					cnt++;
    													 if ( cnt >= LONG_PRESS(10000))
    													 {
    															 cnt = 0;
    															 NRF_LOG_INFO("Long Button press");
    													 }		
    																				
    												//ON_function();
    											  NRF_LOG_INFO("BSP_EVENT_KEY 2 PUSH ON==> LONG PUSH >200ms\n");
    											 //beep_v11();
    											
    												button2_state = STATE_OFF;
    										}
    										else
    										{
    												//OFF_function();
    											   cnt = 0; // reset counter variable
    											  NRF_LOG_INFO("BSP_EVENT_KEY 2 PUSH OFF\n");
    											  //beep_v12();
    											 
    												button2_state = STATE_ON;
    										}
                if (actual_state != BSP_INDICATE_FIRST)
                    actual_state--;
                else
                    actual_state = BSP_INDICATE_LAST;
                break;
    
            case BSP_EVENT_KEY_3:
                //NRF_LOG_INFO("BSP_EVENT_KEY 3 PUSH OFF\n");
    						
               													 
    				     if(button3_state)
    										{  
    														
    																	
    												//ON_function();
    											  NRF_LOG_INFO("BSP_EVENT_KEY 3 PUSH ON==> LONG PUSH >200ms\n");
    											 //beep_v11();
    											
    												button3_state = STATE_OFF;
    										}
    										else
    										{
    												//OFF_function();
    											  NRF_LOG_INFO("BSP_EVENT_KEY 3 PUSH OFF\n");
    											  //beep_v12();
    											 
    												button3_state = STATE_ON;
    										}
    					 								 
                if (actual_state != BSP_INDICATE_LAST)
                    actual_state++;
                else
                    actual_state = BSP_INDICATE_FIRST;
                break;		
    
    
            default:
                break;
        }
    }
    

    I can't get 2000 mS long push, is the timer running ?

    Is it conflicting with BSP ant button ?

    static void utils_setup(void)
    {
        //ret_code_t err_code;
    	  ret_code_t err_code = app_timer_init();
        APP_ERROR_CHECK(err_code);
    
        err_code = bsp_init(BSP_INIT_LEDS | BSP_INIT_BUTTONS,
                            bsp_event_handler);
        APP_ERROR_CHECK(err_code);
    
        err_code = nrf_pwr_mgmt_init();
        APP_ERROR_CHECK(err_code);
    		
    	    
    	
        err_code = bsp_btn_ant_init(m_ant_hrm.channel_number, HRM_DISP_CHANNEL_TYPE);
        APP_ERROR_CHECK(err_code);
    
        //err_code = ant_state_indicator_init(m_ant_hrm.channel_number, HRM_DISP_CHANNEL_TYPE);
        APP_ERROR_CHECK(err_code);
    	
    	  err_code = bsp_btn_ant_init(m_ant_bsc.channel_number, BSC_DISP_CHANNEL_TYPE);
        APP_ERROR_CHECK(err_code);
    		
    		err_code = bsp_btn_ant_init(m_ant_bpwr.channel_number, BPWR_DISP_CHANNEL_TYPE);
        APP_ERROR_CHECK(err_code);
    	
    	  //err_code = ant_state_indicator_init(m_ant_bsc.channel_number, BSC_DISP_CHANNEL_TYPE);
        APP_ERROR_CHECK(err_code);
    		NRF_LOG_INFO("utils_setup\r\n");
    }

    Best regards,

    Rixtronix

  • Read button function :

    void read_button()
    {
    	ret_code_t err_code;
    	  err_code = bsp_event_to_button_action_assign(BSP_BUTTON_0,
                                                                BSP_BUTTON_ACTION_PUSH,
                                                                BSP_EVENT_KEY_0);
    	
    	  
    	
     	  err_code = bsp_event_to_button_action_assign(BSP_BUTTON_1,
                                                                BSP_BUTTON_ACTION_PUSH, 
                                                                BSP_EVENT_KEY_1);//hardware not connected
    	
    	  
    	 
      /************originally***********	
    	 err_code = bsp_event_to_button_action_assign(BSP_BUTTON_2,
                                                                BSP_BUTTON_ACTION_PUSH,
                                                                BSP_EVENT_KEY_2);
    	**************/
    	err_code = bsp_event_to_button_action_assign(BSP_BUTTON_2,
                                                                BSP_BUTTON_ACTION_LONG_PUSH, 
                                                                BSP_EVENT_KEY_2);
    
    	/*************originally button 4
    	err_code = bsp_event_to_button_action_assign(BSP_BUTTON_3,
                                                                BSP_BUTTON_ACTION_PUSH,
                                                                BSP_EVENT_KEY_3);
    																														
      *************Rixtronix LAB 17 Mar 22*/
    	err_code = bsp_event_to_button_action_assign(BSP_BUTTON_3,
                                                                BSP_BUTTON_ACTION_LONG_PUSH, 
                                                                BSP_EVENT_KEY_3);
    
    }	

Reply
  • Read button function :

    void read_button()
    {
    	ret_code_t err_code;
    	  err_code = bsp_event_to_button_action_assign(BSP_BUTTON_0,
                                                                BSP_BUTTON_ACTION_PUSH,
                                                                BSP_EVENT_KEY_0);
    	
    	  
    	
     	  err_code = bsp_event_to_button_action_assign(BSP_BUTTON_1,
                                                                BSP_BUTTON_ACTION_PUSH, 
                                                                BSP_EVENT_KEY_1);//hardware not connected
    	
    	  
    	 
      /************originally***********	
    	 err_code = bsp_event_to_button_action_assign(BSP_BUTTON_2,
                                                                BSP_BUTTON_ACTION_PUSH,
                                                                BSP_EVENT_KEY_2);
    	**************/
    	err_code = bsp_event_to_button_action_assign(BSP_BUTTON_2,
                                                                BSP_BUTTON_ACTION_LONG_PUSH, 
                                                                BSP_EVENT_KEY_2);
    
    	/*************originally button 4
    	err_code = bsp_event_to_button_action_assign(BSP_BUTTON_3,
                                                                BSP_BUTTON_ACTION_PUSH,
                                                                BSP_EVENT_KEY_3);
    																														
      *************Rixtronix LAB 17 Mar 22*/
    	err_code = bsp_event_to_button_action_assign(BSP_BUTTON_3,
                                                                BSP_BUTTON_ACTION_LONG_PUSH, 
                                                                BSP_EVENT_KEY_3);
    
    }	

Children
No Data
Related