cancel
Showing results for 
Search instead for 
Did you mean: 

Slider used as 4 buttons on STM32L-Discovery

gavin2
Associate II
Posted on May 25, 2012 at 15:58

Hi,

I am new to this forum and I am programming the STM32L-Discovery using Crossworks.

I am currently trying to do something which I believe should be relatively simple. 

e.g when I touch the 1st quarter of the slider - do something (like turn on the 1st LCD bar say). Touch the second quarter of the slider and the 2nd LCD bar goes on, etc. Basically use the slider as 4 separate buttons, with 4 separate functions.

I tried to use some code similar to that used in one of the examples, e.g

----

  if ((SLIDER_DETECTED)&&(TSLState == TSL_IDLE_STATE))

  {   

    if( SLIDER_POSITION <= 25 )

     buttonPressed = 0; 

     break;

   

    if( (SLIDER_POSITION > 25 ) && (SLIDER_POSITION <= 110 ))        

     buttonPressed = 1; 

     break;

    

    if( (SLIDER_POSITION > 110 ) && (SLIDER_POSITION <= 200 ))        

     buttonPressed = 2; 

     break;

   

    if( SLIDER_POSITION > 200 )        

     buttonPressed = 3; 

     break;

  }

---

However, regardless of where I touch the slider, ''buttonPressed=0, always seems to be true.

If somebody could point me in the right direction, I would be very grateful.

Can anyone recommend a good reference as to the possible ways to configure the operation of the slider?

Regards
4 REPLIES 4
Thierry GUILHOT
ST Employee
Posted on May 25, 2012 at 16:21

Hello,

Your source code is uncorrect. The first break statement is always executed and all remaining if statements are skipped. Some brackets should be added to get the expected behavior.

 

You can also have a look to the example 3 of the STM32 Touch Sensing Library which does exactly what you are trying to do.

Best Regards,

Thierry,

jdf25252
Associate II
Posted on May 25, 2012 at 16:38

Actually your code is not all that good.  If you rearrange the white space you can see what's happening.

if ((SLIDER_DETECTED)&&(TSLState == TSL_IDLE_STATE))

{   

    if( SLIDER_POSITION <= 25 )

        buttonPressed = 0;

    break;

    if( (SLIDER_POSITION > 25 ) && (SLIDER_POSITION <= 110 ))        

        buttonPressed = 1;

    break;    

    if( (SLIDER_POSITION > 110 ) && (SLIDER_POSITION <= 200 ))        

        buttonPressed = 2;

    break;

    if( SLIDER_POSITION > 200 )        

        buttonPressed = 3;

    break;

}

The first if statement gets evaluated  ''if( SLIDER_POSITION <= 25 )'' and whether its true or false the ''break'' statement following will be executed and the remainder of the ifs will ALWAYS be skipped.

USE SQUIGGLEY BRACKETS they don't cost anything and ensure you get what you really wanted.

I would have used ''if'' ''else if'' anyway.  I didn't think breaks worked on ''if'' statements, so I've learned something here too.

jdf

)

gavin2
Associate II
Posted on May 28, 2012 at 12:10

John and Thierry,

Thank you very much for your replies. This is a silly mistake, which I should have noticed. I've not used C for a many years, so that is my excuse. Or perhaps I did not have a big enough cup of coffee prior to starting this work :D

Thanks again.

Thierry, could you perhaps be more specific regarding the examples you suggest? I have already been looking at one of the examples (the one preloaded on the STM32, from where my code was derived) but I cannot find any good source of documentation/reference other than trawling through the Touch Sensing libraries and looking at random .c/.h files.

Thanks again.

From: Thierry

Posted: Friday, May 25, 2012 5:15 PM

Subject: Slider used as 4 buttons on STM32L-Discovery

Hello,

Your source code is uncorrect. The first break statement is always executed and all remaining if statements are skipped. Some brackets should be added to get the expected behavior.

 

You can also have a look to the example 3 of the STM32 Touch Sensing Library which does exactly what you are trying to do.

Best Regards,

Thierry,

Thierry GUILHOT
ST Employee
Posted on June 13, 2012 at 21:34

Hello,

I'm referring to the STM32 Touch Sensing Library package (

http://www.st.com/internet/com/SOFTWARE_RESOURCES/SW_COMPONENT/FIRMWARE/stm32_touchsensing_lib_setup.zip

).

Best Regards,

Thierry,