def refresh_page():
	tmp = []

	for i in range(4):
		#turn on the columns that are on	
		if d.onoffs[i] == 1:
			launchpad.column(d.times[i], d.pitches[i], octave_dict[d.octaves[i]])
			tmp.append(d.times[i])
		#turn off the columns that are off and not on in another delay line		
		elif not any(d.times[i]==itmp for itmp in tmp):
			launchpad.clear_column(d.times[i])

	#clear the rest of them
	for icolumn in range(8):
		if not any(icolumn==itime for itime in d.times):
			launchpad.clear_column(icolumn)

	###Octave Buttons####
	launchpad.led(8,8-sidebar.octave_up,'Yellow')
	launchpad.led(8,8-sidebar.octave_down,'Red')
def tap_in(val):
	if val:
		launchpad.led(8,8-sidebar.tap_tempo,'Green')
	else:
		launchpad.led(8,8-sidebar.tap_tempo,'Clear')
def press(*A):
	# Python's varargs are implemented as tuples, not lists. (list(A))
	xtime, ypitch, push = list(A)
	#map 0:7 onto 8:1
	ypitch = 8-ypitch
	# print "x:", x, "y:", y, "push:", push
	
	if xtime < 8:

		if d.slider_menu:
			if xtime == wet.column:
				wet.set(ypitch)
			elif xtime == feedback.column:
				feedback.set(ypitch)
			elif xtime == dry.x and ypitch == dry.y:
				dry.push(push)

		elif d.keyboard_menu:
			print 'insert keyboard functions here'
		else:

			if push:
				foundit = 0

				for i, ionoff in enumerate(d.onoffs): 

					#find active lines with same delay time and pitch and turn them off
					if ionoff and d.times[i]==xtime:			
			
						if d.pitches[i]==ypitch and d.octave_cue==0:
							focus(i)
							onoff(0)
							to_max('onoff', d.focus, 0)
							d.octaves[i]=0
							to_max('octave', d.focus, 0)
							foundit = 1
						#or just set the pitch
						else:
							focus(i)
							pitch(ypitch)
							to_max('pitch', d.focus, ypitch)
							if d.octave_cue != 0:
								octave(d.octave_cue)
							foundit = 1

				
				#if none found, turn on a new line
				if not foundit:

					#if there's an open delay line, use it
					if d.onoffs.count(0) > 0:	
						focus(d.onoffs.index(0))
						onoff(1)
						to_max('onoff', d.focus, 1)
					#otherwise use the closest line
					else:
						focus( min(range(len(d.times)), key=lambda index: abs(d.times[index]-xtime)) )

					#In both cases, set the time and pitch
					time(xtime)
					to_max('time', d.focus, xtime)
					pitch(ypitch)
					to_max('pitch', d.focus, ypitch)
					if d.octave_cue != 0:
						octave(d.octave_cue)

				#always refresh the page after a push
				refresh_page()


	#Sidebar Functions 
	elif xtime == 8:

		# For bringing octaves up	
		if ypitch == sidebar.octave_up:
			if push:
				d.octave_cue = 1
			else:
				d.octave_cue = 0

		#For bringing octaves down	
		if ypitch == sidebar.octave_down:
			if push:
				d.octave_cue = -1
			else:
				d.octave_cue = 0

		#For tap tempo	
		if ypitch == sidebar.tap_tempo:
			if push:
				tap_out()
				launchpad.led(8,8-sidebar.tap_tempo,'Green')
			else:
				launchpad.led(8,8-sidebar.tap_tempo,'Clear')

		#For Sliders
		if ypitch == sidebar.sliders:
			if push:
				d.slider_menu = 1
				launchpad.clear_page()
				wet.refresh()
				feedback.refresh()
				dry.refresh()
				launchpad.led(8,8-sidebar.sliders,'Green')
			else:
				d.slider_menu = 0
				refresh_page()
				launchpad.led(8,8-sidebar.sliders,'Clear')

		#For Keyboard
		if ypitch == sidebar.keyboard:
			if push:
				d.keyboard_menu = 1
				launchpad.led(8,8-sidebar.keyboard,'Yellow')
			else:
				d.keyboard_menu = 0
				launchpad.led(8,8-sidebar.keyboard,'Clear')