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;
}
}
}