import parrot
import neopixel
import time 
from machine import I2C,Pin

i2c = I2C(scl=Pin(Pin.P19), sda=Pin(Pin.P20), freq=115200)
led = neopixel.NeoPixel(Pin(Pin.P7), n=12,bpp=3,timing=1)



class Car(object):
    def __init__(self,i2c=i2c):
        self.i2c=i2c
        self.I2C_ADR=0x10
        self.I2C_ADR2=0x39
        self.buf1=bytes(1)
        self.buf4=bytes(2)
        self.mini=100
        self.maxi=180
    
    #前进
    def forward(self,speed=100):
        parrot.set_speed(parrot.MOTOR_1, speed)   
        parrot.set_speed(parrot.MOTOR_2, speed)
    
    #后退
    def backward(self,speed=-100):
        parrot.set_speed(parrot.MOTOR_1, speed)   
        parrot.set_speed(parrot.MOTOR_2, speed)
    
    #左转
    def turn_l(self,l_speed=0,r_speed=100):
        parrot.set_speed(parrot.MOTOR_1, r_speed)   
        parrot.set_speed(parrot.MOTOR_2, l_speed)
    
    #右转
    def turn_r(self,l_speed=100,r_speed=0):
        parrot.set_speed(parrot.MOTOR_1, r_speed)   
        parrot.set_speed(parrot.MOTOR_2, l_speed)
    
    #停止
    def stop(self):
        parrot.set_speed(parrot.MOTOR_1, 0)   
        parrot.set_speed(parrot.MOTOR_2, 0)
    
    #读取巡线数据
    def line(self):
        self.i2c.writeto(self.I2C_ADR, b'\x21')
        self.buf1=self.i2c.readfrom(self.I2C_ADR,1)[0]
        self.i2c.writeto(self.I2C_ADR, b'\x24')  
        self.buf4 = i2c.readfrom(self.I2C_ADR,1)[0]
    
    #返回左边巡线头数值
    def read_l(self):
        self.i2c.writeto(self.I2C_ADR, b'\x24')  
        self.buf4 = i2c.readfrom(self.I2C_ADR,1)[0]
        return self.buf4
    
    #返回右边巡线头数值
    def read_r(self):
        self.i2c.writeto(self.I2C_ADR, b'\x21')
        self.buf1=self.i2c.readfrom(self.I2C_ADR,1)[0]
        return self.buf1
    
    #设置线的参数
    def setline(self,mi=100,ma=180):
        self.mini=mi
        self.maxi=ma
    
    #左边巡线头碰到黑线
    def l_touchline(self):
        self.line()
        if self.mini<self.buf4<self.maxi and self.buf1<self.mini:
            return True
        else:
            return False
    
    #右边巡线头碰到黑线
    def r_touchline(self):
        self.line()
        if self.mini<self.buf1<self.maxi and self.buf4<self.mini:
            return True
        else:
            return False
    
    #两边巡线头都喷到黑线    
    def all_touchline(self):
        self.line()
        if self.mini<self.buf1<self.maxi and self.mini<self.buf4<self.maxi:
            return True
        else:
            return False
    
    #两边巡线头都不碰到黑线    
    def not_touchline(self):
        self.line()
        if self.buf1<self.mini and self.buf4<self.mini:
            return True
        else:
            return False
            
    #颜色传感器初始化
    def colorsenser(self):
        self.i2c.writeto_mem(self.I2C_ADR2, 0x80, b'\x03')
        time.sleep_ms(10)
        self.i2c.writeto_mem(self.I2C_ADR2, 0x81, b'\xD5') 
        self.i2c.writeto_mem(self.I2C_ADR2, 0x8F, b'\x01')
    
    #获取RGB值
    def getRGB(self,r=4,g=5,b=7):
        """
        获取RGB值

        :return: 返回RGB三元组,(color_r,color_g,color_b)
        """
        color = [0, 0, 0]
        buf=self.i2c.readfrom_mem(self.I2C_ADR2, 0x96, 6)
        color_r=buf[1]*256 +buf[0] #RGB值转换
        color_g=buf[3]*256 +buf[2]
        color_b=buf[5]*256 +buf[4]
        color_r=int(color_r/r) #RGB白平衡
        color_g=int(color_g/g)
        color_b=int(color_b/b)
        maxColor = max(color_r, color_g, color_b)
        if maxColor > 255:
                scale = 255 / maxColor
                color_r = int(color_r * scale)
                color_g = int(color_g * scale)
                color_b = int(color_b * scale)
        return (color_r,color_g,color_b)
    
    #获取HSV值
    def getHSV(self):
        """
        获取HSV值

        :return: 返回HSV三元组,(h,s,v)
        """
        rgb = self.getRGB()
        r, g, b = rgb[0], rgb[1], rgb[2]
        r, g, b = r / 255.0, g / 255.0, b / 255.0
        mx = max(r, g, b)
        mn = min(r, g, b)
        df = mx - mn
        if mx == mn:
            h = 0
        elif mx == r:
            h = (60 * ((g - b) / df) + 360) % 360
        elif mx == g:
            h = (60 * ((b - r) / df) + 120) % 360
        elif mx == b:
            h = (60 * ((r - g) / df) + 240) % 360
        if mx == 0:
            s = 0
        else:
            s = df / mx
        v = mx
        return round(h, 1), round(s, 1), round(v, 1) 
    
    #颜色识别
    def discern(self):
        hsv=self.getHSV()
        if 0.1<=hsv[1]<=0.2:
            return '空'
        if 185<=hsv[0]<=249:
            return '蓝色'
        elif 0<=hsv[0]<=5 or 355<=hsv[0]<=360:
            return '红色'
        elif 6<=hsv[0]<=25:
            return '橙色'
        elif 26<=hsv[0]<=70:
            return '黄色'
        elif 71<=hsv[0]<=159:
            return '绿色'
        elif 160<=hsv[0]<=184:
            return '青色'
        elif 250<=hsv[0]<=350:
            return '紫色'
        else:
            return '未成功识别'
            
    
        
        
    
    def distance(self):
        """
        获取超声波测距

        :return: 返回测距,单位mm
        """
        self.i2c.writeto(0x13, b'\x20')
        time.sleep_ms(2)
        buf = self.i2c.readfrom(0x13,2)
        distance_mm = (buf[0]*256+buf[1]) 
        
        return distance_mm
        
        