import sensor
import image
import lcd
import utime
import time
from board import board_info
from fpioa_manager import fm
from machine import UART
from Maix import GPIO
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.run(1)
fm.register(10, fm.fpioa.UART1_TX, force=True)
fm.register(11, fm.fpioa.UART1_RX, force=True)
uart_A = UART(UART.UART1, 9600, 8, None, 1, timeout=1000, read_buf_len=4096)
fm.register(1,fm.fpioa.GPIOHS1,force=True)
fm.register(3,fm.fpioa.GPIOHS3,force=True)
fm.register(7,fm.fpioa.GPIOHS7,force=True)
DIN=GPIO(GPIO.GPIOHS1, GPIO.OUT)
CS =GPIO(GPIO.GPIOHS3, GPIO.OUT)
CLK=GPIO(GPIO.GPIOHS7, GPIO.OUT)
def MAX7219_WriteByte(data):#这是数码管扫描结果显示用的
CS.value(0)
for i in range(8,0,-1):
CLK.value(0)
DIN.value(data&0x80)
data=data<<1
CLK.value(1)
def MAX7219_Write(address,data):#这是数码管扫描结果显示用的
CS.value(0)
MAX7219_WriteByte(address)
MAX7219_WriteByte(data)
CS.value(1)
red_threshold = (0, 80, 8, 50, 0, 59);
green_threshold= (0, 80, -70,-5,-0,30);
blue_threshold = (0, 80, -0,30,-70,-5);
clock = time.clock()
lcd.init()
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
#sensor.set_vflip(1) #开启图像反转
sensor.set_vflip(0) #关闭图像反转
sensor.run(1)
sensor.skip_frames(30)
while True: #扫二维码并且显示
clock.tick()
img = sensor.snapshot()
res = img.find_qrcodes()
fps =clock.fps()
read_str=uart_A.read()
if len(res) > 0:
img.draw_string(2,2, res[0].payload(), color=(0,128,0), scale=2)
result=res[0].payload()
break
lcd.display(img)
result_2=result.partition('+')#扫描的是123+321,这个函数是把’+‘去了,方便发送
top_floor=int(result_2[0])
bottom_floor=int(result_2[2])
MAX7219_Write(0x09, 0xff)#这是数码管扫描结果显示用的
MAX7219_Write(0x0a, 0x03)
MAX7219_Write(0x0b, 0x07)
MAX7219_Write(0x0c, 0x01)
MAX7219_Write(0x0f, 0x01)
time.sleep_ms(1000)
MAX7219_Write(0x0f, 0x00)
MAX7219_Write(1,int(result[6]))
MAX7219_Write(2,int(result[5]))
MAX7219_Write(3,int(result[4]))
MAX7219_Write(4,0xA)
MAX7219_Write(5,3)
MAX7219_Write(6,int(result[2]))
MAX7219_Write(7,int(result[1]))
MAX7219_Write(8,2)
top_left="0"
top_right="0"
bottom_left="0"
bottom_right="0"
i=20
while i:#颜色识别开始,把一个屏幕分成4份,调整车合适的位置,以便减少扫描点的数量
i=i-1
img=sensor.snapshot()
blobs1 = img.find_blobs([red_threshold],pixels_threshold=100, area_threshold=100, merge=True, margin=10)
blobs2 = img.find_blobs([green_threshold],pixels_threshold=100, area_threshold=100, merge=True, margin=10)
blobs3 = img.find_blobs([blue_threshold],pixels_threshold=100, area_threshold=100, merge=True, margin=10)
read_str=uart_A.read()
if blobs1:
for b in blobs1:
if b[6]>0 and b[6]<100:#第一层
if b[5]>0 and b[5]<130:
tmp=img.draw_rectangle(b[0:4])
tmp=img.draw_cross(b[5], b[6])
top_left = "1"
if b[5]>190 and b[5]<320:
tmp=img.draw_rectangle(b[0:4])
tmp=img.draw_cross(b[5], b[6])
top_right = "1"
if b[6]>140 and b[6]<240:#第二层
if b[5]>0 and b[5]<130:
tmp=img.draw_rectangle(b[0:4])
tmp=img.draw_cross(b[5], b[6])
bottom_left = "1"
if b[5]>190 and b[5]<320:
tmp=img.draw_rectangle(b[0:4])
tmp=img.draw_cross(b[5], b[6])
bottom_right = "1"
if blobs2:
for b in blobs2:
if b[6]>0 and b[6]<100:
if b[5]>0 and b[5]<130:
tmp=img.draw_rectangle(b[0:4])
tmp=img.draw_cross(b[5], b[6])
top_left = "2"
if b[5]>190 and b[5]<320:
tmp=img.draw_rectangle(b[0:4])
tmp=img.draw_cross(b[5], b[6])
top_right = "2"
if b[6]>140 and b[6]<240:#第二层
if b[5]>0 and b[5]<130:
tmp=img.draw_rectangle(b[0:4])
tmp=img.draw_cross(b[5], b[6])
bottom_left = "2"
if b[5]>190 and b[5]<320:
tmp=img.draw_rectangle(b[0:4])
tmp=img.draw_cross(b[5], b[6])
bottom_right = "2"
if blobs3:
for b in blobs3:
if b[6]>0 and b[6]<100:
if b[5]>0 and b[5]<130:
tmp=img.draw_rectangle(b[0:4])
tmp=img.draw_cross(b[5], b[6])
#c=img.get_pixel(b[5], b[6])
top_left = "3"
if b[5]>190 and b[5]<320:
tmp=img.draw_rectangle(b[0:4])
tmp=img.draw_cross(b[5], b[6])
top_right = "3"
if b[6]>140 and b[6]<240:#第二层
if b[5]>0 and b[5]<130:
tmp=img.draw_rectangle(b[0:4])
tmp=img.draw_cross(b[5], b[6])
bottom_left = "3"
if b[5]>190 and b[5]<320:
tmp=img.draw_rectangle(b[0:4])
tmp=img.draw_cross(b[5], b[6])
bottom_right = "3"
lcd.display(img)
color_top=int(top_left+top_right)
color_bottom=int(bottom_left+bottom_right)
#第一层
while True: #扫描完毕,这个程序是和二维码结果比对,确定顺序
if top_floor==123:
if color_top=12: #蓝红绿
uart_A.write("b r g")
if color_top=13: #绿红蓝
uart_A.write("g r b")
if color_top=21: #蓝绿红
uart_A.write("b g r")
if color_top=23: #红绿蓝
uart_A.write("r g b")
if color_top=31: #绿蓝红
uart_A.write("g b r")
if color_top=32: #红蓝绿
uart_A.write("r b g")
if top_floor==132:
if color_top=12: #蓝红绿
uart_A.write("b r g")
if color_top=13: #绿红蓝
uart_A.write("g r b")
if color_top=21: #蓝绿红
uart_A.write("b g r")
if color_top=23: #红绿蓝
uart_A.write("r g b")
if color_top=31: #绿蓝红
uart_A.write("g b r")
if color_top=32: #红蓝绿
uart_A.write("r b g")
if top_floor==213:
if color_top=12: #蓝红绿
uart_A.write("b r g")
if color_top=13: #绿红蓝
uart_A.write("g r b")
if color_top=21: #蓝绿红
uart_A.write("b g r")
if color_top=23: #红绿蓝
uart_A.write("r g b")
if color_top=31: #绿蓝红
uart_A.write("g b r")
if color_top=32: #红蓝绿
uart_A.write("r b g")
if top_floor==231:
if color_top=12: #蓝红绿
uart_A.write("b r g")
if color_top=13: #绿红蓝
uart_A.write("g r b")
if color_top=21: #蓝绿红
uart_A.write("b g r")
if color_top=23: #红绿蓝
uart_A.write("r g b")
if color_top=31: #绿蓝红
uart_A.write("g b r")
if color_top=32: #红蓝绿
uart_A.write("r b g")
if top_floor==321:
if color_top=12: #蓝红绿
uart_A.write("b r g")
if color_top=13: #绿红蓝
uart_A.write("g r b")
if color_top=21: #蓝绿红
uart_A.write("b g r")
if color_top=
评论0