Advertisement
Guest User

OV7670 Python reader

a guest
Dec 14th, 2021
704
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.35 KB | None | 0 0
  1. """
  2. Required libraries:
  3. - pyserial
  4. - pygame
  5.  
  6. You can install with pip install [name]
  7. ie, pip install pyserial
  8. """
  9.  
  10. import pygame
  11. import serial # Note: do not do pip install serial, that's a different library
  12.  
  13. # Change "COM1" and the BAUD rate as needed
  14. ser = serial.Serial("COM1", 500000,
  15.                     serial.EIGHTBITS, serial.PARITY_NONE, serial.STOPBITS_ONE,
  16.                     timeout = None)
  17.  
  18. pygame.init()
  19.  
  20. command = ['*', 'R', 'D', 'Y', '*']
  21.  
  22. # These may be actually flipped, so width is really the height and vice versa
  23. WIDTH = 320
  24. HEIGHT = 240
  25.  
  26. # Store image output
  27. rgb = [[0 for _ in range(2 * WIDTH)] for _ in range(2 * HEIGHT)]
  28.  
  29. try:
  30.     while True:
  31.         # Wait until we recieve *RDY*, which marks the start
  32.         # of an image
  33.         # -----------------------------
  34.         valid = False
  35.         print("Waiting for image...")
  36.         i = 0
  37.        
  38.         while True:
  39.             # Try to decode - our command is all UTF-8 chars
  40.             try:
  41.                 cc = ser.read().decode("utf-8")
  42.             except (KeyboardInterrupt, SystemExit):
  43.                 raise
  44.             except:
  45.                 continue
  46.            
  47.             if command[i] == cc:
  48.                 i += 1
  49.             else:
  50.                 i = 0
  51.             if i >= len(command):
  52.                 break
  53.                
  54.         print("Image found")
  55.         # -----------------------------
  56.  
  57.         window = pygame.display.set_mode((2 * WIDTH, 2* HEIGHT))
  58.         window.fill(0)
  59.  
  60.         # Clear read buffer
  61.         _ = ser.read_all()
  62.  
  63.         print("Reading values...")
  64.  
  65.         tmp = 0
  66.        
  67.         # Read the image - just a list of bytes, column by column
  68.         # Each byte represents the brightness 0 - 255
  69.        
  70.         for y in range(HEIGHT):
  71.             for x in range(WIDTH):
  72.                 # byteorder doesn't matter since we're reading 1 byte at a time
  73.                 tmp = int.from_bytes(ser.read(), byteorder="big")
  74.                 rgb[y][x] = tmp & 0xFF
  75.  
  76.             if y % 10 == 0:
  77.                 print(y, ser.inWaiting())
  78.  
  79.         # Draw the image
  80.         for y in range(HEIGHT):
  81.             for x in range(WIDTH):
  82.                 r = g = b = rgb[y][x]
  83.                 pygame.draw.rect(window, (r,g,b), (x, y, 1, 1))
  84.                 pygame.event.clear()
  85.  
  86.         pygame.display.flip()
  87.         pygame.display.update()
  88.  
  89.         print("Image rendered")
  90.  
  91.         pygame.display.flip()
  92.         pygame.display.update()
  93.         ser.read_all()
  94.        
  95. except Exception as e:
  96.     print(e)
  97.     pygame.event.clear()
  98.     ser.close()
  99.     pygame.quit()
  100.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement