import sensor, image, time
from pyb import LED,Pin
from pyb import Timer
import lcd









task_mode = 0  #0:初始模式,就是录像,1:调参模式
blue_led = LED(1)


#thresholds = [
#    (15, 25, 10, 25, -5, 20),  # generic_red_thresholds(15, 23, 10, 24, 0, 18)
#    (30, 100, -64, -8, -32, 32),  # generic_green_thresholds
#    (0, 15, 0, 40, -80, -20),
#]  # generic_blue_thresholds

g_thread_red   =[15, 25, 10, 25, -5, 20]
g_thread_green =[30, 100, -64, -8, -32, 32]
g_thread_blue  =[0, 15, 0, 40, -80, -20]
EXPOSURE_TIME_SCALE = 1.0

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.VGA)
sensor.set_windowing(320,320)
sensor.set_auto_whitebal(False)

#gain_par =0.8
#sensor.set_auto_gain(False, gain_db_ceiling=gain_par)  # Default gain 10.
sensor.set_auto_gain(False)  # Default gain 10.

#EXPOSURE_MICROSECONDS = 500000
##sensor.set_auto_exposure(False, exposure_us=EXPOSURE_MICROSECONDS)
#sensor.set_auto_exposure(True, exposure_us=EXPOSURE_MICROSECONDS)
#sensor.set_auto_exposure(False)



current_exposure_time_in_microseconds = sensor.get_exposure_us()
print("Current Exposure == %d" % current_exposure_time_in_microseconds)

# Auto exposure control (AEC) is enabled by default. Calling the below function
# disables sensor auto exposure control. The additionally "exposure_us"
# argument then overrides the auto exposure value after AEC is disabled.
sensor.set_auto_exposure(
    False, exposure_us=int(current_exposure_time_in_microseconds * EXPOSURE_TIME_SCALE)
)

print("New exposure == %d" % sensor.get_exposure_us())


sensor.skip_frames(time = 500)

try:
    # The camera will now focus on whatever is in front of it.
    sensor.ioctl(sensor.IOCTL_TRIGGER_AUTO_FOCUS)
except:
    raise (Exception("Auto focus is not supported by your sensor/board combination."))

sensor.skip_frames(time = 500)



clock = time.clock()
lcd.init(1, 128, height=160, framesize=0, refresh=30, triple_buffer=False, bgr=False)


#lcd_img =img.scale(0.4,0.5)
#lcd_img.draw_string(0,0,"par_index: %d "% ((par_index)))
#lcd.display(lcd_img)

#blue_led = LED(1)
KEY = Pin('C13',Pin.IN,Pin.PULL_DOWN)

KEY1 = Pin('A0',Pin.IN,Pin.PULL_UP)
KEY2 = Pin('A2',Pin.IN,Pin.PULL_UP)
KEY3 = Pin('A5',Pin.IN,Pin.PULL_UP)#A4A6 不行
KEY4 = Pin('A7',Pin.IN,Pin.PULL_UP)
KEY5 = Pin('C4',Pin.IN,Pin.PULL_UP)
KEY6 = Pin('B0',Pin.IN,Pin.PULL_UP)

print("You're on camera!")
keycount=0
# 轻触开关

waitkeyflag=0
keyval=0
#等开关按下并松开
def wait_key():
    global keyval,waitkeyflag
    while KEY.value():

        while KEY.value():
            while KEY.value(): #wait key up
                clock.tick()
#                i=0   #anything you like
#                print("here01")
            keyval=100
            waitkeyflag=1



key1_status=0
key2_status=0
key3_status=0
key4_status=0
key5_status=0
key6_status=0

key_val =0
key_testnum=0;
key1_step =0


par_index =0

#file_abs = "save1.txt"
f =open("save1.txt","r",encoding = "unicode")
content=""
try :
    while(True):
        chunk = f.read().strip('\x00') #读取文件
        print(chunk)
        if not chunk:
           break
        content =chunk
finally:
    f.seek(0)
    f.close()  #关闭文件
    
print("content=")
print(content)
print(type(content))
readnum = content.split(",")
print("readnum=")
print(readnum)
print(type(readnum))
print("readnum0=")
print(readnum[0])
print(type(readnum[0]))
g_thread_red[0]=int(readnum[0])
g_thread_red[1]=int(readnum[1])
g_thread_red[2]=int(readnum[2])
g_thread_red[3]=int(readnum[3])
g_thread_red[4]=int(readnum[4])
g_thread_red[5]=int(readnum[5])

g_thread_green[0]=int(readnum[6])
g_thread_green[1]=int(readnum[7])
g_thread_green[2]=int(readnum[8])
g_thread_green[3]=int(readnum[9])
g_thread_green[4]=int(readnum[10])
g_thread_green[5]=int(readnum[11])

g_thread_blue[0]=int(readnum[12])
g_thread_blue[1]=int(readnum[13])
g_thread_blue[2]=int(readnum[14])
g_thread_blue[3]=int(readnum[15])
g_thread_blue[4]=int(readnum[16])
g_thread_blue[5]=int(readnum[17])


print("-@@red--start")
print(g_thread_red[0])
print(g_thread_red[1])
print(g_thread_red[2])
print(g_thread_red[3])
print(g_thread_red[4])
print(g_thread_red[5])
print("-@@red--end")

#----------定时器4中断函数
timer4cnt =0
def tick(timer):            # we will receive the timer object when being called
    global timer4cnt
    timer4cnt+=1
#    print("t400")
    if timer4cnt==2:
        timer4cnt =0
        KEY1_Read()
        KEY2_Read()
        KEY3_Read()
        KEY4_Read()
        KEY5_Read()
        KEY6_Read()
#----------创建一个定时器
tim = Timer(4, freq=20)      # create a timer object using timer 4 - trigger at 5Hz
tim.callback(tick)          # set the callback to our tick function
#------------------------------
def KEY1_Read():
    global key1_status
    global waitkeyflag
    global task_mode,key1_step
    global par_index
#    print("key1_read........")
    if KEY1.value()==0 and key1_status==1 and key1_step==0:#2.按键被按下的瞬间,下降沿
        task_mode =1  #调到调参模式
        key1_step =1
        print("h01333333333333333333")

    if KEY1.value()==0 and key1_status==1 and key1_step==1:#2.按键被按下的瞬间,下降沿
        #waitkeyflag =1
        print("task_mode=",task_mode)
        if task_mode==1: #如果是调参模式
            par_index +=1
            if par_index==18:#参数0-17之间,0-5红色,6-11绿,12-17蓝
                par_index=0
            print("par_index=",par_index)
#        print("h08888888888888888")
    elif KEY1.value()==0 and key1_status==0 and key1_step==1 :#3.按键被按下的状态,
        #用户自定义函数
        clock.tick()
        #print("u01444444444444444444")

    key1_status=KEY1.value()	#获取最新状态

def KEY2_Read():
    global key2_status
    global g_thread_red,g_thread_green,g_thread_blue
    global par_index

    if KEY2.value()==0 and key2_status==1 :#2.按键被按下的瞬间,下降沿
        #用户自定义函数
#        print("key2 is up to down")
        if 0<=par_index<6:

            g_thread_red[par_index] +=5
            if g_thread_red[0]>=95:g_thread_red[0] =0
            if g_thread_red[1]>=95:g_thread_red[1] =0
            if g_thread_red[2]>=128:g_thread_red[2] =-127
            if g_thread_red[3]>=128:g_thread_red[3] =-127
            if g_thread_red[4]>=128:g_thread_red[4] =-127
            if g_thread_red[5]>=128:g_thread_red[5] =-127

            print("KEY2 g_thread_red[par_index]=",g_thread_red[par_index])

        elif 6<=par_index<12:
            g_thread_green[par_index-6] +=5
            if g_thread_green[0]>=95:g_thread_green[0] =0
            if g_thread_green[1]>=95:g_thread_green[1] =0
            if g_thread_green[2]>=128:g_thread_green[2] =-127
            if g_thread_green[3]>=128:g_thread_green[3] =-127
            if g_thread_green[4]>=128:g_thread_green[4] =-127
            if g_thread_green[5]>=128:g_thread_green[5] =-127






            print('KEY2 g_thread_green[par_index]=',g_thread_green[par_index-6])
#            print("key2000..")
        elif 12<=par_index<18:
            g_thread_blue[par_index-12] +=5
            if g_thread_blue[0]>=95:g_thread_blue[0] =0
            if g_thread_blue[1]>=95:g_thread_blue[1] =0
            if g_thread_blue[2]>=128:g_thread_blue[2] =-127
            if g_thread_blue[3]>=128:g_thread_blue[3] =-127
            if g_thread_blue[4]>=128:g_thread_blue[4] =-127
            if g_thread_blue[5]>=128:g_thread_blue[5] =-127

            print("KEY2 g_thread_blue[par_index]=",g_thread_blue[par_index-12])
#            print("key2111..")
    elif KEY2.value()==0 and key2_status==0 :#3.按键被按下的状态,
       #用户自定义函数
#        print("key2 is downing")
        if 0<=par_index<6:
           g_thread_red[par_index] +=5
           if g_thread_red[0]>=95:g_thread_red[0] =0
           if g_thread_red[1]>=95:g_thread_red[1] =0
           if g_thread_red[2]>=128:g_thread_red[2] =-127
           if g_thread_red[3]>=128:g_thread_red[3] =-127
           if g_thread_red[4]>=128:g_thread_red[4] =-127
           if g_thread_red[5]>=128:g_thread_red[5] =-127
           print("KEY2 g_thread_red[par_index]=",g_thread_red[par_index])

        elif 6<=par_index<12:
            g_thread_green[par_index-6] +=5
            if g_thread_green[0]>=95:g_thread_green[0] =0
            if g_thread_green[1]>=95:g_thread_green[1] =0
            if g_thread_green[2]>=128:g_thread_green[2] =-127
            if g_thread_green[3]>=128:g_thread_green[3] =-127
            if g_thread_green[4]>=128:g_thread_green[4] =-127
            if g_thread_green[5]>=128:g_thread_green[5] =-127
            print('KEY2 g_thread_green[par_index]=',g_thread_green[par_index-6])
#            print("key2000..")
        elif 12<=par_index<18:
            g_thread_blue[par_index-12] +=5
            if g_thread_blue[0]>=95:g_thread_blue[0] =0
            if g_thread_blue[1]>=95:g_thread_blue[1] =0
            if g_thread_blue[2]>=128:g_thread_blue[2] =-127
            if g_thread_blue[3]>=128:g_thread_blue[3] =-127
            if g_thread_blue[4]>=128:g_thread_blue[4] =-127
            if g_thread_blue[5]>=128:g_thread_blue[5] =-127
            print("KEY2 g_thread_blue[par_index]=",g_thread_blue[par_index-12])
#            print("key2111..")
    key2_status=KEY2.value()	#获取最新状态

def KEY3_Read():
    global key3_status
    global waitkeyflag
    global thresholds
    global par_index

    if KEY3.value()==0 and key3_status==1 :#2.按键被按下的瞬间,下降沿
        #用户自定义函数
        if 0<=par_index<6:
           g_thread_red[par_index] -=5
           if g_thread_red[0]<=0:g_thread_red[0] =95
           if g_thread_red[1]<=0:g_thread_red[1] =95
           if g_thread_red[2]<=-127:g_thread_red[2] =128
           if g_thread_red[3]<=-127:g_thread_red[3] =128
           if g_thread_red[4]<=-127:g_thread_red[4] =128
           if g_thread_red[5]<=-127:g_thread_red[5] =128
           print("KEY3 g_thread_red[par_index]=",g_thread_red[par_index])

        elif 6<=par_index<12:
            g_thread_green[par_index-6] -=5
            if g_thread_green[0]<=0:g_thread_green[0] =95
            if g_thread_green[1]<=0:g_thread_green[1] =95
            if g_thread_green[2]<=-127:g_thread_green[2] =128
            if g_thread_green[3]<=-127:g_thread_green[3] =128
            if g_thread_green[4]<=-127:g_thread_green[4] =128
            if g_thread_green[5]<=-127:g_thread_green[5] =128
            print('KEY3 g_thread_green[par_index]=',g_thread_green[par_index-6])
#            print("key2000..")
        elif 12<=par_index<18:
            g_thread_blue[par_index-12] -=5
            if g_thread_blue[0]<=0:g_thread_blue[0] =100
            if g_thread_blue[1]<=0:g_thread_blue[1] =100
            if g_thread_blue[2]<=-127:g_thread_blue[2] =128
            if g_thread_blue[3]<=-127:g_thread_blue[3] =128
            if g_thread_blue[4]<=-127:g_thread_blue[4] =128
            if g_thread_blue[5]<=-127:g_thread_blue[5] =128
            print("KEY3 g_thread_blue[par_index]=",g_thread_blue[par_index-12])
#            print("key2111..")

#        print("key3 is up to down")

    elif KEY3.value()==0 and key3_status==0 :#3.按键被按下的状态,
       #用户自定义函数
       #用户自定义函数
       if 0<=par_index<6:
          g_thread_red[par_index] -=5
          if g_thread_red[0]<=0:g_thread_red[0] =100
          if g_thread_red[1]<=0:g_thread_red[1] =100
          if g_thread_red[2]<=-127:g_thread_red[2] =128
          if g_thread_red[3]<=-127:g_thread_red[3] =128
          if g_thread_red[4]<=-127:g_thread_red[4] =128
          if g_thread_red[5]<=-127:g_thread_red[5] =128
          print("KEY3 g_thread_red[par_index]=",g_thread_red[par_index])

       elif 6<=par_index<12:
           g_thread_green[par_index-6] -=5
           if g_thread_green[0]<=0:g_thread_green[0] =100
           if g_thread_green[1]<=0:g_thread_green[1] =100
           if g_thread_green[2]<=-127:g_thread_green[2] =128
           if g_thread_green[3]<=-127:g_thread_green[3] =128
           if g_thread_green[4]<=-127:g_thread_green[4] =128
           if g_thread_green[5]<=-127:g_thread_green[5] =128
           print('KEY3 g_thread_green[par_index]=',g_thread_green[par_index-6])
#           print("key2000..")
       elif 12<=par_index<18:
           g_thread_blue[par_index-12] -=5
           if g_thread_blue[0]<=0:g_thread_blue[0] =100
           if g_thread_blue[1]<=0:g_thread_blue[1] =100
           if g_thread_blue[2]<=-127:g_thread_blue[2] =128
           if g_thread_blue[3]<=-127:g_thread_blue[3] =128
           if g_thread_blue[4]<=-127:g_thread_blue[4] =128
           if g_thread_blue[5]<=-127:g_thread_blue[5] =128
           print("KEY3 g_thread_blue[par_index]=",g_thread_blue[par_index-12])
#           print("key2111..")

    key3_status=KEY3.value()	#获取最新状态

key4val=0
def KEY4_Read():
    global key4_status
    global waitkeyflag
    global key4val
#    file_abs = "save.txt"
    if KEY4.value()==0 and key4_status==1 :#2.按键被按下的瞬间,下降沿
        #用户自定义函数
        key4val=1
        #关闭定时器,不然中断会有影响
        print("key4 is up to down")
    elif KEY4.value()==0 and key4_status==0 :#3.按键被按下的状态,
       #用户自定义函数
       clock.tick()
#       print("key4 is downing")
    key4_status=KEY4.value()	#获取最新状态

def KEY5_Read():
    global key5_status
    global task_mode,par_index
    if KEY5.value()==0 and key5_status==1 :#2.按键被按下的瞬间,下降沿
        #用户自定义函数
        if task_mode==1: #如果是调参模式
            par_index -=1
            if par_index==0:#参数0-17之间,0-5红色,6-11绿,12-17蓝
                par_index=17
            print("par_index=",par_index)
#        print("key5 is up to down")

    elif KEY5.value()==0 and key5_status==0 :#3.按键被按下的状态,
       #用户自定义函数
       clock.tick()
#       print("key5 is downing")
    key5_status=KEY5.value()	#获取最新状态


key6cnt=0
task_num=0
def KEY6_Read():
    global key6_status
    global key6cnt
    global task_num
    if KEY6.value()==0 and key6_status==1 :#2.按键被按下的瞬间,下降沿
        #用户自定义函数
        print("key6 is up to down")
        if key6cnt==0:  #红色
            key6cnt=1
            task_num=0

        elif key6cnt==1: #绿色
            key6cnt=2
            task_num=0

        elif key6cnt==2:#blue color
            key6cnt=3
            task_num=0

        elif key6cnt==3:#scan code
                key6cnt=0
                task_num=1


    elif KEY6.value()==0 and key6_status==0 :#3.按键被按下的状态,
       #用户自定义函数
       print("key6 is downing")
    key6_status=KEY6.value()	#获取最新状态



def search_max(blobs):
    max_blob = 0
    max_size = 0
    for blob in blobs:
        blob_area = blob[2] * blob[3]
        if blob_area > max_size:
            max_blob = blob
            max_size = blob_area
    return max_blob
    
    
    
#闪烁5次之后,等5秒,5秒之内看下有没有任意一个按键按下,有的话,进入调参模式,没有的话,比赛模式
num=0
blue_led.on()
for num in range(10):  # 迭代 10 到 20 (不包含) 之间的数字
    blue_led.toggle()
    time.sleep_ms(300)
    print(num)
blue_led.off()


num=0
for num in range(10):  # 迭代 10 到 20 (不包含) 之间的数字
    blue_led.toggle()
    if task_mode==1:#调参模式
        break
    time.sleep_ms(500)
    print(num)

blue_led.off()






g_thread_red00=[(15, 25, 10, 25, -5, 20)]
#绿(24, 52, -40, -4, 0, 34)
while(True):
    if task_mode==1:#调参模式[15, 25, 10, 25, -5, 20]  [g_thread_red][thresholds[threshold_index]]
       #print("tiaocan mode")
        clock.tick()
        img = sensor.snapshot()

        lcd_img =img.scale(0.4,0.5)

        lcd_img.draw_string(0,0,"par_index: %d "% (par_index))
        lcd_img.draw_string(0,10,"rL:%d,%d"% (g_thread_red[0],g_thread_red[1]),color= [200, 0, 0])
        lcd_img.draw_string(0,20,"rA:%d,%d"% (g_thread_red[2],g_thread_red[3]),color= [200, 0, 0])
        lcd_img.draw_string(0,30,"rB:%d,%d"% (g_thread_red[4],g_thread_red[5]),color= [200, 0, 0])
        lcd_img.draw_string(0,40,"gL:%d,%d"% (g_thread_green[0],g_thread_green[1]),color=[0, 255, 0])
        lcd_img.draw_string(0,50,"gA:%d,%d"% (g_thread_green[2],g_thread_green[3]),color=[0, 255, 0])
        lcd_img.draw_string(0,60,"gB:%d,%d"% (g_thread_green[4],g_thread_green[5]),color=[0, 255, 0])
        lcd_img.draw_string(0,70,"gL:%d,%d"% (g_thread_green[0],g_thread_green[1]),color=[0, 91, 170])
        lcd_img.draw_string(0,80,"gA:%d,%d"% (g_thread_green[2],g_thread_green[3]),color=[0, 91, 170])
        lcd_img.draw_string(0,90,"gB:%d,%d"% (g_thread_green[4],g_thread_green[5]),color=[0, 91, 170])
        lcd.display(lcd_img)

        if 0<=par_index<6:
            threadtemp = g_thread_red

        elif  6<=par_index<12:
            threadtemp = g_thread_green


        elif  12<=par_index<18:
            threadtemp = g_thread_blue


        print(threadtemp)
                                            #如果找到了目标颜色
        print("here0000")
        blobs = img.find_blobs([threadtemp], area_threshold=500, margin=10)
            #寻找对应阈值的色块,阈值小于300像素的色块过滤掉,合并相邻像素在10个像素内的色块
        if blobs:                                            #如果找到了目标颜色
                print("here0000")
                max_blob = search_max(blobs)
                lcd_img.draw_rectangle(max_blob[0:4])  # rect
                lcd_img.draw_cross(max_blob[5], max_blob[6]) # cx,cy
                lcd.display(lcd_img)




        if key4val==1:
                    print("key4--------")
                    key4val=0
                    print("-@@red--start")
                    print(g_thread_red[0])
                    print(g_thread_red[1])
                    print(g_thread_red[2])
                    print(g_thread_red[3])
                    print(g_thread_red[4])
                    print(g_thread_red[5])
                    print("-@@red--end")

                    print("-----write data to file---")

                    f =open("save1.txt","w")
                    #写入数据
                    f.seek(0)
                    f.write(str(g_thread_red[0])+","+str(g_thread_red[1])+","+str(g_thread_red[2])+","+str(g_thread_red[3])+","+str(g_thread_red[4])+","+str(g_thread_red[5])+","+\
                            str(g_thread_green[0])+","+str(g_thread_green[1])+","+str(g_thread_green[2])+","+str(g_thread_green[3])+","+str(g_thread_green[4])+","+str(g_thread_green[5])+","+\
                            str(g_thread_blue[0])+","+str(g_thread_blue[1])+","+str(g_thread_blue[2])+","+str(g_thread_blue[3])+","+str(g_thread_blue[4])+","+str(g_thread_blue[5]))
                    time.sleep_ms(10) #等待写入完成
                    f.seek(0)
            #        f.flush() #缓存刷新
                    f.close() #关闭文件
                    task_mode=0
                    print("write ok and close\r\n")


    elif task_mode==0:  #
       print("game mode")
#       print("-@@red--start")
#       print(g_thread_red[0])
#       print(g_thread_red[1])
#       print(g_thread_red[2])
#       print(g_thread_red[3])
#       print(g_thread_red[4])
#       print(g_thread_red[5])
#       print("-@@red--end")
       waitkeyflag =1
       break  #跳出这个循环














#如果上面5秒之内没有按键按下,错过这个时间点,按键1后面执行的是第二阶段的代码
if key1_step==0:
    key1_step=1


print("while(True)...")
threadtemp =g_thread_red
while(True):
    clock.tick()

    if waitkeyflag==0:
        wait_key()
#        print(waitkeyflag)
#        print("wait key00")
    elif waitkeyflag==1 :
        img = sensor.snapshot()
        lcd_img =img.scale(0.4,0.5)

        lcd_img.draw_string(0,0,"par_index: %d "% (par_index))
        lcd_img.draw_string(0,10,"rL:%d,%d"% (g_thread_red[0],g_thread_red[1]),color= [200, 0, 0])
        lcd_img.draw_string(0,20,"rA:%d,%d"% (g_thread_red[2],g_thread_red[3]),color= [200, 0, 0])
        lcd_img.draw_string(0,30,"rB:%d,%d"% (g_thread_red[4],g_thread_red[5]),color= [200, 0, 0])
        lcd_img.draw_string(0,40,"gL:%d,%d"% (g_thread_green[0],g_thread_green[1]),color=[0, 255, 0])
        lcd_img.draw_string(0,50,"gA:%d,%d"% (g_thread_green[2],g_thread_green[3]),color=[0, 255, 0])
        lcd_img.draw_string(0,60,"gB:%d,%d"% (g_thread_green[4],g_thread_green[5]),color=[0, 255, 0])
        lcd_img.draw_string(0,70,"gL:%d,%d"% (g_thread_green[0],g_thread_green[1]),color=[0, 91, 170])
        lcd_img.draw_string(0,80,"gA:%d,%d"% (g_thread_green[2],g_thread_green[3]),color=[0, 91, 170])
        lcd_img.draw_string(0,90,"gB:%d,%d"% (g_thread_green[4],g_thread_green[5]),color=[0, 91, 170])
        lcd_img.draw_string(0,100,"task_num:%d"% (task_num),color=[0, 91, 170])

        lcd.display(lcd_img)
        if key6cnt==1:
            threadtemp = g_thread_red
        elif   key6cnt==2:
            threadtemp = g_thread_green
        elif   key6cnt==3:
                threadtemp = g_thread_blue
        elif   key6cnt==0:
               task_mode=1

        if task_num==0:
            blobs = img.find_blobs([threadtemp], area_threshold=500, margin=10)
                #寻找对应阈值的色块,阈值小于300像素的色块过滤掉,合并相邻像素在10个像素内的色块
            if blobs:                                            #如果找到了目标颜色
                    print("here0000")
                    max_blob = search_max(blobs)
                    lcd_img.draw_rectangle(max_blob[0:4])  # rect
                    lcd_img.draw_cross(max_blob[5], max_blob[6]) # cx,cy
                    lcd.display(lcd_img)
                    
                    
                    
                    
#                    for b in blobs:
#                    #迭代找到的目标颜色区域
#                        lcd_img.draw_cross(int(b[5]), int(b[6]))                  #画十字 cx,cy
#            #            blobs.cx() 返回色块的外框的中心x坐标(int),也可以通过blob[5]来获取。
#            #            blobs.cy() 返回色块的外框的中心y坐标(int),也可以通过blob[6]来获取。
#                        lcd_img.draw_edges(b.min_corners(), color=(0,255,0))#画框
#                        lcd.display(lcd_img)
        elif task_num==1:
            for code in img.find_qrcodes():
                 lcd_img.draw_rectangle(code.rect(), color = 127)
                 outstr1=str(code.payload())
                 lcd_img.draw_string(0,110,"outstr1:%s"% (outstr1),color=[0, 0, 170])

                 print(code)
                 
                 
                 


        lcd_img.draw_string(0,0,"par_index: %d "% (par_index))


        lcd.display(lcd_img)
        print(clock.fps())

#    print(task_mode)
#    print("key1_step="+str(key1_step))
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.
  • 210.
  • 211.
  • 212.
  • 213.
  • 214.
  • 215.
  • 216.
  • 217.
  • 218.
  • 219.
  • 220.
  • 221.
  • 222.
  • 223.
  • 224.
  • 225.
  • 226.
  • 227.
  • 228.
  • 229.
  • 230.
  • 231.
  • 232.
  • 233.
  • 234.
  • 235.
  • 236.
  • 237.
  • 238.
  • 239.
  • 240.
  • 241.
  • 242.
  • 243.
  • 244.
  • 245.
  • 246.
  • 247.
  • 248.
  • 249.
  • 250.
  • 251.
  • 252.
  • 253.
  • 254.
  • 255.
  • 256.
  • 257.
  • 258.
  • 259.
  • 260.
  • 261.
  • 262.
  • 263.
  • 264.
  • 265.
  • 266.
  • 267.
  • 268.
  • 269.
  • 270.
  • 271.
  • 272.
  • 273.
  • 274.
  • 275.
  • 276.
  • 277.
  • 278.
  • 279.
  • 280.
  • 281.
  • 282.
  • 283.
  • 284.
  • 285.
  • 286.
  • 287.
  • 288.
  • 289.
  • 290.
  • 291.
  • 292.
  • 293.
  • 294.
  • 295.
  • 296.
  • 297.
  • 298.
  • 299.
  • 300.
  • 301.
  • 302.
  • 303.
  • 304.
  • 305.
  • 306.
  • 307.
  • 308.
  • 309.
  • 310.
  • 311.
  • 312.
  • 313.
  • 314.
  • 315.
  • 316.
  • 317.
  • 318.
  • 319.
  • 320.
  • 321.
  • 322.
  • 323.
  • 324.
  • 325.
  • 326.
  • 327.
  • 328.
  • 329.
  • 330.
  • 331.
  • 332.
  • 333.
  • 334.
  • 335.
  • 336.
  • 337.
  • 338.
  • 339.
  • 340.
  • 341.
  • 342.
  • 343.
  • 344.
  • 345.
  • 346.
  • 347.
  • 348.
  • 349.
  • 350.
  • 351.
  • 352.
  • 353.
  • 354.
  • 355.
  • 356.
  • 357.
  • 358.
  • 359.
  • 360.
  • 361.
  • 362.
  • 363.
  • 364.
  • 365.
  • 366.
  • 367.
  • 368.
  • 369.
  • 370.
  • 371.
  • 372.
  • 373.
  • 374.
  • 375.
  • 376.
  • 377.
  • 378.
  • 379.
  • 380.
  • 381.
  • 382.
  • 383.
  • 384.
  • 385.
  • 386.
  • 387.
  • 388.
  • 389.
  • 390.
  • 391.
  • 392.
  • 393.
  • 394.
  • 395.
  • 396.
  • 397.
  • 398.
  • 399.
  • 400.
  • 401.
  • 402.
  • 403.
  • 404.
  • 405.
  • 406.
  • 407.
  • 408.
  • 409.
  • 410.
  • 411.
  • 412.
  • 413.
  • 414.
  • 415.
  • 416.
  • 417.
  • 418.
  • 419.
  • 420.
  • 421.
  • 422.
  • 423.
  • 424.
  • 425.
  • 426.
  • 427.
  • 428.
  • 429.
  • 430.
  • 431.
  • 432.
  • 433.
  • 434.
  • 435.
  • 436.
  • 437.
  • 438.
  • 439.
  • 440.
  • 441.
  • 442.
  • 443.
  • 444.
  • 445.
  • 446.
  • 447.
  • 448.
  • 449.
  • 450.
  • 451.
  • 452.
  • 453.
  • 454.
  • 455.
  • 456.
  • 457.
  • 458.
  • 459.
  • 460.
  • 461.
  • 462.
  • 463.
  • 464.
  • 465.
  • 466.
  • 467.
  • 468.
  • 469.
  • 470.
  • 471.
  • 472.
  • 473.
  • 474.
  • 475.
  • 476.
  • 477.
  • 478.
  • 479.
  • 480.
  • 481.
  • 482.
  • 483.
  • 484.
  • 485.
  • 486.
  • 487.
  • 488.
  • 489.
  • 490.
  • 491.
  • 492.
  • 493.
  • 494.
  • 495.
  • 496.
  • 497.
  • 498.
  • 499.
  • 500.
  • 501.
  • 502.
  • 503.
  • 504.
  • 505.
  • 506.
  • 507.
  • 508.
  • 509.
  • 510.
  • 511.
  • 512.
  • 513.
  • 514.
  • 515.
  • 516.
  • 517.
  • 518.
  • 519.
  • 520.
  • 521.
  • 522.
  • 523.
  • 524.
  • 525.
  • 526.
  • 527.
  • 528.
  • 529.
  • 530.
  • 531.
  • 532.
  • 533.
  • 534.
  • 535.
  • 536.
  • 537.
  • 538.
  • 539.
  • 540.
  • 541.
  • 542.
  • 543.
  • 544.
  • 545.
  • 546.
  • 547.
  • 548.
  • 549.
  • 550.
  • 551.
  • 552.
  • 553.
  • 554.
  • 555.
  • 556.
  • 557.
  • 558.
  • 559.
  • 560.
  • 561.
  • 562.
  • 563.
  • 564.
  • 565.
  • 566.
  • 567.
  • 568.
  • 569.
  • 570.
  • 571.
  • 572.
  • 573.
  • 574.
  • 575.
  • 576.
  • 577.
  • 578.
  • 579.
  • 580.
  • 581.
  • 582.
  • 583.
  • 584.
  • 585.
  • 586.
  • 587.
  • 588.
  • 589.
  • 590.
  • 591.
  • 592.
  • 593.
  • 594.
  • 595.
  • 596.
  • 597.
  • 598.
  • 599.
  • 600.
  • 601.
  • 602.
  • 603.
  • 604.
  • 605.
  • 606.
  • 607.
  • 608.
  • 609.
  • 610.
  • 611.
  • 612.
  • 613.
  • 614.
  • 615.
  • 616.
  • 617.
  • 618.
  • 619.
  • 620.
  • 621.
  • 622.
  • 623.
  • 624.
  • 625.
  • 626.
  • 627.
  • 628.
  • 629.
  • 630.
  • 631.
  • 632.
  • 633.
  • 634.
  • 635.
  • 636.
  • 637.
  • 638.
  • 639.
  • 640.
  • 641.
  • 642.
  • 643.
  • 644.
  • 645.
  • 646.
  • 647.
  • 648.
  • 649.
  • 650.
  • 651.
  • 652.
  • 653.
  • 654.
  • 655.
  • 656.
  • 657.
  • 658.
  • 659.
  • 660.
  • 661.
  • 662.
  • 663.
  • 664.
  • 665.
  • 666.
  • 667.
  • 668.
  • 669.
  • 670.