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

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):  # 迭代 1020 (不包含) 之间的数字
    blue_led.toggle()
    time.sleep_ms(200)
    print(num)
blue_led.off()


num=0
for num in range(10):  # 迭代 1020 (不包含) 之间的数字
    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))

目的
设置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的代码,改进版本要加边界处理,因为数据是
在这里插入图片描述
注意,如果比赛对两种颜色进行处理,那么自己根据实际调节。

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值