capsense interrupt.

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
Doorknob
Level 4
Level 4
First solution authored 50 replies posted 25 replies posted

how can i make a capsense switch break a loop? some example code would be greatly appreciated. thank you.

0 Likes
1 Solution
Hari
Moderator
Moderator
Moderator
750 replies posted 500 replies posted 250 solutions authored

Hello ScCl_3419596

I am attaching the code for the SingleLedMultiColor function with a few modifications.

I have commented the CapSense_Start(), and UdateAllBaselines API since they are not required.


Also, notice that I have added a CapSense_ScanAllWidgets after if (CapSense_IsWidgetActive(CapSense_BUTTON0_WDGT_ID)) so that the CapSense data is refreshed.

void SingleLedMultiColor(uint32 delay, uint32 timer_cnt)

{

    CyGlobalIntEnable;

    // CapSense_Start();

    // CapSense_UpdateAllBaselines();

    CapSense_ScanAllWidgets();

    uint32 ledPosition = 0; // On LED position

    uint32 colorIndex = 0;

    uint8_t flag = 0;

    timer_cnt = 0;


    // Loop until SW1 pressed

    while (flag == 0)

    {

        // Wait for last update to finish

        while (StripLights_Ready() == 0)

            ;


        // Clear all LEDs to background color


        StripLights_MemClear(StripLights_BLACK);


        // Set the color of a single LED


        StripLights_Pixel(ledPosition, 0, StripLights_getColor(colorIndex));


        // Trigger update of all LEDs at once


        StripLights_Trigger(1);


        // Loop delay


        CyDelay(delay);


        // Advance to next position.  If too far, start back at 0


        ledPosition++;


        if (ledPosition >= StripLights_TOTAL_LEDS)

            ledPosition = 0;


        colorIndex++;


        if (colorIndex >= StripLights_COLOR_WHEEL_SIZE)

            colorIndex = 0;


        //CapSense_ScanAllWidgets();


        if (CapSense_NOT_BUSY == CapSense_IsBusy())

        {


            CapSense_ProcessWidget(CapSense_BUTTON0_WDGT_ID);


            if (CapSense_IsWidgetActive(CapSense_BUTTON0_WDGT_ID))


                flag = 1;


            CapSense_ScanAllWidgets();

        }


        // If SW1 is pressed, leave loop


        //if(SW1_Read() == 0) break;

    }


    StripLights_MemClear(StripLights_BLACK);


    StripLights_Trigger(1);


    CyDelay(2000);

}

Please try this code and let us know if it fixes the issue.

Thanks,
Hari

View solution in original post

15 Replies
Hari
Moderator
Moderator
Moderator
750 replies posted 500 replies posted 250 solutions authored

Hello ScCl_3419596,

Could you please elaborate your request?

If the requirement is to exit a loop whenever a finger is touched, you can use the break statement when CapSense_IsWidgetActive API returns true.

if ( CapSense_IsWidgetActive  ( CapSense_BTN0_WDGT_ID))

{
     break;

}

Best regards,
Hari

0 Likes
Doorknob
Level 4
Level 4
First solution authored 50 replies posted 25 replies posted

so i have both a proximity sensor and a  capsense button. the capsense proximity is used to start a timer and run some LEDs in a loop for a certain amount of time. i have tried your above example but i camp seem to get it to work. does the break function only work in a for loop? and can i scan for a button if my main loop is already scanning for the proximity?

0 Likes

pastedImage_1.pngv

0 Likes

pastedImage_0.pngattached are photos from my main loop and the subroutine.

0 Likes
Hari
Moderator
Moderator
Moderator
750 replies posted 500 replies posted 250 solutions authored

Hello ScCl_3419596

Is the requirement to exit the first infinite for loop whenever a sensor is touched and move to the next one?

If so, you can place a break statement inside the if (CapSense_IsWidgetActive(...)) statement.

Can you share the code that you are using so that we have a better picture of the requirement?

Thanks,
Hari

0 Likes
Doorknob
Level 4
Level 4
First solution authored 50 replies posted 25 replies posted

#include <project.h>

#define MIN_PROXIMITY_SIGNAL    (CapSense_PROXIMITY0_FINGER_TH - CapSense_PROXIMITY0_HYSTERESIS)

// Function prototypes for pattern functions.

void SingleLED(uint32 delay, uint32 timer_cnt);

void RgbChase(uint32 delay);

void Clock(uint32 delay);

void Rainbow(uint32 delay);

void OppositeRings(uint32 delay);

void OneColor(uint32 delay);

void SingleLedMultiColor(uint32 delay, uint32 timer_cnt);

extern const uint32 StripLights_CLUT[ ];

int32 timer_cnt;

CY_ISR(timer_int_handler) {

    timer_cnt++;

    if(timer_cnt>=120)

        timer_cnt=0;

    Timer_ClearInterrupt(Timer_TC_INTERRUPT_MASK);   

}

int main()

{

    EN_Write(1u);

    Timer_Start();

    timer_int_StartEx(timer_int_handler);

       

  

    // Initialize StripLights

    StripLights_Start(); 

// Set dim level 0 = full power, 4 = lowest power

    StripLights_Dim(1);

// Clear all memory to black

StripLights_MemClear(StripLights_BLACK);

// Enable global interrupts, required for StripLights

    CyGlobalIntEnable;   

   

    CapSense_Start();

    EZI2C_Start();

   

    EZI2C_EzI2CSetBuffer1(sizeof(CapSense_dsRam), sizeof(CapSense_dsRam), (uint8 *)&CapSense_dsRam);

   

    CapSense_Start();

   

    CapSense_ScanAllWidgets();

// Loop through the different patterns each time SW1 is pressed.

for(;;)

{

       

        if(CapSense_NOT_BUSY == CapSense_IsBusy())

        {

           

           CapSense_ProcessWidget(CapSense_PROXIMITY0_WDGT_ID);

           //CapSense_ProcessAllWidgets();

           // CapSense_ProcessWidget(CapSense_BUTTON0_WDGT_ID);

            //CapSense_ProcessWidget(CapSense_BUTTON1_WDGT_ID);

           

            if(CapSense_IsWidgetActive(CapSense_PROXIMITY0_WDGT_ID)) {

                //imer_cnt=0;

              

   

                SingleLedMultiColor(100, timer_cnt);

      

               

     

            }

            /* Transmits all CapSense values to the tuner */

            CapSense_RunTuner();

           

            /* Scans the proximity sensor */

            CapSense_ScanAllWidgets();

        }

    }

}

void SingleLedMultiColor(uint32 delay, uint32 timer_cnt)

{

    CyGlobalIntEnable;   

   

    CapSense_Start();

    CapSense_UpdateAllBaselines();

    CapSense_ScanAllWidgets();

  

    uint32 ledPosition = 0;  // On LED position

uint32 colorIndex = 0;

    uint8_t flag = 0;

    timer_cnt=0;

  

// Loop until SW1 pressed

    while(flag == 0) 

    {

           

    // Wait for last update to finish

        while( StripLights_Ready() == 0);                

  

    // Clear all LEDs to background color

    StripLights_MemClear(StripLights_BLACK);  

  

    // Set the color of a single LED

        StripLights_Pixel(ledPosition, 0,  StripLights_getColor( colorIndex ) );

    // Trigger update of all LEDs at once

            StripLights_Trigger(1);   

  

    // Loop delay

            CyDelay(delay);   

    // Advance to next position.  If too far, start back at 0

    ledPosition++; 

    if(ledPosition >= StripLights_TOTAL_LEDS) ledPosition = 0;

  

    colorIndex++; 

            if(colorIndex >= StripLights_COLOR_WHEEL_SIZE) colorIndex = 0;

           

            //CapSense_ScanAllWidgets();

            if(CapSense_NOT_BUSY == CapSense_IsBusy()) {

                 CapSense_ProcessWidget(CapSense_BUTTON0_WDGT_ID);

                 if(CapSense_IsWidgetActive(CapSense_BUTTON0_WDGT_ID)) 

                    flag=1;

            }

           

      

// If SW1 is pressed, leave loop

//if(SW1_Read() == 0) break;

    }

    StripLights_MemClear(StripLights_BLACK);

    StripLights_Trigger(1);

 

CyDelay(2000);

}

0 Likes

above is all of my code.

in the main loop i scan for proximity that sends it to the subroutine that runs some LEDs.

then in that subroutine i would like to also scan for a button that will break that loop and maybe redefine a variable.

does this make sense?

thanks

0 Likes
Hari
Moderator
Moderator
Moderator
750 replies posted 500 replies posted 250 solutions authored

Hello ScCl_3419596

I am attaching the code for the SingleLedMultiColor function with a few modifications.

I have commented the CapSense_Start(), and UdateAllBaselines API since they are not required.


Also, notice that I have added a CapSense_ScanAllWidgets after if (CapSense_IsWidgetActive(CapSense_BUTTON0_WDGT_ID)) so that the CapSense data is refreshed.

void SingleLedMultiColor(uint32 delay, uint32 timer_cnt)

{

    CyGlobalIntEnable;

    // CapSense_Start();

    // CapSense_UpdateAllBaselines();

    CapSense_ScanAllWidgets();

    uint32 ledPosition = 0; // On LED position

    uint32 colorIndex = 0;

    uint8_t flag = 0;

    timer_cnt = 0;


    // Loop until SW1 pressed

    while (flag == 0)

    {

        // Wait for last update to finish

        while (StripLights_Ready() == 0)

            ;


        // Clear all LEDs to background color


        StripLights_MemClear(StripLights_BLACK);


        // Set the color of a single LED


        StripLights_Pixel(ledPosition, 0, StripLights_getColor(colorIndex));


        // Trigger update of all LEDs at once


        StripLights_Trigger(1);


        // Loop delay


        CyDelay(delay);


        // Advance to next position.  If too far, start back at 0


        ledPosition++;


        if (ledPosition >= StripLights_TOTAL_LEDS)

            ledPosition = 0;


        colorIndex++;


        if (colorIndex >= StripLights_COLOR_WHEEL_SIZE)

            colorIndex = 0;


        //CapSense_ScanAllWidgets();


        if (CapSense_NOT_BUSY == CapSense_IsBusy())

        {


            CapSense_ProcessWidget(CapSense_BUTTON0_WDGT_ID);


            if (CapSense_IsWidgetActive(CapSense_BUTTON0_WDGT_ID))


                flag = 1;


            CapSense_ScanAllWidgets();

        }


        // If SW1 is pressed, leave loop


        //if(SW1_Read() == 0) break;

    }


    StripLights_MemClear(StripLights_BLACK);


    StripLights_Trigger(1);


    CyDelay(2000);

}

Please try this code and let us know if it fixes the issue.

Thanks,
Hari

Doorknob
Level 4
Level 4
First solution authored 50 replies posted 25 replies posted

this seemed to work thank you. are you able to help me tune my proximity sensor i have the tuner running but it is unclear to me how to adjust the thresholds.

thanks you.

0 Likes

i actually have figured out the tuning portion but the newest problem i am having is when the proximity sensor is triggered it seems to always be high.

0 Likes
Hari
Moderator
Moderator
Moderator
750 replies posted 500 replies posted 250 solutions authored

Hi ScCl_3419596

Can you please send the tuner screenshot of the proximity sensor?

Are you using a PCB trace or a wire loop as proximity sensor? If you are using a wire loop, you might be touching the wire loop and that could change the orientation of the wire loop, causing Cp to change.

Thanks,
Hari

0 Likes
Doorknob
Level 4
Level 4
First solution authored 50 replies posted 25 replies posted

im using a PCB trace as the sensor. is therre a way to use the proximity loop as both a proximity sensor and a touch sensor?

0 Likes
Hari
Moderator
Moderator
Moderator
750 replies posted 500 replies posted 250 solutions authored

Hello ScCl_3419596

Yes. If you take a look at the widget threshold parameters, there is an option to set the Proximity threshold and Touch Threshold.

pastedImage_3.png

These indicate the threshold value (signal level = Raw count - Baseline) where the proximity detection starts and touch is detected. You can touch the proximity loop in you design, check the signal level you are getting and set that as the Touch threshold parameter.

The CapSense_IsProximitySensorActive API returns a 32 bit value where bit 0 indicates proximity is detected and bit 1 indicates touch is detected.

Thanks,
Hari

0 Likes
Doorknob
Level 4
Level 4
First solution authored 50 replies posted 25 replies posted

awesome thank you for all the help. could you possibly show me some example code of how i would determine a touch from proximity?

0 Likes
lock attach
Attachments are accessible only for community members.
Hari
Moderator
Moderator
Moderator
750 replies posted 500 replies posted 250 solutions authored

Hello ScCl_3419596

I am attaching a sample project to demonstrate both proximity and touch sensing with the same widget. Please try this project and let me know if you have any further queries.

Best regards,
Hari

0 Likes