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]
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 = 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()
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
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
if g_thread_red[0]>=100:g_thread_red[0] =0
if g_thread_red[1]>=100: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]>=100:g_thread_green[0] =0
if g_thread_green[1]>=100: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]>=100:g_thread_blue[0] =0
if g_thread_blue[1]>=100: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]>=100:g_thread_red[0] =0
if g_thread_red[1]>=100: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]>=100:g_thread_green[0] =0
if g_thread_green[1]>=100: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]>=100:g_thread_blue[0] =0
if g_thread_blue[1]>=100: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] =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..")
# 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() #获取最新状态
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()
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)
blob = img.find_blobs([threadtemp], area_threshold=300, margin=10)
#寻找对应阈值的色块,阈值小于300像素的色块过滤掉,合并相邻像素在10个像素内的色块
if blob: #如果找到了目标颜色
print("here0000")
for b in blob:
#迭代找到的目标颜色区域
lcd_img.draw_cross(int(b[5]*0.4), int(b[6]*0.5)) #画十字 cx,cy
# blob.cx() 返回色块的外框的中心x坐标(int),也可以通过blob[5]来获取。
# blob.cy() 返回色块的外框的中心y坐标(int),也可以通过blob[6]来获取。
# lcd_img.draw_edges(b.min_corners(), color=(0,255,0))#画框
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)...")
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.display(lcd_img)
print(clock.fps())
# print(task_mode)
# print("key1_step="+str(key1_step))
卡机