View unanswered posts | View active topics It is currently Sat Aug 30, 2014 4:33 pm






Reply to topic  [ 9 posts ] 
Button handling 
Author Message
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Button handling
hey,
I'm currently porting some C code from EV3/C and NXT/NXC to RobotC syntax and have some questions about it.
So I'm looking for some button press functions in RobotC:

1) Wait for any Button-press, and stop program until either one has been pressed, don't wait for btn up, sort of
Code:
while(!btnpressed() ) {...}


2.) check if a button has been pressed intermediately and accidentally, does not read which one but just returns true (1) or false (0) , the program ignores this statement and skips if no btn was pessed in that moment, sort of
Code:
 if( btnpressed() ) {...}


3.) wait for any button press, read buttons, wait for btn up and return which button has been pressed actually, sort of getchar(), but for btns, not for keyboard:
Code:
int btn=getbtn();


4.) combination of 2 + 3: if no button has been pressed during runtime,
the program ignores this statement and skips if no btn was pessed in that moment,
but if either one was pressed then it reads it and then one may decide what to do,
like wellknown in Pascal for keyboard: if (keypressed) then char:=readkey , so sort of
Code:
if( btnpressed() ) {
   int btn=getbtn();
   switch(btn) {
      case 1:...;
      case 2:...;
      case 3:...;
   }
}


5.) program reads btns as long as btns are pressed, but does not wait for btn up (so continuous repetitive reading possible), sort of cin() for keyboard keys, but for btns instead:
Code:
int btn=btnin();

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Last edited by Ford Prefect on Thu Jan 02, 2014 8:45 am, edited 2 times in total.



Thu Jan 02, 2014 6:35 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Button handling
I don't have exactly what you asked for but here is the nxt button module of our library.
http://proj.titanrobotics.net/hg/Ftc/20 ... b/nxtbtn.h
If you call the BtnTask periodically, it will monitor button changes and do an event callback on both button press or releases.


Thu Jan 02, 2014 7:33 am
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: Button handling
thank you, but that's weird and too overcomplicated and confusing to me :roll:

what I need is a simple, convenient, and intuitive functionality like

Code:
if ( SensorTouch(S1) ) beep;   // if accidentally the Touch sensor is pressed then beep sound
if ( btnpressed() ) beep;      // if accidentally a btn  is pressed then beep sound

while (!SensorTouch(S1) );    // stops program intermediately and waits until Touch sensor is pressed
while (!btnpressed()  );      // stops program intermediately and waits until a btn  is pressed



I don't want to have to poll the touch sensor states or the button states continuously over 30 hours if I only need to konw about it maybe 1 second.
To pack it into a parallel task is surely a useless waste of cpu power.
I want to read the button state just in that moment when the program flow reaches this executed line!



_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Last edited by Ford Prefect on Fri Jan 03, 2014 4:33 pm, edited 1 time in total.



Thu Jan 02, 2014 8:26 am
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: Button handling
is there not at least a simple function like

Code:
(bool) btncheck(btn_1) // just check, return 1 if pressed, no wait until pressed or released !
(bool) btncheck(btn_2) // just check, return 1 if pressed, no wait until pressed or released !
(bool) btncheck(btn_3) // just check, return 1 if pressed, no wait until pressed or released !
...
...



???

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Last edited by Ford Prefect on Sat Jan 04, 2014 4:53 am, edited 1 time in total.



Fri Jan 03, 2014 4:05 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Button handling
Like I said, it's not exactly what you are looking for. I was thinking you can look at the logic and write the functions you need using our code as a reference regarding debounce and all that. For what you need, it looks like RobotC already has support for them. For example:
Code:
if ( SensorTouch(S1) ) beep;   // if accidentally the Touch sensor is pressed then beep sound
if ( btnpressed() ) beep;      // if accidentally a btn  is pressed then beep sound

while (!SensorTouch(S1) );    // stops program intermediately and waits until Touch sensor is pressed
while (!btnpressed()  );      // stops program intermediately and waits until a btn  is pressed

SensorTouch(S1) is supported in RobotC as SensorValue[S1] where S1 is the touch sensor.
btnpressed() can be written as nNxtButtonPressed != kNoButton.
CheckBtn(btn) can be written as nNxtButtonPressed == btn

BTW, I got all this info from RobotC help (look under Buttons->nNxtButtonPressed).


Fri Jan 03, 2014 4:25 am
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: Button handling
Thanks a lot, I think that would do it.
I have no 3.x installed (I have no licence), I'm waiting for 4.0 for EV3, so I have no RobotC help yet.
I didn't expect a statement like kNoButton or even nNxtButtonPressed instead of buttonpressed() or btnpressed(), this "nNxt..." prefix was not expected by me.
anyway, thanks again!

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Fri Jan 03, 2014 4:37 am
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: Button handling
so nNxtButtonPressed returns the btn-ID of the currently pressed button, and 0 if no btn is pressed, right?
or what value is kNoButton (if not zero)?
// btnin() cancelled
Code:
#define btnpressed()  nNxtButtonPressed != kNoButton // just check if any button is pressed or no one
#define btncheck(btn) nNxtButtonPressed == btn       // check if a pressed btn is of a certain btn_ID

inline int getbtn(void) {     // wait for btn_press, read which btn_ID, wait for btn_up, then return btn_ID
  while( !btnpressed() );       // wait for btn_press
  int btn=nNxtButtonPressed;    // read which btn_ID
  while( btnpressed() );        // wait for btn_up
  return btn;                   // return btn_ID
}


correct?

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Last edited by Ford Prefect on Sat Jan 04, 2014 4:52 am, edited 5 times in total.



Fri Jan 03, 2014 7:27 am
Profile
Guru
User avatar

Joined: Sun Nov 15, 2009 5:46 am
Posts: 1347
Post Re: Button handling
I would do the following:
Code:
#define btnpressed()  (nNxtButtonPressed != kNoButton) // just check if any button is pressed or no one
#define checkbtn(btn) (nNxtButtonPressed == btn)       // check if a pressed btn is of a certain btn_ID
#define btnin()       nNxtButtonPressed              // read a button press repeatedly, returns btn_ID or 0 if no btn_press

inline int getbtn(void) {     // wait for btn_press, read which btn_ID, wait for btn_up, then return btn_ID
  while( !btnpressed() )
  {
    EndTimeSlice();
  }       // wait for btn_press
  TNxtButtons btn=nNxtButtonPressed;    // read which btn_ID
  while( btnpressed() )
  {
    EndTimeSlice();
  }        // wait for btn_up
  return btn;                   // return btn_ID
}

BTW, the definitions of the buttons are defined in nxtFileIODefs.h. I don't think this has changed much so it should still be there in 2.x.
Code:
typedef enum TNxtButtons
{
  #define kNoButton (-1)   // Don't want to put in enum because enum size will change from 8-bits to 16/32-bits
  kExitButton    = 0,      //  BTN1
  kRightButton   = 1,      //  BTN2
  kLeftButton    = 2,      //  BTN3
  kEnterButton   = 3       //  BTN4
} TNxtButtons;  // NXT Only


Fri Jan 03, 2014 3:17 pm
Profile
Guru
User avatar

Joined: Sat Mar 01, 2008 12:52 pm
Posts: 1030
Post Re: Button handling
ok, EndTimeSlice() == yield() == sleep(1_ms) ? good idea, thanks!

then btnin() makes no sense, but no problem as it's nothing else than
Code:
if (btnpressed() ) int btn= nNxtButtonPressed;

the rest may then stay like this:

Code:
#define btnpressed()  (nNxtButtonPressed != kNoButton) // just check if any button is pressed or no one
#define btncheck(btn) (nNxtButtonPressed == btn)       // check if a pressed btn is of a certain btn_ID

inline int getbtn(void) {     // wait for btn_press, read which btn_ID, wait for btn_up, then return btn_ID
  while( !btnpressed() ) {  yield(); }      // wait for btn_press
  int btn=nNxtButtonPressed;                // read which btn_ID
  while( btnpressed() ) {  yield(); }       // wait for btn_up
  return btn;                               // return bt_ID
}


but IIRC, for EV3 the btn_IDs are 1,2,4,8,16,32 anyway,
and I must confess: I don't like enums, I always use char or int ;)

(BTW, I once had RC 1.4 or so, so I don't quite recall...^^)

thanks again for your contribution!

_________________
regards,
HaWe aka Ford
#define S sqrt(t+2*i*i)<2
#define F(a,b) for(a=0;a<b;++a)
float x,y,r,i,s,j,t,n;task main(){F(y,64){F(x,99){r=i=t=0;s=x/33-2;j=y/32-1;F(n,50&S){t=r*r-i*i;i=2*r*i+j;r=t+s;}if(S){PutPixel(x,y);}}}while(1)}


Fri Jan 03, 2014 3:54 pm
Profile
Display posts from previous:  Sort by  
Reply to topic   [ 9 posts ] 

Who is online

Users browsing this forum: KeesSmit and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  



Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software for PTF.