Tuesday, December 4, 2018

How to detect button hold in CC26x2, CC13x0, CC13x2 SDK.

The following steps show you how to detect button hold in CC26x2, CC13x0, CC13x2 SDK. I use zed_switch example to add related code to detect if right button (active-low) is pressed (low status) and hold for 3 seconds.

1. In board_key.c, change Board_PIN_BUTTON1 to use PIN_IRQ_BOTHEDGES

static PIN_Config keyPinTable[] = {
      Board_PIN_BUTTON0 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_NEGEDGE,
      Board_PIN_BUTTON1 | PIN_INPUT_EN | PIN_PULLUP | PIN_IRQ_BOTHEDGES,
      PIN_TERMINATE /* Terminate list */
    };


2. Add the following red codes in board_key.c

#include <ti/sysbios/knl/Clock.h>
...
UInt32 button_pressed_time=0;
UInt32 button_released_time=0;
static void board_key_keyFxn(PIN_Handle keyPinHandle, PIN_Id keyPinId)
{
    (void)keyPinHandle;

    if(keyPinId == keyButton0)
    {
        keysPressed |= KEY_LEFT;
    }
    else if(keyPinId == keyButton1)
    {
        if(PIN_getInputValue(keyButton1) == false){
            button_pressed_time=Clock_getTicks(); //Clock_getTicks will return counts in 10us.
            return;
         } else {
            if (PIN_getInputValue(keyButton1) == true){
                button_released_time=Clock_getTicks(); //Clock_getTicks will return counts in 10us.
            }
        }
        if ( (button_released_time-button_pressed_time)>300000 )
            keysPressed |= KEY_RIGHT_HOLD_3SEC;
        else if ( (button_released_time-button_pressed_time)>0 )
            keysPressed |= KEY_RIGHT;

    }

    if(Timer_isActive(&keyChangeClock) != true)
    {
        Timer_start(&keyChangeClock);
    }
}

3. Define KEY_RIGHT_HOLD_3SEC in board_key.h

/*! Select Key ID */
#define KEY_SELECT            0x01
/*! Up Key ID */
#define KEY_UP                0x02
/*! Down Key ID */
#define KEY_DOWN              0x04
/*! Left Key ID */
#define KEY_LEFT              0x08
/*! Right Key ID */
#define KEY_RIGHT             0x10
/*! Hold 3 seconds Right Key ID */
#define KEY_RIGHT_HOLD_3SEC   0x20


4. Add "if(keysPressed == KEY_RIGHT_HOLD_3SEC){...}" in zclSampleSw_processKey of zcl_samplesw.c to process event for right button pressing and holding for 3 seconds.

static void zclSampleSw_processKey(uint8 keysPressed)
{
    //Button 1
    if(keysPressed == KEY_LEFT)
    {
        zstack_bdbStartCommissioningReq_t zstack_bdbStartCommissioningReq;

        if(ZG_BUILD_COORDINATOR_TYPE && ZG_DEVICE_COORDINATOR_TYPE)
        {
            zstack_bdbStartCommissioningReq.commissioning_mode = BDB_COMMISSIONING_MODE_NWK_FORMATION | BDB_COMMISSIONING_MODE_NWK_STEERING | BDB_COMMISSIONING_MODE_FINDING_BINDING;
            Zstackapi_bdbStartCommissioningReq(zclSampleSw_Entity,&zstack_bdbStartCommissioningReq);
        }
        else if (ZG_BUILD_JOINING_TYPE && ZG_DEVICE_JOINING_TYPE)
        {
            zstack_bdbStartCommissioningReq.commissioning_mode = BDB_COMMISSIONING_MODE_NWK_STEERING | BDB_COMMISSIONING_MODE_FINDING_BINDING;
            Zstackapi_bdbStartCommissioningReq(zclSampleSw_Entity,&zstack_bdbStartCommissioningReq);
        }
    }
    //Button 2
    if(keysPressed == KEY_RIGHT)
    {
        zstack_bdbGetZCLFrameCounterRsp_t Rsp;

        Zstackapi_bdbGetZCLFrameCounterReq(zclSampleSw_Entity, &Rsp);
        zclGeneral_SendOnOff_CmdToggle( SAMPLESW_ENDPOINT, &zclSampleSw_DstAddr, FALSE, Rsp.zclFrameCounter );
    }
    if(keysPressed == KEY_RIGHT_HOLD_3SEC)
    {
        //Event for right button pressing and holding for 3 seconds.
    }


}

No comments:

Post a Comment