import cv2
import sys
import Image
import mraa

import socket
import fcntl
import struct

import pyupm_i2clcd as lcd

cap=cv2.VideoCapture(0)

def get_ip_address(ifname):

    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    return socket.inet_ntoa(fcntl.ioctl(
        s.fileno(),
        0x8915,  # SIOCGIFADDR
        struct.pack('256s', ifname[:15])
    )[20:24])

# Initialize Jhd1313m1 at 0x3E (LCD_ADDRESS) and 0x62 (RGB_ADDRESS)
myLcd = lcd.Jhd1313m1(0, 0x3E, 0x62)

# Clear
myLcd.clear()

# Green
myLcd.setColor(255, 255, 0)

# Zero the cursor
myLcd.setCursor(0,0)

# Print it.
ip_address = get_ip_address('wlan0')
myLcd.write(ip_address)

try:
    while True:
        print "Press key to start"
        myLcd.setCursor(1,0)
        myLcd.write("Press key start")
        raw_input()
        #cap.open(0)
        #res,img=cap.read()
        #cv2.imwrite("./camTest.jpg",img)
        #cap.release()
        print "photo taken"
        
        try:
            im = Image.open("./camTest.jpg")
            print im.format, "%dx%d" % im.size, im.mode
            xsize, ysize = im.size
            xcenter = xsize/2
            ycenter = ysize/2
            box = (xcenter-60, ycenter-60, xcenter+60, ycenter+60)
            region = im.crop(box)
            
            print "photo RIDOTTA"
            #print "photo -------------- colori -----------"
            #hsv = cv2.cvtColor(region, cv2.COLOR_RGB2HSV)
            #colors = region.convert('RGB').getcolors()
            #colors = region.getcolors(250)
            #---TODO--------------region.save("./camTestRit.jpg", "JPEG")

            image1 = cv2.imread("./camTestRit.jpg")
            #r1 = image1[60][60][0]
            #g1 = image1[60][60][1]
            #b1 = image1[60][60][2]
            r1 = 0
            g1 = 0
            b1 = 0
            conta=0
            for xx in xrange(40, 91):
                for yy in xrange(40, 91):
                    r1 = r1 + image1[xx][yy][0]
                    g1 = g1 + image1[xx][yy][1]
                    b1 = b1 + image1[xx][yy][2]
                    conta=conta+1
            r1 = r1 / conta
            g1 = g1 / conta
            b1 = b1 / conta

            print "--------MEDIA COLORE------------"
            print r1
            print g1
            print b1
            print conta

            myLcd.clear()
            myLcd.setCursor(0,0)
            myLcd.write("R"+str(r1)+" G"+str(g1)+" B"+str(b1))             

            coloreNum = 0
            coloreStr = "" 
            if r1 >= 0 and r1 <= 170 and g1 >= 100 and g1 <= 255 and b1 >= 0 and b1 <= 170 : 
                #verde
                coloreNum = 1
                coloreStr = "verde"
            elif r1 >= 245 and r1 <= 255 and g1 >= 235 and g1 <= 255 and b1 >= 170 and b1 <= 255 : 
                #bianco
                coloreNum = 2
                coloreStr = "bianco"                
            elif r1 >= 128 and r1 <= 255 and g1 >= 0 and g1 <= 160 and b1 >= 0 and b1 <= 147 : 
                #rosso
                coloreNum = 3
                coloreStr = "rosso"                  
            else:
                #scarto 
                coloreNum = 4
                coloreStr = "scarto"  

            print "--------COLORE------------"
            print coloreNum
            print coloreStr 
            myLcd.setCursor(1,0)
            myLcd.write("col "+str(coloreStr)+" cod "+str(coloreNum))            
            #mean1 = image1.mean()
            #imagecol = cv2.imread("./camTestRit.jpg")
            #color = int(imagecol[100, 100])
            #print mean1
            #hsv = cv2.cvtColor(region, cv2.COLOR_RGB2HSV)
            #cv2.imwrite("./camTestRit.jpg",region)
            # TODO
            while True:
                raw_input()
            #TODO
            
        except IOError:
            pass        
except KeyboardInterrupt:
    print "done"
    cap.release()
