/**
 * grab_pipe.c
 *
 * Written by Evan Raskob 2002-2003
 * :: devices [at] lowfrequency [dot] org ::
 *
 * pipe-based dj/saxophone audio/video controller
 *
 * Platform: PIC18F248
 * Uses 4 momentary switches on (top to bottom) RB7,RB6,RB5,RB4
 * Uses 2 ADC channels for a crossfader potentiometer RA,RA1
 * Uses 2 ADC channels for an accelerometer (y,x) RA2,RA3
 * Uses 1 ADC channel for rotary pot RA4
 *
 *
 * Not for use without permission of the author.
 */


/* Standard Include for 16F876 Chip */
#include <18F248.h>
/* library of midi functions (expanded in midi.c) */
#include "midi.h"

#device PIC18F248    ADC=10

//this is the ADC control register
#byte ADCON0   =     0x1F

#define  HIGH     0x1
#define  LOW      0x0



#FUSES HS,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP

/* Delay for 20 mhz crystal */
#use delay (clock=20000000)

/* Setup buit-in RS232 serial ports to work work at MIDI speeds */
#use rs232(baud=MIDI_BAUD_RATE, xmit=PIN_C6,rcv=PIN_C7)

int sendByte (byte byteToSend) {
        if (byteToSend < 0) output_bit(PIN_B1, HIGH);

        putc(byteToSend);
        return 0;
}

/** DEBUG **
//Setup buit-in RS232 serial ports to work work at serial port speeds
#use rs232(baud=9600, xmit=PIN_C4,rcv=PIN_C5)
** END DEBUG **/

#use standard_io(B)


// send a midi NOTE ON message from the uart of the form [0x9n, note, vel]
// where n is the midi channel from 0-F, note and vel are 7-bit numbers
int midiNoteOn(byte channel, byte note, byte vel) {
        sendByte(MIDI_NOTE_ON | (channel & MIDI_CHANNEL_MASK));
        sendByte(MIDI_DATA_MASK & note);
        sendByte(MIDI_DATA_MASK & vel);
        return 0;
}


inline void setLeftLED(int state) {
   output_bit(PIN_B4, state);
}
inline void setCenterLED(int state) {
   output_bit(PIN_B3, state);
}
inline void setRightLED(int state) {
   output_bit(PIN_B2, state);
}

main()
{

   // 3 momentary switches
   // hooked up to pins B7-5
   int8 leftSwitch   = 0;
   int8 note1 = 67;
   // hooked up to pins B6
   int8 centerSwitch   = 0;
   int8 note2 = 68;
   // hooked up to pins B5
   int8 rightSwitch   = 0;
   int8 note3 = 69;

   int8 tempSwitch= 0;

   bit_set(ADCON0,7);

   delay_ms(500);

   setup_adc_ports( ALL_ANALOG );
   setup_adc(ADC_CLOCK_INTERNAL );

   //flash lights - say "hello"
   setLeftLED(HIGH);
   delay_ms(600);
   setLeftLED(LOW);
   setCenterLED(HIGH);
   delay_ms(600);
   setCenterLED(LOW);
   setRightLED(HIGH);
   delay_ms(600);
   setRightLED(LOW);
   delay_ms(200);


   while (TRUE)
   {

      //switches
      tempSwitch = input(PIN_B7);
      if (tempSwitch != leftSwitch) {
        leftSwitch = tempSwitch;
        midiNoteOn(15, note1, leftSwitch*64);
        if (leftSwitch == 1) {
         setLeftLED(HIGH);
        } else {
         setLeftLED(LOW);
        }
      }

      tempSwitch = input(PIN_B6);
      if (tempSwitch != centerSwitch) {
        centerSwitch = tempSwitch;
        midiNoteOn(15, note2, centerSwitch*64);
        if (centerSwitch == 1) {
         setCenterLED(HIGH);
        } else {
         setCenterLED(LOW);
        }
      }

      tempSwitch = input(PIN_B5);
      if (tempSwitch != rightSwitch) {
        rightSwitch = tempSwitch;
        midiNoteOn(15, note3, rightSwitch*64);
        if (rightSwitch == 1) {
         setRightLED(HIGH);
        } else {
         setRightLED(LOW);
        }
      }

   }

 }
