/*
	SC1SEQ.c

	CM71xx/Z1 and CM72xx/Z2
	
	Select a LED blink sequence per pushbutton switch pushed. When a selection
	is chosen, the beeper beeps for 100 ms. Four patterns are defined in SEQ1,
	SEQ2, SEQ3 and SEQ4.
*/

#use vdriver.lib
#define BeepScale 0.04		// scale the time in up_beep(ms) to turn on beeper
									// for the correct millisecond ON time. Since
									// lc_beepscan is called every 25ms, 1/25 = 0.04. 

char SEQ1[] = {'\B00001000', '\B00000100', '\B00000010', '\B00000001'};
char SEQ2[] = {'\B00001111', '\B00000000', '\B00001111', '\B00000000'};
char SEQ3[] = {'\B00000011', '\B00001100', '\B00001100', '\B00000011'};
char SEQ4[] = {'\B00001110', '\B00001101', '\B00001011', '\B00000111'};

void out_leds(data);
main(){
	char *theseq;
	char *whichseq;
	char count; 
	int input1;
	int input2;
	VdInit();									// initialize the virtual driver

	theseq = whichseq = SEQ1;				// start with sequence 1
	count = 4;
	for(;;){										// endless loop
		costate{
			waitfor(DelayMs(25L));			// monitors beeper every 25 ms.
			lc_beepscan();
		}
		costate{
			waitfor(( input1 = inport(CS2+1) & 0x0F) != 0xF );	// get a pushbutton
			waitfor(((input2 = inport(CS2+1) & 0x0F) != input1) || DelayMs(25L) );
			if( input2 == input1 ){				// debounce the input
				up_beep(100);						// beep for 100 ms. 
				switch( input1 ){
					case '\B00001110':			// SW2 pressed
						whichseq = SEQ1; break;
					case '\B00001101':      	// SW3 pressed
						whichseq = SEQ2; break;
					case '\B00001011':      	// SW4 pressed
						whichseq = SEQ3; break;
					case '\B00000111':      	// SW5 pressed
						whichseq = SEQ4; break;
				}
				theseq = whichseq;
				count = 4;
			}
		}
		costate{
			out_leds( *(theseq++) );
			if(--count == 0){
				count = 4;	// each sequence is finished before reloading
				theseq = whichseq;
			}
			waitfor(DelayMs(250L));
		}              
	}
}
/***********************************************************/
void out_leds( char data ){		// only bit 0 is of interest
	outport( CS1  , data     );		
	outport( CS1+1, data >> 1);
	outport( CS1+2, data >> 2);
	outport( CS1+3, data >> 3);
}
/**/