select statement

New to XMOS and XCore? Get started here.
daleonpz
Member++
Posts: 26
Joined: Thu Nov 04, 2010 1:18 pm

select statement

Postby daleonpz » Fri Dec 24, 2010 10:33 pm

I have some doubts about select statement, because I do not really understand what exactly it does. I want to use the 2 switches of XK-1 to turn-on and turn-off the leds depend on which switch you push. But it doesnt work, here's my code.

Code: Select all

#include <platform.h>
#include <xs1.h>

# define FLASH_PERIOD 100000000

out port 	led = PORT_LED ;
in  port 	sw1 = PORT_BUT_1;
in  port 	sw2 = PORT_BUT_2;


select sw_sel(in port sw1, in port sw2,unsigned &temp);

int main ( void ) {
	timer tmr ;
	unsigned isOn = 1;
	unsigned temp;
	unsigned t;
	temp=isOn;
	tmr :> t;
	while (1) {
			led <: isOn ;
			t += FLASH_PERIOD ;
			tmr when timerafter (t) :> void ;
			isOn = ! isOn ;
			led <: isOn;
			t += FLASH_PERIOD ;
			tmr when timerafter (t) :> void ;
			sw_sel(sw1,sw2,temp);
			isOn=temp;
			}
	return 0;
}

select sw_sel(in port sw1, in port sw2,unsigned &temp){
case sw1==1:
	temp*=2;
	if (temp==16) temp=1;
	break;
case sw2==1:
	if (temp==1) temp=16;
	temp/=2;
	break;
default:
	return -1;
	}
}
i hope you can help... When you pull one switch the port becomes "1" and I want to read that value and then switch the led.
User avatar
msar
Member++
Posts: 19
Joined: Wed Sep 08, 2010 11:04 am

Postby msar » Sat Dec 25, 2010 2:28 pm

Have you tried the tutorial exercises included with the XDK? The button project does almost exactly what you're attempting to do.

To start this project, go to File->New->XC Project, and select the XK-1 as your Platform in the drop-down menu.

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

Postby Bianco » Sat Dec 25, 2010 3:31 pm

I'm not sure what you are trying to reach. You say that the leds must turn on or off when buttons are pressed, yet i see some timer which toggles the LEDs.

Few things for you to look at though:

You can't use the == operator on a port like you did in the select statement.

The buttons are normally high and low when pressed.

A skeleton for your case could be something like this (i prefer not using the parameterized select):

Code: Select all

select {
    
  // case if sw1 has the value of 0
  case sw1 when pinseq(0) :> void:
    // sw1 was pressed, do your debouncing thing
    break;
    
  case sw2 when pinseq(0) :> void:
    // sw1 was pressed, do your debouncing thing
    break;
    
  default:
    break;   
}
daleonpz
Member++
Posts: 26
Joined: Thu Nov 04, 2010 1:18 pm

Postby daleonpz » Mon Dec 27, 2010 4:35 am

Bianco wrote:I'm not sure what you are trying to reach. You say that the leds must turn on or off when buttons are pressed, yet i see some timer which toggles the LEDs.

Few things for you to look at though:

You can't use the == operator on a port like you did in the select statement.

The buttons are normally high and low when pressed.

A skeleton for your case could be something like this (i prefer not using the parameterized select):

Code: Select all

select {
    
  // case if sw1 has the value of 0
  case sw1 when pinseq(0) :> void:
    // sw1 was pressed, do your debouncing thing
    break;
    
  case sw2 when pinseq(0) :> void:
    // sw1 was pressed, do your debouncing thing
    break;
    
  default:
    break;   
}
I did what you have written and I worked really nice. But when I was reading the manual I read that pinseq means that the port is sampling the data on input edges until the value is equal to the value... does it mean that the proccesor is waiting for that and wasting proccesing time?
User avatar
Bianco
XCore Expert
Posts: 754
Joined: Thu Dec 10, 2009 6:56 pm

Postby Bianco » Tue Dec 28, 2010 8:38 pm

daleonpz wrote:
Bianco wrote:I'm not sure what you are trying to reach. You say that the leds must turn on or off when buttons are pressed, yet i see some timer which toggles the LEDs.

Few things for you to look at though:

You can't use the == operator on a port like you did in the select statement.

The buttons are normally high and low when pressed.

A skeleton for your case could be something like this (i prefer not using the parameterized select):

Code: Select all

select {
    
  // case if sw1 has the value of 0
  case sw1 when pinseq(0) :> void:
    // sw1 was pressed, do your debouncing thing
    break;
    
  case sw2 when pinseq(0) :> void:
    // sw1 was pressed, do your debouncing thing
    break;
    
  default:
    break;   
}
I did what you have written and I worked really nice. But when I was reading the manual I read that pinseq means that the port is sampling the data on input edges until the value is equal to the value... does it mean that the proccesor is waiting for that and wasting proccesing time?
No the ports are smart and can operate without continuous CPU intervention (i.e serializing, deserializing etc). Pinseq and pinsneq are conditional inputs. As long as the condition is not met your thread will be blocked, which allows other threads to use the CPU time (in case you run more than 4 threads).

Who is online

Users browsing this forum: No registered users and 3 guests