/**
 * footcontroller.c
 *
 * Written by Evan Raskob 2002-2003
 * :: devices [at] lowfrequency [dot] org ::
 *
 * Three momentary switch midi foot controller
 *
 * Platform: PIC16C773
 * Uses 3 momentary footswitches on RB7,RB6,RB5
 * and displays on 3 LEDs wired to RB4,RB3,RB2
 *
 * Not for use without permission of the author.
 */


/* Standard Include for 16F876 Chip */
#include <16C773.h>

/* library of midi functions (expanded in midi.c) */
#include "midi.h"

#FUSES HS,NOWDT,NOPROTECT,NOBROWNOUT

/* 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 (char byteToSend) {
        printf("%c", 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)

#define HIGH 1
#define LOW  0

// 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(char channel, char note, char vel) {
        sendByte(MIDI_NOTE_ON | (channel & MIDI_CHANNEL_MASK));
        sendByte(MIDI_DATA_MASK & note);
        sendByte(MIDI_DATA_MASK & vel);
        return 0;
}

// send a midi NOTE ON message from the uart of the form [0x9n, note, velocity]
// where n is the midi channel from 0-F, note is a 7-bit number and velocity is 0
int midiNoteOFF(char channel, char note) {
        sendByte(MIDI_NOTE_ON | (channel & MIDI_CHANNEL_MASK));
        sendByte(MIDI_DATA_MASK & note);
        sendByte(MIDI_DATA_MASK & 0);
        return 0;
}

// send a midi POLYPHONIC AFTERTOUCH message from the uart of the form [0xCn, controller, value]
// where n is the midi channel from 0-F, note and pressure are 7-bit numbers
int midiControlChange(char channel, char controller, char value) {
        sendByte(MIDI_CONTROL_CHANGE | (channel & MIDI_CHANNEL_MASK));
        sendByte(MIDI_DATA_MASK & controller);
        sendByte(MIDI_DATA_MASK & value);
        return 0;
}

int midiPolyTouch(char channel, char note, char pressure) {
        sendByte(MIDI_POLY_TOUCH | (channel & MIDI_CHANNEL_MASK));
        sendByte(MIDI_DATA_MASK & note);
        sendByte(MIDI_DATA_MASK & pressure);
        return 0;
}

// send a midi CHANNEL AFTERTOUCH message from the uart of the form [0xDn, pressure]
// where n is the midi channel from 0-F, and pressure is a 7-bit number
int midiChannelTouch(char channel, char pressure) {
        sendByte(MIDI_CHANNEL_TOUCH | (channel & MIDI_CHANNEL_MASK));
        sendByte(MIDI_DATA_MASK & pressure);
        return 0;
}


// send a midi PITCH BEND message from the uart of the form [0xEn, bendLSB, bendMSB ]
// where n is the midi channel from 0-F, and bendLSB and bendMSB are 7-bit numbers
// note that MIDI devices normally pack together bendLSB and bendMSB to make a 14-bit number
int midiPitchBend(char channel, char bendLSB, char bendMSB) {
        sendByte(MIDI_PITCH_BEND | (channel & MIDI_CHANNEL_MASK));
        sendByte(MIDI_DATA_MASK & bendLSB);
        sendByte(MIDI_DATA_MASK & bendMSB);
        return 0;
}


void setLeftLED(int state) {
   output_bit(PIN_B4, state);
}
void setCenterLED(int state) {
   output_bit(PIN_B3, state);
}
void setRightLED(int state) {
   output_bit(PIN_B2, state);
}

main()
{

   const int mspb =  150;  //(int) ((1000/16)*1/(120*60));

   // 4 magnetic momentary switches
   // hooked up to pins B7 B6 B5
   int leftSwitch   = 0;
   int centerSwitch = 0;
   int rightSwitch  = 0;

   int tempSwitch   = 0;

   //DEBUG:
   setLeftLED(HIGH);
   delay_ms(300);
   setLeftLED(LOW);
   setCenterLED(HIGH);
   delay_ms(300);
   setCenterLED(LOW);
   setRightLED(HIGH);
   delay_ms(300);
   setRightLED(LOW);

   /**
    * This next block sets up RA3 and RA2 as reference pins
    * (one must be shorted to ground, other to power)
    * and A0 and A1 as the two ADC input pins
    */
   /** NOT NEEDED **
   setup_adc(ADC_CLOCK_DIV_32);
   setup_adc_ports(AN0_TO_AN1_ANALOG);
   set_adc_channel(0);  // set to read from PIN_A0
   ** NOT NEEDED **/

   // DEBUG: print to console
   //printf("Started.");

   while (TRUE)
   {

	/** DEBUG ******
      //channel, note, velocity
      midiNoteOn(0x9A, 0x33, 0x48);
      delay_ms(500);
      //channel, note
      midiNoteOff(0x9A, 0x33);
     ** END DEBUG **/


      //footswitches
      tempSwitch = input(PIN_B7);
      if (tempSwitch != leftSwitch) {
        //switch state has changed
        setLeftLED(tempSwitch);
        if (tempSwitch ==1) midiControlChange(0, 1, 1);
        leftSwitch = tempSwitch;
      }

      tempSwitch = input(PIN_B6);
      if (tempSwitch != centerSwitch) {
        //switch state has changed
        setCenterLED(tempSwitch);
        if (tempSwitch ==1) midiControlChange(0, 1, 2);
        centerSwitch = tempSwitch;
      }

      tempSwitch = input(PIN_B5);
      if (tempSwitch != rightSwitch) {
        //switch state has changed
        setRightLED(tempSwitch);
        if (tempSwitch ==1) midiControlChange(0, 1, 3);
        rightSwitch = tempSwitch;
      }
   }

 }
