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))
08-02
864
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-10
487
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
04-26