Process 2 ports at the same time

New to XMOS and XCore? Get started here.
User avatar
feniksa
Member++
Posts: 22
Joined: Tue Apr 03, 2012 1:12 pm

Process 2 ports at the same time

Post by feniksa »

I have 2 ports for 2 buttons on XK-1A devkit.
I want to make very simple task:
1. when pressed Button1 -> Led1 is on
2. when pressed Button2 -> Led2 is on

But i got stuck with processing 2 buttons at the same time. Problem, that i freeze in processing of button. How i can modify select statement to correctly process two ports at the same time?

Code: Select all

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

on stdcore[0] : out port PortOutLed = XS1_PORT_4F;
on stdcore[0] : in port PortInButton1 = XS1_PORT_1K;
on stdcore[0] : in port PortInButton2 = XS1_PORT_1L;

const unsigned int FlashInterval = 10000000;	// one second

void ledMain(out port ledPort, chanend ledValue);
void buttonMain(in port portInButton1, in port portInButton2, chanend ledValueChan);

int main()
{
	chan ledValueChan;
	//XS1_TIMER_HZ;

	par
	{
		on stdcore[0] : ledMain(PortOutLed, ledValueChan);
		on stdcore[0] : buttonMain(PortInButton1, PortInButton2, ledValueChan);
	}

	return 0;
}

void buttonMain(in port portInButton1, in port portInButton2, chanend ledValueChan)
{
	unsigned int currentLedValue = 0;
	unsigned portValue;

	ledValueChan <: currentLedValue;

	while(1)
	{
		select
		{
			case portInButton1 when pinseq(0) :> void:
				printf("pressed portInButton1\n");

				currentLedValue |= 0b0001;
				ledValueChan <: currentLedValue;

				portInButton1 when pinseq(1) :> void;

				printf("released portInButton1\n");
				currentLedValue &= 0b1110;
				ledValueChan <: currentLedValue;
			break;
			case portInButton2 when pinseq(0) :> void:
				printf("pressed portInButton1\n");

				currentLedValue |= 0b0010;
				ledValueChan <: currentLedValue;

				portInButton2 when pinseq(1) :> void;

				printf("released portInButton2\n");
				currentLedValue &= 0b1101;
				ledValueChan <: currentLedValue;
			break;
		}
	}
}

void ledMain(out port ledPort, chanend ledValueChan)
{
	timer hardwareTimer;
	unsigned int currentTime;
	unsigned int ledIsOn = 1;
	unsigned int ledValue = 0;

	hardwareTimer :> currentTime;

	while (1)
	{
		select
		{
			case hardwareTimer when timerafter(currentTime) :> void:
				if (ledIsOn) {
					ledPort <: ledValue;
					currentTime += 1000000 * 9 / 10;
				} else {
					ledPort <: ledValue;
					currentTime += 1000000 / 10;
				}
				ledIsOn = !ledIsOn;

			break;

			case ledValueChan :> ledValue:
			break;
		}
	}
}



mmar
Experienced Member
Posts: 123
Joined: Fri Jul 05, 2013 5:55 pm

Post by mmar »

Try simply add to your selects empty
default: break;
User avatar
feniksa
Member++
Posts: 22
Joined: Tue Apr 03, 2012 1:12 pm

Post by feniksa »

Hehey, i found easy and good performance solution. See function buttonMain:

Code: Select all

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

on stdcore[0] : out port PortOutLed = XS1_PORT_4F;
on stdcore[0] : in port PortInButton1 = XS1_PORT_1K;
on stdcore[0] : in port PortInButton2 = XS1_PORT_1L;

const unsigned int FlashInterval = 10000000;	// one second

void ledMain(out port ledPort, chanend ledValue);
void buttonMain(in port portInButton1, in port portInButton2, chanend ledValueChan);

int main()
{
	chan ledValueChan;
	//XS1_TIMER_HZ;

	par
	{
		on stdcore[0] : ledMain(PortOutLed, ledValueChan);
		on stdcore[0] : buttonMain(PortInButton1, PortInButton2, ledValueChan);
	}

	return 0;
}

void buttonMain(in port portInButton1, in port portInButton2, chanend ledValueChan)
{
	unsigned int currentLedValue = 0;

	unsigned int pin1Pressed = 0;
	unsigned int pin2Pressed = 0;

	unsigned int counter = 0;

	while(1)
	{
		select
		{
			case portInButton1 when pinseq(pin1Pressed) :> void:
				if (pin1Pressed == 0) {
					printf("pressed portInButton1\n");

					currentLedValue |= 0b0001;
					ledValueChan <: currentLedValue;
				} else {
					printf("released portInButton1\n");

					currentLedValue &= 0b1110;
					ledValueChan <: currentLedValue;
				}

				pin1Pressed = !pin1Pressed;
			break;

			case portInButton2 when pinseq(pin2Pressed) :> void:
				if (pin2Pressed == 0) {
					printf("pressed portInButton2\n");

					currentLedValue |= 0b0010;
					ledValueChan <: currentLedValue;
				} else {
					printf("released portInButton2\n");

					currentLedValue &= 0b1101;
					ledValueChan <: currentLedValue;
				}

				pin2Pressed = !pin2Pressed;
			break;
		} // end of select

		++counter;
		printf("counter: %i", counter);
	}
}

void ledMain(out port ledPort, chanend ledValueChan)
{
	timer hardwareTimer;
	unsigned int currentTime;
	unsigned int ledIsOn = 1;
	unsigned int ledValue = 0;

	hardwareTimer :> currentTime;

	while (1)
	{
		select
		{
			case hardwareTimer when timerafter(currentTime) :> void:
				if (ledIsOn) {
					ledPort <: ledValue;
					currentTime += 1000000 * 9 / 10;
				} else {
					ledPort <: ledValue;
					currentTime += 1000000 / 10;
				}
				ledIsOn = !ledIsOn;

			break;

			case ledValueChan :> ledValue:
			break;
		}
	}
}