Created
November 24, 2010 13:11
-
-
Save folknology/713631 to your computer and use it in GitHub Desktop.
TFL-Blinken xc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <xs1.h> | |
#define BLD 1000000 | |
#define TLD 200000000 | |
out port biled = XS1_PORT_1F ; | |
in port button_0 = XS1_PORT_1K ; | |
in port button_1 = XS1_PORT_1L ; | |
enum cmds {STOP,START,ADVANCE,TOGGLE}; | |
tl_control(out port bl, chanend cntrl){ | |
enum tl_states {RED,GREEN,AMBER}; | |
enum bl {OFF,ON}; | |
int delay = TLD; | |
int tl_state = RED; | |
int bl_state = ON; | |
int free_running = ON; | |
unsigned int tl_delay; | |
unsigned int bl_delay; | |
int cmd; | |
timer tlt; | |
timer blt; | |
tlt :> tl_delay; | |
blt :> bl_delay; | |
// by using a buffered port colour could be control much more simply by the port being loaded with different values x00 green, xFF red and xAA for amber! | |
while (1) | |
{ | |
select | |
{ | |
case cntrl :> cmd : | |
{ | |
switch(cmd) | |
{ | |
case STOP : | |
{ | |
tl_state = AMBER; | |
break; | |
} | |
case START : | |
{ | |
tl_state = GREEN; | |
bl <: bl_state = ON; | |
break; | |
} | |
case ADVANCE : | |
{ | |
tl_state = (tl_state == AMBER) ? RED : tl_state + 1; | |
if(tl_state != AMBER) bl <: bl_state = tl_state; | |
break; | |
} | |
case TOGGLE : | |
{ | |
free_running = free_running ? 0 : 1; | |
break; | |
} | |
default : | |
delay = cmd; | |
break; | |
} | |
break; | |
} | |
case tlt when timerafter(tl_delay) :> void: | |
{ | |
tl_delay += delay; | |
if(free_running){ | |
tl_state = (tl_state == AMBER) ? RED : tl_state + 1; | |
if(tl_state != AMBER) bl <: bl_state = tl_state; | |
} | |
break; | |
} | |
case blt when timerafter(bl_delay) :> void: | |
{ | |
bl_delay += BLD; | |
if(tl_state == AMBER) | |
bl <: bl_state = bl_state ? 0 : 1; | |
break; | |
} | |
} | |
} | |
} | |
tl_controller(in port mode ,in port state, chanend cntrl){ | |
while(1) | |
{ | |
select | |
{ | |
case mode when pinseq (0) :> void : | |
{ | |
cntrl <: TOGGLE; | |
mode when pinseq (1) :> void ; | |
break ; | |
} | |
case state when pinseq (0) :> void : | |
{ | |
cntrl <: ADVANCE; | |
state when pinseq (1) :> void ; | |
break ; | |
} | |
} | |
} | |
} | |
main(){ | |
chan cntrl; | |
par | |
{ | |
tl_control(biled, cntrl); | |
tl_controller(button_0,button_1 ,cntrl); | |
} | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment