// Set Of Utilities Functions for Launchpad Mini mkIII  		
//
// Author: LoopToGo
//
// To use: include the following in your script:
//			loadScript LoopToGo\libs\Novation\novationLaunchPadMiniUtilities.ltgl


// Functions in this library :
//   setDAWMode // device [in] mode [in]
//   setLaunchPadMode // device [in] mode [in]
//   setLaunchPadLayout // device [in] mode [in] (see getLayoutHexCode For possible code) 	
//   getLayoutHexCode // variable [out] decimalCode [in]
//   setPadColor // device [in] padNumber [in] color [in] 
//   setPadFlashing // device [in] padNumber [in] color1 [in] color1 [in]
//   setPadPulsing // device [in] padNumber [in] color [in]
//   sendLightMessage // device [in] message [in] padNumber [in] color [in]
//   setPadColorPM // device [in] padNumber [in] red [in] green [in] blue [in]
//   setPadFlashingPM // device [in] padNumber [in] color1 [in] color2 [in]
//   setPadPulsingPM // device [in] padNumber [in] color [in]
//   setTextScrolling // device [in] textIsLooping [in] speed[in] colourSpec [in] text [in]
//   endTextScrolling // device [in]
//   clearPads // device [in] red [in] green [in] blue [in]    r g b are optionnal
//   clearAllPads // device [in] red [in] green [in] blue [in]    r g b are optionnal
//   getLaunchPadLayout // device [in]
//   setFaderPosition // device [in] fader [in] position [in]
//   setFaderColor // device [in] fader [in] color [in]
//   setFader // device [in] orientation [in] faderIndex [in] type [in] controlChangeValue [in] color [in]

skipIfAlreadyLoaded // This command will make sure this script (library) is only loaded once when a child script try to load it after its parent script loaded it

// Aliases for Color Palette functions
loadScript LoopToGo\libs\Novation\colorPalette.ltgl

alias aNovationVertical 0
alias aNovationHorizontal 0
alias aNovationUnipolar 0
alias aNovationBipolar 1

// Set Launchpad STandalone or DAW mode : 0 = standalone 1 = DAW 
function setDAWMode // device [in] mode [in] 
	if $nbArgs = 2

		sendMidiRawBytes $arg1 "F0 00 20 29 02 0D 10 " & $arg2 & " F7" 
	else
		print "setDAWMode: wrong number of args!"
	endIf
endFunction

// set Launchpad mode: 0 = Live, 1 = programmer 
function setLaunchPadMode // device [in] mode [in]

	if $nbArgs = 2
		sendMidiRawBytes $arg1 "F0 00 20 29 02 0D 0E " & $arg2 & " F7" 
	else
		print "setLaunchPadMode: wrong number of args!"
	endIf
	
endFunction

function setLaunchPadLayout // device [in] mode [in] (see getLayoutHexCode For possible code)

	if $nbArgs = 2
	
		callFunction getLayoutHexCode hexCode $arg2
	
		sendMidiRawBytes $arg1 "F0 00 20 29 02 0D 00 " & $hexCode & " F7" 
	else
				print "setLaunchPadMode: wrong number of args!"
	endIf
	
endFunction

function getLayoutHexCode // variable [out] decimalCode [in]

	if $arg2 = 0
		parentVariable $arg1 "00"	// Session
		exitFunction
	endIf

	if $arg2 = 1
		parentVariable $arg1 "04"	// Custom mode 1 (Drums)
		exitFunction
	endIf

	if $arg2 = 2
		parentVariable $arg1 "05"	// Custom mode 2 (Keys)
		exitFunction
	endIf

	if $arg2 = 3
		parentVariable $arg1 "06"	// Custom mode 2 (User)
		exitFunction
	endIf

	if $arg2 = 4
		parentVariable $arg1 "0D"	// Faders (Only in DAW mode!)
		exitFunction
	endIf

	if $arg2 = 5
		parentVariable $arg1 "7F"	// Programmer mode
		exitFunction
	endIf
	

endFunction

// Function to set pad color in Session Mode, note : arguments must be specified as decimal values
function setPadColor // device [in] padNumber [in] color [in]
	callFunction sendLightMessage $arg1 "90" $arg2 $arg3	
endFunction

// Function to set pad color in Session Mode, note : arguments must be specified as decimal values
function setPadFlashing // device [in] padNumber [in] color1 [in] color1 [in]
	callFunction sendLightMessage $arg1 "90" $arg2 $arg3
	callFunction sendLightMessage $arg1 "91" $arg2 $arg4
	endFunction

// Function to set pad Pulsing in Session Mode, note : arguments must be specified as decimal values
function setPadPulsing // device [in] padNumber [in] color [in] 
	callFunction sendLightMessage $arg1 "92" $arg2 $arg3	
endFunction


function sendLightMessage // device [in] message [in] padNumber [in] color [in]
//print setPadColor $arg1 $arg2 $arg3	
		
	if $nbArgs = 4
		variable device $arg1
		variable padNo $arg3 -1
		variable row $padNo/9 1
		variable col $arg3 - $row*9 - 1
		
		convertDecimalsToHex hexPadNumber 11 + $row*10 + $col  // 11 = First (left bottom)
		
		convertDecimalsToHex hexColor $arg4						
			
		sendMidiRawBytes $device $arg2 & " " & $hexPadNumber & " " & $hexColor
		//print sendMidiRawBytes $device "90 " & $hexPadNumber &" " & $hexColor

	else
		print "setPadColor: wrong number of args!"
	endIf	
endFunction

// Function to set pad color in Programmer Mode, note : arguments must be specified as decimal values
function setPadColorPM // device [in] padNumber [in] red [in] green [in] blue [in] 

	variable cond1 $nbArgs = 3
	variable cond2 $nbArgs = 5
	
	if $cond1 + $cond2
		variable device $arg1
		variable padNo $arg2 -1
		variable row $padNo/9 1
		variable col $arg2 - $row*9 - 1
		
		convertDecimalsToHex hexPadNumber 11 + $row*10 + $col  // 11 = First (left bottom)

		
		if $cond1   // Color from palette
		
			variable color $arg3
			
												
			convertDecimalsToHex hexColor $color
			
			//print $arg2 : $row $col hexPadNumber : $hexPadNumber 11 + $row*10 + $col
			//print hexColor : $hexColor [ $red & " " & $green & " " & $blue ]
			//print $hexPadNumber $hexColor
			
			sendMidiRawBytes $device "F0 00 20 29 02 0D 03  00 " & $hexPadNumber & " " & $hexColor & " F7"
			
		else  // Color from RGB
		
			variable red $arg3
			variable green $arg4
			variable blue $arg5		
												
			convertDecimalsToHex hexColor $red & " " & $green & " " & $blue
			
			//print $arg2 : $row $col hexPadNumber : $hexPadNumber 11 + $row*10 + $col
			print hexColor : $hexColor [ $red & " " & $green & " " & $blue ]
			//print $hexPadNumber $hexColor
			
			sendMidiRawBytes $device "F0 00 20 29 02 0D 03  03 " & $hexPadNumber & " " & $hexColor & " F7"
		
			print  sendMidiRawBytes $device       "F0 00 20 29 02 0D 03 00 1A 0D 03 " & $hexPadNumber & " " & $hexColor & " F7"
		endIf
	else
		print "setPadColorPM: wrong number of args!"
	endIf

endFunction

// Function to set pad Flashing in Programmer Mode, note : arguments must be specified as decimal values
function setPadFlashingPM // device [in] padNumber [in] color1 [in] color2 [in]  

	if $nbArgs = 4
		variable device $arg1
		variable padNo $arg2 -1
		variable row $padNo/9 1
		variable col $arg2 - $row*9 - 1
		variable color1 $arg3
		variable color2 $arg4
				
			
		convertDecimalsToHex hexPadNumber 11 + $row*10 + $col  // 11 = First (left bottom)
		convertDecimalsToHex hexColor $color1 & " " & $color2 
		
		//print $arg2 : $row $col hexPadNumber : $hexPadNumber 11 + $row*10 + $col
		//print hexColor : $hexColor [ $red & " " & $green & " " & $blue ]
		//print $hexPadNumber $hexColor
		
		sendMidiRawBytes $device "F0 00 20 29 02 0D 03  01 " & $hexPadNumber & " " & $hexColor & " F7"
		
		//print  sendMidiRawBytes $device       "F0 00 20 29 02 0D 03 00 1A 0D 03 " & $hexPadNumber & " " & $hexColor & " F7"
		
	else
		print "setPadFlashingPM: wrong number of args!"
	endIf

endFunction

// Function to set pad Pulsing in Programmer Mode, note : arguments must be specified as decimal values
function setPadPulsingPM // device [in] padNumber [in] color [in] 

	if $nbArgs = 3
		variable device $arg1
		variable padNo $arg2 -1
		variable row $padNo/9 1
		variable col $arg2 - $row*9 - 1
		variable color $arg3
							
		convertDecimalsToHex hexPadNumber 11 + $row*10 + $col  // 11 = First (left bottom)
		convertDecimalsToHex hexColor $color 
		
		//print $arg2 : $row $col hexPadNumber : $hexPadNumber 11 + $row*10 + $col
		//print hexColor : $hexColor [ $red & " " & $green & " " & $blue ]
		//print $hexPadNumber $hexColor
		
		sendMidiRawBytes $device "F0 00 20 29 02 0D 03  02 " & $hexPadNumber & " " & $hexColor & " F7"
		
		//print  sendMidiRawBytes $device       "F0 00 20 29 02 0D 03 00 1A 0D 03 " & $hexPadNumber & " " & $hexColor & " F7"
		
	else
		print "setPadPulsingPM: wrong number of args!"
	endIf

endFunction

function setTextScrolling // device [in] textIsLooping [in] speed[in] colourSpec [in] text [in]

	// colourSpec can be 1 or 3 args
	// text can be 0 or 1 args
	
	variable cond1 $nbArgs = 4	// colour is from palette (1 arg), text is omitted (0 arg)
	variable cond2 $nbArgs = 5	// colour is from palette (1 arg), text is provided (1 arg)
	variable cond3 $nbArgs = 6	// colour is from rgb (3 arg), text is omitted (0 arg)
	variable cond4 $nbArgs = 7	// colour is from rgb (3 arg), text is provided (1 arg)
	
	if $cond1 + $cond2 + $cond3 + $cond4
		variable device $arg1
		convertDecimalsToHex textIsLooping $arg2
		convertDecimalsToHex speed $arg3
		
		if $cond1 + $cond2
			convertDecimalsToHex hexColor "00 " & $arg4	 // Color is from palette
		else
			variable red $arg4
			variable green $arg5
			variable blue $arg6
			convertDecimalsToHex hexColor "01 " &$red & " " & $green & " " & $blue
		endIf
		
		if $cond1 + $cond3		// text is omitted
			variable text " "	 // Color is from palette
		else
			variable textType 01
			if $cond2
				convertStringToHex textHex $arg5
			else
				convertStringToHex textHex $arg7
			endIf
			variable text " " & $textHex & " "	 // Color is from rgb so text is arg7
		endIf
		
		// We need to split into 2 parts because otherwise we have too many arguments
		variable part1  "F0 00 20 29 02 0D 07 " & $textIsLooping & " " & $speed
		variable part2 " " & $hexColor & " " & $text & " F7"
		
		sendMidiRawBytes  $device $part1 & $part2
		
		//print sendMidiRawBytes $part1 & $part2
		
	endIf
	
endFunction

function endTextScrolling // device [in]

	sendMidiRawBytes  $arg1 "F0 00 20 29 02 0D 07 F7"

endFunction

// Clear all pads (pads section only, not control section)
function clearPads // device [in] red [in] green [in] blue [in]    r g b are optionnal

	if $nbArgs > 0
		variable device $arg1
		
		if $nbArgs = 1
			variable colorPalette 0
		else
			callFunction getPaletteColorfromRGB colorPalette $arg2 $arg3 $arg4
		endIf
		
		//print colorPalette: $colorPalette from $arg2 $arg3 $arg4

		for row 0 7
			for col 0 7 
				callFunction setPadColor $device $row*9 + $col + 1  $colorPalette				
			endFor
		endFor
	else
		print "clearPad: wrong number of args!"
	endIf

endFunction

// Clear pad section and control section
function clearAllPads // device [in] red [in] green [in] blue [in]    r g b are optionnal

	if $nbArgs > 0
		variable device $arg1
		
		if $nbArgs = 1
			variable colorPalette 0
		else
			callFunction getPaletteColorfromRGB colorPalette $arg2 $arg3 $arg4
		endIf

		for row 0 8
			for col 0 8 
				callFunction setPadColor $device $row*9 + $col + 1  $colorPalette				
			endFor
		endFor
	else
		print "clearPad: wrong number of args!"
	endIf

endFunction

function getLaunchPadLayout // device [in]
	
	if $nbArgs = 1
		sendMidiRawBytes  $arg1 "F0 00 20 29 02 0D 00 F7"
	else
		print "getLaunchPadLayout: wrong number of args!"
	endIf

endFunction


function setFaderPosition // device [in] fader [in] position [in]

	//print setFaderPosition $arg1 $arg2 $arg3

	if $nbArgs = 3
		convertDecimalsToHex faderHex $arg2
		convertDecimalsToHex positionHex $arg3
		sendMidiRawBytes  $arg1 "B5 " & $faderHex & " " & $positionHex // B4 and B5 are inverted in documentation
	else
		print "setFaderPosition: wrong number of args!"
	endIf

endFunction


function setFaderColor // device [in] fader [in] color [in]
	if $nbArgs = 3
		convertDecimalsToHex colorHex $arg3
		sendMidiRawBytes  $arg1 "B4 " & $arg2 & " " & $colorHex	// B4 and B5 are inverted in documentation
	else
		print "setFaderColor: wrong number of args!"
	endIf
endFunction

function setFader // device [in] orientation [in] faderIndex [in] type [in] controlChangeValue [in] color [in]

	if $nbArgs = 6
		convertDecimalsToHex controlChangeHex $arg5
		convertDecimalsToHex colorHex $arg6
		variable part1 "F0 00 20 29 02 0D 01 00 " &  $arg2  & " " &  $arg3  // Too many arguments so we split in 2
		variable part2 " " & $arg4 & " " & $controlChangeHex & " " & $colorHex  & " F7"
		sendMidiRawBytes  $arg1 $part1 & $part2

		//print sendMidiRawBytes  $arg1 $part1 & $part2
	else
		print "setFader: wrong number of args!"
	endIf
endFunction
