2012-05-25 06:58 AM
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?Regards2012-05-25 07:21 AM
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,2012-05-25 07:38 AM
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 )2012-05-28 03:10 AM
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 :DThanks 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: ThierryPosted: Friday, May 25, 2012 5:15 PMSubject: Slider used as 4 buttons on STM32L-DiscoveryHello,
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,2012-06-13 12:34 PM
Hello,
I'm referring to the STM32 Touch Sensing Library package (). Best Regards, Thierry,