/ examples / ov2640_simpletest.py
ov2640_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_ov2640
30  
31  bus = busio.I2C(scl=board.CAMERA_SIOC, sda=board.CAMERA_SIOD)
32  cam = adafruit_ov2640.OV2640(
33      bus,
34      data_pins=board.CAMERA_DATA,
35      clock=board.CAMERA_PCLK,
36      vsync=board.CAMERA_VSYNC,
37      href=board.CAMERA_HREF,
38      mclk=board.CAMERA_XCLK,
39      mclk_frequency=20_000_000,
40      size=adafruit_ov2640.OV2640_SIZE_QQVGA,
41  )
42  cam.colorspace = adafruit_ov2640.OV2640_COLOR_YUV
43  cam.flip_y = True
44  # cam.test_pattern = True
45  
46  buf = bytearray(2 * cam.width * cam.height)
47  chars = b" .:-=+*#%@"
48  remap = [chars[i * (len(chars) - 1) // 255] for i in range(256)]
49  
50  width = cam.width
51  row = bytearray(2 * width)
52  
53  sys.stdout.write("\033[2J")
54  while True:
55      cam.capture(buf)
56      for j in range(cam.height // 2):
57          sys.stdout.write(f"\033[{j}H")
58          for i in range(cam.width // 2):
59              row[i * 2] = row[i * 2 + 1] = remap[buf[4 * (width * j + i)]]
60          sys.stdout.write(row)
61          sys.stdout.write("\033[K")
62      sys.stdout.write("\033[J")
63      time.sleep(0.05)