import sensor, image, time
from pyb import LED,Pin
from pyb import Timer
task_mode = 0  #0:初始模式,就是录像,1:调参模式
blue_led = LED(1)


thresholds = [
    (30, 100, 15, 127, 15, 127),  # generic_red_thresholds
    (30, 100, -64, -8, -32, 32),  # generic_green_thresholds
    (0, 15, 0, 40, -80, -20),
]  # generic_blue_thresholds

g_thread_red   =[30, 100, 15, 127, 15, 127]
g_thread_green =[30, 100, -64, -8, -32, 32]
g_thread_blue  =[0, 15, 0, 40, -80, -20]

sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)

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 = 100000
#sensor.set_auto_exposure(False, exposure_us=EXPOSURE_MICROSECONDS)
sensor.set_auto_exposure(True, exposure_us=EXPOSURE_MICROSECONDS)

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 = 200)
clock = time.clock()

#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
                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")

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()
#f =open(file_abs,"w")
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
           print("KEY2 g_thread_red[par_index]=",g_thread_red[par_index])

        elif 6<=par_index<12:
            g_thread_green[par_index-6] +=5
            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
            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
           print("KEY2 g_thread_red[par_index]=",g_thread_red[par_index])

        elif 6<=par_index<12:
            g_thread_green[par_index-6] +=5
            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
            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
           print("KEY3 g_thread_red[par_index]=",g_thread_red[par_index])

        elif 6<=par_index<12:
            g_thread_green[par_index-6] -=5
            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
            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
          print("KEY3 g_thread_red[par_index]=",g_thread_red[par_index])

       elif 6<=par_index<12:
           g_thread_green[par_index-6] -=5
           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
           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()	#获取最新状态

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

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




#闪烁5次之后,等5秒,5秒之内看下有没有任意一个按键按下,有的话,进入调参模式,没有的话,比赛模式
num=0
blue_led.on()
for num in range(10):  # 迭代 10 到 20 (不包含) 之间的数字
    blue_led.toggle()
    time.sleep_ms(200)
    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(100)
    print(num)

blue_led.off()


while(True):
    if task_mode==1:#调参模式
       #print("tiaocan mode")
        clock.tick()
        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")
       break  #跳出这个循环

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


print("while(True)...")
while(True):
    clock.tick()

    if waitkeyflag==0:
        wait_key()
#        print(waitkeyflag)
#        print("wait key00")
    elif waitkeyflag==1 :
        img = sensor.snapshot()
        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.

目的
设置5个按键
K1—开机5秒之内检测判断K1有没有暗下来,如果暗下来,那么进入调参模式。如果没有暗下来,进入比赛使用模式。
在弹出来的U盘新建一个文件,save1.txt,具体要和代码的文件名一致;

开机,读取文件名的数据,然后把数据赋值给对应阈值;
如果是凋参模式,那么对读出来的数据进行加+5家或者减5。
调参模式下,K1更换另外一个功能,切换参数。参数范围0-17;
0-5是红色阈值,LAB,Lmin ,Lmax,Amin,Amax,Bmin,Bmax;
6-11是绿色阈值;
12-17是蓝色阈值;
K1是参数选择0-15之间,按一下参数+1;
K5是参数选择0-15,按下,参数-1;

K2是调参模式下,那么对数据+5,5是参数变化步长,具体可以根据实际调节;
K3是调参模式下,数据-5;
K4是保存数据,然后跳出保存。

注意,保存数据下,要重新按下板子的rst复位按键,才可以看到U盘的文件的数据有变化。

读取数据之后,如果要重新读取,记得把读取指针放到最开头,f.seek(0);

注意,参数的范围;

注意,加5和减5的代码,改进版本要加边界处理,因为数据是

mv:按键调参数,定时器,文件保存参数,调出参数01_自定义函数


注意,如果比赛对两种颜色进行处理,那么自己根据实际调节。