// Script to use pad Leds to show jam mode state on a Novation Launchpad  Mini MK3
// Arguments:  
//		$arg1 : 1 to always run, none or 0 to stop with song
//
// Author: LoopToGo
//
// Description  : 
// 		This is an example on how to show the state of a LoopToGo jamming session
//		with a LaunchPad Mini MkIII. 
//		We suggest you copy this file in another folder before modifying it.
//		This script is designed to be called by manageLaunchPadMini.ltgs in Jam Mode	
//		as it does not ensure that the LaunchPad Mini is in Session mode (which is needed!)
//
//		This script will set the following color for each column (1 row for each track)
//			col 1: Same color as the track (a little bit brighter if the track is selected). Note that some pale color might be shown as black because of Launchapd limitations
//			col 2: Red if a record loop exist in the Jam Mode section. Red Pulse if loop is recording. Black otherwise
//			col 3: Yellow if a record loop exist in the Jam Mode section. Yellow Pulse if loop is overdubbing. Black otherwise
//			col 4: White if a record loop exist in the Jam Mode section and is temporary locked. Grey if loop is permanently locked. Black otherwise
//			col 5: Light blue 
// 			col 6: Same color as the track if the track is not muted
//
//		When used with manageLaunchPadMini.ltgs in Jam Mode the buttons will have the following behavior on each corresponding track
//			col 1: Select the track
//			col 2: Select the track and Set the loop in record mode (unless the loop is permanently locked),
//			col 3: Select the track and Set the loop in overdub mode (unless the loop is permanently locked). 
//			col 4: Select the track and Toggle the loop between Locked and Permanently Locked
//			col 5: Select the track and Erase the audio and MIDI data if the loop is not Locked or Permanently Locked
// 			col 6: Select the track and Toggle the track between mute and unmute
//

loadScript ..\aliases.txt

loadScript ..\libs\Novation\novationLaunchPadMiniUtilities.ltgl

alias aDeviceOut LaunchPadMini

//print Initializing $scriptFileName


while $arg1 + $isPlaying + $isRecording

	getBankInfo actualBank nbTrackPerBank
	
	variable firstTrackInBank 1 + $actualBank*$nbTrackPerBank - $nbTrackPerBank
	variable lastTrackInBank $firstTrackInBank + 8

	if $lastTrackInBank > $nbTracks
		variable lastTrackInBank $nbTracks
	endIf

	getTrackPosition selectedTrackPos $selectedTrack

	//print $isInJamMode

	for track $firstTrackInBank $lastTrackInBank
	
		callFunction getLoopAtSongTick loopID $track
				
		variable color  aColorPaletteVeryBlack
		
		if $loopID
			getLoopColor red green blue $loopID 
			callFunction getPaletteColorfromRGB color $red*1.5 $green*1.5 $blue*1.5 // We make the color brihter because LaunchPadMini is not good at dark color (it display black)					
		endIf

		//print track $track loopID $loopID color $color
				
		variable rowFromTop 1 + $track - $firstTrackInBank
		variable firstTrackButton 72 - $rowFromTop*9 + 1		
				
		variable firstTrackButton 72 - $rowFromTop*9 + 1
		
		
		getTrackColor red green blue $track
		
		//if $red + $green + $blue = 0
		
		//callFunction getPaletteColorfromRGB noColorTrack $red $green $blue
		callFunction getPaletteColorfromRGB noColorTrackBright $red*1.5 $green*1.5 $blue*1.5
		
		// First button shows track's color and allow selection of track
		if $track = $selectedTrackPos
			callFunction setPadFlashing aDeviceOut $firstTrackButton $noColorTrackBright aColorPaletteDarkGray
		else	
			callFunction setPadColor aDeviceOut $firstTrackButton $noColorTrackBright		
		endIf
		
		// Button 1 to 3 show loopr color and allows to set Jam mode (1: Record 2: Overdub 3: lock)
		for button 1 3
			callFunction setPadColor aDeviceOut $firstTrackButton + $button $color
		endFor
		
		// Button 4: Clear loop button
		callFunction setPadColor aDeviceOut $firstTrackButton + 4 aColorPaletteBlue   

		
		// Button 5 will indicate a muted track (when flashing)
		getTrackVolume trackVolume $track
		if $trackVolume = 0
			callFunction setPadFlashing aDeviceOut $firstTrackButton + 5 aColorPalettePurple aColorPaletteDarkGray   // Mute loop button
		else
			callFunction setPadColor aDeviceOut $firstTrackButton + 5 aColorPalettePurple   // Mute loop button
		endIf
		
		// Unused button for now!!!
		//for button 6 7
		//	callFunction setPadColor aDeviceOut $firstTrackButton + $button aColorPaletteGreen
		//endFor
	endFor
	
	// We clear the remaining row
	for rowFromTop $lastTrackInBank - $firstTrackInBank + 2 8	
		variable firstTrackButton 72 - $rowFromTop*9 + 1
		for button 0 7
			callFunction setPadColor aDeviceOut $firstTrackButton + $button aColorPaletteBlack
		endFor
	endFor

	waitDelta .05
endWhile



function getLoopAtSongTick // loopID [out] track [in] 
		
	getNbLoopsForTrack nbLoopsInTrack $arg2
	
	for loop $nbLoopsInTrack 1 0-1 // do in reverse so we find the last loop
	
		getLoopIDForTrack loopID  $arg2  $loop
		
		getLoopStart loopStart $loopID
	
		ifNot $loopStart > $songBar   // Use ifNot to do lower or equal
			getLoopEnd loopEnd $loopID
			if $loopEnd > $songBar
				parentVariable $arg1 $loopID
				exitFunction
			endIf	
		endIf	
	endFor
	
	parentVariable $arg1 0		// No loop found!

	
endFunction