/*
 *  MIDIKey.h
 *  MIDI Keybed Library for the Arduino Leonardo(Atmega32U4) & UNO(Atmega 328) & Mega (Atmega1280, 2560)
 *	Version 1.1
 *  by Kodylab 
 *  http://www.kodylab.com
 *	2012/12/14
 *  License		GPL Forty Seven Effects - 2012
 */

#ifndef LIB_MIDIKEY_H_
#define LIB_MIDIKEY_H_

#include <inttypes.h> 

#define MAX_KEY_SIZE 61	// how many max. key number

#define USE_SERIAL_PORT         Serial      // Change the number (to Serial1 for example) if you want
                                            // to use a different serial port for MIDI I/O.
typedef uint8_t byte;
typedef uint16_t word;

class MIDIKEY_Class {
	
	
public:
	// Constructor and Destructor
	MIDIKEY_Class();
	~MIDIKEY_Class();
	
	void begin(const byte MidiKeyCount = 61, const word Baudrate = 31250);
	
	// Timer & Flag Define		
	word TIMER_COUNTER;
	word UI_COUNTER;
	byte FLAG_PEDAL;
	byte FLAG_BUTTON;
	byte FLAG_STICK;
		
// extern Func.	
	void Scan(void);
	
	// MIDI Lib.
	void SendNoteOn(byte channel, byte note, byte velocity);
	void SendNoteOff(byte channel, byte note, byte velocity);
	void SendControl(byte channel, byte number, byte value);
	void SendProgramChange(byte channel, byte number);
	void SendPitchBend(byte channel, int bend);

	// Callback Func.
	void setHandleNote(void (*fptr)(byte number, byte value, byte velocity));
	void setHandlePedal(void (*fptr)(byte number, byte value));
	void setHandleButton(void (*fptr)(byte number, byte value));
	void setHandleStick(void (*fptr)(byte number, byte value));

	void resetHandleNote(void);
	void resetHandlePedal(void);
	void resetHandleButton(void);
	void resetHandleStick(void);

private:
	// UI Data
	int KEY_NUMBER;	// Keybed Number Count

// Low Data
	byte SCAN_LINE;
	byte KEY_DATA[16];

// Note Calc
	word KEY_TIME[MAX_KEY_SIZE];
	byte KEY_STATUS[MAX_KEY_SIZE];
	byte KEY_TYPE[MAX_KEY_SIZE];

// 
	void KeyScan(void);
	void KeyBitCheck(byte Data);
	void KeyCalc(byte KeyPos, byte KeyType, byte OnOff);
	void KeyNote(byte KeyPos, byte Message);
	void PedalCheck(void);
	void ButtonCheck(void);
	void StickCheck(void);
	
	void (*mNoteCallback)(byte number, byte value, byte velocity);
	void (*mPedalCallback)(byte number, byte value);
	void (*mButtonCallback)(byte number, byte value);
	void (*mStickCallback)(byte number, byte value);
	
};

extern MIDIKEY_Class MIDIKey;

#endif // LIB_MIDIKEY_H_
