Printf strange behaviour

New to XMOS and XCore? Get started here.
oMG
Junior Member
Posts: 7
Joined: Mon Jul 26, 2010 12:18 pm

Printf strange behaviour

Postby oMG » Sat Aug 06, 2011 8:20 am

Hi i was just checking to see printf in action but i get a strange behaviour. Leds work fine but in console i don't get what i should. Pls help ty

Code: Select all


out port led_1 = PORT_BUTTON_LED_0;
out port led_2 = PORT_BUTTON_LED_1;
in port btna=PORT_BUTTON_A;
in port btnb=PORT_BUTTON_B;
int x;
int y;
int a=1;
int b=1;
int main()
{

  while(1)
  {

	  //btna when pinsneq(x):> x;
	  //btnb when pinsneq(y):> y;
	  btna:>x;
	  btnb:>y;
	  if(x==14 && a==1)
	  {
		  led_1 <: 1;
		  printf("\n test 1 ");
		  a=0;
	  }
	  else if(x==14 && a==0)
	  {
		  led_1 <: 1;
		  a=0;
	  }
	  else
	  {
		  led_1 <: 0;
		  a=1;
	  }


	  if(y==14 && b==1)
	  {
		  led_2 <: 1;
		  b=0;
		  printf("test 2");
	  }
	  if(y==14 && b==0)
	  {
		  led_2 <: 1;
		  b=0;
	  }
	  else
	  {
		  led_2 <: 0;
		  b=1;
	  }

  }
  return 0;
}

User avatar
Bianco
XCore Expert
Posts: 754
Joined: Thu Dec 10, 2009 6:56 pm

Postby Bianco » Sat Aug 06, 2011 1:40 pm

What do you get?
If you use xrun, don't forget the --io parameter.
oMG
Junior Member
Posts: 7
Joined: Mon Jul 26, 2010 12:18 pm

Postby oMG » Sat Aug 06, 2011 2:13 pm

When i press the buttons i don't get anything at the beggining but the leds turn on then pressing the buttons some more times i get the message printed in my terminal but it get's printed more times than i press the button. Some times i press the button and the message prints in the terminal but it prints like 2-3 times if i don't press the button fast. Run I/O Server is cheked if that is what you are saying.
User avatar
Bianco
XCore Expert
Posts: 754
Joined: Thu Dec 10, 2009 6:56 pm

Postby Bianco » Sat Aug 06, 2011 2:44 pm

Try add a newline at the end of each format string in the printf statements.
This should flush the buffers.

Furthermore, if you press a button (long enough) the main loop can execute the printf statements multiple times.
To prevent this you will need some sort of edge triggering instead of logic level triggering.
To use edge triggering you might need a proper software button debouncing routine also.
oMG
Junior Member
Posts: 7
Joined: Mon Jul 26, 2010 12:18 pm

Postby oMG » Sat Aug 06, 2011 3:20 pm

Ty for the reply :-D but if you see in my code you will notice that it shouldn't do that multiple times because after pressed i don't allow it to execute tha same code again. If i press a button and keep it pressed it will print like 1-3 times no more but it should print it only once. Can u give me an example of what you suggest to do?
User avatar
Bianco
XCore Expert
Posts: 754
Joined: Thu Dec 10, 2009 6:56 pm

Postby Bianco » Sat Aug 06, 2011 5:16 pm

Ah you are right about that, but are you sure that the else statement that sets a and b back to 1 is not executed? What is your hardware setup? you currently check that bit 0 must be low and bit 1,2,3 must be high if the button is pressed. So if this is a four-bit port with the button connected to port bit 0 and all pins have pull-ups it will jump to the else if the button is released and x/y is not 14 anymore (but 15). Due to button bouncing it will detect that the button is pressed in one of the next loop iterations and it will print that statement again.
oMG
Junior Member
Posts: 7
Joined: Mon Jul 26, 2010 12:18 pm

Postby oMG » Sat Aug 06, 2011 5:39 pm

I'm using the XC-2 Ethernet Card and i'm using the 2 buttons of the card. Can you explain me plz what do you mean about bounsing and also do you know another way to flush the data of printf without using the new line? ty
User avatar
Bianco
XCore Expert
Posts: 754
Joined: Thu Dec 10, 2009 6:56 pm

Postby Bianco » Sat Aug 06, 2011 5:48 pm

Here is some info:

http://www.elexp.com/t_bounc.htm
http://www.allaboutcircuits.com/vol_4/chpt_4/4.html

Solutions can be either hardware based, software based or both.
Search for "debouncing" in your favorite search engine to get more info about common solutions.

An easy solution would be to ignore all input for an amount of time after you have detected a released button.
That would give enough time for the button to stabilize.

You can do this with adding a delay at the end of the button press handler using a hardware timer.
oMG
Junior Member
Posts: 7
Joined: Mon Jul 26, 2010 12:18 pm

Postby oMG » Sat Aug 06, 2011 5:59 pm

TY :D :D :D :D now i that i know what's the problem i will fix it by the way do you know how to flush printf in XMOS without using new line?
User avatar
segher
XCore Expert
Posts: 843
Joined: Sun Jul 11, 2010 1:31 am

Postby segher » Sun Aug 07, 2011 4:26 pm

I expect

Code: Select all

fflush(stdout);
to work for that, like on any other platform. Not tested, of course.

Who is online

Users browsing this forum: No registered users and 2 guests