/*
	SC1PTTRN.C

	CM71xx/Z1 and CM72xx/Z2

	An LED on/off pattern is displayed. A new pattern is read when any of the
	pushbutton switches are pushed. Wait until all LEDs are off before pushing
	in the new pattern. A beep signals the start of pattern entry. Only the
	first 20 seconds of any inputted pattern is kept. When the switches have
	not been pushed for 2 second, the program switches over to periodic display
	of the stored pattern. A beep also signal the end of data entry. The inputs
	are scanned every 10 ms.
*/

#use vdriver.lib

void showled( int state );
CoData ShowPattern;  // show the current pattern and monitor for new pattern
CoData GetPattern;   // get a pattern, maximum length is 20 seconds.
main(){
	char pattern[2200];	// to store the pattern
	int maxstate;
	int inactivity;
	int instate;
	int i;

	VdInit();      	// initialize the virtual driver
	instate = 0;
	for( i= 0; i< 50;i++ ) pattern[i] = 0x00;		// all off 1/2 sec
	for( i=50; i<100;i++ ) pattern[i] = 0x0F;		// all on  1/2 sec
	maxstate = 99;		// original pattern is only 1 seconds long
	
	i = 0;				// display pattern counter
	for(;;){				// endless loop
		hitwd(); runwatch();
		costate ShowPattern init_on { 
			for(;;){
				waitfor(DelayMs(10L));		// advance pattern every 10 ms
				showled( pattern[i++] );
				if( i > maxstate) i = 0;	// and repeat. But...
				
			// detect switch activity				
				if((inport(CS2+1)&0x0F) != 0x0F){ 
					showled(0x05);	waitfor(DelaySec(1L));	// 0101
					showled(0x0A);	waitfor(DelaySec(1L));	// 1010
					showled(0x05);	waitfor(DelaySec(1L));	// 0101
					showled(0x0A);	waitfor(DelaySec(1L));	// 1010
					showled(0x0F);          // all leds off
					outport(CS1+4,1);       // turn on beeper
					waitfor(DelayMs(150L)); // wait 150 ms
					outport(CS1+4,0);       // turn off beeper
					maxstate = 0;           // reset variables and start GetPattern
					inactivity = 0;
					i = 0;
					CoBegin(&GetPattern);	// start GetPattern 
					abort;						// kills ShowPattern
				}
			}
		}
		costate GetPattern {					// (initially off, by the way)
			while(1)			{
				waitfor(DelayMs(10L));     // get data every 10 ms
				instate = inport(CS2+1) & 0x0F;
				showled( instate );			// display the state
				if( instate == 0x0F ){		// nothing pressed
					if( ++inactivity >= 200 ){	// see if there is no input for 2 secs    
						if(maxstate == 0) maxstate = i - 200; // do not need last part
						i = 0;
						outport(CS1+4,1);          // turn beeper on
						waitfor(DelayMs(150L));    // wait for 150 ms
						outport(CS1+4,0);          // turn beeper off
						CoBegin(&ShowPattern);     // Start ShowPattern 
						abort;							// Kills GetPattern
					}
				}else{
					inactivity = 0;
				}
				pattern[i++] = instate;		// store the input
				if( i >= 2200 ){				// if we max out, roll over
					i = 0;
					maxstate = 2199;
				}
			}                    
		}
	}
}           

// Display the state of lcd. A 1 bit is LED off. A 0 bit is LED on. State
// contains the 4 bit information for the LEDs. Often from the pushbuttons.
// bit 0 is for LED D2. bit 1 is for LED D3.
// bit 2 is for LED D4. bit 3 is for LED D5.

void showled( int state ){					// only bit 0 is of interest
	outport( CS1  , ~state      );	
	outport( CS1+1, ~(state>>1) );
	outport( CS1+2, ~(state>>2) );
	outport( CS1+3, ~(state>>3) );
}










