串口接收有没有检测接收中断,怎么避免出现直接调用接收函数来等待检测接收的数据
有
有点好奇 发布的帖子
-
RE: 摄像头
import sensor, image, time ,math from pyb import UART import json red_threshold = (100, 38, 9, 51, 47, -82) yellow_threshold = (100, 6, -40, -11, -69, 116) black_threshold =(0, 25, -31, 26, 25, -44) sensor.reset() sensor.set_pixformat(sensor.RGB565) sensor.set_framesize(sensor.QVGA) sensor.skip_frames(50) sensor.set_auto_whitebal(False) clock = time.clock() uart = UART(3, 115200) def find_max(blobs): max_size=0 for blob in blobs: if blob[2]*blob[3] > max_size: max_blob=blob max_size = blob[2]*blob[3] return max_blob def find_red(blobs): max_size = 0 for blob in blobs: if blob[2]*blob[3] > max_size: max_blob=blob max_size = blob[2]*blob[3] return max_size def line_filter_copy(src, dst): for i in range(0, len(dst), 1): dst[i] = src[i<<1] def line_filter_bw(src, dst): for i in range(0, len(dst), 1): if (src[i<<1] > 200 and src[i<<1] < 255): dst[i] = 0xFF else: dst[i] = 0x00 i = 0 max_number = 0 while(True): clock.tick() lines = 0 img = sensor.snapshot(line_filter = line_filter_copy) black_blobs = img.find_blobs([black_threshold]) red_blobs = img.find_blobs([red_threshold]) if black_blobs: max_blob=find_max(black_blobs) #print('sum :', len(blobs)) if i == 0: img.draw_rectangle(max_blob.rect()) img.draw_cross(max_blob.cx(), max_blob.cy()) else : pass bx_x=max_blob.cx()-160 by_y=120-max_blob.cy() x=math.fabs(bx_x) y=math.fabs(by_y) if 0 <= x < 10 : bx_value = 1 elif 10 <= x < 100 : bx_value = 2 else : bx_value = 3 if 0 <= y < 10 : by_value = 1 elif 10 <= y < 100 : by_value = 2 else : by_value = 3 b_output_str="x%d%d,y%d%d" % (bx_value,bx_x,by_value,by_y) #print('you send:',b_output_str) #uart.write(b_output_str+'\r\n') else: print('not found!') b_output_str = "0" #uart.write(b_output_str+'\r\n') #img = sensor.snapshot() #blobs = img.find_blobs([yellow_threshold]) if red_blobs: max_blob=find_max(red_blobs) max_number=find_red(red_blobs) #print('sum :', len(blobs)) if i == 1 : img.draw_rectangle(max_blob.rect()) img.draw_cross(max_blob.cx(), max_blob.cy()) else : pass x_x=max_blob.cx()-160 y_y=120-max_blob.cy() x=math.fabs(x_x) y=math.fabs(y_y) if 0 <= x < 10 : x_value = 1 elif 10 <= x < 100 : x_value = 2 else : x_value = 3 if 0 <= y < 10 : y_value = 1 elif 10 <= y < 100 : y_value = 2 else : y_value = 3 output_str="x%d%d,y%d%d" % (x_value,x_x,y_value,y_y) #print('you send:',output_str) #uart.write(output_str+'\r\n') else: #print('not found!') output_str = "0" #uart.write(output_str+'\r\n') #print('max_number:',max_number) if max_number > 1250 : i = 1 while(i): clock.tick() lines = 0 img = sensor.snapshot(line_filter = line_filter_copy) red_blobs = img.find_blobs([red_threshold]) if red_blobs: max_blob=find_max(red_blobs) max_number=find_red(red_blobs) #print('sum :', len(blobs)) if i == 1 : img.draw_rectangle(max_blob.rect()) img.draw_cross(max_blob.cx(), max_blob.cy()) else : pass x_x=max_blob.cx()-160 y_y=120-max_blob.cy() x=math.fabs(x_x) y=math.fabs(y_y) if 0 <= x < 10 : x_value = 1 elif 10 <= x < 100 : x_value = 2 else : x_value = 3 if 0 <= y < 10 : y_value = 1 elif 10 <= y < 100 : y_value = 2 else : y_value = 3 output_str="x%d%d,y%d%d" % (x_value,x_x,y_value,y_y) #print('you send:',output_str) #uart.write(output_str+'\r\n') else: #print('not found!') output_str = "0" #uart.write(output_str+'\r\n') uart.write(output_str+'\r\n') print('you send red:',output_str) if max_number < 100 : i = 0 else : i = 1 else : uart.write(b_output_str+'\r\n') print('you send black:',b_output_str) i = 0 max_number = 0