/* FINAL VERSION of the Ghetto Drum System code
   (not very pretty, but it works)

   Last modified 10/27/01 - jjp 
   - Added checks/outputs for all 8 inputs
   - Added toggle for LED while pulse is high

   Target: PIC 16F84
   
*/

/* Pin definitions

	RA0 - Serial receive from PC
	RA1 - Serial send to PC
	RA2 - LED output
	RB0..RB7 - Input for drum triggers
*/

#pragma RS232_RXPORT PORTA
#pragma RS232_TXPORT PORTA
#pragma RS232_RXPIN 0
#pragma RS232_TXPIN 1
//#pragma RS232_BAUD 9600
//#pragma RS232_BAUD 19200
#pragma RS232_BAUD 38400
#pragma TRUE_RS232 1
#pragma CLOCK_FREQ 10000000

/* Function defs */
void serial_printf(const char *c);
void beepled();
void waitforlow(char bitmask);


/* Main routine */
main(){
    
	char i, c;
    disable_interrupt( GIE );
    set_bit( STATUS, RP0 );		//Select bank 1
	TRISB = 0xFF;				//Set PORTB to all input
	TRISA = 0x01;				//Set RA0 as input, rest output
    clear_bit( STATUS, RP0 );	//Select bank 0

	PORTA = 0x02;				//RS232 idles high, set pin RA1 high
		
	for(i = 0; i < 3; i++){
		beepled();
	}//for
		
	while(1){

		if(PORTB & 00000001b){			//If pin 0 high
			PORTA = (PORTA | 00000100b);//Turn LED on
			putchar('0');				//Send to serial port
			waitforlow(00000001b);		//Wait for pin to go back low
			PORTA = (PORTA & 11111011b);//Turn LED back off
		}
		if(PORTB & 00000010b){
			PORTA = (PORTA | 00000100b);//Turn LED on
			putchar('1');
			waitforlow(00000010b);
			PORTA = (PORTA & 11111011b);//Turn LED back off
		}
		if(PORTB & 00000100b){
			PORTA = (PORTA | 00000100b);//Turn LED on
			putchar('2');
			waitforlow(00000100b);
			PORTA = (PORTA & 11111011b);//Turn LED back off
		}
		if(PORTB & 00001000b){
			PORTA = (PORTA | 00000100b);//Turn LED on
			putchar('3');
			waitforlow(00001000b);
			PORTA = (PORTA & 11111011b);//Turn LED back off
		}
		if(PORTB & 00010000b){
			PORTA = (PORTA | 00000100b);//Turn LED on
			putchar('4');
			waitforlow(00010000b);
			PORTA = (PORTA & 11111011b);//Turn LED back off
		}
		if(PORTB & 00100000b){
			PORTA = (PORTA | 00000100b);//Turn LED on
			putchar('5');
			waitforlow(00100000b);
			PORTA = (PORTA & 11111011b);//Turn LED back off
		}
		if(PORTB & 01000000b){
			PORTA = (PORTA | 00000100b);//Turn LED on
			putchar('6');
			waitforlow(01000000b);
			PORTA = (PORTA & 11111011b);//Turn LED back off
		}
		if(PORTB & 10000000b){
			PORTA = (PORTA | 00000100b);//Turn LED on
			putchar('7');
			waitforlow(10000000b);
			PORTA = (PORTA & 11111011b);//Turn LED back off
		}
		
	}//while

}//main function

/* This function waits for a pin(s) to go low on PORTB. */
void waitforlow(char bitmask){
	while(PORTB & bitmask){
	}
}//waitforlow

//This function will print a null terminated string to the 
//serial port.
void serial_printf(const char *c){
    char i = 0;
    while(c[i]!=0){
        putchar(c[i]);
		i++;
    }//while
}//serial_printf

void beepled(){
      output_high_port_a(2);
      delay_ms(250);
      output_low_port_a(2);
      delay_ms(250);
}//beepled

