//-----------------------------------------------------------------------------
//------------ Copyright (c)2003-2018 Xylio. All Rights Reserved. -------------
//-----------------------------------------------------------------------------
// file implementing MIDI controller 'Hercules DJ Control Steel'
//-----------------------------------------------------------------------------
function getLanguageVersion()
{
	return 1;// do not modify this, this should be 1 for all scripts
}
//-----------------------------------------------------------------------------
function getFileVersion()
{
	return 2;// update this as needed
}
//-----------------------------------------------------------------------------
// return a user friendly name for the controller supported
function getControllerUserFriendlyName()
{
	return "Hercules DJ Control Steel";
}
//-----------------------------------------------------------------------------
// return the communication type that the controller supports
// this can be either 'midi' or 'hid' (please return only one of them in case the controller supports both)
function getControllerType()
{
	return 'midi';
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// preferred way to identify the controller (using the USB device VID and PID -- vendor and product id)
// return false if the controller does not support this kind of identification
// osId -- 0 Windows; 1 MacOSX; 2 Linux; -1 unknown
function getSupportedHidDeviceVID(osId)
{
	return false;
}
//-----------------------------------------------------------------------------
function getSupportedHidDevicePID(osId)
{
	return false;
}
//-----------------------------------------------------------------------------
// this is NOT the preferred way to identify the controller but you should use
// it if the controller does not support standard USB VID and PID identification
// return false if the controller does not support this kind of identification
// osId -- 0 Windows; 1 MacOSX; 2 Linux; -1 unknown
function getSupportedHidDeviceName(osId)
{
	return false;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// preferred way to identify the controller (using the USB device VID and PID -- vendor and product id)
// return false if the controller does not support this kind of identification
// osId -- 0 Windows; 1 MacOSX; 2 Linux; -1 unknown
function getSupportedMidiDeviceVID(osId)
{
	return 0x06f8;
}
//-----------------------------------------------------------------------------
function getSupportedMidiDevicePID(osId)
{
	return 0xb102;// DJ Control Steel
}
//-----------------------------------------------------------------------------
// preferred way to identify the controller (if it supports MIDI SysEx)
// return false if the controller does not support this kind of identification
function getSupportedMidiSysExIdString(osId)
{
	// sysex response to 'F0 7E 7F 06 01 F7' (identification request sysex)
	// please note that the response contains 5 bytes in hex format containing
	// the Manufacturer ID (3 bytes) and Model Number (2 bytes)
	// ATTN: the returned value should omit the standard 'F0 7E 00 06 02' message start
	//if(osId==1) return false;
	return "00014E010005";
	// F0 7E 00 06 02 00 01 4E 02 00 02 00 01 00 00 00 F7 -- Hercules DJ Console MK2 complete sysex
	// F0 7E 00 06 02 00 01 4E 01 00 03 00 01 00 00 00 F7 -- Hercules DJ Control MP3 complete sysex
	// F0 7E 00 06 02 00 01 4E 02 00 04 00 01 00 00 00 F7 -- Hercules DJ Console RMX complete sysex
	// F0 7E 00 06 02 00 01 4E 01 00 05 00 01 00 00 00 F7 -- Hercules DJ Control Steel complete sysex
	// F0 7E 00 06 02 00 01 4E 01 00 07 00 01 00 00 00 F7 -- Hercules DJ Control MP3e2 complete sysex
	// F0 7E 00 06 02 00 01 4E 02 00 08 00 01 00 00 00 F7 -- Hercules DJ Console MK4 complete sysex
}
//-----------------------------------------------------------------------------
// this is NOT the preferred way to identify the controller but you should use
// it if the controller does not support standard MIDI SysEx identification
// return false if the controller does not support this kind of identification
// osId -- 0 Windows; 1 MacOSX; 2 Linux; -1 unknown
function getSupportedMidiDeviceNameForInput(osId)
{
	//if(osId==1) return "DJ Console Steel";
	return false;
}
//-----------------------------------------------------------------------------
function getSupportedMidiDeviceNameForOutput(osId)
{
	//if(osId==1) return "DJ Console Steel";
	return false;
}
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// some variables necessary only for this particular script
var jogWheelsInScratchMode=0;
// used for the 2 selectable encoders
var cutoffValue=[63,63,63,63];
var pitchkeyValue=[63,63,63,63];
//-----------------------------------------------------------------------------
// this is called when the controller is initialized and the control is passed to this script
// use this to initialize any variables you might need, the controller' leds, etc
// uniqueId is a unique integer between 0..15 that is guaranteed not to change for
// a certain opened controller -- please note that there can be many controllers
// of the same type in use at the same time ! Use uniqueId to identify between them
function controllerInit(uniqueId)
{
	debug('controllerInit Hercules DJ Control Steel ['+uniqueId+']');// this outputs a string to DebugView app on Win
	//sendMidi(uniqueId, status, channel, data1, data2);
	sendMidi(uniqueId,0xB0,0,0x29,(jogWheelsInScratchMode ? 0x7F : 0x00));// scratch mode
	// set to off some not-used leds
	sendMidi(uniqueId,0xB0,0,0x11,0x00);// A pitch bend -
	sendMidi(uniqueId,0xB0,0,0x08,0x00);// A pitch bend +
	sendMidi(uniqueId,0xB0,0,0x20,0x00);// B pitch bend -
	sendMidi(uniqueId,0xB0,0,0x15,0x00);// B pitch bend +
	sendMidi(uniqueId,0xB0,0,0x0D,0x00);// A stop (cueplay)
	sendMidi(uniqueId,0xB0,0,0x25,0x00);// B stop (cueplay)
	call('sleep', 10);// delay for 10ms to allow the MIDI out buffer to flush
	//------------------------------------
	callSendAllNotifications(uniqueId, true);// send all notifications to the controller (so that we don't have to manually init all leds/lcd screens)
}
//-----------------------------------------------------------------------------
// called just before the controller will be deinitialized and the control will go away from the script
// use to power off any powered leds, etc
function controllerDeinit(uniqueId)
{
	debug('controllerDeinit Hercules DJ Control Steel ['+uniqueId+']');
	//------------------------------------
	callSendAllNotifications(uniqueId, false);// send all notifications to the controller (so that we don't have to manually de-init all leds/lcd screens)
	// set leds off
	sendMidi(uniqueId,0xB0,0,0x29,0x00);// scratch mode
}
//-----------------------------------------------------------------------------
// value is 0x7F for left (-1); 0x01 for right(+1);
function trimEncoderValue(value, increment, initialValue)
{
	var outValue = 0;
	var outIncrement = 0;
	if(value>0x40) // left (decrease)
		outIncrement = increment * (value - 0x80);
	else // left (increase)
		outIncrement = increment * (value);
	outValue = initialValue + outIncrement;
	if(outValue<0) outValue = 0;
	if(outValue>0x7F) outValue = 0x7F;
	return outValue;
}
//-----------------------------------------------------------------------------
// this is your main target function to implement the controller -- this is called
// for each and every MIDI message that comes from the controller itself
// return true if the message was processed by the script and false to let it
// pass thru the software (eg. so it can be assigned by the user using the LEARN function)
function messageReceived(uniqueId,status,channel,data1,data2)
{
	var messageNotProcessed=false;
	var tmp=0;
	switch(status)
	{
	//-----------------------------------------------
	// controllers: sliders, knobs, jog-wheels, encoders ('moving' things)
	case 0xB0:
		switch(data1)
		{
		// global
		case 0x37: call('setMasterBalance', data2); break;
		case 0x38: call('setMasterVolume', data2); break;
		case 0x39: call('setCrossfader', data2); break;
		case 0x3A: call('setCuePgmMix', data2); break;
		case 0x47: call('setMonitorVolume', data2); break;

		// the 2 selectable encoders (3 modes: DeckA/Select/DeckB) sends 0x7F for left; 0x01 for right;
		case 0x41: cutoffValue[0]   = trimEncoderValue(data2, 3, cutoffValue[0]);   call('setDeckCutoff',   0, cutoffValue[0]);   break;// control 1 encoder (Deck A mode)
		case 0x42: pitchkeyValue[0] = trimEncoderValue(data2, 3, pitchkeyValue[0]); call('setDeckPitchKey', 0, pitchkeyValue[0]); break;// control 2 encoder (Deck A mode)
		case 0x43: cutoffValue[1]   = trimEncoderValue(data2, 3, cutoffValue[1]);   call('setDeckCutoff',   1, cutoffValue[1]);   break;// control 1 encoder (Deck B mode)
		case 0x44: pitchkeyValue[1] = trimEncoderValue(data2, 3, pitchkeyValue[1]); call('setDeckPitchKey', 1, pitchkeyValue[1]); break;// control 2 encoder (Deck B mode)
		case 0x45: tmp = trimEncoderValue(data2, 3, getValue('cfAudio')); call('setCFAudio', tmp); break;// control 1 encoder (Select mode)
		case 0x46: tmp = trimEncoderValue(data2, 3, getValue('cfVideo')); call('setCFVideo', tmp); break;// control 2 encoder (Select mode)

		// per deck
		// deck A
		case 0x36: call('setDeckEqLow', 0, data2); break;
		case 0x35: call('setDeckEqMid', 0, data2); break;
		case 0x34: call('setDeckEqHigh', 0, data2); break;
		case 0x32: call('setDeckVolume', 0, data2); break;
		case 0x33: call('setDeckGain', 0, data2); break;
		case 0x31: call('setDeckPitch', 0, data2); break;// increment mode
		case 0x2F: // params: (deck, increment, total positions per complete revolution)
			var val1 = (data2>0x40 ? data2-0x80 : data2);
			if(jogWheelsInScratchMode) call('scratchDeckJW', 0, val1, 128);else call('moveDeckJogWheel', 0, val1, 128);
			break;

		// deck B
		case 0x40: call('setDeckEqLow', 1, data2); break;
		case 0x3F: call('setDeckEqMid', 1, data2); break;
		case 0x3E: call('setDeckEqHigh', 1, data2); break;
		case 0x3C: call('setDeckVolume', 1, data2); break;
		case 0x3D: call('setDeckGain', 1, data2); break;
		case 0x3B: call('setDeckPitch', 1, data2); break;// increment mode
		case 0x30: // params: (deck, increment, total positions per complete revolution)
			var val1 = (data2>0x40 ? data2-0x80 : data2);
			if(jogWheelsInScratchMode) call('scratchDeckJW', 1, val1, 128);else call('moveDeckJogWheel', 1, val1, 128);
			break;
		//buttons
		//global
		case 0x29:
			if(data2>0x3F)
			{
				if(jogWheelsInScratchMode==0) jogWheelsInScratchMode=1;else jogWheelsInScratchMode=0;
				sendMidi(uniqueId,0xB0,0,0x29,(jogWheelsInScratchMode ? 0x7F : 0x00));
			}
			break;

		case 0x2A: if(data2>0x3F) call('lbMoveCursor', -1); break;
		case 0x2B: if(data2>0x3F) call('lbMoveCursor', +1); break;
		case 0x2C: if(data2>0x3F) call('lbBrowserSelect', data2); break;
		case 0x2D: if(data2>0x3F) call('lbListSelect', data2); break;
		//case 0x2E: call('micOnAir', data2); break;
		case 0x2E:
			//if(data>=0x40)
			{
				call('micOnAir', 0x7F);
				call('micOnAir', 0x00);
			}
			break;

		//per deck
		//deck A
		case 0x0C: call('deckCue', 0, data2); break;
		case 0x0D: call('deckCuePlay', 0, data2); break;
		case 0x0B: call('deckPlayPause', 0, data2); break;
		case 0x07: call('deckSync', 0, data2); break;
		case 0x12: if(data2>0x3F) call('deckLBLoad', 0, data2); break;
		case 0x14: call('deckMonitor', 0, data2); break;
		case 0x09: call('deckSearchLeft', 0, data2); break;
		case 0x0A: call('deckSearchRight', 0, data2); break;
		case 0x11: call('deckBendDown', 0, data2); sendMidi(uniqueId,0xB0,0,data1,data2); break;
		case 0x08: call('deckBendUp', 0, data2); sendMidi(uniqueId,0xB0,0,data1,data2); break;
		case 0x0E: call('deckEqHighKill', 0, data2); break;
		case 0x0F: call('deckEqMidKill', 0, data2); break;
		case 0x10: call('deckEqLowKill', 0, data2); break;
		case 0x01: call('deckCuePos1', 0, data2); break;
		case 0x02: call('deckCuePos2', 0, data2); break;
		case 0x03: call('deckCuePos3', 0, data2); break;
		case 0x04: call('deckCuePos4', 0, data2); break;
		case 0x05: call('deckCuePos5', 0, data2); break;
		case 0x06: call('deckCuePos6', 0, data2); break;
		case 0x64: call('deckFxFlanger', 0, data2); break;
		case 0x65: call('deckFxEcho', 0, data2); break;
		//case 0x66: call('deckFxBeatwaw', 0, data2); break;
		case 0x66: call('deckLoopInEnd', 0, data2); break;
		case 0x67: call('deckReverse', 0, data2); break;
		case 0x68: call('deckBrake', 0, data2); break;
		//case 0x69: call('deckKeyLock', 0, data2); break;
		case 0x69: call('deckLoopExitOrReloop', 0, data2); break;

		//deck B
		case 0x24: call('deckCue', 1, data2); break;
		case 0x25: call('deckCuePlay', 1, data2); break;
		case 0x23: call('deckPlayPause', 1, data2); break;
		case 0x1F: call('deckSync', 1, data2); break;
		case 0x16: if(data2>0x3F) call('deckLBLoad', 1, data2); break;
		case 0x18: call('deckMonitor', 1, data2); break;
		case 0x21: call('deckSearchLeft', 1, data2); break;
		case 0x22: call('deckSearchRight', 1, data2); break;
		case 0x20: call('deckBendDown', 1, data2); sendMidi(uniqueId,0xB0,0,data1,data2); break;
		case 0x15: call('deckBendUp', 1, data2); sendMidi(uniqueId,0xB0,0,data1,data2); break;
		case 0x26: call('deckEqHighKill', 1, data2); break;
		case 0x27: call('deckEqMidKill', 1, data2); break;
		case 0x28: call('deckEqLowKill', 1, data2); break;
		case 0x19: call('deckCuePos1', 1, data2); break;
		case 0x1A: call('deckCuePos2', 1, data2); break;
		case 0x1B: call('deckCuePos3', 1, data2); break;
		case 0x1C: call('deckCuePos4', 1, data2); break;
		case 0x1D: call('deckCuePos5', 1, data2); break;
		case 0x1E: call('deckCuePos6', 1, data2); break;
		case 0x6A: call('deckFxFlanger', 1, data2); break;
		case 0x6B: call('deckFxEcho', 1, data2); break;
		//case 0x6C: call('deckFxBeatwaw', 1, data2); break;
		case 0x6C: call('deckLoopInEnd', 1, data2); break;
		case 0x6D: call('deckReverse', 1, data2); break;
		case 0x6E: call('deckBrake', 1, data2); break;
		//case 0x6F: call('deckKeyLock', 1, data2); break;
		case 0x6F: call('deckLoopExitOrReloop', 1, data2); break;

		default: messageNotProcessed = true;
		}
		break;
	//-----------------------------------------------
	default: messageNotProcessed = true;
	}
	if(messageNotProcessed) debug('messageReceived ['+uniqueId+'] - status:'+status+' channel:'+channel+' data1:'+data1+' data2:'+data2);
	return !messageNotProcessed;
}
//-----------------------------------------------------------------------------
// this is called by the software whenever something important changes (eg. play status, song changed, etc)
// depending on the notification type it may refer to a global event (deck is -1) or a specific deck (deck >= 0)
// and can contain any/all of the parameters: param 1 is an integer, param 2 is a float, param 3 is a string
function notificationReceived(uniqueId,name,deck,param1,param2,param3)
{
	//debug('notif ['+uniqueId+'] name='+name + ' deck='+deck + ' p1(int)='+param1 + ' p2(float)='+param2 + ' p3(str)='+param3);
	if(deck >= 2) return;
	if(name=='killLow') 	sendMidi(uniqueId,0xB0,0,(deck==0)?0x10:0x28,(param1 ? 0x7F : 0x00));
	if(name=='killMid') 	sendMidi(uniqueId,0xB0,0,(deck==0)?0x0F:0x27,(param1 ? 0x7F : 0x00));
	if(name=='killHigh') 	sendMidi(uniqueId,0xB0,0,(deck==0)?0x0E:0x26,(param1 ? 0x7F : 0x00));
	if(name=='cue') 		sendMidi(uniqueId,0xB0,0,(deck==0)?0x0C:0x24,(param1 ? 0x7F : 0x00));
	if(name=='play') 		sendMidi(uniqueId,0xB0,0,(deck==0)?0x0B:0x23,(param1 ? 0x7F : 0x00));
	if(name=='monitor') 	sendMidi(uniqueId,0xB0,0,(deck==0)?0x14:0x18,(param1 ? 0x7F : 0x00));
	if(name=='onbeat') 		sendMidi(uniqueId,0xB0,0,(deck==0)?0x07:0x1F,(param1 ? 0x7F : 0x00));
	//if(name=='analogIn') 	sendMidi(uniqueId,0xB0,0,(deck==0)?0x13:0x17,(param1 ? 0x7F : 0x00));
	//if(name=='onAir') 		sendMidi(uniqueId,0xB0,0,0x2E,(param1 ? 0x7F : 0x00));

	if(name=='cuepos1') 	sendMidi(uniqueId,0xB0,0,(deck==0)?0x01:0x19,(param1 ? 0x7F : 0x00));
	if(name=='cuepos2') 	sendMidi(uniqueId,0xB0,0,(deck==0)?0x02:0x1A,(param1 ? 0x7F : 0x00));
	if(name=='cuepos3') 	sendMidi(uniqueId,0xB0,0,(deck==0)?0x03:0x1B,(param1 ? 0x7F : 0x00));
	if(name=='cuepos4') 	sendMidi(uniqueId,0xB0,0,(deck==0)?0x04:0x1C,(param1 ? 0x7F : 0x00));
	if(name=='cuepos5') 	sendMidi(uniqueId,0xB0,0,(deck==0)?0x05:0x1D,(param1 ? 0x7F : 0x00));
	if(name=='cuepos6') 	sendMidi(uniqueId,0xB0,0,(deck==0)?0x06:0x1E,(param1 ? 0x7F : 0x00));

	if(name=='fxFlanger') 	sendMidi(uniqueId,0xB0,0,(deck==0)?0x64:0x6A,(param1 ? 0x7F : 0x00));
	if(name=='fxEcho') 	    sendMidi(uniqueId,0xB0,0,(deck==0)?0x65:0x6B,(param1 ? 0x7F : 0x00));
	//if(name=='fxBeatwaw') sendMidi(uniqueId,0xB0,0,(deck==0)?0x66:0x6C,(param1 ? 0x7F : 0x00));
	if(name=='reverse') 	sendMidi(uniqueId,0xB0,0,(deck==0)?0x67:0x6D,(param1 ? 0x7F : 0x00));
	if(name=='brake') 		sendMidi(uniqueId,0xB0,0,(deck==0)?0x68:0x6E,(param1 ? 0x7F : 0x00));
	//if(name=='keyLock') 	sendMidi(uniqueId,0xB0,0,(deck==0)?0x69:0x6F,(param1 ? 0x7F : 0x00));

	if(name=='loopBlink')
	{
		// buttons leds
		sendMidi(uniqueId,0xB0,0,(deck==0)?0x66:0x6C,(param1 ? 0x7F : 0x00));// loop in // deckLoopInEnd
		sendMidi(uniqueId,0xB0,0,(deck==0)?0x69:0x6F,(param1 ? 0x7F : 0x00));// loop exit(out) // deckLoopExitOrReloop
	}
}
//-----------------------------------------------------------------------------
// this is called from time to time by the software (several times per second)
// (it is guaranteed that 'timerTick' will get executed ALWAYS right after notificationReceived)
function timerTick(uniqueId)
{
	//debug('timerTick ['+uniqueId+']');
}
//-----------------------------------------------------------------------------
