// Example on how to create Led effects on a Launchpad Mini MkIII when a button is pressed
//			It should be see as a demo, not a perfect solution. 
//			You should adapt it to your own needs and modify and/or cerate other Led Effects
//
// Arguments:  must be provided by the midi events from the LaunchPad Mini
//
// Author: LoopToGo
//
// Description  : 
//			Parse the noteOn and noteOff event to call different led Effects:
//				Note 44: ledFxRow
//				Note 45: ledFxColumn
//				Note 54: ledFxRowColumn
//				Note 55: ledFxGrow
//				Note > 45: ledFxGrow3
//				Default: ledFxGrow2

// We enable script concurrency because event can occur again even if the script has not completed!!!!

loadScript LoopToGo\libs\Novation\novationLaunchPadMiniUtilities.ltgl


alias aDeviceOut LaunchPadMini		// Should be set to your own LaunchPad Mini MkIII mapped name (Preferences/MIDI)

setScriptConcurrencyMode 0		// if the script is already running, it will launch another script. In other words, 2 instances of the same script can run together

ifNot $gCurrentMode = 0
	endScriptDynamic			// We must be in Session mode to run this script!!!!
endIf


// To stop enay text scrolling!
callFunction endTextScrolling aDeviceOut

		
variable note $arg4
variable status $arg2
variable velocity $arg5

if $status = aMidiNoteOn	
	if $velocity = 0
		variable status aMidiNoteOff
	endIf
endIf		

//print $status $note $velocity $scriptID $gLaunchPadCurrentPage $gCurrentMode


alias aMidiNoteOn	144
alias aMidiNoteOff	128

if $note = 44
	callFunction ledFxRow $note 64
	endScriptDynamic
endIf

if $note = 45
	callFunction ledFxColumn $note 64
	endScriptDynamic
endIf

if $note = 54
	callFunction ledFxRowColumn $note 64
	endScriptDynamic
endIf

if $note = 55
	callFunction ledFxGrow $note 64
	endScriptDynamic
endIf

if $note = $note   // Default
	//callFunction ledFxRow $note 64
	//callFunction ledFxColumn $note 64
	//callFunction ledFxRowColumn $note 64
	//callFunction ledFxGrow $note 33
	
	if $note > 45
		callFunction ledFxGrow3 $note 34
	else
		callFunction ledFxGrow2 $note 34
	endIf
	endScriptDynamic
endIf

// Default behavior
if $status = aMidiNoteOn			
		sendMidiNoteOn aDeviceOut  $note 100
		endScriptDynamic
endIf

if $status = aMidiNoteOff
	sendMidiNoteOff aDeviceOut  $note 
	endScriptDynamic
endIf

function ledFxOn // row [in] column [in] color [in]
	if $arg1 < 1
		exitFunction
	endIf
	if $arg2 < 1
		exitFunction
	endIf
	
	sendMidiNoteOn aDeviceOut  $arg1*10 + $arg2 $arg3
endFunction

function ledFxOff // row [in] column [in] 
	sendMidiNoteOff aDeviceOut  $arg1*10 + $arg2 
endFunction		

function ledFxColumn // note [in] color [in]
	variable note $arg1
	variable row $note/10 1
	variable column $note - $row*10
	
	if $status = aMidiNoteOn
		for row 1 8
			callFunction ledFxOn  $row $column $arg2
		endFor
		exitFunction
	endIf
	
	if $status = aMidiNoteOff
		for row 1 8
			callFunction ledFxOff $row $column  
		endFor
	endIf

endFunction

function ledFxRow // note [in] color [in]
	
	variable note $arg1
	variable row $note/10 1
	
	if $status = aMidiNoteOn
		for column 1 8
			callFunction ledFxOn  $row $column $arg2
		endFor
		exitFunction
	endIf
	
	if $status = aMidiNoteOff
		for column 1 8
			callFunction ledFxOff $row $column  
		endFor
	endIf

endFunction
 
function ledFxRowColumn // note [in] color [in]
	callFunction ledFxRow $arg1 $arg2
	callFunction ledFxColumn $arg1 $arg2
endFunction

function ledFxGrow // note [in] color  [in]
	variable note $arg1
	variable row $note/10 1
	variable column $note - $row*10
	
	if $status = aMidiNoteOn
		for i 0 3
			callFunction ledFxOn $row - $i $column - $i $arg2
			callFunction ledFxOn $row - $i $column + $i $arg2
			callFunction ledFxOn $row + $i $column + $i $arg2
			callFunction ledFxOn $row + $i $column - $i $arg2
			waitDelta .015
		endFor
		exitFunction
	endIf
	
	if $status = aMidiNoteOff
		for i 0 3
			callFunction ledFxOff $row - $i $column - $i 
			callFunction ledFxOff $row - $i $column + $i 
			callFunction ledFxOff $row + $i $column + $i 
			callFunction ledFxOff $row + $i $column - $i 
			waitDelta .015
		endFor
	endIf

endFunction

function ledFxGrow2 // note [in] color  [in]
	variable note $arg1
	variable row $note/10 1
	variable column $note - $row*10
	
	if $status = aMidiNoteOn
		for i 0 3
			variable color $arg2 + $i*10
			callFunction ledFxOn $row - $i $column - $i $color
			callFunction ledFxOn $row - $i $column + $i $color
			callFunction ledFxOn $row + $i $column + $i $color
			callFunction ledFxOn $row + $i $column - $i $color
			waitDelta .015
		endFor
		exitFunction
	endIf
	
	if $status = aMidiNoteOff
		for i 0 3
			callFunction ledFxOff $row - $i $column - $i 
			callFunction ledFxOff $row - $i $column + $i 
			callFunction ledFxOff $row + $i $column + $i 
			callFunction ledFxOff $row + $i $column - $i 
			waitDelta .015
		endFor
	endIf

endFunction

function ledFxGrow3 // note [in] color  [in]
	variable note $arg1
	variable row $note/10 1
	variable column $note - $row*10
	
	if $status = aMidiNoteOn
		for i 0 2
			variable color $arg2 + $i*10
			
			for j 0-$i $i
				callFunction ledFxOn $row - $i $column + $j $color
				callFunction ledFxOn $row + $i $column + $j $color
			endFor	
			
			for j 1 - $i $i - 1
				callFunction ledFxOn $row + $j $column - $i $color
				callFunction ledFxOn $row + $j $column + $i $color
			endFor	
			
			
			waitDelta .015
		endFor
		exitFunction
	endIf
	
	if $status = aMidiNoteOff
		for i 0 2
			for j 0-$i $i
				callFunction ledFxOff $row - $i $column + $j 
				callFunction ledFxOff $row + $i $column + $j 
			endFor	
			
			for j 1 - $i $i - 1
				callFunction ledFxOff $row + $j $column - $i 
				callFunction ledFxOff $row + $j $column + $i 
			endFor	
			waitDelta .015
		endFor
	endIf

endFunction




