import time
import sys
from neopixel import *

class TheGrid:

    def __init__(self, height, width):
	LED_PIN = 18
	LED_FREQ_HZ = 800000
	LED_DMA = 5
	LED_BRIGHTNESS = 16
	LED_INVERT = False
        self.width = width
        self.height = height
	#print "height,width: "
	#print self.height,
	#print ",",
	#print self.width
#        self.strands = range(height)
#        for i in range(height):
#            self.strands[i] = Adafruit_NeoPixel(height, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
#            self.strands[i].begin()
	self.strand = Adafruit_NeoPixel(300, LED_PIN, LED_FREQ_HZ, LED_DMA, LED_INVERT, LED_BRIGHTNESS)
	self.strand.begin()
	#print "strand is now ",
	#print self.strand.numPixels()

    def setPixel(self, t, row, col, color):
	if row % 2 == 1:
		#pix = pix + self.width - 1 - col
		#pix = pix - 2 * col + self.width - 1
		col = 20 - col
	pix = self.width * row + col + row * 4 + 2
	if row >= 7:
		pix = pix + 1
	if row == 11:
		pix = pix + 1
	#print pix
    	#print "setting pixel: ",
	#print pix
        self.strand.setPixelColor(pix, color)
	#print "set the pixel"
    	#print "start: ", time.time()
    	#print "end: ", time.time()
	#print "shown the pixel"
	#time.sleep(t / 1000)

    def showGrid(self):
        self.strand.show()

    #def setGrid(self, grid):
        #for i in range(len(grid)):
            #for j in range(len(grid[0])):
                #self.strands[i].setPixelColor(j, color)

#row = 12
#col = 21
#lights = TheGrid(row, col)
#
#
#
#while True:
    #line = sys.stdin.readline()
    ##t = time.time()
    #try:
	    #lineSplit = line.split(" ")
	    #r = int(lineSplit[3][0:2], 16)
	    #g = int(lineSplit[3][2:4], 16)
	    #b = int(lineSplit[3][4:6], 16)
	    ##print "setting pixel " + lineSplit[1] + "," + lineSplit[2] + " to " + lineSplit[3] + " and waiting " + lineSplit[0]
	    #lights.setPixel(int(lineSplit[0]), int(lineSplit[1]), int(lineSplit[2]), Color(r, g, b))
	    ##print time.time() - t
    #except:
    	    #print "bad input"
