'jdl+cdk 11.4.05: Sample menu code
'All fingers begin locked. To unlock a finger hold down for
'One second. Then tapping the finger will cycle through the
'drum sounds. Once you find the one you like hold the finger
'down again for one second and the finger locks

'Define Clock Speed at 20
DEFINE OSC 20
' Define ADCIN parameters
DEFINE  ADC_BITS        10     ' Set number of bits in result
DEFINE  ADC_CLOCK       3      ' Set clock source (3=rc)
DEFINE  ADC_SAMPLEUS    15'50     ' Set sampling time in uS
'Enable communication out to MIDI
DEFINE HSER_RCSTA 90h ' enable the receive register
DEFINE HSER_TXSTA 20h  ' enable the transmit register
DEFINE HSER_BAUD 31250 ' set the baud rate

TRISA = %11111111       ' Set PORTA to all input
ADCON1 = %10000010      ' Set PORTA analog and right justify result

'Declare CONSTANTS AND COUNTER VARIABLEs
    numinput CON 8
    numlibrary CON 8
    locktime CON 300
    menuth CON 300
    i VAR BYTE
    timecount VAR WORD

'Declare input VARIABLE
    ADCmenu VAR WORD

'Declare ARRAYS
    hold VAR WORD[numinput]
    sounds VAR BYTE[numinput]
    soundlib VAR BYTE[numinput]
    lock VAR BIT[numinput]  '1 is open to change, 0 is locked

'initialize sounds ARRAY
    FOR i=0 TO (numinput-1)
        sounds[i]=i
    NEXT

'initialize soundlib ARRAY
    soundlib[0]=35
    soundlib[1]=38
    soundlib[2]=42
    soundlib[3]=46
    soundlib[4]=49
    soundlib[5]=35
    soundlib[6]=38
    soundlib[7]=42
    
'intialize lock as locked
    FOR i=0 TO (numinput-1)
	   lock[i]=0
    NEXT

menu:
	
    FOR i = 0  TO (numinput-1)
		timecount=0
		'read input
		ADCIN i, ADCmenu
		hold[i]=ADCmenu	
		IF hold[i] > menuth THEN
			WHILE hold[i] > menuth
				timecount=timecount+1
				'pause
				PAUSE 2
				ADCIN i, ADCmenu
				hold[i]=ADCmenu
			WEND
			IF timecount > locktime THEN
				lock[i]=lock[i] +1  'lock is a bit so this flips it
			ELSE
				IF lock[i]==1 THEN 'system open: change note
					sounds[i]=(sounds[i] + 1) // (numlibrary-1)
				ENDIF			
				HSEROUT [$90, soundlib[sounds[i]] ,70] 'use constant volume			
			ENDIF
		ENDIF
	NEXT

    PAUSE 100 'Helps get rid of noise

GOTO menu