This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC2541 enable/disable advertising by long pressing S3 button

So currently the advertising is enabled/disabled by the S1 button press. I want to make it so the the advertising toggle happens by long pressing the S3 button.

Now I know that I'll have to use some kind of a timer, but don't know how. If someone could post the code snippet I'd greatly appreciate it.

  • Hello,
    Look at the sensorTag_HandleKeys function. There is already implemented a long press key functionality on the side-key where if it is pressed for more than 3 seconds it will reset the chip (HAL_SYSTEM_RESET();). It uses a OSAL timer to check on the key again 3 seconds after the initial press.

    if (keys & HAL_KEY_SW_1)
    {
    // Reset the system if side key is pressed for more than 3 seconds
    sysResetRequest = TRUE;
    osal_start_timerEx( sensorTag_TaskID, ST_SYS_RESET_EVT, ST_SYS_RESET_DELAY );
    .....
  • Hi,

    It really simple.. You need to set a timer event e.g at every 100ms, and in that event increment the counter if the switch is pressed. If the counter is reached to the expected value then enable/disable the advertisement.

    You need to write code for that on you own!

    Thanks,

    Dhaval

  • I did check this out, There is no macro for advertising.
    There is this code which toggles the advertising

    if (!testMode ) // Side key
    {
    // If device is not in a connection, pressing the side key should toggle
    // advertising on and off
    if ( gapProfileState != GAPROLE_CONNECTED )
    {
    uint8 current_adv_enabled_status;
    uint8 new_adv_enabled_status;

    // Find the current GAP advertising status
    GAPRole_GetParameter( GAPROLE_ADVERT_ENABLED, &current_adv_enabled_status );

    if( current_adv_enabled_status == FALSE )
    {
    new_adv_enabled_status = TRUE;
    }
    else
    {
    new_adv_enabled_status = FALSE;
    }

    // Change the GAP advertisement status to opposite of current status
    GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &new_adv_enabled_status );
    }

    if ( gapProfileState == GAPROLE_CONNECTED )
    {
    uint8 adv_enabled = TRUE;

    // Disconnect
    GAPRole_TerminateConnection();
    // Start advertising
    GAPRole_SetParameter( GAPROLE_ADVERT_ENABLED, sizeof( uint8 ), &adv_enabled );
    }
    }
    else
    {
    // Test mode
    if ( keys & HAL_KEY_SW_1 ) // Side key
    {
    SK_Keys |= SK_KEY_SIDE;
    }
    }

    but I don't know how to implement it with osal_start_timerEx( Param1, Param2, Param3 );
    I understand that I need not change the first parameter, the third one is delay duration but what should be my second parameter.
  • Hi,
    Thanks for the reply, could you atleast tell me which timer function should I use ?

    Thanks
  • Hello Sanat,
    The middle argument is a event id, where all is defined in the top of SensorTag.h (if you add another event make sure to only set one bit high, i.e. 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80). The first argument given is this case is set to "sensorTag_TaskID" means that when the timer expires the function SensorTag_ProcessEvent will be called which handles the event given. In this case it will reset the chip if sysResetRequest is set to true. The sensorTag_HandleKeys will be called both for rising and falling edge, meaning that when the button is released before 3 seconds has passed the following code will set sysResetRequest to false and prohibit a system reset:

    if (!(keys & HAL_KEY_SW_1))
    {
    // Cancel system reset request
    sysResetRequest = FALSE;
    }


    There are several ways of solving your problem. For example:
    1)
    Start a timer after you detect a button push (you can use timer1, timer3 or timer 4). You can also try to use osal_GetSystemClock, store the results and compare between each time sensorTag_HandleKeys is called.
    2)
    Add another event and implement in the same manner as is already done for system reset.
    ...
  • Thankyou for such a comprehensive answer.