/ README.rst
README.rst
  1  Introduction
  2  ============
  3  
  4  
  5  .. image:: https://readthedocs.org/projects/adafruit-circuitpython-ov5640/badge/?version=latest
  6      :target: https://docs.circuitpython.org/projects/ov5640/en/latest/
  7      :alt: Documentation Status
  8  
  9  
 10  .. image:: https://raw.githubusercontent.com/adafruit/Adafruit_CircuitPython_Bundle/main/badges/adafruit_discord.svg
 11      :target: https://adafru.it/discord
 12      :alt: Discord
 13  
 14  
 15  .. image:: https://github.com/adafruit/Adafruit_CircuitPython_ov5640/workflows/Build%20CI/badge.svg
 16      :target: https://github.com/adafruit/Adafruit_CircuitPython_ov5640/actions
 17      :alt: Build Status
 18  
 19  
 20  .. image:: https://img.shields.io/badge/code%20style-black-000000.svg
 21      :target: https://github.com/psf/black
 22      :alt: Code Style: Black
 23  
 24  CircuitPython driver for OV5640 Camera
 25  
 26  
 27  Installing to a Connected CircuitPython Device with Circup
 28  ==========================================================
 29  
 30  Make sure that you have ``circup`` installed in your Python environment.
 31  Install it with the following command if necessary:
 32  
 33  .. code-block:: shell
 34  
 35      pip3 install circup
 36  
 37  With ``circup`` installed and your CircuitPython device connected use the
 38  following command to install:
 39  
 40  .. code-block:: shell
 41  
 42      circup install adafruit_ov5640
 43  
 44  Or the following command to update an existing version:
 45  
 46  .. code-block:: shell
 47  
 48      circup update
 49  
 50  Usage Example
 51  =============
 52  
 53  .. code-block: python
 54  
 55      """Capture an image from the camera and display it as ASCII art.
 56  
 57      This demo is designed to run on the Kaluga, but you can adapt it
 58      to other boards by changing the constructors for `bus` and `cam`
 59      appropriately.
 60  
 61      The camera is placed in YUV mode, so the top 8 bits of each color
 62      value can be treated as "greyscale".
 63  
 64      It's important that you use a terminal program that can interpret
 65      "ANSI" escape sequences.  The demo uses them to "paint" each frame
 66      on top of the prevous one, rather than scrolling.
 67  
 68      Remember to take the lens cap off, or un-comment the line setting
 69      the test pattern!
 70      """
 71  
 72      import sys
 73      import time
 74  
 75      import busio
 76      import board
 77  
 78      import adafruit_ov5640
 79  
 80      print("construct bus")
 81      bus = busio.I2C(scl=board.CAMERA_SIOC, sda=board.CAMERA_SIOD)
 82      print("construct camera")
 83      cam = adafruit_ov5640.OV5640(
 84          bus,
 85          data_pins=board.CAMERA_DATA,
 86          clock=board.CAMERA_PCLK,
 87          vsync=board.CAMERA_VSYNC,
 88          href=board.CAMERA_HREF,
 89          mclk=board.CAMERA_XCLK,
 90          size=adafruit_ov5640.OV5640_SIZE_QQVGA,
 91      )
 92      print("print chip id")
 93      print(cam.chip_id)
 94  
 95  
 96      cam.colorspace = adafruit_ov5640.OV5640_COLOR_YUV
 97      cam.flip_y = True
 98      cam.flip_x = True
 99      cam.test_pattern = False
100  
101      buf = bytearray(cam.capture_buffer_size)
102      chars = b" .':-+=*%$#"
103      remap = [chars[i * (len(chars) - 1) // 255] for i in range(256)]
104  
105      width = cam.width
106      row = bytearray(width)
107  
108      print("capturing")
109      cam.capture(buf)
110      print("capture complete")
111  
112      sys.stdout.write("\033[2J")
113      while True:
114          cam.capture(buf)
115          for j in range(0, cam.height, 2):
116              sys.stdout.write(f"\033[{j//2}H")
117              for i in range(cam.width):
118                  row[i] = remap[buf[2 * (width * j + i)]]
119              sys.stdout.write(row)
120              sys.stdout.write("\033[K")
121          sys.stdout.write("\033[J")
122          time.sleep(0.05)
123  
124  Documentation
125  =============
126  
127  API documentation for this library can be found on `Read the Docs <https://docs.circuitpython.org/projects/ov5640/en/latest/>`_.
128  
129  For information on building library documentation, please check out
130  `this guide <https://learn.adafruit.com/creating-and-sharing-a-circuitpython-library/sharing-our-docs-on-readthedocs#sphinx-5-1>`_.
131  
132  Contributing
133  ============
134  
135  Contributions are welcome! Please read our `Code of Conduct
136  <https://github.com/adafruit/Adafruit_CircuitPython_ov5640/blob/HEAD/CODE_OF_CONDUCT.md>`_
137  before contributing to help this project stay welcoming.