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

  • Hi Karl,

    Looks like it's not responding to :

    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;
    										}

    How can I relate this code to timer as I can see in :

    https://devzone.nordicsemi.com/f/nordic-q-a/43506/how-to-use-bsp_button_action_push-and-bsp_button_action_long_push-on-the-same-button-with-two-different-functions

    or

    https://devzone.nordicsemi.com/f/nordic-q-a/37842/how-to-use-bsp_event_to_button_action_assign-function-to-assign-long-push-action-to-an-event

    Regards,

    Rixtronix

  • Hi Karl,

    I made ,

    Callback :

    case BSP_EVENT_KEY_2:
    					 NRF_LOG_INFO("BSP_EVENT_KEY 2 PUSH ON\n");
    				   //if(button2_state)
    							//			{  
    																				
    												//ON_function();
    											   err_code = app_timer_start(m_button_action, 
                                         APP_TIMER_TICKS(BUTTON_STATE_POLL_INTERVAL_MS),
                                         NULL);
    												APP_ERROR_CHECK(err_code);
    											 // NRF_LOG_INFO("BSP_EVENT_KEY 2 PUSH ON==> LONG PUSH >200ms\n");
    											 //beep_v11();
    											
    												button2_state = STATE_OFF;
    								//		}
    									
                if (actual_state != BSP_INDICATE_FIRST)
                    actual_state--;
                else
                    actual_state = BSP_INDICATE_LAST;
                    
                    //timeout handler 
                    
                    
    void button_push_timeout_handler(void * p_context)
    { 
         uint32_t err_code;
         static uint32_t cnt;
    
         if (BUTTON_3==0)
         {
             cnt++;
             if ( cnt >= LONG_PRESS(1000))
             {
                 cnt = 0;
                 NRF_LOG_INFO("Long Button press");
             }
             else
             {
                  err_code = app_timer_start(m_button_action, 
                                             APP_TIMER_TICKS(BUTTON_STATE_POLL_INTERVAL_MS),
                                             NULL);
                  APP_ERROR_CHECK(err_code);
             }
         }
         else
         {
              cnt = 0; // reset counter variable
              NRF_LOG_INFO("Short button press");
         }
    }     
    
    err_code = app_timer_create(&m_button_action, APP_TIMER_MODE_SINGLE_SHOT, button_push_timeout_handler);
          APP_ERROR_CHECK(err_code);
    	      NRF_LOG_INFO("&m_button_action init Line2119...");
                break;

    Timer init :

    //long press button timer 16Mar22 Rixtronix LAB
    APP_TIMER_DEF(m_button_action);
    #define BUTTON_STATE_POLL_INTERVAL_MS  100UL
    #define LONG_PRESS(MS)    (uint32_t)(MS)/BUTTON_STATE_POLL_INTERVAL_MS 
    

    I got always in :

       NRF_LOG_INFO("Short button press");

    although I push long enough...

    What do we miss here ?

    Best regards,

    Rixtronix

  • Hi Karl,

    I pushed BSP_BUTTON_2, but on timeout handler I got :


    the log :
    nfo> app: BSP_BUTTON_0
                                                           

    nfo> app: BSP_BUTTON_1
                                                           

    nfo> app: BSP_BUTTON_2
                                                           

    nfo> app: BSP_BUTTON_3
                                                           

    nfo> app: BSP_BUTTON_2
                                                           

    nfo> app: BSP_BUTTON_0

    Any ideas?

    Kind regards,

    Rixtronix LAB

    Rixtronix LAB

  • Hi Karl,

    Why isn't responding to

    bsp_button_event_handler on bsp.c, but only responding to bsp_event_handler in main.c ?

    Regards,

    Rixtronix

  • Hello again, Rixtronix

    Thank you for your patience with this.

    RixtronixLAB said:

    I pushed BSP_BUTTON_2, but on timeout handler I got :


    the log :
    nfo> app: BSP_BUTTON_0
                                                           

    nfo> app: BSP_BUTTON_1
                                                           

    nfo> app: BSP_BUTTON_2
                                                           

    nfo> app: BSP_BUTTON_3
                                                           

    nfo> app: BSP_BUTTON_2
                                                           

    nfo> app: BSP_BUTTON_0

    This looks for me like a possible fallthrough in a switch case handling, could this be the case?
    I do not see any break statements at the end of the BSP_EVENT_KEY_2 case you have shared, at least.

    I notice that you are not checking the returned error code from you calls to bsp_event_to_button_action_assign, please make sure to do so to see if they are succeeding.

    I also notice that you are using the ant bsp init, bsp_btn_ant_init, could you elaborate on why you are using this, and what your intentions are for calling it multiple times in a row?
    The bsp init function should only have to be called once.

    Best regards,
    Karl

Reply
  • Hello again, Rixtronix

    Thank you for your patience with this.

    RixtronixLAB said:

    I pushed BSP_BUTTON_2, but on timeout handler I got :


    the log :
    nfo> app: BSP_BUTTON_0
                                                           

    nfo> app: BSP_BUTTON_1
                                                           

    nfo> app: BSP_BUTTON_2
                                                           

    nfo> app: BSP_BUTTON_3
                                                           

    nfo> app: BSP_BUTTON_2
                                                           

    nfo> app: BSP_BUTTON_0

    This looks for me like a possible fallthrough in a switch case handling, could this be the case?
    I do not see any break statements at the end of the BSP_EVENT_KEY_2 case you have shared, at least.

    I notice that you are not checking the returned error code from you calls to bsp_event_to_button_action_assign, please make sure to do so to see if they are succeeding.

    I also notice that you are using the ant bsp init, bsp_btn_ant_init, could you elaborate on why you are using this, and what your intentions are for calling it multiple times in a row?
    The bsp init function should only have to be called once.

    Best regards,
    Karl

Children
  • Hi Karl,

    Thanks for the reply,

    Like this ?

    *********Button input 1Mar22 Rixtronix LAB********/
    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);
    	   APP_ERROR_CHECK(err_code);
    	  
    	
     	  err_code = bsp_event_to_button_action_assign(BSP_BUTTON_1,
                                                                BSP_BUTTON_ACTION_PUSH, 
                                                                BSP_EVENT_KEY_1);//hardware not connected
    	   APP_ERROR_CHECK(err_code);
    	  
    	 
      /************originally***********/
    	 err_code = bsp_event_to_button_action_assign(BSP_BUTTON_2,
                                                                BSP_BUTTON_ACTION_PUSH,
                                                                BSP_EVENT_KEY_2);
    	 APP_ERROR_CHECK(err_code);
    	/**************
    	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);
       APP_ERROR_CHECK(err_code);																														
    																														
      /*************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);
      */
    }	

    So I need to use only

     bsp_init

    and don't have to use bsp_btn_ant_init, I took it from ANT example ?

    Regards,

    Rixtronix

  • Hi Karl,

    from :

     err_code = bsp_event_to_button_action_assign(BSP_BUTTON_2,
                                                                BSP_BUTTON_ACTION_PUSH,
                                                                BSP_EVENT_KEY_2);
    	 //APP_ERROR_CHECK(err_code);
    	 NRF_LOG_INFO("Error code %u",err_code);

    I got "error code 7"

    what does it mean ?

    Regards,

    Rixtronix

  • Hello Rixtronix.

    RixtronixLAB said:

    I got "error code 7"

    what does it mean ?

    Please make sure to have DEBUG defined in your preprocessor defines, like shown in the included image:

    This will make your logger output detailed debug information whenever a non-NRF_SUCCESS error code is passed to an APP_ERROR_CHECK.
    You may then check the returned error code against the functions API reference, to see why it would return this error and how you can resolve it.

    RixtronixLAB said:
    don't have to use bsp_btn_ant_init, I took it from ANT example ?

    The ant and ble options loads the usual button support package configuration for those cases.
    For instance, in our BLE examples we always use the same button layout and functionality - i.e erase bonds on a long press wakeup, or advertising indication by blinking of LED 1, etc.
    If you do not wish for any of this functionality to be already implemented you may instead not use the ant or ble bsp options. You should then go into the ant or ble bsp init functions to see how they initialize the bsp module, so that you may do it in a similar way for your own bsp usage.

    Best regards,
    Karl

Related