/*******************************************************************************

	music2.c
	Z-World, 2000

	Demonstration of playing "background music" while other processing
	is going on by using costatements.  Keys pressed on the keypad will
	be printed to the display while the music is playing.

	See comments in MUSIC.C for more information on playing music with
	the Intellicom.
	
 *******************************************************************************/

#define NUM_NOTES		12
#define NUM_OCTAVES	 4

// definitions for the notes in each octave
#define	A		0
#define	As		1
#define	B		2
#define	C		3
#define	Cs		4
#define	D		5
#define	Ds		6
#define	E		7
#define	F		8
#define	Fs		9
#define	G		10
#define	Gs		11

const int	freq[NUM_OCTAVES][NUM_NOTES] = {
	{ 440, 466, 494, 523, 554, 587, 622, 659, 698, 740, 784, 831 },
	{ 880, 932, 988, 1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661 },
	{ 1760, 1865, 1976, 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322 },
	{ 3520, 3729, 3951, 4186, 4435, 4699, 4978, 5274, 5588, 5920, 6272, 6645 } };

#define	REST	0x00
	
// note lengths (in milliseconds)
#define	WHOLE		  2500
#define	HALF		 (WHOLE/2)
#define	QUARTER	 (WHOLE/4)
#define	EIGHTH	 (WHOLE/8)
#define	SIXTEENTH (WHOLE/16)

#define	THREEQTR	 (HALF + QUARTER)

// note volume (0-3)
#define VOL	1
 				
////////////////////////////////////////////////////////////////////////////////

main()
{
	int	wKey;

	brdInit();
	keypadDef();		// set default keypad layout
	
	while (1) {			// loop forever

		// large costatement that will play Fur Elise (Beethoven) in the background
		costate {
			spkrOut(freq[2][E], VOL);	waitfor(DelayMs(SIXTEENTH));
			spkrOut(freq[2][Ds], VOL);	waitfor(DelayMs(SIXTEENTH));
		
			spkrOut(freq[2][E], VOL);	waitfor(DelayMs(SIXTEENTH));
			spkrOut(freq[2][Ds], VOL);	waitfor(DelayMs(SIXTEENTH));
			spkrOut(freq[2][E], VOL);	waitfor(DelayMs(SIXTEENTH));
			spkrOut(freq[2][B], VOL);	waitfor(DelayMs(SIXTEENTH));
			spkrOut(freq[2][D], VOL);	waitfor(DelayMs(SIXTEENTH));
			spkrOut(freq[2][C], VOL);	waitfor(DelayMs(SIXTEENTH));
		
			spkrOut(freq[2][A], VOL);	waitfor(DelayMs(EIGHTH));
			spkrOut(REST, VOL);			waitfor(DelayMs(SIXTEENTH));
			spkrOut(freq[1][C], VOL);	waitfor(DelayMs(SIXTEENTH));
			spkrOut(freq[1][E], VOL);	waitfor(DelayMs(SIXTEENTH));
			spkrOut(freq[2][A], VOL);	waitfor(DelayMs(SIXTEENTH));
		
			spkrOut(freq[2][B], VOL);	waitfor(DelayMs(EIGHTH));
			spkrOut(REST, VOL);			waitfor(DelayMs(SIXTEENTH));
			spkrOut(freq[1][E], VOL);	waitfor(DelayMs(SIXTEENTH));
			spkrOut(freq[1][Gs], VOL);	waitfor(DelayMs(SIXTEENTH));
			spkrOut(freq[2][B], VOL);	waitfor(DelayMs(SIXTEENTH));
			
			spkrOut(freq[2][C], VOL);	waitfor(DelayMs(EIGHTH));
			spkrOut(REST, VOL);			waitfor(DelayMs(SIXTEENTH));
			spkrOut(freq[1][E], VOL);	waitfor(DelayMs(SIXTEENTH));
			spkrOut(freq[2][E], VOL);	waitfor(DelayMs(SIXTEENTH));
			spkrOut(freq[2][Ds], VOL);	waitfor(DelayMs(SIXTEENTH));
		
			spkrOut(freq[2][E], VOL);	waitfor(DelayMs(SIXTEENTH));
			spkrOut(freq[2][Ds], VOL);	waitfor(DelayMs(SIXTEENTH));
			spkrOut(freq[2][E], VOL);	waitfor(DelayMs(SIXTEENTH));
			spkrOut(freq[2][B], VOL);	waitfor(DelayMs(SIXTEENTH));
			spkrOut(freq[2][D], VOL);	waitfor(DelayMs(SIXTEENTH));
			spkrOut(freq[2][C], VOL);	waitfor(DelayMs(SIXTEENTH));
			
			spkrOut(freq[2][A], VOL);	waitfor(DelayMs(EIGHTH));
			spkrOut(REST, VOL);			waitfor(DelayMs(SIXTEENTH));
			spkrOut(freq[1][C], VOL);	waitfor(DelayMs(SIXTEENTH));
			spkrOut(freq[1][E], VOL);	waitfor(DelayMs(SIXTEENTH));
			spkrOut(freq[2][A], VOL);	waitfor(DelayMs(SIXTEENTH));

			spkrOut(freq[2][B], VOL);	waitfor(DelayMs(EIGHTH));
			spkrOut(REST, VOL);			waitfor(DelayMs(SIXTEENTH));
			spkrOut(freq[1][E], VOL);	waitfor(DelayMs(SIXTEENTH));
			spkrOut(freq[2][C], VOL);	waitfor(DelayMs(SIXTEENTH));
			spkrOut(freq[2][B], VOL);	waitfor(DelayMs(SIXTEENTH));
		
			spkrOut(freq[2][A], VOL);	waitfor(DelayMs(QUARTER));

			spkrOut(0, 0);					waitfor(DelayMs(WHOLE));
		}

		
		//	costatement to process keypad press/hold/release
		costate {
			keyProcess ();
			waitfor ( DelayMs(10) );
		}

		
		// costatement to handle any key presses			
		costate {
			waitfor (wKey = keyGet());		//	Wait for Keypress
			dispPrintf ("%c", wKey);
		}

	}	// end of while(1)
}

////////////////////////////////////////////////////////////////////////////////

