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.

CCS/CC1350: How to perform long press, double press and triple press

Part Number: CC1350

Tool/software: Code Composer Studio

Hi TI,

There is a button, BTN-1 on the CC1350 launchpad, and I would like to perform Long press, double press and triple press functions.  My codes are as below;

if (PIN_registerIntCb(PCPinHandle, &PCCallbackFxn) != 0) {
    /* Error registering PC callback function */
    while(1);
}

void PCCallbackFxn(PIN_Handle handle, PIN_Id pinId) {

    /* Debounce logic, only toggle if the button is still pushed (low) */
    CPUdelay(80 * 8000);

    switch (pinId) {
        case Board_PIN_BUTTON0:
             CPUdelay(100 * 8000);
             if (!PIN_getInputValue(Board_PIN_BUTTON0)) {    //Button0 still keep pressed, long press?
                 CPUdelay(1200 * 8000);
                 if (!PIN_getInputValue(Board_PIN_BUTTON0)) {    //long press detected
                         //LONG PRESS FUNCTION
                 }
             } else {           //short press detected
                 clickCount++;
                 CPUdelay(3000 * 8000);
                 if (clickCount > 2) {      //Triple click
                     //TRIPLE CLICK FUNCTION
                     clickCount = 0;
                 }

                 if (clickCount == 2) {       //double click
                     //DOUBLE CLICK FUNCTION
                     clickCount = 0;
                 }
             }
             break;
   }
}

At this moment, Long Press is fine, but I have to double-click 2 times (totally 4 click) to trigger double click function, and I cannot trigger triple click fucntion.

Would you please provide us some hint on that?

Wishes,

Andy

  • Hi Andy,

    So I gather you're writing control logic for your button. First thing to note is to understand the delay function that you're using. Documentation can be found in the following link:

    software-dl.ti.com/.../group__cpu__api.html

    Basically whatever argument you pass it, it takes 3 cycles. So if you pass it 1, it will delay the clock 3 cycles.

    Assuming you're running on a 48MHz clock, consider the following for example:

    CPUdelay(3000 * 8000);
    3 * (1/48000000) * 3000 * 8000 = 1.5 second delay

    Once you understand that, think about the user experience you want. How fast should a double click occur? How long should a long press take? Do these system definitions make sense and could I implement that in code? Define that in your application code and make sure your logic is correct.

    Regarding the logic in your code, I don't know how fast you perform a double click but I doubt it's more than 1.5 seconds. In other words, when you press it the second time the CPU never sees it because it's still in the delay. I presume that's why you have to press it 4 times instead of twice for a double click.

    Your triple click doesn't trigger because of your click count control logic. If you notice click count will never reach 3 because it will be set to 0 when it's equal to 2.

    Hope this helps!

    Regards,
    Jesus
  • Hi Jesus,

    Thanks for your information and pointing out. I really have no idea to handle the case of triple click. Would you please to provide us more hints?

    Your help is highly appreciated!

    Warm regards,

    Andy
  • Hey Andy,

    I suggest you try implementing this using a timer instead of CPU delays. Right now you have no sense of time. For example: I can press the button once, wait 10 seconds, press it again and it will register as a double click. If you implement a timer you can use that to define things like how fast is a double click or how long is a long press.  

    Regarding the triple click, the issue is that you need a way to differentiate a triple click from a double click. This is another example where using a timer is better. So let's say you define a double click to be 2 clicks that occur under 200ms. You can define the triple click to be under 300ms. Defining this way gives you a grace period of 100ms and you can use that period to determine whether a double click will become a triple click.

    For example:

    If two clicks occur under 200ms, that will register as a double click.

    At this point you will let 100ms go by where you are basically waiting to see if the registered double click is really a triple click.

    After that grace period, if no additional click occurs, then it's safe to call it a double click. Otherwise, a click did occur and therefore it's a triple click.

    TI Resource explorer would be a good starting point if you want to learn more about the timers and how to use them.

    dev.ti.com/.../_g_p_timer_c_c26_x_x_8h.html

    Hope this points you in the right direction. If you have any TI related questions please let me know.

    Regards,

    Jesus