/ examples / ov5640_simpletest.py
ov5640_simpletest.py
 1  # SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries
 2  # SPDX-FileCopyrightText: Copyright (c) 2021 Jeff Epler for Adafruit Industries
 3  #
 4  # SPDX-License-Identifier: Unlicense
 5  
 6  """Capture an image from the camera and display it as ASCII art.
 7  
 8  This demo is designed to run on the Kaluga, but you can adapt it
 9  to other boards by changing the constructors for `bus` and `cam`
10  appropriately.
11  
12  The camera is placed in YUV mode, so the top 8 bits of each color
13  value can be treated as "greyscale".
14  
15  It's important that you use a terminal program that can interpret
16  "ANSI" escape sequences.  The demo uses them to "paint" each frame
17  on top of the prevous one, rather than scrolling.
18  
19  Remember to take the lens cap off, or un-comment the line setting
20  the test pattern!
21  """
22  
23  import sys
24  import time
25  
26  import busio
27  import board
28  
29  import adafruit_ov5640
30  
31  print("construct bus")
32  bus = busio.I2C(scl=board.CAMERA_SIOC, sda=board.CAMERA_SIOD)
33  print("construct camera")
34  cam = adafruit_ov5640.OV5640(
35      bus,
36      data_pins=board.CAMERA_DATA,
37      clock=board.CAMERA_PCLK,
38      vsync=board.CAMERA_VSYNC,
39      href=board.CAMERA_HREF,
40      mclk=board.CAMERA_XCLK,
41      size=adafruit_ov5640.OV5640_SIZE_QQVGA,
42  )
43  print("print chip id")
44  print(cam.chip_id)
45  
46  
47  cam.colorspace = adafruit_ov5640.OV5640_COLOR_YUV
48  cam.flip_y = True
49  cam.flip_x = True
50  cam.test_pattern = False
51  
52  buf = bytearray(cam.capture_buffer_size)
53  chars = b" .':-+=*%$#"
54  remap = [chars[i * (len(chars) - 1) // 255] for i in range(256)]
55  
56  width = cam.width
57  row = bytearray(width)
58  
59  print("capturing")
60  cam.capture(buf)
61  print("capture complete")
62  
63  sys.stdout.write("\033[2J")
64  while True:
65      cam.capture(buf)
66      for j in range(0, cam.height, 2):
67          sys.stdout.write(f"\033[{j//2}H")
68          for i in range(cam.width):
69              row[i] = remap[buf[2 * (width * j + i)]]
70          sys.stdout.write(row)
71          sys.stdout.write("\033[K")
72      sys.stdout.write("\033[J")
73      time.sleep(0.05)