├── docs ├── _static │ ├── favicon.ico │ ├── favicon.ico.license │ └── custom.css ├── requirements.txt ├── api.rst.license ├── examples.rst.license ├── index.rst.license ├── api.rst ├── index.rst ├── examples.rst └── conf.py ├── optional_requirements.txt ├── requirements.txt ├── README.rst.license ├── .gitattributes ├── .github ├── workflows │ ├── build.yml │ ├── release_pypi.yml │ ├── release_gh.yml │ └── failure-help-text.yml └── PULL_REQUEST_TEMPLATE │ └── adafruit_circuitpython_pr.md ├── .readthedocs.yaml ├── .pre-commit-config.yaml ├── examples ├── ov2640_jpeg_kaluga1_3_boot.py ├── ov2640_jpeg_kaluga1_3.py ├── ov2640_simpletest.py ├── ov2640_displayio_kaluga1_3_ili9341.py ├── ov2640_displayio_kaluga1_3_st7789.py ├── ov2640_displayio_pico_st7789_2in.py ├── ov2640_displayio_kaluga1_3_ili9341_ulab.py ├── ov2640_aio_saola.py ├── ov2640_aio_kaluga1_3.py ├── ov2640_jpeg_sd_kaluga1_3.py ├── ov2640_jpeg_sd_pico_st7789_2in.py ├── ov2640_directio_kaluga1_3_ili9341.py └── ov2640_bmp_sd_kaluga1_3.py ├── LICENSE ├── LICENSES ├── MIT.txt ├── Unlicense.txt └── CC-BY-4.0.txt ├── pyproject.toml ├── .gitignore ├── README.rst ├── ruff.toml ├── CODE_OF_CONDUCT.md └── adafruit_ov2640.py /docs/_static/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/adafruit/Adafruit_CircuitPython_OV2640/HEAD/docs/_static/favicon.ico -------------------------------------------------------------------------------- /optional_requirements.txt: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2022 Alec Delaney, for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: Unlicense 4 | -------------------------------------------------------------------------------- /docs/_static/favicon.ico.license: -------------------------------------------------------------------------------- 1 | SPDX-FileCopyrightText: 2018 Phillip Torrone for Adafruit Industries 2 | 3 | SPDX-License-Identifier: CC-BY-4.0 4 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2022 Alec Delaney, for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: Unlicense 4 | 5 | Adafruit-Blinka 6 | adafruit-circuitpython-busdevice 7 | -------------------------------------------------------------------------------- /docs/requirements.txt: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2021 Kattni Rembor for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: Unlicense 4 | 5 | sphinx 6 | sphinxcontrib-jquery 7 | sphinx-rtd-theme 8 | -------------------------------------------------------------------------------- /README.rst.license: -------------------------------------------------------------------------------- 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: MIT 5 | -------------------------------------------------------------------------------- /docs/api.rst.license: -------------------------------------------------------------------------------- 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: MIT 5 | -------------------------------------------------------------------------------- /docs/examples.rst.license: -------------------------------------------------------------------------------- 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: MIT 5 | -------------------------------------------------------------------------------- /docs/index.rst.license: -------------------------------------------------------------------------------- 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: MIT 5 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2024 Justin Myers for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: Unlicense 4 | 5 | .py text eol=lf 6 | .rst text eol=lf 7 | .txt text eol=lf 8 | .yaml text eol=lf 9 | .toml text eol=lf 10 | .license text eol=lf 11 | .md text eol=lf 12 | -------------------------------------------------------------------------------- /docs/_static/custom.css: -------------------------------------------------------------------------------- 1 | /* SPDX-FileCopyrightText: 2025 Sam Blenny 2 | * SPDX-License-Identifier: MIT 3 | */ 4 | 5 | /* Monkey patch the rtd theme to prevent horizontal stacking of short items 6 | * see https://github.com/readthedocs/sphinx_rtd_theme/issues/1301 7 | */ 8 | .py.property{display: block !important;} 9 | -------------------------------------------------------------------------------- /docs/api.rst: -------------------------------------------------------------------------------- 1 | 2 | .. If you created a package, create one automodule per module in the package. 3 | 4 | .. If your library file(s) are nested in a directory (e.g. /adafruit_foo/foo.py) 5 | .. use this format as the module name: "adafruit_foo.foo" 6 | 7 | API Reference 8 | ############# 9 | 10 | .. automodule:: adafruit_ov2640 11 | :members: 12 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: MIT 4 | 5 | name: Build CI 6 | 7 | on: [pull_request, push] 8 | 9 | jobs: 10 | test: 11 | runs-on: ubuntu-latest 12 | steps: 13 | - name: Run Build CI workflow 14 | uses: adafruit/workflows-circuitpython-libs/build@main 15 | -------------------------------------------------------------------------------- /.readthedocs.yaml: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: Unlicense 4 | 5 | # Read the Docs configuration file 6 | # See https://docs.readthedocs.io/en/stable/config-file/v2.html for details 7 | 8 | # Required 9 | version: 2 10 | 11 | sphinx: 12 | configuration: docs/conf.py 13 | 14 | build: 15 | os: ubuntu-lts-latest 16 | tools: 17 | python: "3" 18 | 19 | python: 20 | install: 21 | - requirements: docs/requirements.txt 22 | - requirements: requirements.txt 23 | -------------------------------------------------------------------------------- /.github/workflows/release_pypi.yml: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: MIT 4 | 5 | name: PyPI Release Actions 6 | 7 | on: 8 | release: 9 | types: [published] 10 | 11 | jobs: 12 | upload-release-assets: 13 | runs-on: ubuntu-latest 14 | steps: 15 | - name: Run PyPI Release CI workflow 16 | uses: adafruit/workflows-circuitpython-libs/release-pypi@main 17 | with: 18 | pypi-username: ${{ secrets.pypi_username }} 19 | pypi-password: ${{ secrets.pypi_password }} 20 | -------------------------------------------------------------------------------- /.github/workflows/release_gh.yml: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: MIT 4 | 5 | name: GitHub Release Actions 6 | 7 | on: 8 | release: 9 | types: [published] 10 | 11 | jobs: 12 | upload-release-assets: 13 | runs-on: ubuntu-latest 14 | steps: 15 | - name: Run GitHub Release CI workflow 16 | uses: adafruit/workflows-circuitpython-libs/release-gh@main 17 | with: 18 | github-token: ${{ secrets.GITHUB_TOKEN }} 19 | upload-url: ${{ github.event.release.upload_url }} 20 | -------------------------------------------------------------------------------- /.github/workflows/failure-help-text.yml: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2021 Scott Shawcroft for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: MIT 4 | 5 | name: Failure help text 6 | 7 | on: 8 | workflow_run: 9 | workflows: ["Build CI"] 10 | types: 11 | - completed 12 | 13 | jobs: 14 | post-help: 15 | runs-on: ubuntu-latest 16 | if: ${{ github.event.workflow_run.conclusion == 'failure' && github.event.workflow_run.event == 'pull_request' }} 17 | steps: 18 | - name: Post comment to help 19 | uses: adafruit/circuitpython-action-library-ci-failed@v1 20 | -------------------------------------------------------------------------------- /.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2024 Justin Myers for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: Unlicense 4 | 5 | repos: 6 | - repo: https://github.com/pre-commit/pre-commit-hooks 7 | rev: v4.5.0 8 | hooks: 9 | - id: check-yaml 10 | - id: end-of-file-fixer 11 | - id: trailing-whitespace 12 | - repo: https://github.com/astral-sh/ruff-pre-commit 13 | rev: v0.3.4 14 | hooks: 15 | - id: ruff-format 16 | - id: ruff 17 | args: ["--fix"] 18 | - repo: https://github.com/fsfe/reuse-tool 19 | rev: v3.0.1 20 | hooks: 21 | - id: reuse 22 | -------------------------------------------------------------------------------- /examples/ov2640_jpeg_kaluga1_3_boot.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 | """Use this file as CIRCUITPY/boot.py in conjunction with ov2640_jpeg_kaluga1_3.py 6 | 7 | It makes the CIRCUITPY filesystem writable to CircuitPython 8 | (and read-only to the PC) unless the "MODE" button on the audio 9 | daughterboard is held while the board is powered on or reset. 10 | """ 11 | 12 | import analogio 13 | import board 14 | import storage 15 | 16 | V_MODE = 1.98 17 | V_RECORD = 2.41 18 | 19 | a = analogio.AnalogIn(board.IO6) 20 | a_voltage = a.value * a.reference_voltage / 65535 21 | if abs(a_voltage - V_MODE) > 0.05: # If mode is NOT pressed... 22 | print("storage writable by CircuitPython") 23 | storage.remount("/", readonly=False) 24 | -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE/adafruit_circuitpython_pr.md: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2021 Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: MIT 4 | 5 | Thank you for contributing! Before you submit a pull request, please read the following. 6 | 7 | Make sure any changes you're submitting are in line with the CircuitPython Design Guide, available here: https://docs.circuitpython.org/en/latest/docs/design_guide.html 8 | 9 | If your changes are to documentation, please verify that the documentation builds locally by following the steps found here: https://adafru.it/build-docs 10 | 11 | Before submitting the pull request, make sure you've run Pylint and Black locally on your code. You can do this manually or using pre-commit. Instructions are available here: https://adafru.it/check-your-code 12 | 13 | Please remove all of this text before submitting. Include an explanation or list of changes included in your PR, as well as, if applicable, a link to any related issues. 14 | -------------------------------------------------------------------------------- /docs/index.rst: -------------------------------------------------------------------------------- 1 | 2 | .. include:: ../README.rst 3 | 4 | Table of Contents 5 | ================= 6 | 7 | .. toctree:: 8 | :maxdepth: 4 9 | :hidden: 10 | 11 | self 12 | 13 | .. toctree:: 14 | :caption: Examples 15 | 16 | examples 17 | 18 | .. toctree:: 19 | :caption: API Reference 20 | :maxdepth: 3 21 | 22 | api 23 | 24 | .. toctree:: 25 | :caption: Tutorials 26 | 27 | .. toctree:: 28 | :caption: Other Links 29 | 30 | Download from GitHub 31 | Download Library Bundle 32 | CircuitPython Reference Documentation 33 | CircuitPython Support Forum 34 | Discord Chat 35 | Adafruit Learning System 36 | Adafruit Blog 37 | Adafruit Store 38 | 39 | Indices and tables 40 | ================== 41 | 42 | * :ref:`genindex` 43 | * :ref:`modindex` 44 | * :ref:`search` 45 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2021 Jeff Epler for Adafruit Industries 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LICENSES/MIT.txt: -------------------------------------------------------------------------------- 1 | MIT License Copyright (c) 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is furnished 8 | to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice (including the next 11 | paragraph) shall be included in all copies or substantial portions of the 12 | Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS 17 | OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 18 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF 19 | OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /LICENSES/Unlicense.txt: -------------------------------------------------------------------------------- 1 | This is free and unencumbered software released into the public domain. 2 | 3 | Anyone is free to copy, modify, publish, use, compile, sell, or distribute 4 | this software, either in source code form or as a compiled binary, for any 5 | purpose, commercial or non-commercial, and by any means. 6 | 7 | In jurisdictions that recognize copyright laws, the author or authors of this 8 | software dedicate any and all copyright interest in the software to the public 9 | domain. We make this dedication for the benefit of the public at large and 10 | to the detriment of our heirs and successors. We intend this dedication to 11 | be an overt act of relinquishment in perpetuity of all present and future 12 | rights to this software under copyright law. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 16 | FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS 17 | BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 18 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH 19 | THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. For more information, 20 | please refer to 21 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2022 Alec Delaney for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: MIT 4 | 5 | [build-system] 6 | requires = [ 7 | "setuptools", 8 | "wheel", 9 | "setuptools-scm", 10 | ] 11 | 12 | [project] 13 | name = "adafruit-circuitpython-ov2640" 14 | description = "CircuitPython driver for OV2640 cameras" 15 | version = "0.0.0+auto.0" 16 | readme = "README.rst" 17 | authors = [ 18 | {name = "Adafruit Industries", email = "circuitpython@adafruit.com"} 19 | ] 20 | urls = {Homepage = "https://github.com/adafruit/Adafruit_CircuitPython_OV2640"} 21 | keywords = [ 22 | "adafruit", 23 | "ov2640", 24 | "camera", 25 | "breakout", 26 | "hardware", 27 | "micropythoncircuitpython", 28 | ] 29 | license = {text = "MIT"} 30 | classifiers = [ 31 | "Intended Audience :: Developers", 32 | "Topic :: Software Development :: Libraries", 33 | "Topic :: Software Development :: Embedded Systems", 34 | "Topic :: System :: Hardware", 35 | "License :: OSI Approved :: MIT License", 36 | "Programming Language :: Python :: 3", 37 | ] 38 | dynamic = ["dependencies", "optional-dependencies"] 39 | 40 | [tool.setuptools] 41 | py-modules = ["adafruit_ov2640"] 42 | 43 | [tool.setuptools.dynamic] 44 | dependencies = {file = ["requirements.txt"]} 45 | optional-dependencies = {optional = {file = ["optional_requirements.txt"]}} 46 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2022 Kattni Rembor, written for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: MIT 4 | 5 | # Do not include files and directories created by your personal work environment, such as the IDE 6 | # you use, except for those already listed here. Pull requests including changes to this file will 7 | # not be accepted. 8 | 9 | # This .gitignore file contains rules for files generated by working with CircuitPython libraries, 10 | # including building Sphinx, testing with pip, and creating a virual environment, as well as the 11 | # MacOS and IDE-specific files generated by using MacOS in general, or the PyCharm or VSCode IDEs. 12 | 13 | # If you find that there are files being generated on your machine that should not be included in 14 | # your git commit, you should create a .gitignore_global file on your computer to include the 15 | # files created by your personal setup. To do so, follow the two steps below. 16 | 17 | # First, create a file called .gitignore_global somewhere convenient for you, and add rules for 18 | # the files you want to exclude from git commits. 19 | 20 | # Second, configure Git to use the exclude file for all Git repositories by running the 21 | # following via commandline, replacing "path/to/your/" with the actual path to your newly created 22 | # .gitignore_global file: 23 | # git config --global core.excludesfile path/to/your/.gitignore_global 24 | 25 | # CircuitPython-specific files 26 | *.mpy 27 | 28 | # Python-specific files 29 | __pycache__ 30 | *.pyc 31 | 32 | # Sphinx build-specific files 33 | _build 34 | 35 | # This file results from running `pip -e install .` in a local repository 36 | *.egg-info 37 | 38 | # Virtual environment-specific files 39 | .env 40 | .venv 41 | 42 | # MacOS-specific files 43 | *.DS_Store 44 | 45 | # IDE-specific files 46 | .idea 47 | .vscode 48 | *~ 49 | -------------------------------------------------------------------------------- /examples/ov2640_jpeg_kaluga1_3.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 | """ 7 | The Kaluga development kit comes in two versions (v1.2 and v1.3); this demo is 8 | tested on v1.3. 9 | 10 | The audio board must be mounted between the Kaluga and the LCD, it provides the 11 | I2C pull-ups(!) 12 | 13 | You also need to place ov2640_jpeg_kaluga1_3_boot.py at CIRCUITPY/boot.py 14 | and reset the board to make the internal flash readable by CircuitPython. 15 | You can make CIRCUITPY readable from your PC by booting CircuitPython in 16 | safe mode or holding the "MODE" button on the audio daughterboard while 17 | powering on or resetting the board. 18 | """ 19 | 20 | import board 21 | import busio 22 | 23 | import adafruit_ov2640 24 | 25 | bus = busio.I2C(scl=board.CAMERA_SIOC, sda=board.CAMERA_SIOD) 26 | cam = adafruit_ov2640.OV2640( 27 | bus, 28 | data_pins=board.CAMERA_DATA, 29 | clock=board.CAMERA_PCLK, 30 | vsync=board.CAMERA_VSYNC, 31 | href=board.CAMERA_HREF, 32 | mclk=board.CAMERA_XCLK, 33 | mclk_frequency=20_000_000, 34 | size=adafruit_ov2640.OV2640_SIZE_QVGA, 35 | ) 36 | 37 | pid = cam.product_id 38 | ver = cam.product_version 39 | print(f"Detected pid={pid:x} ver={ver:x}") 40 | # cam.test_pattern = True 41 | 42 | cam.colorspace = adafruit_ov2640.OV2640_COLOR_JPEG 43 | b = bytearray(cam.capture_buffer_size) 44 | jpeg = cam.capture(b) 45 | 46 | print(f"Captured {len(jpeg)} bytes of jpeg data") 47 | try: 48 | with open("/jpeg.jpg", "wb") as f: 49 | f.write(jpeg) 50 | except OSError as e: 51 | print(e) 52 | print( 53 | "A 'read-only filesystem' error occurs if you did not correctly install" 54 | "\nov2640_jpeg_kaluga1_3_boot.py as CIRCUITPY/boot.py and reset the board" 55 | ) 56 | print("Wrote to CIRCUITPY/jpeg.jpg") 57 | -------------------------------------------------------------------------------- /examples/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 board 27 | import busio 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) 64 | -------------------------------------------------------------------------------- /examples/ov2640_displayio_kaluga1_3_ili9341.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 | """ 7 | The Kaluga development kit comes in two versions (v1.2 and v1.3); this demo is 8 | tested on v1.3. It probably won't work on v1.2 without modification. 9 | 10 | The v1.3 development kit's LCD can have one of two chips, the ili9341 or 11 | st7789. Furthermore, there are at least 2 ILI9341 variants, one of which needs 12 | rotation=90! This demo is for the ili9341. If the display is garbled, try adding 13 | rotation=90, or try modifying it to use ST7799. 14 | 15 | The audio board must be mounted between the Kaluga and the LCD, it provides the 16 | I2C pull-ups(!) 17 | """ 18 | 19 | import board 20 | import busio 21 | import displayio 22 | import fourwire 23 | from adafruit_ili9341 import ILI9341 24 | 25 | import adafruit_ov2640 26 | 27 | # Pylint is unable to see that the "size" property of OV2640_GrandCentral exists 28 | 29 | # Release any resources currently in use for the displays 30 | displayio.release_displays() 31 | 32 | spi = busio.SPI(MOSI=board.LCD_MOSI, clock=board.LCD_CLK) 33 | display_bus = fourwire.FourWire( 34 | spi, command=board.LCD_D_C, chip_select=board.LCD_CS, reset=board.LCD_RST 35 | ) 36 | display = ILI9341(display_bus, width=320, height=240, rotation=90) 37 | 38 | bus = busio.I2C(scl=board.CAMERA_SIOC, sda=board.CAMERA_SIOD) 39 | cam = adafruit_ov2640.OV2640( 40 | bus, 41 | data_pins=board.CAMERA_DATA, 42 | clock=board.CAMERA_PCLK, 43 | vsync=board.CAMERA_VSYNC, 44 | href=board.CAMERA_HREF, 45 | mclk=board.CAMERA_XCLK, 46 | mclk_frequency=20_000_000, 47 | size=adafruit_ov2640.OV2640_SIZE_QVGA, 48 | ) 49 | 50 | cam.flip_x = False 51 | cam.flip_y = True 52 | pid = cam.product_id 53 | ver = cam.product_version 54 | print(f"Detected pid={pid:x} ver={ver:x}") 55 | # cam.test_pattern = True 56 | 57 | g = displayio.Group(scale=1) 58 | bitmap = displayio.Bitmap(320, 240, 65536) 59 | tg = displayio.TileGrid( 60 | bitmap, 61 | pixel_shader=displayio.ColorConverter(input_colorspace=displayio.Colorspace.BGR565_SWAPPED), 62 | ) 63 | g.append(tg) 64 | display.root_group = g 65 | 66 | display.auto_refresh = False 67 | while True: 68 | cam.capture(bitmap) 69 | bitmap.dirty() 70 | display.refresh(minimum_frames_per_second=0) 71 | 72 | cam.deinit() 73 | -------------------------------------------------------------------------------- /examples/ov2640_displayio_kaluga1_3_st7789.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 | """ 7 | The Kaluga development kit comes in two versions (v1.2 and v1.3); this demo is 8 | tested on v1.3. 9 | 10 | The v1.3 development kit's LCD can have one of two chips, the ili9341 or 11 | st7789. This demo is for the ili9341. There is no marking to distinguish the 12 | two chips. If the visible portion of the display's flexible cable has a bunch 13 | of straight lines, it may be an ili9341. If it has a bunch of wiggly traces, 14 | it may be an st7789. If in doubt, try both demos. 15 | 16 | The audio board must be mounted between the Kaluga and the LCD, it provides the 17 | I2C pull-ups(!) 18 | """ 19 | 20 | import board 21 | import busio 22 | import displayio 23 | import fourwire 24 | from adafruit_st7789 import ST7789 25 | 26 | import adafruit_ov2640 27 | 28 | # Pylint is unable to see that the "size" property of OV2640_GrandCentral exists 29 | 30 | # Release any resources currently in use for the displays 31 | displayio.release_displays() 32 | 33 | spi = busio.SPI(MOSI=board.LCD_MOSI, clock=board.LCD_CLK) 34 | display_bus = fourwire.FourWire( 35 | spi, command=board.LCD_D_C, chip_select=board.LCD_CS, reset=board.LCD_RST 36 | ) 37 | display = ST7789(display_bus, width=320, height=240, rotation=90, reverse_bytes_in_word=True) 38 | 39 | bus = busio.I2C(scl=board.CAMERA_SIOC, sda=board.CAMERA_SIOD) 40 | cam = adafruit_ov2640.OV2640( 41 | bus, 42 | data_pins=board.CAMERA_DATA, 43 | clock=board.CAMERA_PCLK, 44 | vsync=board.CAMERA_VSYNC, 45 | href=board.CAMERA_HREF, 46 | mclk=board.CAMERA_XCLK, 47 | mclk_frequency=20_000_000, 48 | size=adafruit_ov2640.OV2640_SIZE_QVGA, 49 | ) 50 | 51 | # cam.flip_x = False 52 | # cam.flip_y = True 53 | pid = cam.product_id 54 | ver = cam.product_version 55 | print(f"Detected pid={pid:x} ver={ver:x}") 56 | # cam.test_pattern = True 57 | 58 | g = displayio.Group(scale=1) 59 | bitmap = displayio.Bitmap(320, 240, 65536) 60 | tg = displayio.TileGrid( 61 | bitmap, 62 | pixel_shader=displayio.ColorConverter(input_colorspace=displayio.Colorspace.BGR565_SWAPPED), 63 | ) 64 | g.append(tg) 65 | display.root_group = g 66 | 67 | display.auto_refresh = False 68 | while True: 69 | cam.capture(bitmap) 70 | bitmap.dirty() 71 | display.refresh(minimum_frames_per_second=0) 72 | print(".") 73 | 74 | cam.deinit() 75 | -------------------------------------------------------------------------------- /examples/ov2640_displayio_pico_st7789_2in.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 | """ 7 | Capture an image from the camera and display it on a supported LCD. 8 | """ 9 | 10 | import time 11 | 12 | import board 13 | import busio 14 | import digitalio 15 | from adafruit_st7789 import ST7789 16 | from displayio import ( 17 | Bitmap, 18 | ColorConverter, 19 | Colorspace, 20 | FourWire, 21 | Group, 22 | TileGrid, 23 | release_displays, 24 | ) 25 | 26 | import adafruit_ov2640 27 | 28 | release_displays() 29 | # Set up the display (You must customize this block for your display!) 30 | spi = busio.SPI(clock=board.GP2, MOSI=board.GP3) 31 | display_bus = FourWire(spi, command=board.GP0, chip_select=board.GP1, reset=None) 32 | display = ST7789(display_bus, width=320, height=240, rotation=270) 33 | display.auto_refresh = False 34 | 35 | # Ensure the camera is shut down, so that it releases the SDA/SCL lines, 36 | # then create the configuration I2C bus 37 | 38 | with digitalio.DigitalInOut(board.GP10) as reset: 39 | reset.switch_to_output(False) 40 | time.sleep(0.001) 41 | bus = busio.I2C(board.GP9, board.GP8) 42 | 43 | # Set up the camera (you must customize this for your board!) 44 | cam = adafruit_ov2640.OV2640( 45 | bus, 46 | data_pins=[ 47 | board.GP12, 48 | board.GP13, 49 | board.GP14, 50 | board.GP15, 51 | board.GP16, 52 | board.GP17, 53 | board.GP18, 54 | board.GP19, 55 | ], # [16] [org] etc 56 | clock=board.GP11, # [15] [blk] 57 | vsync=board.GP7, # [10] [brn] 58 | href=board.GP21, # [27/o14] [red] 59 | mclk=board.GP20, # [16/o15] 60 | shutdown=None, 61 | reset=board.GP10, 62 | ) # [14] 63 | 64 | width = display.width 65 | height = display.height 66 | 67 | cam.size = adafruit_ov2640.OV2640_SIZE_QQVGA 68 | # cam.test_pattern = True 69 | bitmap = Bitmap(cam.width, cam.height, 65536) 70 | 71 | print(width, height, cam.width, cam.height) 72 | if bitmap is None: 73 | raise SystemExit("Could not allocate a bitmap") 74 | 75 | g = Group(scale=1, x=(width - cam.width) // 2, y=(height - cam.height) // 2) 76 | tg = TileGrid(bitmap, pixel_shader=ColorConverter(input_colorspace=Colorspace.BGR565_SWAPPED)) 77 | g.append(tg) 78 | display.root_group = g 79 | 80 | display.auto_refresh = False 81 | while True: 82 | cam.capture(bitmap) 83 | bitmap.dirty() 84 | display.refresh(minimum_frames_per_second=0) 85 | -------------------------------------------------------------------------------- /examples/ov2640_displayio_kaluga1_3_ili9341_ulab.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 | """ 7 | The Kaluga development kit comes in two versions (v1.2 and v1.3); this demo is 8 | tested on v1.3. It probably won't work on v1.2 without modification. 9 | 10 | The v1.3 development kit's LCD can have one of two chips, the ili9341 or 11 | st7789. Furthermore, there are at least 2 ILI9341 variants, one of which needs 12 | rotation=90! This demo is for the ili9341. If the display is garbled, try adding 13 | rotation=90, or try modifying it to use ST7799. 14 | 15 | The camera included with the Kaluga development kit is the incompatible OV2640, 16 | it won't work. 17 | 18 | The audio board must be mounted between the Kaluga and the LCD, it provides the 19 | I2C pull-ups(!) 20 | """ 21 | 22 | import board 23 | import busio 24 | import displayio 25 | import fourwire 26 | import ulab.numpy as np 27 | from adafruit_ili9341 import ILI9341 28 | 29 | import adafruit_ov2640 30 | 31 | # Pylint is unable to see that the "size" property of OV2640_GrandCentral exists 32 | 33 | # Release any resources currently in use for the displays 34 | displayio.release_displays() 35 | 36 | spi = busio.SPI(MOSI=board.LCD_MOSI, clock=board.LCD_CLK) 37 | display_bus = fourwire.FourWire( 38 | spi, command=board.LCD_D_C, chip_select=board.LCD_CS, reset=board.LCD_RST 39 | ) 40 | display = ILI9341(display_bus, width=320, height=240, rotation=90) 41 | 42 | bus = busio.I2C(scl=board.CAMERA_SIOC, sda=board.CAMERA_SIOD) 43 | cam = adafruit_ov2640.OV2640( 44 | bus, 45 | data_pins=board.CAMERA_DATA, 46 | clock=board.CAMERA_PCLK, 47 | vsync=board.CAMERA_VSYNC, 48 | href=board.CAMERA_HREF, 49 | mclk=board.CAMERA_XCLK, 50 | mclk_frequency=20_000_000, 51 | size=adafruit_ov2640.OV2640_SIZE_QVGA, 52 | ) 53 | 54 | cam.flip_x = False 55 | cam.flip_y = True 56 | pid = cam.product_id 57 | ver = cam.product_version 58 | print(f"Detected pid={pid:x} ver={ver:x}") 59 | cam.test_pattern = True 60 | 61 | g = displayio.Group(scale=1) 62 | bitmap = displayio.Bitmap(320, 240, 65536) 63 | arr = np.frombuffer(bitmap, dtype=np.uint16) 64 | tg = displayio.TileGrid( 65 | bitmap, 66 | pixel_shader=displayio.ColorConverter(input_colorspace=displayio.Colorspace.RGB565_SWAPPED), 67 | ) 68 | g.append(tg) 69 | display.root_group = g 70 | 71 | display.auto_refresh = False 72 | while True: 73 | cam.capture(bitmap) 74 | arr[:] = ~arr # Invert every pixel in the bitmap, via the array 75 | bitmap.dirty() 76 | display.refresh(minimum_frames_per_second=0) 77 | 78 | cam.deinit() 79 | -------------------------------------------------------------------------------- /README.rst: -------------------------------------------------------------------------------- 1 | Introduction 2 | ============ 3 | 4 | 5 | .. image:: https://readthedocs.org/projects/adafruit-circuitpython-ov2640/badge/?version=latest 6 | :target: https://docs.circuitpython.org/projects/ov2640/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_OV2640/workflows/Build%20CI/badge.svg 16 | :target: https://github.com/adafruit/Adafruit_CircuitPython_OV2640/actions 17 | :alt: Build Status 18 | 19 | 20 | .. image:: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json 21 | :target: https://github.com/astral-sh/ruff 22 | :alt: Code Style: Ruff 23 | 24 | CircuitPython driver for OV2640 Camera. 25 | 26 | This driver is designed to work directly with the OV2640 camera module through an 18-pin header. 27 | It does not work with products such as ArduCam which process the camera data themselves. 28 | 29 | Dependencies 30 | ============= 31 | This driver depends on: 32 | 33 | * `Adafruit CircuitPython `_ 34 | * `Bus Device `_ 35 | 36 | Please ensure all dependencies are available on the CircuitPython filesystem. 37 | This is easily achieved by downloading 38 | `the Adafruit library and driver bundle `_ 39 | or individual libraries can be installed using 40 | `circup `_. 41 | 42 | 43 | * `ESP32-S2 Kaluga Dev Kit featuring ESP32-S2 WROVER `_ 44 | 45 | 46 | 47 | Usage Example 48 | ============= 49 | 50 | Using the ESP32-S2 Kaluga Dev Kit and its included camera, capture a 160x120 image into a buffer: 51 | 52 | .. code-block:: python3 53 | 54 | import board 55 | from adafruit_ov2640 import OV2640, OV2640_SIZE_QQVGA 56 | 57 | bus = busio.I2C(scl=board.CAMERA_SIOC, sda=board.CAMERA_SIOD) 58 | cam = OV2640( 59 | bus, 60 | data_pins=board.CAMERA_DATA, 61 | clock=board.CAMERA_PCLK, 62 | vsync=board.CAMERA_VSYNC, 63 | href=board.CAMERA_HREF, 64 | mclk=board.CAMERA_XCLK, 65 | size=OV2640_SIZE_QQVGA, 66 | ) 67 | buf = bytearray(2 * cam.width * cam.height) 68 | 69 | cam.capture(buf) 70 | 71 | 72 | Documentation 73 | ============= 74 | 75 | API documentation for this library can be found on `Read the Docs `_. 76 | 77 | For information on building library documentation, please check out `this guide `_. 78 | 79 | Contributing 80 | ============ 81 | 82 | Contributions are welcome! Please read our `Code of Conduct 83 | `_ 84 | before contributing to help this project stay welcoming. 85 | -------------------------------------------------------------------------------- /examples/ov2640_aio_saola.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 | """ 7 | This exampl us for the Espressif Soala Wrover with an OV2640 Camera 8 | 9 | This example requires that your WIFI and Adafruit IO credentials be configured 10 | in CIRCUITPY/settings.toml, and that you have created a feed called "image" with 11 | history disabled. 12 | 13 | The maximum image size is 100kB after base64 encoding, or about 65kB before 14 | base64 encoding. In practice, "SVGA" (800x600) images are typically around 15 | 40kB even though the "capture_buffer_size" (theoretical maximum size) is 16 | (width*height/5) bytes or 96kB. 17 | """ 18 | 19 | import binascii 20 | import time 21 | from os import getenv 22 | 23 | import adafruit_connection_manager 24 | import adafruit_minimqtt.adafruit_minimqtt as MQTT 25 | import board 26 | import busio 27 | import wifi 28 | from adafruit_io.adafruit_io import IO_MQTT 29 | 30 | import adafruit_ov2640 31 | 32 | feed_name = "image-saola-ov2640" 33 | 34 | # Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml 35 | # (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) 36 | ssid = getenv("CIRCUITPY_WIFI_SSID") 37 | password = getenv("CIRCUITPY_WIFI_PASSWORD") 38 | aio_username = getenv("ADAFRUIT_AIO_USERNAME") 39 | aio_key = getenv("ADAFRUIT_AIO_KEY") 40 | 41 | print("Connecting to WIFI") 42 | wifi.radio.connect(ssid, password) 43 | pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) 44 | ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) 45 | 46 | print("Connecting to Adafruit IO") 47 | mqtt_client = MQTT.MQTT( 48 | broker="io.adafruit.com", 49 | username=aio_username, 50 | password=aio_key, 51 | socket_pool=pool, 52 | ssl_context=ssl_context, 53 | ) 54 | mqtt_client.connect() 55 | io = IO_MQTT(mqtt_client) 56 | 57 | bus = busio.I2C(scl=board.IO7, sda=board.IO8) 58 | cam = adafruit_ov2640.OV2640( 59 | bus, 60 | data_pins=( 61 | board.IO36, 62 | board.IO37, 63 | board.IO41, 64 | board.IO42, 65 | board.IO39, 66 | board.IO40, 67 | board.IO21, 68 | board.IO38, 69 | ), 70 | clock=board.IO33, 71 | vsync=board.IO2, 72 | href=board.IO3, 73 | mclk=board.IO1, 74 | mclk_frequency=20_000_000, 75 | size=adafruit_ov2640.OV2640_SIZE_QVGA, 76 | ) 77 | 78 | cam.flip_x = False 79 | cam.flip_y = False 80 | 81 | cam.size = adafruit_ov2640.OV2640_SIZE_SVGA 82 | cam.colorspace = adafruit_ov2640.OV2640_COLOR_JPEG 83 | jpeg_buffer = bytearray(cam.capture_buffer_size) 84 | while True: 85 | jpeg = cam.capture(jpeg_buffer) 86 | print(f"Captured {len(jpeg)} bytes of jpeg data") 87 | 88 | # b2a_base64() appends a trailing newline, which IO does not like 89 | encoded_data = binascii.b2a_base64(jpeg).strip() 90 | print(f"Expanded to {len(encoded_data)} for IO upload") 91 | io.publish(feed_name, encoded_data) 92 | print("Waiting 10s") 93 | time.sleep(10) 94 | -------------------------------------------------------------------------------- /docs/examples.rst: -------------------------------------------------------------------------------- 1 | Simple test 2 | ------------ 3 | 4 | Ensure your device works with this simple test. 5 | 6 | .. literalinclude:: ../examples/ov2640_simpletest.py 7 | :caption: ov2640_simpletest.py 8 | :linenos: 9 | 10 | 11 | LCD tests 12 | --------- 13 | 14 | Kaluga 1.3 with ili9341 15 | ~~~~~~~~~~~~~~~~~~~~~~~ 16 | 17 | Display an image from the camera on the Kaluga 1.3 board, if it is fitted with an ili9341 display. 18 | 19 | .. literalinclude:: ../examples/ov2640_displayio_kaluga1_3_ili9341.py 20 | :caption: ov2640_displayio_kaluga1_3_ili9341.py 21 | :linenos: 22 | 23 | Kaluga 1.3 with st7789 24 | ~~~~~~~~~~~~~~~~~~~~~~ 25 | 26 | Display an image from the camera on the Kaluga 1.3 board, if it is fitted with an st7789 display. 27 | 28 | .. literalinclude:: ../examples/ov2640_displayio_kaluga1_3_st7789.py 29 | :caption: ov2640_displayio_kaluga1_3_st7789.py 30 | :linenos: 31 | 32 | Raspberry Pi Pico with st7789 33 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 34 | 35 | Display an image from the camera connected to a Raspberry Pi Pico with an st7789 2" display 36 | 37 | .. literalinclude:: ../examples/ov2640_displayio_pico_st7789_2in.py 38 | :caption: ov2640_displayio_pico_st7789_2in.py 39 | :linenos: 40 | 41 | Kaluga 1.3 with ili9341, direct display 42 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 43 | 44 | Preview images on LCD, bypassing displayio for slightly higher framerate 45 | 46 | .. literalinclude:: ../examples/ov2640_directio_kaluga1_3_ili9341.py 47 | :caption: ../examples/ov2640_directio_kaluga1_3_ili9341.py 48 | :linenos: 49 | 50 | 51 | Image-saving tests 52 | ------------------ 53 | 54 | Kaluga 1.3 with ili9341, internal flash, JPEG 55 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 56 | 57 | Preview images on LCD t hen save JPEG images to internal flash on Kaluga 1.3. Requires the second snippet of 58 | code to be saved as ``boot.py``. 59 | 60 | .. literalinclude:: ../examples/ov2640_jpeg_kaluga1_3.py 61 | :caption: ov2640_jpeg_kaluga1_3.py 62 | :linenos: 63 | 64 | ``boot.py`` for the above program 65 | 66 | .. literalinclude:: ../examples/ov2640_jpeg_kaluga1_3_boot.py 67 | :caption: ov2640_jpeg_kaluga1_3_boot.py 68 | :linenos: 69 | 70 | Kaluga 1.3 with ili9341, external SD card, JPEG 71 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 72 | 73 | Preview images on LCD then save JPEG images to SD on Kaluga 1.3 fitted with an ili9341 display. 74 | 75 | .. literalinclude:: ../examples/ov2640_jpeg_sd_kaluga1_3.py 76 | :caption: ov2640_jpeg_sd_kaluga1_3.py 77 | :linenos: 78 | 79 | Kaluga 1.3 with ili9341, external SD card, BMP 80 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 81 | 82 | Preview images on LCD then save BMP images to SD on Kaluga 1.3 fitted with an ili9341 display. 83 | 84 | .. literalinclude:: ../examples/ov2640_bmp_sd_kaluga1_3.py 85 | :caption: ov2640_bmp_sd_kaluga1_3.py 86 | :linenos: 87 | 88 | 89 | Kaluga 1.3 with Adafruit IO 90 | ~~~~~~~~~~~~~~~~~~~~~~~~~~~ 91 | 92 | Upload JPEG images to Adafruit IO. Requires that WIFI and Adafruit IO be configured in ``settings.toml``. 93 | 94 | .. literalinclude:: ../examples/ov2640_aio_kaluga1_3.py 95 | :caption: ov2640_aio_kaluga1_3.py 96 | :linenos: 97 | -------------------------------------------------------------------------------- /examples/ov2640_aio_kaluga1_3.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 | """ 7 | The Kaluga development kit comes in two versions (v1.2 and v1.3); this demo is 8 | tested on v1.3. 9 | 10 | The audio board must be mounted between the Kaluga and the LCD, it provides the 11 | I2C pull-ups(!) 12 | 13 | This example requires that your WIFI and Adafruit IO credentials be configured 14 | in CIRCUITPY/settings.toml, and that you have created a feed called "image" with 15 | history disabled. 16 | 17 | The maximum image size is 100kB after base64 encoding, or about 65kB before 18 | base64 encoding. In practice, "SVGA" (800x600) images are typically around 19 | 40kB even though the "capture_buffer_size" (theoretical maximum size) is 20 | (width*height/5) bytes or 96kB. 21 | """ 22 | 23 | import binascii 24 | import time 25 | from os import getenv 26 | 27 | import adafruit_connection_manager 28 | import adafruit_minimqtt.adafruit_minimqtt as MQTT 29 | import board 30 | import busio 31 | import wifi 32 | from adafruit_io.adafruit_io import IO_MQTT 33 | 34 | import adafruit_ov2640 35 | 36 | feed_name = "image" 37 | 38 | # Get WiFi details and Adafruit IO keys, ensure these are setup in settings.toml 39 | # (visit io.adafruit.com if you need to create an account, or if you need your Adafruit IO key.) 40 | ssid = getenv("CIRCUITPY_WIFI_SSID") 41 | password = getenv("CIRCUITPY_WIFI_PASSWORD") 42 | aio_username = getenv("ADAFRUIT_AIO_USERNAME") 43 | aio_key = getenv("ADAFRUIT_AIO_KEY") 44 | 45 | print("Connecting to WIFI") 46 | wifi.radio.connect(ssid, password) 47 | pool = adafruit_connection_manager.get_radio_socketpool(wifi.radio) 48 | ssl_context = adafruit_connection_manager.get_radio_ssl_context(wifi.radio) 49 | 50 | print("Connecting to Adafruit IO") 51 | mqtt_client = MQTT.MQTT( 52 | broker="io.adafruit.com", 53 | username=aio_username, 54 | password=aio_key, 55 | socket_pool=pool, 56 | ssl_context=ssl_context, 57 | ) 58 | mqtt_client.connect() 59 | io = IO_MQTT(mqtt_client) 60 | 61 | bus = busio.I2C(scl=board.CAMERA_SIOC, sda=board.CAMERA_SIOD) 62 | cam = adafruit_ov2640.OV2640( 63 | bus, 64 | data_pins=board.CAMERA_DATA, 65 | clock=board.CAMERA_PCLK, 66 | vsync=board.CAMERA_VSYNC, 67 | href=board.CAMERA_HREF, 68 | mclk=board.CAMERA_XCLK, 69 | mclk_frequency=20_000_000, 70 | size=adafruit_ov2640.OV2640_SIZE_QVGA, 71 | ) 72 | 73 | cam.flip_x = False 74 | cam.flip_y = False 75 | cam.test_pattern = False 76 | 77 | cam.size = adafruit_ov2640.OV2640_SIZE_SVGA 78 | cam.colorspace = adafruit_ov2640.OV2640_COLOR_JPEG 79 | jpeg_buffer = bytearray(cam.capture_buffer_size) 80 | while True: 81 | jpeg = cam.capture(jpeg_buffer) 82 | print(f"Captured {len(jpeg)} bytes of jpeg data") 83 | 84 | # b2a_base64() appends a trailing newline, which IO does not like 85 | encoded_data = binascii.b2a_base64(jpeg).strip() 86 | print(f"Expanded to {len(encoded_data)} for IO upload") 87 | 88 | io.publish("image", encoded_data) 89 | 90 | print("Waiting 3s") 91 | time.sleep(3) 92 | -------------------------------------------------------------------------------- /ruff.toml: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2024 Tim Cocks for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: MIT 4 | 5 | target-version = "py38" 6 | line-length = 100 7 | 8 | [lint] 9 | preview = true 10 | select = ["I", "PL", "UP"] 11 | 12 | extend-select = [ 13 | "D419", # empty-docstring 14 | "E501", # line-too-long 15 | "W291", # trailing-whitespace 16 | "PLC0414", # useless-import-alias 17 | "PLC2401", # non-ascii-name 18 | "PLC2801", # unnecessary-dunder-call 19 | "PLC3002", # unnecessary-direct-lambda-call 20 | "PLE0101", # return-in-init 21 | "F706", # return-outside-function 22 | "F704", # yield-outside-function 23 | "PLE0116", # continue-in-finally 24 | "PLE0117", # nonlocal-without-binding 25 | "PLE0241", # duplicate-bases 26 | "PLE0302", # unexpected-special-method-signature 27 | "PLE0604", # invalid-all-object 28 | "PLE0605", # invalid-all-format 29 | "PLE0643", # potential-index-error 30 | "PLE0704", # misplaced-bare-raise 31 | "PLE1141", # dict-iter-missing-items 32 | "PLE1142", # await-outside-async 33 | "PLE1205", # logging-too-many-args 34 | "PLE1206", # logging-too-few-args 35 | "PLE1307", # bad-string-format-type 36 | "PLE1310", # bad-str-strip-call 37 | "PLE1507", # invalid-envvar-value 38 | "PLE2502", # bidirectional-unicode 39 | "PLE2510", # invalid-character-backspace 40 | "PLE2512", # invalid-character-sub 41 | "PLE2513", # invalid-character-esc 42 | "PLE2514", # invalid-character-nul 43 | "PLE2515", # invalid-character-zero-width-space 44 | "PLR0124", # comparison-with-itself 45 | "PLR0202", # no-classmethod-decorator 46 | "PLR0203", # no-staticmethod-decorator 47 | "UP004", # useless-object-inheritance 48 | "PLR0206", # property-with-parameters 49 | "PLR0904", # too-many-public-methods 50 | "PLR0911", # too-many-return-statements 51 | "PLR0912", # too-many-branches 52 | "PLR0913", # too-many-arguments 53 | "PLR0914", # too-many-locals 54 | "PLR0915", # too-many-statements 55 | "PLR0916", # too-many-boolean-expressions 56 | "PLR1702", # too-many-nested-blocks 57 | "PLR1704", # redefined-argument-from-local 58 | "PLR1711", # useless-return 59 | "C416", # unnecessary-comprehension 60 | "PLR1733", # unnecessary-dict-index-lookup 61 | "PLR1736", # unnecessary-list-index-lookup 62 | 63 | # ruff reports this rule is unstable 64 | #"PLR6301", # no-self-use 65 | 66 | "PLW0108", # unnecessary-lambda 67 | "PLW0120", # useless-else-on-loop 68 | "PLW0127", # self-assigning-variable 69 | "PLW0129", # assert-on-string-literal 70 | "B033", # duplicate-value 71 | "PLW0131", # named-expr-without-context 72 | "PLW0245", # super-without-brackets 73 | "PLW0406", # import-self 74 | "PLW0602", # global-variable-not-assigned 75 | "PLW0603", # global-statement 76 | "PLW0604", # global-at-module-level 77 | 78 | # fails on the try: import typing used by libraries 79 | #"F401", # unused-import 80 | 81 | "F841", # unused-variable 82 | "E722", # bare-except 83 | "PLW0711", # binary-op-exception 84 | "PLW1501", # bad-open-mode 85 | "PLW1508", # invalid-envvar-default 86 | "PLW1509", # subprocess-popen-preexec-fn 87 | "PLW2101", # useless-with-lock 88 | "PLW3301", # nested-min-max 89 | ] 90 | 91 | ignore = [ 92 | "PLR2004", # magic-value-comparison 93 | "UP030", # format literals 94 | "PLW1514", # unspecified-encoding 95 | "PLR0913", # too-many-arguments 96 | "PLR0915", # too-many-statements 97 | "PLR0917", # too-many-positional-arguments 98 | "PLR0904", # too-many-public-methods 99 | "PLR0912", # too-many-branches 100 | "PLR0916", # too-many-boolean-expressions 101 | ] 102 | 103 | [format] 104 | line-ending = "lf" 105 | -------------------------------------------------------------------------------- /examples/ov2640_jpeg_sd_kaluga1_3.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 | """ 7 | Display an image on the LCD, then record an image when the REC button is pressed/held. 8 | 9 | The Kaluga development kit comes in two versions (v1.2 and v1.3); this demo is 10 | tested on v1.3. 11 | 12 | The audio board must be mounted between the Kaluga and the LCD, it provides the 13 | I2C pull-ups(!) 14 | 15 | The v1.3 development kit's LCD can have one of two chips, the ili9341 or 16 | st7789. Furthermore, there are at least 2 ILI9341 variants, one of which needs 17 | rotation=90! This demo is for the ili9341. If the display is garbled, try adding 18 | rotation=90, or try modifying it to use ST7799. 19 | 20 | This example also requires an SD card breakout wired as follows: 21 | * IO18: SD Clock Input 22 | * IO17: SD Serial Output (MISO) 23 | * IO14: SD Serial Input (MOSI) 24 | * IO12: SD Chip Select 25 | 26 | Insert a CircuitPython-compatible SD card before powering on the Kaluga. 27 | Press the "Record" button on the audio daughterboard to take a photo. 28 | """ 29 | 30 | import os 31 | 32 | import analogio 33 | import board 34 | import busio 35 | import displayio 36 | import fourwire 37 | import sdcardio 38 | import storage 39 | from adafruit_ili9341 import ILI9341 40 | 41 | import adafruit_ov2640 42 | 43 | V_MODE = 1.98 44 | V_RECORD = 2.41 45 | 46 | a = analogio.AnalogIn(board.IO6) 47 | 48 | # Release any resources currently in use for the displays 49 | displayio.release_displays() 50 | 51 | spi = busio.SPI(MOSI=board.LCD_MOSI, clock=board.LCD_CLK) 52 | display_bus = fourwire.FourWire( 53 | spi, command=board.LCD_D_C, chip_select=board.LCD_CS, reset=board.LCD_RST 54 | ) 55 | display = ILI9341(display_bus, width=320, height=240, rotation=90) 56 | 57 | bus = busio.I2C(scl=board.CAMERA_SIOC, sda=board.CAMERA_SIOD) 58 | cam = adafruit_ov2640.OV2640( 59 | bus, 60 | data_pins=board.CAMERA_DATA, 61 | clock=board.CAMERA_PCLK, 62 | vsync=board.CAMERA_VSYNC, 63 | href=board.CAMERA_HREF, 64 | mclk=board.CAMERA_XCLK, 65 | mclk_frequency=20_000_000, 66 | size=adafruit_ov2640.OV2640_SIZE_QVGA, 67 | ) 68 | 69 | cam.flip_x = False 70 | cam.flip_y = True 71 | pid = cam.product_id 72 | ver = cam.product_version 73 | print(f"Detected pid={pid:x} ver={ver:x}") 74 | # cam.test_pattern = True 75 | 76 | g = displayio.Group(scale=1) 77 | bitmap = displayio.Bitmap(320, 240, 65536) 78 | tg = displayio.TileGrid( 79 | bitmap, 80 | pixel_shader=displayio.ColorConverter(input_colorspace=displayio.Colorspace.BGR565_SWAPPED), 81 | ) 82 | g.append(tg) 83 | display.root_group = g 84 | 85 | display.auto_refresh = False 86 | 87 | sd_spi = busio.SPI(clock=board.IO18, MOSI=board.IO14, MISO=board.IO17) 88 | sd_cs = board.IO12 89 | sdcard = sdcardio.SDCard(sd_spi, sd_cs) 90 | vfs = storage.VfsFat(sdcard) 91 | storage.mount(vfs, "/sd") 92 | 93 | 94 | def exists(filename): 95 | try: 96 | os.stat(filename) 97 | return True 98 | except OSError: 99 | return False 100 | 101 | 102 | _image_counter = 0 103 | 104 | 105 | def open_next_image(): 106 | global _image_counter # noqa: PLW0603 107 | while True: 108 | filename = f"/sd/img{_image_counter:04d}.jpg" 109 | _image_counter += 1 110 | if exists(filename): 111 | continue 112 | print("#", filename) 113 | return open(filename, "wb") 114 | 115 | 116 | def capture_image(): 117 | old_size = cam.size 118 | old_colorspace = cam.colorspace 119 | 120 | try: 121 | cam.size = adafruit_ov2640.OV2640_SIZE_UXGA 122 | cam.colorspace = adafruit_ov2640.OV2640_COLOR_JPEG 123 | b = bytearray(cam.capture_buffer_size) 124 | jpeg = cam.capture(b) 125 | 126 | print(f"Captured {len(jpeg)} bytes of jpeg data") 127 | with open_next_image() as f: 128 | f.write(jpeg) 129 | finally: 130 | cam.size = old_size 131 | cam.colorspace = old_colorspace 132 | 133 | 134 | display.auto_refresh = False 135 | while True: 136 | a_voltage = a.value * a.reference_voltage / 65535 137 | record_pressed = abs(a_voltage - V_RECORD) < 0.05 138 | if record_pressed: 139 | capture_image() 140 | cam.capture(bitmap) 141 | bitmap.dirty() 142 | display.refresh(minimum_frames_per_second=0) 143 | -------------------------------------------------------------------------------- /examples/ov2640_jpeg_sd_pico_st7789_2in.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 | """ 7 | Display an image on the LCD, then record an image when a button is pressed/held. 8 | 9 | Uses the SD Card present on the 2inch st7789 breakout board 10 | 11 | This example also requires an SD card breakout wired as follows: 12 | SPI BUS - same as the ST7789 -- with MISO connected 13 | * GP2: SD Clock Input 14 | * GP3: SD Serial Input (MOSI) 15 | * GP4: SD Serial Output (MISO) 16 | * GP5: SD Chip Select 17 | 18 | A button is needed to trigger the capture 19 | Attach a button to GROUND and GP22 20 | 21 | Insert a CircuitPython-compatible SD card before powering on the Kaluga. 22 | Press the "Record" button on the audio daughterboard to take a photo. 23 | """ 24 | 25 | import os 26 | import time 27 | 28 | import board 29 | import busio 30 | import digitalio 31 | import sdcardio 32 | import storage 33 | from adafruit_st7789 import ST7789 34 | from displayio import ( 35 | Bitmap, 36 | ColorConverter, 37 | Colorspace, 38 | FourWire, 39 | Group, 40 | TileGrid, 41 | release_displays, 42 | ) 43 | 44 | import adafruit_ov2640 45 | 46 | release_displays() 47 | # Set up the display (You must customize this block for your display!) 48 | spi = busio.SPI(clock=board.GP2, MOSI=board.GP3, MISO=board.GP4) 49 | # setup the SD Card 50 | sd_cs = board.GP5 51 | sdcard = sdcardio.SDCard(spi, sd_cs) 52 | vfs = storage.VfsFat(sdcard) 53 | storage.mount(vfs, "/sd") 54 | # setup the button 55 | button = digitalio.DigitalInOut(board.GP22) 56 | button.pull = digitalio.Pull.UP 57 | 58 | display_bus = FourWire(spi, command=board.GP0, chip_select=board.GP1, reset=None) 59 | display = ST7789(display_bus, width=320, height=240, rotation=270) 60 | display.auto_refresh = False 61 | 62 | # Ensure the camera is shut down, so that it releases the SDA/SCL lines, 63 | # then create the configuration I2C bus 64 | 65 | with digitalio.DigitalInOut(board.GP10) as reset: 66 | reset.switch_to_output(False) 67 | time.sleep(0.001) 68 | bus = busio.I2C(board.GP9, board.GP8) 69 | 70 | # Set up the camera (you must customize this for your board!) 71 | cam = adafruit_ov2640.OV2640( 72 | bus, 73 | data_pins=[ 74 | board.GP12, 75 | board.GP13, 76 | board.GP14, 77 | board.GP15, 78 | board.GP16, 79 | board.GP17, 80 | board.GP18, 81 | board.GP19, 82 | ], # [16] [org] etc 83 | clock=board.GP11, # [15] [blk] 84 | vsync=board.GP7, # [10] [brn] 85 | href=board.GP21, # [27/o14] [red] 86 | mclk=board.GP20, # [16/o15] 87 | shutdown=None, 88 | reset=board.GP10, 89 | ) # [14] 90 | 91 | width = display.width 92 | height = display.height 93 | 94 | cam.size = adafruit_ov2640.OV2640_SIZE_QQVGA 95 | # cam.test_pattern = True 96 | bitmap = Bitmap(cam.width, cam.height, 65536) 97 | 98 | print(width, height, cam.width, cam.height) 99 | if bitmap is None: 100 | raise SystemExit("Could not allocate a bitmap") 101 | 102 | g = Group(scale=1, x=(width - cam.width) // 2, y=(height - cam.height) // 2) 103 | tg = TileGrid(bitmap, pixel_shader=ColorConverter(input_colorspace=Colorspace.RGB565_SWAPPED)) 104 | g.append(tg) 105 | display.root_group = g 106 | 107 | display.auto_refresh = False 108 | 109 | 110 | def exists(filename): 111 | try: 112 | os.stat(filename) 113 | return True 114 | except OSError: 115 | return False 116 | 117 | 118 | _image_counter = 0 119 | 120 | 121 | def open_next_image(): 122 | global _image_counter # noqa: PLW0603 123 | while True: 124 | filename = f"/sd/img{_image_counter:04d}.jpg" 125 | _image_counter += 1 126 | if exists(filename): 127 | continue 128 | print("#", filename) 129 | return open(filename, "wb") 130 | 131 | 132 | def capture_image(): 133 | old_size = cam.size 134 | old_colorspace = cam.colorspace 135 | 136 | try: 137 | cam.size = adafruit_ov2640.OV2640_SIZE_QVGA 138 | cam.colorspace = adafruit_ov2640.OV2640_COLOR_JPEG 139 | b = bytearray(cam.capture_buffer_size) 140 | jpeg = cam.capture(b) 141 | 142 | print(f"Captured {len(jpeg)} bytes of jpeg data") 143 | with open_next_image() as f: 144 | f.write(jpeg) 145 | finally: 146 | cam.size = old_size 147 | cam.colorspace = old_colorspace 148 | 149 | 150 | def main(): 151 | display.auto_refresh = False 152 | while True: 153 | if not button.value: # button pressed 154 | capture_image() 155 | cam.capture(bitmap) 156 | bitmap.dirty() 157 | display.refresh(minimum_frames_per_second=0) 158 | 159 | 160 | main() 161 | -------------------------------------------------------------------------------- /examples/ov2640_directio_kaluga1_3_ili9341.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 | """ 7 | The Kaluga development kit comes in two versions (v1.2 and v1.3); this demo is 8 | tested on v1.3. 9 | 10 | The audio board must be mounted between the Kaluga and the LCD, it provides the 11 | I2C pull-ups(!) 12 | 13 | The v1.3 development kit's LCD can have one of two chips, the ili9341 or 14 | st7789. Furthermore, there are at least 2 ILI9341 variants, which differ 15 | by rotation. This example is written for one if the ILI9341 variants, 16 | the one which usually uses rotation=90 to get a landscape display. 17 | 18 | This example also requires an SD card breakout wired as follows: 19 | * IO18: SD Clock Input 20 | * IO17: SD Serial Output (MISO) 21 | * IO14: SD Serial Input (MOSI) 22 | * IO12: SD Chip Select 23 | 24 | Insert a CircuitPython-compatible SD card before powering on the Kaluga. 25 | Press the "Record" button on the audio daughterboard to take a photo. 26 | """ 27 | 28 | import os 29 | import struct 30 | 31 | import analogio 32 | import board 33 | import busdisplay 34 | import busio 35 | import displayio 36 | import fourwire 37 | import sdcardio 38 | import storage 39 | 40 | import adafruit_ov2640 41 | 42 | V_MODE = 1.98 43 | V_RECORD = 2.41 44 | 45 | a = analogio.AnalogIn(board.IO6) 46 | 47 | # Release any resources currently in use for the displays 48 | displayio.release_displays() 49 | 50 | spi = busio.SPI(MOSI=board.LCD_MOSI, clock=board.LCD_CLK) 51 | display_bus = fourwire.FourWire( 52 | spi, 53 | command=board.LCD_D_C, 54 | chip_select=board.LCD_CS, 55 | reset=board.LCD_RST, 56 | baudrate=80_000_000, 57 | ) 58 | _INIT_SEQUENCE = ( 59 | b"\x01\x80\x80" # Software reset then delay 0x80 (128ms) 60 | b"\xef\x03\x03\x80\x02" 61 | b"\xcf\x03\x00\xc1\x30" 62 | b"\xed\x04\x64\x03\x12\x81" 63 | b"\xe8\x03\x85\x00\x78" 64 | b"\xcb\x05\x39\x2c\x00\x34\x02" 65 | b"\xf7\x01\x20" 66 | b"\xea\x02\x00\x00" 67 | b"\xc0\x01\x23" # Power control VRH[5:0] 68 | b"\xc1\x01\x10" # Power control SAP[2:0];BT[3:0] 69 | b"\xc5\x02\x3e\x28" # VCM control 70 | b"\xc7\x01\x86" # VCM control2 71 | b"\x36\x01\x40" # Memory Access Control 72 | b"\x37\x01\x00" # Vertical scroll zero 73 | b"\x3a\x01\x55" # COLMOD: Pixel Format Set 74 | b"\xb1\x02\x00\x18" # Frame Rate Control (In Normal Mode/Full Colors) 75 | b"\xb6\x03\x08\x82\x27" # Display Function Control 76 | b"\xf2\x01\x00" # 3Gamma Function Disable 77 | b"\x26\x01\x01" # Gamma curve selected 78 | b"\xe0\x0f\x0f\x31\x2b\x0c\x0e\x08\x4e\xf1\x37\x07\x10\x03\x0e\x09\x00" # Set Gamma 79 | b"\xe1\x0f\x00\x0e\x14\x03\x11\x07\x31\xc1\x48\x08\x0f\x0c\x31\x36\x0f" # Set Gamma 80 | b"\x11\x80\x78" # Exit Sleep then delay 0x78 (120ms) 81 | b"\x29\x80\x78" # Display on then delay 0x78 (120ms) 82 | ) 83 | 84 | display = busdisplay.BusDisplay(display_bus, _INIT_SEQUENCE, width=320, height=240) 85 | 86 | bus = busio.I2C(scl=board.CAMERA_SIOC, sda=board.CAMERA_SIOD) 87 | cam = adafruit_ov2640.OV2640( 88 | bus, 89 | data_pins=board.CAMERA_DATA, 90 | clock=board.CAMERA_PCLK, 91 | vsync=board.CAMERA_VSYNC, 92 | href=board.CAMERA_HREF, 93 | mclk=board.CAMERA_XCLK, 94 | mclk_frequency=20_000_000, 95 | size=adafruit_ov2640.OV2640_SIZE_QVGA, 96 | ) 97 | 98 | cam.flip_x = False 99 | cam.flip_y = True 100 | pid = cam.product_id 101 | ver = cam.product_version 102 | print(f"Detected pid={pid:x} ver={ver:x}") 103 | # cam.test_pattern = True 104 | 105 | bitmap = displayio.Bitmap(320, 240, 65536) 106 | 107 | display.auto_refresh = False 108 | 109 | sd_spi = busio.SPI(clock=board.IO18, MOSI=board.IO14, MISO=board.IO17) 110 | sd_cs = board.IO12 111 | sdcard = sdcardio.SDCard(sd_spi, sd_cs) 112 | vfs = storage.VfsFat(sdcard) 113 | storage.mount(vfs, "/sd") 114 | 115 | 116 | def exists(filename): 117 | try: 118 | os.stat(filename) 119 | return True 120 | except OSError: 121 | return False 122 | 123 | 124 | _image_counter = 0 125 | 126 | 127 | def open_next_image(): 128 | global _image_counter # noqa: PLW0603 129 | while True: 130 | filename = f"/sd/img{_image_counter:04d}.jpg" 131 | _image_counter += 1 132 | if exists(filename): 133 | continue 134 | print("#", filename) 135 | return open(filename, "wb") 136 | 137 | 138 | def capture_image(): 139 | old_size = cam.size 140 | old_colorspace = cam.colorspace 141 | exposure = cam.exposure 142 | try: 143 | cam.size = adafruit_ov2640.OV2640_SIZE_UXGA 144 | cam.colorspace = adafruit_ov2640.OV2640_COLOR_JPEG 145 | cam.exposure = exposure 146 | b = bytearray(cam.capture_buffer_size) 147 | jpeg = cam.capture(b) 148 | 149 | print(f"Captured {len(jpeg)} bytes of jpeg data") 150 | with open_next_image() as f: 151 | f.write(jpeg) 152 | finally: 153 | cam.size = old_size 154 | cam.colorspace = old_colorspace 155 | cam.exposure = exposure 156 | 157 | 158 | def main(): 159 | display.auto_refresh = False 160 | display_bus.send(42, struct.pack(">hh", 0, 319)) 161 | display_bus.send(43, struct.pack(">hh", 0, 239)) 162 | while True: 163 | a_voltage = a.value * a.reference_voltage / 65535 164 | record_pressed = abs(a_voltage - V_RECORD) < 0.05 165 | if record_pressed: 166 | capture_image() 167 | cam.capture(bitmap) 168 | display_bus.send(44, bitmap) 169 | 170 | 171 | main() 172 | -------------------------------------------------------------------------------- /docs/conf.py: -------------------------------------------------------------------------------- 1 | # SPDX-FileCopyrightText: 2017 Scott Shawcroft, written for Adafruit Industries 2 | # 3 | # SPDX-License-Identifier: MIT 4 | 5 | import datetime 6 | import os 7 | import sys 8 | 9 | sys.path.insert(0, os.path.abspath("..")) 10 | 11 | # -- General configuration ------------------------------------------------ 12 | 13 | # Add any Sphinx extension module names here, as strings. They can be 14 | # extensions coming with Sphinx (named 'sphinx.ext.*') or your custom 15 | # ones. 16 | extensions = [ 17 | "sphinx.ext.autodoc", 18 | "sphinxcontrib.jquery", 19 | "sphinx.ext.intersphinx", 20 | "sphinx.ext.napoleon", 21 | "sphinx.ext.todo", 22 | ] 23 | 24 | # TODO: Please Read! 25 | # Uncomment the below if you use native CircuitPython modules such as 26 | # digitalio, micropython and busio. List the modules you use. Without it, the 27 | # autodoc module docs will fail to generate with a warning. 28 | autodoc_mock_imports = ["adafruit_bus_device", "digitalio", "imagecapture", "pwmio"] 29 | 30 | 31 | intersphinx_mapping = { 32 | "python": ("https://docs.python.org/3", None), 33 | "BusDevice": ( 34 | "https://docs.circuitpython.org/projects/busdevice/en/latest/", 35 | None, 36 | ), 37 | "CircuitPython": ("https://docs.circuitpython.org/en/latest/", None), 38 | } 39 | 40 | # Show the docstring from both the class and its __init__() method. 41 | autoclass_content = "both" 42 | 43 | # Add any paths that contain templates here, relative to this directory. 44 | templates_path = ["_templates"] 45 | 46 | source_suffix = ".rst" 47 | 48 | # The master toctree document. 49 | master_doc = "index" 50 | 51 | # General information about the project. 52 | project = "Adafruit CircuitPython OV2640 Library" 53 | creation_year = "2021" 54 | current_year = str(datetime.datetime.now().year) 55 | year_duration = ( 56 | current_year if current_year == creation_year else creation_year + " - " + current_year 57 | ) 58 | copyright = year_duration + " Jeff Epler" 59 | author = "Jeff Epler" 60 | 61 | # The version info for the project you're documenting, acts as replacement for 62 | # |version| and |release|, also used in various other places throughout the 63 | # built documents. 64 | # 65 | # The short X.Y version. 66 | version = "1.0" 67 | # The full version, including alpha/beta/rc tags. 68 | release = "1.0" 69 | 70 | # The language for content autogenerated by Sphinx. Refer to documentation 71 | # for a list of supported languages. 72 | # 73 | # This is also used if you do content translation via gettext catalogs. 74 | # Usually you set "language" from the command line for these cases. 75 | language = "en" 76 | 77 | # List of patterns, relative to source directory, that match files and 78 | # directories to ignore when looking for source files. 79 | # This patterns also effect to html_static_path and html_extra_path 80 | exclude_patterns = [ 81 | "_build", 82 | "Thumbs.db", 83 | ".DS_Store", 84 | ".env", 85 | "CODE_OF_CONDUCT.md", 86 | ] 87 | 88 | # The reST default role (used for this markup: `text`) to use for all 89 | # documents. 90 | # 91 | default_role = "any" 92 | 93 | # If true, '()' will be appended to :func: etc. cross-reference text. 94 | # 95 | add_function_parentheses = True 96 | 97 | # The name of the Pygments (syntax highlighting) style to use. 98 | pygments_style = "sphinx" 99 | 100 | # If true, `todo` and `todoList` produce output, else they produce nothing. 101 | todo_include_todos = False 102 | 103 | # If this is True, todo emits a warning for each TODO entries. The default is False. 104 | todo_emit_warnings = True 105 | 106 | napoleon_numpy_docstring = False 107 | 108 | # -- Options for HTML output ---------------------------------------------- 109 | 110 | # The theme to use for HTML and HTML Help pages. See the documentation for 111 | # a list of builtin themes. 112 | # 113 | import sphinx_rtd_theme 114 | 115 | html_theme = "sphinx_rtd_theme" 116 | 117 | # Add any paths that contain custom static files (such as style sheets) here, 118 | # relative to this directory. They are copied after the builtin static files, 119 | # so a file named "default.css" will overwrite the builtin "default.css". 120 | html_static_path = ["_static"] 121 | 122 | # Include extra css to work around rtd theme glitches 123 | html_css_files = ["custom.css"] 124 | 125 | # The name of an image file (relative to this directory) to use as a favicon of 126 | # the docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32 127 | # pixels large. 128 | # 129 | html_favicon = "_static/favicon.ico" 130 | 131 | # Output file base name for HTML help builder. 132 | htmlhelp_basename = "Adafruit_CircuitPython_Ov2640Librarydoc" 133 | 134 | # -- Options for LaTeX output --------------------------------------------- 135 | 136 | latex_elements = { 137 | # The paper size ('letterpaper' or 'a4paper'). 138 | # 'papersize': 'letterpaper', 139 | # The font size ('10pt', '11pt' or '12pt'). 140 | # 'pointsize': '10pt', 141 | # Additional stuff for the LaTeX preamble. 142 | # 'preamble': '', 143 | # Latex figure (float) alignment 144 | # 'figure_align': 'htbp', 145 | } 146 | 147 | # Grouping the document tree into LaTeX files. List of tuples 148 | # (source start file, target name, title, 149 | # author, documentclass [howto, manual, or own class]). 150 | latex_documents = [ 151 | ( 152 | master_doc, 153 | "Adafruit_CircuitPython_OV2640Library.tex", 154 | "Adafruit CircuitPython OV2640 Library Documentation", 155 | author, 156 | "manual", 157 | ), 158 | ] 159 | 160 | # -- Options for manual page output --------------------------------------- 161 | 162 | # One entry per manual page. List of tuples 163 | # (source start file, name, description, authors, manual section). 164 | man_pages = [ 165 | ( 166 | master_doc, 167 | "Adafruit_CircuitPython_OV2640Library", 168 | "Adafruit CircuitPython OV2640 Library Documentation", 169 | [author], 170 | 1, 171 | ), 172 | ] 173 | 174 | # -- Options for Texinfo output ------------------------------------------- 175 | 176 | # Grouping the document tree into Texinfo files. List of tuples 177 | # (source start file, target name, title, author, 178 | # dir menu entry, description, category) 179 | texinfo_documents = [ 180 | ( 181 | master_doc, 182 | "Adafruit_CircuitPython_OV2640Library", 183 | "Adafruit CircuitPython OV2640 Library Documentation", 184 | author, 185 | "Adafruit_CircuitPython_OV2640Library", 186 | "One line description of project.", 187 | "Miscellaneous", 188 | ), 189 | ] 190 | -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | 7 | # Adafruit Community Code of Conduct 8 | 9 | ## Our Pledge 10 | 11 | In the interest of fostering an open and welcoming environment, we as 12 | contributors and leaders pledge to making participation in our project and 13 | our community a harassment-free experience for everyone, regardless of age, body 14 | size, disability, ethnicity, gender identity and expression, level or type of 15 | experience, education, socio-economic status, nationality, personal appearance, 16 | race, religion, or sexual identity and orientation. 17 | 18 | ## Our Standards 19 | 20 | We are committed to providing a friendly, safe and welcoming environment for 21 | all. 22 | 23 | Examples of behavior that contributes to creating a positive environment 24 | include: 25 | 26 | * Be kind and courteous to others 27 | * Using welcoming and inclusive language 28 | * Being respectful of differing viewpoints and experiences 29 | * Collaborating with other community members 30 | * Gracefully accepting constructive criticism 31 | * Focusing on what is best for the community 32 | * Showing empathy towards other community members 33 | 34 | Examples of unacceptable behavior by participants include: 35 | 36 | * The use of sexualized language or imagery and sexual attention or advances 37 | * The use of inappropriate images, including in a community member's avatar 38 | * The use of inappropriate language, including in a community member's nickname 39 | * Any spamming, flaming, baiting or other attention-stealing behavior 40 | * Excessive or unwelcome helping; answering outside the scope of the question 41 | asked 42 | * Trolling, insulting/derogatory comments, and personal or political attacks 43 | * Promoting or spreading disinformation, lies, or conspiracy theories against 44 | a person, group, organisation, project, or community 45 | * Public or private harassment 46 | * Publishing others' private information, such as a physical or electronic 47 | address, without explicit permission 48 | * Other conduct which could reasonably be considered inappropriate 49 | 50 | The goal of the standards and moderation guidelines outlined here is to build 51 | and maintain a respectful community. We ask that you don’t just aim to be 52 | "technically unimpeachable", but rather try to be your best self. 53 | 54 | We value many things beyond technical expertise, including collaboration and 55 | supporting others within our community. Providing a positive experience for 56 | other community members can have a much more significant impact than simply 57 | providing the correct answer. 58 | 59 | ## Our Responsibilities 60 | 61 | Project leaders are responsible for clarifying the standards of acceptable 62 | behavior and are expected to take appropriate and fair corrective action in 63 | response to any instances of unacceptable behavior. 64 | 65 | Project leaders have the right and responsibility to remove, edit, or 66 | reject messages, comments, commits, code, issues, and other contributions 67 | that are not aligned to this Code of Conduct, or to ban temporarily or 68 | permanently any community member for other behaviors that they deem 69 | inappropriate, threatening, offensive, or harmful. 70 | 71 | ## Moderation 72 | 73 | Instances of behaviors that violate the Adafruit Community Code of Conduct 74 | may be reported by any member of the community. Community members are 75 | encouraged to report these situations, including situations they witness 76 | involving other community members. 77 | 78 | You may report in the following ways: 79 | 80 | In any situation, you may send an email to . 81 | 82 | On the Adafruit Discord, you may send an open message from any channel 83 | to all Community Moderators by tagging @community moderators. You may 84 | also send an open message from any channel, or a direct message to 85 | @kattni#1507, @tannewt#4653, @danh#1614, @cater#2442, 86 | @sommersoft#0222, @Mr. Certainly#0472 or @Andon#8175. 87 | 88 | Email and direct message reports will be kept confidential. 89 | 90 | In situations on Discord where the issue is particularly egregious, possibly 91 | illegal, requires immediate action, or violates the Discord terms of service, 92 | you should also report the message directly to Discord. 93 | 94 | These are the steps for upholding our community’s standards of conduct. 95 | 96 | 1. Any member of the community may report any situation that violates the 97 | Adafruit Community Code of Conduct. All reports will be reviewed and 98 | investigated. 99 | 2. If the behavior is an egregious violation, the community member who 100 | committed the violation may be banned immediately, without warning. 101 | 3. Otherwise, moderators will first respond to such behavior with a warning. 102 | 4. Moderators follow a soft "three strikes" policy - the community member may 103 | be given another chance, if they are receptive to the warning and change their 104 | behavior. 105 | 5. If the community member is unreceptive or unreasonable when warned by a 106 | moderator, or the warning goes unheeded, they may be banned for a first or 107 | second offense. Repeated offenses will result in the community member being 108 | banned. 109 | 110 | ## Scope 111 | 112 | This Code of Conduct and the enforcement policies listed above apply to all 113 | Adafruit Community venues. This includes but is not limited to any community 114 | spaces (both public and private), the entire Adafruit Discord server, and 115 | Adafruit GitHub repositories. Examples of Adafruit Community spaces include 116 | but are not limited to meet-ups, audio chats on the Adafruit Discord, or 117 | interaction at a conference. 118 | 119 | This Code of Conduct applies both within project spaces and in public spaces 120 | when an individual is representing the project or its community. As a community 121 | member, you are representing our community, and are expected to behave 122 | accordingly. 123 | 124 | ## Attribution 125 | 126 | This Code of Conduct is adapted from the [Contributor Covenant], 127 | version 1.4, available at 128 | , 129 | and the [Rust Code of Conduct](https://www.rust-lang.org/en-US/conduct.html). 130 | 131 | For other projects adopting the Adafruit Community Code of 132 | Conduct, please contact the maintainers of those projects for enforcement. 133 | If you wish to use this code of conduct for your own project, consider 134 | explicitly mentioning your moderation policy or making a copy with your 135 | own moderation policy so as to avoid confusion. 136 | 137 | [Contributor Covenant]: https://www.contributor-covenant.org 138 | -------------------------------------------------------------------------------- /examples/ov2640_bmp_sd_kaluga1_3.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 | """ 7 | The Kaluga development kit comes in two versions (v1.2 and v1.3); this demo is 8 | tested on v1.3. 9 | 10 | The audio board must be mounted between the Kaluga and the LCD, it provides the 11 | I2C pull-ups(!) 12 | 13 | The v1.3 development kit's LCD can have one of two chips, the ili9341 or 14 | st7789. Furthermore, there are at least 2 ILI9341 variants, one of which needs 15 | rotation=90! This demo is for the ili9341. If the display is garbled, try adding 16 | rotation=90, or try modifying it to use ST7799. 17 | 18 | This example also requires an SD card breakout wired as follows: 19 | * IO18: SD Clock Input 20 | * IO17: SD Serial Output (MISO) 21 | * IO14: SD Serial Input (MOSI) 22 | * IO12: SD Chip Select 23 | 24 | Insert a CircuitPython-compatible SD card before powering on the Kaluga. 25 | Press the "Record" button on the audio daughterboard to take a photo in BMP format. 26 | """ 27 | 28 | import os 29 | import struct 30 | 31 | import analogio 32 | import board 33 | import busdisplay 34 | import busio 35 | import displayio 36 | import fourwire 37 | import sdcardio 38 | import storage 39 | import ulab.numpy as np 40 | 41 | import adafruit_ov2640 42 | 43 | # Nominal voltages of several of the buttons on the audio daughterboard 44 | V_MODE = 1.98 45 | V_RECORD = 2.41 46 | 47 | a = analogio.AnalogIn(board.IO6) 48 | 49 | # Release any resources currently in use for the displays 50 | displayio.release_displays() 51 | 52 | spi = busio.SPI(MOSI=board.LCD_MOSI, clock=board.LCD_CLK) 53 | display_bus = fourwire.FourWire( 54 | spi, 55 | command=board.LCD_D_C, 56 | chip_select=board.LCD_CS, 57 | reset=board.LCD_RST, 58 | baudrate=80_000_000, 59 | ) 60 | _INIT_SEQUENCE = ( 61 | b"\x01\x80\x80" # Software reset then delay 0x80 (128ms) 62 | b"\xef\x03\x03\x80\x02" 63 | b"\xcf\x03\x00\xc1\x30" 64 | b"\xed\x04\x64\x03\x12\x81" 65 | b"\xe8\x03\x85\x00\x78" 66 | b"\xcb\x05\x39\x2c\x00\x34\x02" 67 | b"\xf7\x01\x20" 68 | b"\xea\x02\x00\x00" 69 | b"\xc0\x01\x23" # Power control VRH[5:0] 70 | b"\xc1\x01\x10" # Power control SAP[2:0];BT[3:0] 71 | b"\xc5\x02\x3e\x28" # VCM control 72 | b"\xc7\x01\x86" # VCM control2 73 | b"\x36\x01\x90" # Memory Access Control 74 | b"\x37\x01\x00" # Vertical scroll zero 75 | b"\x3a\x01\x55" # COLMOD: Pixel Format Set 76 | b"\xb1\x02\x00\x18" # Frame Rate Control (In Normal Mode/Full Colors) 77 | b"\xb6\x03\x08\x82\x27" # Display Function Control 78 | b"\xf2\x01\x00" # 3Gamma Function Disable 79 | b"\x26\x01\x01" # Gamma curve selected 80 | b"\xe0\x0f\x0f\x31\x2b\x0c\x0e\x08\x4e\xf1\x37\x07\x10\x03\x0e\x09\x00" # Set Gamma 81 | b"\xe1\x0f\x00\x0e\x14\x03\x11\x07\x31\xc1\x48\x08\x0f\x0c\x31\x36\x0f" # Set Gamma 82 | b"\x11\x80\x78" # Exit Sleep then delay 0x78 (120ms) 83 | b"\x29\x80\x78" # Display on then delay 0x78 (120ms) 84 | ) 85 | 86 | display = busdisplay.BusDisplay( 87 | display_bus, _INIT_SEQUENCE, width=320, height=240, auto_refresh=False 88 | ) 89 | 90 | bus = busio.I2C(scl=board.CAMERA_SIOC, sda=board.CAMERA_SIOD) 91 | cam = adafruit_ov2640.OV2640( 92 | bus, 93 | data_pins=board.CAMERA_DATA, 94 | clock=board.CAMERA_PCLK, 95 | vsync=board.CAMERA_VSYNC, 96 | href=board.CAMERA_HREF, 97 | mclk=board.CAMERA_XCLK, 98 | mclk_frequency=20_000_000, 99 | size=adafruit_ov2640.OV2640_SIZE_QVGA, 100 | ) 101 | 102 | cam.flip_x = False 103 | cam.flip_y = False 104 | cam.test_pattern = False 105 | 106 | g = displayio.Group(scale=1) 107 | bitmap = displayio.Bitmap(320, 240, 65536) 108 | tg = displayio.TileGrid( 109 | bitmap, 110 | pixel_shader=displayio.ColorConverter(input_colorspace=displayio.Colorspace.RGB565_SWAPPED), 111 | ) 112 | g.append(tg) 113 | display.root_group = g 114 | 115 | 116 | sd_spi = busio.SPI(clock=board.IO18, MOSI=board.IO14, MISO=board.IO17) 117 | sd_cs = board.IO12 118 | sdcard = sdcardio.SDCard(sd_spi, sd_cs) 119 | vfs = storage.VfsFat(sdcard) 120 | storage.mount(vfs, "/sd") 121 | 122 | 123 | def exists(filename): 124 | try: 125 | os.stat(filename) 126 | return True 127 | except OSError: 128 | return False 129 | 130 | 131 | _image_counter = 0 132 | 133 | 134 | def open_next_image(extension="jpg"): 135 | global _image_counter # noqa: PLW0603 136 | while True: 137 | filename = f"/sd/img{_image_counter:04d}.{extension}" 138 | _image_counter += 1 139 | if exists(filename): 140 | continue 141 | print("#", filename) 142 | return open(filename, "wb") 143 | 144 | 145 | ### These routines are for writing BMP files in the RGB565 or BGR565 formats. 146 | _BI_BITFIELDS = 3 147 | 148 | _bitmask_rgb565 = (0xF800, 0x7E0, 0x1F) 149 | _bitmask_bgr565 = (0x1F, 0x7E0, 0xF800) 150 | 151 | 152 | def write_header(output_file, width, height, masks): 153 | def put_word(value): 154 | output_file.write(struct.pack(" pixels/meter 183 | put_long(11811) # 72dpi -> pixels/meter 184 | put_dword(0) # palette size 185 | put_dword(0) # important color count 186 | put_dword(masks[0]) # red mask 187 | put_dword(masks[1]) # green mask 188 | put_dword(masks[2]) # blue mask 189 | put_dword(0) # alpha mask 190 | put_dword(0) # CS Type 191 | put_padding(3 * 3 * 4) # CIEXYZ infrmation 192 | put_dword(144179) # 2.2 gamma red 193 | put_dword(144179) # 2.2 gamma green 194 | put_dword(144179) # 2.2 gamma blue 195 | 196 | 197 | def capture_image_bmp(the_bitmap): 198 | with open_next_image("bmp") as f: 199 | swapped = np.frombuffer(the_bitmap, dtype=np.uint16) 200 | swapped.byteswap(inplace=True) 201 | write_header(f, the_bitmap.width, the_bitmap.height, _bitmask_rgb565) 202 | f.write(swapped) 203 | 204 | 205 | display.auto_refresh = False 206 | old_record_pressed = True 207 | 208 | while True: 209 | a_voltage = a.value * a.reference_voltage / 65535 210 | cam.capture(bitmap) 211 | bitmap.dirty() 212 | 213 | record_pressed = abs(a_voltage - V_RECORD) < 0.05 214 | display.refresh(minimum_frames_per_second=0) 215 | if record_pressed and not old_record_pressed: 216 | capture_image_bmp(bitmap) 217 | old_record_pressed = record_pressed 218 | -------------------------------------------------------------------------------- /LICENSES/CC-BY-4.0.txt: -------------------------------------------------------------------------------- 1 | Creative Commons Attribution 4.0 International Creative Commons Corporation 2 | ("Creative Commons") is not a law firm and does not provide legal services 3 | or legal advice. Distribution of Creative Commons public licenses does not 4 | create a lawyer-client or other relationship. Creative Commons makes its licenses 5 | and related information available on an "as-is" basis. Creative Commons gives 6 | no warranties regarding its licenses, any material licensed under their terms 7 | and conditions, or any related information. Creative Commons disclaims all 8 | liability for damages resulting from their use to the fullest extent possible. 9 | 10 | Using Creative Commons Public Licenses 11 | 12 | Creative Commons public licenses provide a standard set of terms and conditions 13 | that creators and other rights holders may use to share original works of 14 | authorship and other material subject to copyright and certain other rights 15 | specified in the public license below. The following considerations are for 16 | informational purposes only, are not exhaustive, and do not form part of our 17 | licenses. 18 | 19 | Considerations for licensors: Our public licenses are intended for use by 20 | those authorized to give the public permission to use material in ways otherwise 21 | restricted by copyright and certain other rights. Our licenses are irrevocable. 22 | Licensors should read and understand the terms and conditions of the license 23 | they choose before applying it. Licensors should also secure all rights necessary 24 | before applying our licenses so that the public can reuse the material as 25 | expected. Licensors should clearly mark any material not subject to the license. 26 | This includes other CC-licensed material, or material used under an exception 27 | or limitation to copyright. More considerations for licensors : wiki.creativecommons.org/Considerations_for_licensors 28 | 29 | Considerations for the public: By using one of our public licenses, a licensor 30 | grants the public permission to use the licensed material under specified 31 | terms and conditions. If the licensor's permission is not necessary for any 32 | reason–for example, because of any applicable exception or limitation to copyright–then 33 | that use is not regulated by the license. Our licenses grant only permissions 34 | under copyright and certain other rights that a licensor has authority to 35 | grant. Use of the licensed material may still be restricted for other reasons, 36 | including because others have copyright or other rights in the material. A 37 | licensor may make special requests, such as asking that all changes be marked 38 | or described. Although not required by our licenses, you are encouraged to 39 | respect those requests where reasonable. More considerations for the public 40 | : wiki.creativecommons.org/Considerations_for_licensees Creative Commons Attribution 41 | 4.0 International Public License 42 | 43 | By exercising the Licensed Rights (defined below), You accept and agree to 44 | be bound by the terms and conditions of this Creative Commons Attribution 45 | 4.0 International Public License ("Public License"). To the extent this Public 46 | License may be interpreted as a contract, You are granted the Licensed Rights 47 | in consideration of Your acceptance of these terms and conditions, and the 48 | Licensor grants You such rights in consideration of benefits the Licensor 49 | receives from making the Licensed Material available under these terms and 50 | conditions. 51 | 52 | Section 1 – Definitions. 53 | 54 | a. Adapted Material means material subject to Copyright and Similar Rights 55 | that is derived from or based upon the Licensed Material and in which the 56 | Licensed Material is translated, altered, arranged, transformed, or otherwise 57 | modified in a manner requiring permission under the Copyright and Similar 58 | Rights held by the Licensor. For purposes of this Public License, where the 59 | Licensed Material is a musical work, performance, or sound recording, Adapted 60 | Material is always produced where the Licensed Material is synched in timed 61 | relation with a moving image. 62 | 63 | b. Adapter's License means the license You apply to Your Copyright and Similar 64 | Rights in Your contributions to Adapted Material in accordance with the terms 65 | and conditions of this Public License. 66 | 67 | c. Copyright and Similar Rights means copyright and/or similar rights closely 68 | related to copyright including, without limitation, performance, broadcast, 69 | sound recording, and Sui Generis Database Rights, without regard to how the 70 | rights are labeled or categorized. For purposes of this Public License, the 71 | rights specified in Section 2(b)(1)-(2) are not Copyright and Similar Rights. 72 | 73 | d. Effective Technological Measures means those measures that, in the absence 74 | of proper authority, may not be circumvented under laws fulfilling obligations 75 | under Article 11 of the WIPO Copyright Treaty adopted on December 20, 1996, 76 | and/or similar international agreements. 77 | 78 | e. Exceptions and Limitations means fair use, fair dealing, and/or any other 79 | exception or limitation to Copyright and Similar Rights that applies to Your 80 | use of the Licensed Material. 81 | 82 | f. Licensed Material means the artistic or literary work, database, or other 83 | material to which the Licensor applied this Public License. 84 | 85 | g. Licensed Rights means the rights granted to You subject to the terms and 86 | conditions of this Public License, which are limited to all Copyright and 87 | Similar Rights that apply to Your use of the Licensed Material and that the 88 | Licensor has authority to license. 89 | 90 | h. Licensor means the individual(s) or entity(ies) granting rights under this 91 | Public License. 92 | 93 | i. Share means to provide material to the public by any means or process that 94 | requires permission under the Licensed Rights, such as reproduction, public 95 | display, public performance, distribution, dissemination, communication, or 96 | importation, and to make material available to the public including in ways 97 | that members of the public may access the material from a place and at a time 98 | individually chosen by them. 99 | 100 | j. Sui Generis Database Rights means rights other than copyright resulting 101 | from Directive 96/9/EC of the European Parliament and of the Council of 11 102 | March 1996 on the legal protection of databases, as amended and/or succeeded, 103 | as well as other essentially equivalent rights anywhere in the world. 104 | 105 | k. You means the individual or entity exercising the Licensed Rights under 106 | this Public License. Your has a corresponding meaning. 107 | 108 | Section 2 – Scope. 109 | 110 | a. License grant. 111 | 112 | 1. Subject to the terms and conditions of this Public License, the Licensor 113 | hereby grants You a worldwide, royalty-free, non-sublicensable, non-exclusive, 114 | irrevocable license to exercise the Licensed Rights in the Licensed Material 115 | to: 116 | 117 | A. reproduce and Share the Licensed Material, in whole or in part; and 118 | 119 | B. produce, reproduce, and Share Adapted Material. 120 | 121 | 2. Exceptions and Limitations. For the avoidance of doubt, where Exceptions 122 | and Limitations apply to Your use, this Public License does not apply, and 123 | You do not need to comply with its terms and conditions. 124 | 125 | 3. Term. The term of this Public License is specified in Section 6(a). 126 | 127 | 4. Media and formats; technical modifications allowed. The Licensor authorizes 128 | You to exercise the Licensed Rights in all media and formats whether now known 129 | or hereafter created, and to make technical modifications necessary to do 130 | so. The Licensor waives and/or agrees not to assert any right or authority 131 | to forbid You from making technical modifications necessary to exercise the 132 | Licensed Rights, including technical modifications necessary to circumvent 133 | Effective Technological Measures. For purposes of this Public License, simply 134 | making modifications authorized by this Section 2(a)(4) never produces Adapted 135 | Material. 136 | 137 | 5. Downstream recipients. 138 | 139 | A. Offer from the Licensor – Licensed Material. Every recipient of the Licensed 140 | Material automatically receives an offer from the Licensor to exercise the 141 | Licensed Rights under the terms and conditions of this Public License. 142 | 143 | B. No downstream restrictions. You may not offer or impose any additional 144 | or different terms or conditions on, or apply any Effective Technological 145 | Measures to, the Licensed Material if doing so restricts exercise of the Licensed 146 | Rights by any recipient of the Licensed Material. 147 | 148 | 6. No endorsement. Nothing in this Public License constitutes or may be construed 149 | as permission to assert or imply that You are, or that Your use of the Licensed 150 | Material is, connected with, or sponsored, endorsed, or granted official status 151 | by, the Licensor or others designated to receive attribution as provided in 152 | Section 3(a)(1)(A)(i). 153 | 154 | b. Other rights. 155 | 156 | 1. Moral rights, such as the right of integrity, are not licensed under this 157 | Public License, nor are publicity, privacy, and/or other similar personality 158 | rights; however, to the extent possible, the Licensor waives and/or agrees 159 | not to assert any such rights held by the Licensor to the limited extent necessary 160 | to allow You to exercise the Licensed Rights, but not otherwise. 161 | 162 | 2. Patent and trademark rights are not licensed under this Public License. 163 | 164 | 3. To the extent possible, the Licensor waives any right to collect royalties 165 | from You for the exercise of the Licensed Rights, whether directly or through 166 | a collecting society under any voluntary or waivable statutory or compulsory 167 | licensing scheme. In all other cases the Licensor expressly reserves any right 168 | to collect such royalties. 169 | 170 | Section 3 – License Conditions. 171 | 172 | Your exercise of the Licensed Rights is expressly made subject to the following 173 | conditions. 174 | 175 | a. Attribution. 176 | 177 | 1. If You Share the Licensed Material (including in modified form), You must: 178 | 179 | A. retain the following if it is supplied by the Licensor with the Licensed 180 | Material: 181 | 182 | i. identification of the creator(s) of the Licensed Material and any others 183 | designated to receive attribution, in any reasonable manner requested by the 184 | Licensor (including by pseudonym if designated); 185 | 186 | ii. a copyright notice; 187 | 188 | iii. a notice that refers to this Public License; 189 | 190 | iv. a notice that refers to the disclaimer of warranties; 191 | 192 | v. a URI or hyperlink to the Licensed Material to the extent reasonably practicable; 193 | 194 | B. indicate if You modified the Licensed Material and retain an indication 195 | of any previous modifications; and 196 | 197 | C. indicate the Licensed Material is licensed under this Public License, and 198 | include the text of, or the URI or hyperlink to, this Public License. 199 | 200 | 2. You may satisfy the conditions in Section 3(a)(1) in any reasonable manner 201 | based on the medium, means, and context in which You Share the Licensed Material. 202 | For example, it may be reasonable to satisfy the conditions by providing a 203 | URI or hyperlink to a resource that includes the required information. 204 | 205 | 3. If requested by the Licensor, You must remove any of the information required 206 | by Section 3(a)(1)(A) to the extent reasonably practicable. 207 | 208 | 4. If You Share Adapted Material You produce, the Adapter's License You apply 209 | must not prevent recipients of the Adapted Material from complying with this 210 | Public License. 211 | 212 | Section 4 – Sui Generis Database Rights. 213 | 214 | Where the Licensed Rights include Sui Generis Database Rights that apply to 215 | Your use of the Licensed Material: 216 | 217 | a. for the avoidance of doubt, Section 2(a)(1) grants You the right to extract, 218 | reuse, reproduce, and Share all or a substantial portion of the contents of 219 | the database; 220 | 221 | b. if You include all or a substantial portion of the database contents in 222 | a database in which You have Sui Generis Database Rights, then the database 223 | in which You have Sui Generis Database Rights (but not its individual contents) 224 | is Adapted Material; and 225 | 226 | c. You must comply with the conditions in Section 3(a) if You Share all or 227 | a substantial portion of the contents of the database. 228 | 229 | For the avoidance of doubt, this Section 4 supplements and does not replace 230 | Your obligations under this Public License where the Licensed Rights include 231 | other Copyright and Similar Rights. 232 | 233 | Section 5 – Disclaimer of Warranties and Limitation of Liability. 234 | 235 | a. Unless otherwise separately undertaken by the Licensor, to the extent possible, 236 | the Licensor offers the Licensed Material as-is and as-available, and makes 237 | no representations or warranties of any kind concerning the Licensed Material, 238 | whether express, implied, statutory, or other. This includes, without limitation, 239 | warranties of title, merchantability, fitness for a particular purpose, non-infringement, 240 | absence of latent or other defects, accuracy, or the presence or absence of 241 | errors, whether or not known or discoverable. Where disclaimers of warranties 242 | are not allowed in full or in part, this disclaimer may not apply to You. 243 | 244 | b. To the extent possible, in no event will the Licensor be liable to You 245 | on any legal theory (including, without limitation, negligence) or otherwise 246 | for any direct, special, indirect, incidental, consequential, punitive, exemplary, 247 | or other losses, costs, expenses, or damages arising out of this Public License 248 | or use of the Licensed Material, even if the Licensor has been advised of 249 | the possibility of such losses, costs, expenses, or damages. Where a limitation 250 | of liability is not allowed in full or in part, this limitation may not apply 251 | to You. 252 | 253 | c. The disclaimer of warranties and limitation of liability provided above 254 | shall be interpreted in a manner that, to the extent possible, most closely 255 | approximates an absolute disclaimer and waiver of all liability. 256 | 257 | Section 6 – Term and Termination. 258 | 259 | a. This Public License applies for the term of the Copyright and Similar Rights 260 | licensed here. However, if You fail to comply with this Public License, then 261 | Your rights under this Public License terminate automatically. 262 | 263 | b. Where Your right to use the Licensed Material has terminated under Section 264 | 6(a), it reinstates: 265 | 266 | 1. automatically as of the date the violation is cured, provided it is cured 267 | within 30 days of Your discovery of the violation; or 268 | 269 | 2. upon express reinstatement by the Licensor. 270 | 271 | c. For the avoidance of doubt, this Section 6(b) does not affect any right 272 | the Licensor may have to seek remedies for Your violations of this Public 273 | License. 274 | 275 | d. For the avoidance of doubt, the Licensor may also offer the Licensed Material 276 | under separate terms or conditions or stop distributing the Licensed Material 277 | at any time; however, doing so will not terminate this Public License. 278 | 279 | e. Sections 1, 5, 6, 7, and 8 survive termination of this Public License. 280 | 281 | Section 7 – Other Terms and Conditions. 282 | 283 | a. The Licensor shall not be bound by any additional or different terms or 284 | conditions communicated by You unless expressly agreed. 285 | 286 | b. Any arrangements, understandings, or agreements regarding the Licensed 287 | Material not stated herein are separate from and independent of the terms 288 | and conditions of this Public License. 289 | 290 | Section 8 – Interpretation. 291 | 292 | a. For the avoidance of doubt, this Public License does not, and shall not 293 | be interpreted to, reduce, limit, restrict, or impose conditions on any use 294 | of the Licensed Material that could lawfully be made without permission under 295 | this Public License. 296 | 297 | b. To the extent possible, if any provision of this Public License is deemed 298 | unenforceable, it shall be automatically reformed to the minimum extent necessary 299 | to make it enforceable. If the provision cannot be reformed, it shall be severed 300 | from this Public License without affecting the enforceability of the remaining 301 | terms and conditions. 302 | 303 | c. No term or condition of this Public License will be waived and no failure 304 | to comply consented to unless expressly agreed to by the Licensor. 305 | 306 | d. Nothing in this Public License constitutes or may be interpreted as a limitation 307 | upon, or waiver of, any privileges and immunities that apply to the Licensor 308 | or You, including from the legal processes of any jurisdiction or authority. 309 | 310 | Creative Commons is not a party to its public licenses. Notwithstanding, Creative 311 | Commons may elect to apply one of its public licenses to material it publishes 312 | and in those instances will be considered the "Licensor." The text of the 313 | Creative Commons public licenses is dedicated to the public domain under the 314 | CC0 Public Domain Dedication. Except for the limited purpose of indicating 315 | that material is shared under a Creative Commons public license or as otherwise 316 | permitted by the Creative Commons policies published at creativecommons.org/policies, 317 | Creative Commons does not authorize the use of the trademark "Creative Commons" 318 | or any other trademark or logo of Creative Commons without its prior written 319 | consent including, without limitation, in connection with any unauthorized 320 | modifications to any of its public licenses or any other arrangements, understandings, 321 | or agreements concerning use of licensed material. For the avoidance of doubt, 322 | this paragraph does not form part of the public licenses. 323 | 324 | Creative Commons may be contacted at creativecommons.org. 325 | -------------------------------------------------------------------------------- /adafruit_ov2640.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: MIT 5 | """ 6 | `adafruit_ov2640` 7 | ================================================================================ 8 | 9 | CircuitPython driver for OV2640 Camera. 10 | 11 | 12 | * Author(s): Jeff Epler 13 | 14 | Implementation Notes 15 | -------------------- 16 | 17 | **Hardware:** 18 | 19 | * `ESP32-S2 Kaluga Dev Kit featuring ESP32-S2 WROVER `_ 20 | 21 | **Software and Dependencies:** 22 | 23 | * Adafruit CircuitPython firmware for the supported boards: 24 | https://github.com/adafruit/circuitpython/releases 25 | 26 | * Adafruit's Bus Device library: https:# github.com/adafruit/Adafruit_CircuitPython_BusDevice 27 | """ 28 | 29 | # imports 30 | 31 | __version__ = "0.0.0+auto.0" 32 | __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_OV7670.git" 33 | 34 | import time 35 | 36 | import digitalio 37 | import imagecapture 38 | import pwmio 39 | from adafruit_bus_device.i2c_device import I2CDevice 40 | from micropython import const 41 | 42 | try: 43 | from typing import List, Optional, Type, Union 44 | 45 | from busio import I2C 46 | from circuitpython_typing import WriteableBuffer 47 | from microcontroller import Pin 48 | except ImportError: 49 | pass 50 | 51 | CTRLI = const(0x50) 52 | _R_BYPASS = const(0x05) 53 | _QS = const(0x44) 54 | _CTRLI = const(0x50) 55 | _HSIZE = const(0x51) 56 | _VSIZE = const(0x52) 57 | _XOFFL = const(0x53) 58 | _YOFFL = const(0x54) 59 | _VHYX = const(0x55) 60 | _DPRP = const(0x56) 61 | _TEST = const(0x57) 62 | _ZMOW = const(0x5A) 63 | _ZMOH = const(0x5B) 64 | _ZMHH = const(0x5C) 65 | _BPADDR = const(0x7C) 66 | _BPDATA = const(0x7D) 67 | _CTRL2 = const(0x86) 68 | _CTRL3 = const(0x87) 69 | _SIZEL = const(0x8C) 70 | _HSIZE8 = const(0xC0) 71 | _VSIZE8 = const(0xC1) 72 | _CTRL0 = const(0xC2) 73 | _CTRL1 = const(0xC3) 74 | _R_DVP_SP = const(0xD3) 75 | _IMAGE_MODE = const(0xDA) 76 | _RESET = const(0xE0) 77 | _MS_SP = const(0xF0) 78 | _SS_ID = const(0xF7) 79 | _SS_CTRL = const(0xF7) 80 | _MC_BIST = const(0xF9) 81 | _MC_AL = const(0xFA) 82 | _MC_AH = const(0xFB) 83 | _MC_D = const(0xFC) 84 | _P_CMD = const(0xFD) 85 | _P_STATUS = const(0xFE) 86 | _BANK_SEL = const(0xFF) 87 | 88 | _CTRLI_LP_DP = const(0x80) 89 | _CTRLI_ROUND = const(0x40) 90 | 91 | _CTRL0_AEC_EN = const(0x80) 92 | _CTRL0_AEC_SEL = const(0x40) 93 | _CTRL0_STAT_SEL = const(0x20) 94 | _CTRL0_VFIRST = const(0x10) 95 | _CTRL0_YUV422 = const(0x08) 96 | _CTRL0_YUV_EN = const(0x04) 97 | _CTRL0_RGB_EN = const(0x02) 98 | _CTRL0_RAW_EN = const(0x01) 99 | 100 | _CTRL2_DCW_EN = const(0x20) 101 | _CTRL2_SDE_EN = const(0x10) 102 | _CTRL2_UV_ADJ_EN = const(0x08) 103 | _CTRL2_UV_AVG_EN = const(0x04) 104 | _CTRL2_CMX_EN = const(0x01) 105 | 106 | _CTRL3_BPC_EN = const(0x80) 107 | _CTRL3_WPC_EN = const(0x40) 108 | 109 | _R_DVP_SP_AUTO_MODE = const(0x80) 110 | 111 | _R_BYPASS_DSP_EN = const(0x00) 112 | _R_BYPASS_DSP_BYPAS = const(0x01) 113 | 114 | OV2640_COLOR_RGB = 0 115 | OV2640_COLOR_YUV = 1 116 | OV2640_COLOR_JPEG = 2 117 | 118 | _IMAGE_MODE_Y8_DVP_EN = const(0x40) 119 | _IMAGE_MODE_JPEG_EN = const(0x10) 120 | _IMAGE_MODE_YUV422 = const(0x00) 121 | _IMAGE_MODE_RAW10 = const(0x04) 122 | _IMAGE_MODE_RGB565 = const(0x08) 123 | _IMAGE_MODE_HREF_VSYNC = const(0x02) 124 | _IMAGE_MODE_LBYTE_FIRST = const(0x01) 125 | 126 | _RESET_MICROC = const(0x40) 127 | _RESET_SCCB = const(0x20) 128 | _RESET_JPEG = const(0x10) 129 | _RESET_DVP = const(0x04) 130 | _RESET_IPU = const(0x02) 131 | _RESET_CIF = const(0x01) 132 | 133 | _MC_BIST_RESET = const(0x80) 134 | _MC_BIST_BOOT_ROM_SEL = const(0x40) 135 | _MC_BIST_12KB_SEL = const(0x20) 136 | _MC_BIST_12KB_MASK = const(0x30) 137 | _MC_BIST_512KB_SEL = const(0x08) 138 | _MC_BIST_512KB_MASK = const(0x0C) 139 | _MC_BIST_BUSY_BIT_R = const(0x02) 140 | _MC_BIST_MC_RES_ONE_SH_W = const(0x02) 141 | _MC_BIST_LAUNCH = const(0x01) 142 | 143 | 144 | _BANK_DSP = const(0) 145 | _BANK_SENSOR = const(1) 146 | 147 | # Sensor register bank FF=0x01 148 | _GAIN = const(0x00) 149 | _COM1 = const(0x03) 150 | _REG04 = const(0x04) 151 | _REG08 = const(0x08) 152 | _COM2 = const(0x09) 153 | _REG_PID = const(0x0A) 154 | _REG_VER = const(0x0B) 155 | _COM3 = const(0x0C) 156 | _COM4 = const(0x0D) 157 | _AEC = const(0x10) 158 | _CLKRC = const(0x11) 159 | _COM7 = const(0x12) 160 | _COM8 = const(0x13) 161 | _COM9 = const(0x14) # AGC gain ceiling 162 | _COM10 = const(0x15) 163 | _HSTART = const(0x17) 164 | _HSTOP = const(0x18) 165 | _VSTART = const(0x19) 166 | _VSTOP = const(0x1A) 167 | _MIDH = const(0x1C) 168 | _MIDL = const(0x1D) 169 | _AEW = const(0x24) 170 | _AEB = const(0x25) 171 | _VV = const(0x26) 172 | _REG2A = const(0x2A) 173 | _FRARL = const(0x2B) 174 | _ADDVSL = const(0x2D) 175 | _ADDVSH = const(0x2E) 176 | _YAVG = const(0x2F) 177 | _HSDY = const(0x30) 178 | _HEDY = const(0x31) 179 | _REG32 = const(0x32) 180 | _ARCOM2 = const(0x34) 181 | _REG45 = const(0x45) 182 | _FLL = const(0x46) 183 | _FLH = const(0x47) 184 | _COM19 = const(0x48) 185 | _ZOOMS = const(0x49) 186 | _COM22 = const(0x4B) 187 | _COM25 = const(0x4E) 188 | _BD50 = const(0x4F) 189 | _BD60 = const(0x50) 190 | _REG5D = const(0x5D) 191 | _REG5E = const(0x5E) 192 | _REG5F = const(0x5F) 193 | _REG60 = const(0x60) 194 | _HISTO_LOW = const(0x61) 195 | _HISTO_HIGH = const(0x62) 196 | 197 | _REG04_DEFAULT = const(0x28) 198 | _REG04_HFLIP_IMG = const(0x80) 199 | _REG04_VFLIP_IMG = const(0x40) 200 | _REG04_VREF_EN = const(0x10) 201 | _REG04_HREF_EN = const(0x08) 202 | _REG04_SET = lambda x: (_REG04_DEFAULT | x) 203 | 204 | _COM2_STDBY = const(0x10) 205 | _COM2_OUT_DRIVE_1x = const(0x00) 206 | _COM2_OUT_DRIVE_2x = const(0x01) 207 | _COM2_OUT_DRIVE_3x = const(0x02) 208 | _COM2_OUT_DRIVE_4x = const(0x03) 209 | 210 | _COM3_DEFAULT = const(0x38) 211 | _COM3_BAND_50Hz = const(0x04) 212 | _COM3_BAND_60Hz = const(0x00) 213 | _COM3_BAND_AUTO = const(0x02) 214 | _COM3_BAND_SET = lambda x: (_COM3_DEFAULT | x) 215 | 216 | _COM7_SRST = const(0x80) 217 | _COM7_RES_UXGA = const(0x00) # UXGA 218 | _COM7_RES_SVGA = const(0x40) # SVGA 219 | _COM7_RES_CIF = const(0x20) # CIF 220 | _COM7_ZOOM_EN = const(0x04) # Enable Zoom 221 | _COM7_COLOR_BAR = const(0x02) # Enable Color Bar Test 222 | 223 | _COM8_DEFAULT = const(0xC0) 224 | _COM8_BNDF_EN = const(0x20) # Enable Banding filter 225 | _COM8_AGC_EN = const(0x04) # AGC Auto/Manual control selection 226 | _COM8_AEC_EN = const(0x01) # Auto/Manual Exposure control 227 | _COM8_SET = lambda x: (_COM8_DEFAULT | x) 228 | 229 | _COM9_DEFAULT = const(0x08) 230 | _COM9_AGC_GAIN_2x = const(0x00) # AGC: 2x 231 | _COM9_AGC_GAIN_4x = const(0x01) # AGC: 4x 232 | _COM9_AGC_GAIN_8x = const(0x02) # AGC: 8x 233 | _COM9_AGC_GAIN_16x = const(0x03) # AGC: 16x 234 | _COM9_AGC_GAIN_32x = const(0x04) # AGC: 32x 235 | _COM9_AGC_GAIN_64x = const(0x05) # AGC: 64x 236 | _COM9_AGC_GAIN_128x = const(0x06) # AGC: 128x 237 | _COM9_AGC_SET = lambda x: (_COM9_DEFAULT | (x << 5)) 238 | 239 | _COM10_HREF_EN = const(0x80) # HSYNC changes to HREF 240 | _COM10_HSYNC_EN = const(0x40) # HREF changes to HSYNC 241 | _COM10_PCLK_FREE = const(0x20) # PCLK output option: free running PCLK 242 | _COM10_PCLK_EDGE = const(0x10) # Data is updated at the rising edge of PCLK 243 | _COM10_HREF_NEG = const(0x08) # HREF negative 244 | _COM10_VSYNC_NEG = const(0x02) # VSYNC negative 245 | _COM10_HSYNC_NEG = const(0x01) # HSYNC negative 246 | 247 | _CTRL1_AWB = const(0x08) # Enable AWB 248 | 249 | _VV_AGC_TH_SET = lambda h, l: ((h << 4) | (l & 0x0F)) 250 | 251 | _REG32_UXGA = const(0x36) 252 | _REG32_SVGA = const(0x09) 253 | _REG32_CIF = const(0x89) 254 | 255 | _CLKRC_2X = const(0x80) 256 | _CLKRC_2X_UXGA = const(0x01 | _CLKRC_2X) 257 | _CLKRC_2X_SVGA = _CLKRC_2X 258 | _CLKRC_2X_CIF = _CLKRC_2X 259 | 260 | _OV2640_MODE_CIF = const(0) 261 | _OV2640_MODE_SVGA = const(1) 262 | _OV2640_MODE_UXGA = const(2) 263 | 264 | OV2640_SIZE_96X96 = 0 # 96x96 265 | OV2640_SIZE_QQVGA = 1 # 160x120 266 | OV2640_SIZE_QCIF = 2 # 176x144 267 | OV2640_SIZE_HQVGA = 3 # 240x176 268 | OV2640_SIZE_240X240 = 4 # 240x240 269 | OV2640_SIZE_QVGA = 5 # 320x240 270 | OV2640_SIZE_CIF = 6 # 400x296 271 | OV2640_SIZE_HVGA = 7 # 480x320 272 | OV2640_SIZE_VGA = 8 # 640x480 273 | OV2640_SIZE_SVGA = 9 # 800x600 274 | OV2640_SIZE_XGA = 10 # 1024x768 275 | OV2640_SIZE_HD = 11 # 1280x720 276 | OV2640_SIZE_SXGA = 12 # 1280x1024 277 | OV2640_SIZE_UXGA = 13 # 1600x1200 278 | 279 | _ASPECT_RATIO_4X3 = const(0) 280 | _ASPECT_RATIO_3X2 = const(1) 281 | _ASPECT_RATIO_16X10 = const(2) 282 | _ASPECT_RATIO_5X3 = const(3) 283 | _ASPECT_RATIO_16X9 = const(4) 284 | _ASPECT_RATIO_21X9 = const(5) 285 | _ASPECT_RATIO_5X4 = const(6) 286 | _ASPECT_RATIO_1X1 = const(7) 287 | _ASPECT_RATIO_9X16 = const(8) 288 | 289 | _resolution_info = [ 290 | [96, 96, _ASPECT_RATIO_1X1], # 96x96 291 | [160, 120, _ASPECT_RATIO_4X3], # QQVGA 292 | [176, 144, _ASPECT_RATIO_5X4], # QCIF 293 | [240, 176, _ASPECT_RATIO_4X3], # HQVGA 294 | [240, 240, _ASPECT_RATIO_1X1], # 240x240 295 | [320, 240, _ASPECT_RATIO_4X3], # QVGA 296 | [400, 296, _ASPECT_RATIO_4X3], # CIF 297 | [480, 320, _ASPECT_RATIO_3X2], # HVGA 298 | [640, 480, _ASPECT_RATIO_4X3], # VGA 299 | [800, 600, _ASPECT_RATIO_4X3], # SVGA 300 | [1024, 768, _ASPECT_RATIO_4X3], # XGA 301 | [1280, 720, _ASPECT_RATIO_16X9], # HD 302 | [1280, 1024, _ASPECT_RATIO_5X4], # SXGA 303 | [1600, 1200, _ASPECT_RATIO_4X3], # UXGA 304 | ] 305 | 306 | _ratio_table = [ 307 | # ox, oy, mx, my 308 | [0, 0, 1600, 1200], # 4x3 309 | [8, 72, 1584, 1056], # 3x2 310 | [0, 100, 1600, 1000], # 16x10 311 | [0, 120, 1600, 960], # 5x3 312 | [0, 150, 1600, 900], # 16x9 313 | [2, 258, 1596, 684], # 21x9 314 | [50, 0, 1500, 1200], # 5x4 315 | [200, 0, 1200, 1200], # 1x1 316 | [462, 0, 676, 1200], # 9x16 317 | ] 318 | 319 | # 30fps@24MHz 320 | _ov2640_settings_cif = bytes( 321 | [ 322 | _BANK_SEL, 323 | _BANK_DSP, 324 | 0x2C, 325 | 0xFF, 326 | 0x2E, 327 | 0xDF, 328 | _BANK_SEL, 329 | _BANK_SENSOR, 330 | 0x3C, 331 | 0x32, 332 | _CLKRC, 333 | 0x01, 334 | _COM2, 335 | _COM2_OUT_DRIVE_3x, 336 | _REG04, 337 | _REG04_DEFAULT, 338 | _COM8, 339 | _COM8_DEFAULT | _COM8_BNDF_EN | _COM8_AGC_EN | _COM8_AEC_EN, 340 | _COM9, 341 | _COM9_AGC_SET(_COM9_AGC_GAIN_8x), 342 | 0x2C, 343 | 0x0C, 344 | 0x33, 345 | 0x78, 346 | 0x3A, 347 | 0x33, 348 | 0x3B, 349 | 0xFB, 350 | 0x3E, 351 | 0x00, 352 | 0x43, 353 | 0x11, 354 | 0x16, 355 | 0x10, 356 | 0x39, 357 | 0x92, 358 | 0x35, 359 | 0xDA, 360 | 0x22, 361 | 0x1A, 362 | 0x37, 363 | 0xC3, 364 | 0x23, 365 | 0x00, 366 | _ARCOM2, 367 | 0xC0, 368 | 0x06, 369 | 0x88, 370 | 0x07, 371 | 0xC0, 372 | _COM4, 373 | 0x87, 374 | 0x0E, 375 | 0x41, 376 | 0x4C, 377 | 0x00, 378 | 0x4A, 379 | 0x81, 380 | 0x21, 381 | 0x99, 382 | _AEW, 383 | 0x40, 384 | _AEB, 385 | 0x38, 386 | _VV, 387 | _VV_AGC_TH_SET(8, 2), 388 | 0x5C, 389 | 0x00, 390 | 0x63, 391 | 0x00, 392 | _HISTO_LOW, 393 | 0x70, 394 | _HISTO_HIGH, 395 | 0x80, 396 | 0x7C, 397 | 0x05, 398 | 0x20, 399 | 0x80, 400 | 0x28, 401 | 0x30, 402 | 0x6C, 403 | 0x00, 404 | 0x6D, 405 | 0x80, 406 | 0x6E, 407 | 0x00, 408 | 0x70, 409 | 0x02, 410 | 0x71, 411 | 0x94, 412 | 0x73, 413 | 0xC1, 414 | 0x3D, 415 | 0x34, 416 | 0x5A, 417 | 0x57, 418 | _BD50, 419 | 0xBB, 420 | _BD60, 421 | 0x9C, 422 | _COM7, 423 | _COM7_RES_CIF, 424 | _HSTART, 425 | 0x11, 426 | _HSTOP, 427 | 0x43, 428 | _VSTART, 429 | 0x00, 430 | _VSTOP, 431 | 0x25, 432 | _REG32, 433 | 0x89, 434 | 0x37, 435 | 0xC0, 436 | _BD50, 437 | 0xCA, 438 | _BD60, 439 | 0xA8, 440 | 0x6D, 441 | 0x00, 442 | 0x3D, 443 | 0x38, 444 | _BANK_SEL, 445 | _BANK_DSP, 446 | 0xE5, 447 | 0x7F, 448 | _MC_BIST, 449 | _MC_BIST_RESET | _MC_BIST_BOOT_ROM_SEL, 450 | 0x41, 451 | 0x24, 452 | _RESET, 453 | _RESET_JPEG | _RESET_DVP, 454 | 0x76, 455 | 0xFF, 456 | 0x33, 457 | 0xA0, 458 | 0x42, 459 | 0x20, 460 | 0x43, 461 | 0x18, 462 | 0x4C, 463 | 0x00, 464 | _CTRL3, 465 | _CTRL3_WPC_EN | 0x10, 466 | 0x88, 467 | 0x3F, 468 | 0xD7, 469 | 0x03, 470 | 0xD9, 471 | 0x10, 472 | _R_DVP_SP, 473 | _R_DVP_SP_AUTO_MODE | 0x02, 474 | 0xC8, 475 | 0x08, 476 | 0xC9, 477 | 0x80, 478 | _BPADDR, 479 | 0x00, 480 | _BPDATA, 481 | 0x00, 482 | _BPADDR, 483 | 0x03, 484 | _BPDATA, 485 | 0x48, 486 | _BPDATA, 487 | 0x48, 488 | _BPADDR, 489 | 0x08, 490 | _BPDATA, 491 | 0x20, 492 | _BPDATA, 493 | 0x10, 494 | _BPDATA, 495 | 0x0E, 496 | 0x90, 497 | 0x00, 498 | 0x91, 499 | 0x0E, 500 | 0x91, 501 | 0x1A, 502 | 0x91, 503 | 0x31, 504 | 0x91, 505 | 0x5A, 506 | 0x91, 507 | 0x69, 508 | 0x91, 509 | 0x75, 510 | 0x91, 511 | 0x7E, 512 | 0x91, 513 | 0x88, 514 | 0x91, 515 | 0x8F, 516 | 0x91, 517 | 0x96, 518 | 0x91, 519 | 0xA3, 520 | 0x91, 521 | 0xAF, 522 | 0x91, 523 | 0xC4, 524 | 0x91, 525 | 0xD7, 526 | 0x91, 527 | 0xE8, 528 | 0x91, 529 | 0x20, 530 | 0x92, 531 | 0x00, 532 | 0x93, 533 | 0x06, 534 | 0x93, 535 | 0xE3, 536 | 0x93, 537 | 0x05, 538 | 0x93, 539 | 0x05, 540 | 0x93, 541 | 0x00, 542 | 0x93, 543 | 0x04, 544 | 0x93, 545 | 0x00, 546 | 0x93, 547 | 0x00, 548 | 0x93, 549 | 0x00, 550 | 0x93, 551 | 0x00, 552 | 0x93, 553 | 0x00, 554 | 0x93, 555 | 0x00, 556 | 0x93, 557 | 0x00, 558 | 0x96, 559 | 0x00, 560 | 0x97, 561 | 0x08, 562 | 0x97, 563 | 0x19, 564 | 0x97, 565 | 0x02, 566 | 0x97, 567 | 0x0C, 568 | 0x97, 569 | 0x24, 570 | 0x97, 571 | 0x30, 572 | 0x97, 573 | 0x28, 574 | 0x97, 575 | 0x26, 576 | 0x97, 577 | 0x02, 578 | 0x97, 579 | 0x98, 580 | 0x97, 581 | 0x80, 582 | 0x97, 583 | 0x00, 584 | 0x97, 585 | 0x00, 586 | 0xA4, 587 | 0x00, 588 | 0xA8, 589 | 0x00, 590 | 0xC5, 591 | 0x11, 592 | 0xC6, 593 | 0x51, 594 | 0xBF, 595 | 0x80, 596 | 0xC7, 597 | 0x10, 598 | 0xB6, 599 | 0x66, 600 | 0xB8, 601 | 0xA5, 602 | 0xB7, 603 | 0x64, 604 | 0xB9, 605 | 0x7C, 606 | 0xB3, 607 | 0xAF, 608 | 0xB4, 609 | 0x97, 610 | 0xB5, 611 | 0xFF, 612 | 0xB0, 613 | 0xC5, 614 | 0xB1, 615 | 0x94, 616 | 0xB2, 617 | 0x0F, 618 | 0xC4, 619 | 0x5C, 620 | _CTRL1, 621 | 0xFD, 622 | 0x7F, 623 | 0x00, 624 | 0xE5, 625 | 0x1F, 626 | 0xE1, 627 | 0x67, 628 | 0xDD, 629 | 0x7F, 630 | _IMAGE_MODE, 631 | 0x00, 632 | _RESET, 633 | 0x00, 634 | _R_BYPASS, 635 | _R_BYPASS_DSP_EN, 636 | ] 637 | ) 638 | 639 | _ov2640_settings_to_cif = bytes( 640 | [ 641 | _BANK_SEL, 642 | _BANK_SENSOR, 643 | _COM7, 644 | _COM7_RES_CIF, 645 | # Set the sensor output window 646 | _COM1, 647 | 0x0A, 648 | _REG32, 649 | _REG32_CIF, 650 | _HSTART, 651 | 0x11, 652 | _HSTOP, 653 | 0x43, 654 | _VSTART, 655 | 0x00, 656 | _VSTOP, 657 | 0x25, 658 | # _CLKRC, 0x00, 659 | _BD50, 660 | 0xCA, 661 | _BD60, 662 | 0xA8, 663 | 0x5A, 664 | 0x23, 665 | 0x6D, 666 | 0x00, 667 | 0x3D, 668 | 0x38, 669 | 0x39, 670 | 0x92, 671 | 0x35, 672 | 0xDA, 673 | 0x22, 674 | 0x1A, 675 | 0x37, 676 | 0xC3, 677 | 0x23, 678 | 0x00, 679 | _ARCOM2, 680 | 0xC0, 681 | 0x06, 682 | 0x88, 683 | 0x07, 684 | 0xC0, 685 | _COM4, 686 | 0x87, 687 | 0x0E, 688 | 0x41, 689 | 0x4C, 690 | 0x00, 691 | _BANK_SEL, 692 | _BANK_DSP, 693 | _RESET, 694 | _RESET_DVP, 695 | # Set the sensor resolution (UXGA, SVGA, CIF) 696 | _HSIZE8, 697 | 0x32, 698 | _VSIZE8, 699 | 0x25, 700 | _SIZEL, 701 | 0x00, 702 | # Set the image window size >= output size 703 | _HSIZE, 704 | 0x64, 705 | _VSIZE, 706 | 0x4A, 707 | _XOFFL, 708 | 0x00, 709 | _YOFFL, 710 | 0x00, 711 | _VHYX, 712 | 0x00, 713 | _TEST, 714 | 0x00, 715 | _CTRL2, 716 | _CTRL2_DCW_EN | 0x1D, 717 | _CTRLI, 718 | _CTRLI_LP_DP | 0x00, 719 | # _R_DVP_SP, 0x08, 720 | ] 721 | ) 722 | 723 | _ov2640_settings_to_svga = bytes( 724 | [ 725 | _BANK_SEL, 726 | _BANK_SENSOR, 727 | _COM7, 728 | _COM7_RES_SVGA, 729 | # Set the sensor output window 730 | _COM1, 731 | 0x0A, 732 | _REG32, 733 | _REG32_SVGA, 734 | _HSTART, 735 | 0x11, 736 | _HSTOP, 737 | 0x43, 738 | _VSTART, 739 | 0x00, 740 | _VSTOP, 741 | 0x4B, 742 | # _CLKRC, 0x00, 743 | 0x37, 744 | 0xC0, 745 | _BD50, 746 | 0xCA, 747 | _BD60, 748 | 0xA8, 749 | 0x5A, 750 | 0x23, 751 | 0x6D, 752 | 0x00, 753 | 0x3D, 754 | 0x38, 755 | 0x39, 756 | 0x92, 757 | 0x35, 758 | 0xDA, 759 | 0x22, 760 | 0x1A, 761 | 0x37, 762 | 0xC3, 763 | 0x23, 764 | 0x00, 765 | _ARCOM2, 766 | 0xC0, 767 | 0x06, 768 | 0x88, 769 | 0x07, 770 | 0xC0, 771 | _COM4, 772 | 0x87, 773 | 0x0E, 774 | 0x41, 775 | 0x42, 776 | 0x03, 777 | 0x4C, 778 | 0x00, 779 | _BANK_SEL, 780 | _BANK_DSP, 781 | _RESET, 782 | _RESET_DVP, 783 | # Set the sensor resolution (UXGA, SVGA, CIF) 784 | _HSIZE8, 785 | 0x64, 786 | _VSIZE8, 787 | 0x4B, 788 | _SIZEL, 789 | 0x00, 790 | # Set the image window size >= output size 791 | _HSIZE, 792 | 0xC8, 793 | _VSIZE, 794 | 0x96, 795 | _XOFFL, 796 | 0x00, 797 | _YOFFL, 798 | 0x00, 799 | _VHYX, 800 | 0x00, 801 | _TEST, 802 | 0x00, 803 | _CTRL2, 804 | _CTRL2_DCW_EN | 0x1D, 805 | _CTRLI, 806 | _CTRLI_LP_DP | 0x00, 807 | # _R_DVP_SP, 0x08, 808 | ] 809 | ) 810 | 811 | _ov2640_settings_to_uxga = bytes( 812 | [ 813 | _BANK_SEL, 814 | _BANK_SENSOR, 815 | _COM7, 816 | _COM7_RES_UXGA, 817 | # Set the sensor output window 818 | _COM1, 819 | 0x0F, 820 | _REG32, 821 | _REG32_UXGA, 822 | _HSTART, 823 | 0x11, 824 | _HSTOP, 825 | 0x75, 826 | _VSTART, 827 | 0x01, 828 | _VSTOP, 829 | 0x97, 830 | # _CLKRC, 0x00, 831 | 0x3D, 832 | 0x34, 833 | _BD50, 834 | 0xBB, 835 | _BD60, 836 | 0x9C, 837 | 0x5A, 838 | 0x57, 839 | 0x6D, 840 | 0x80, 841 | 0x39, 842 | 0x82, 843 | 0x23, 844 | 0x00, 845 | 0x07, 846 | 0xC0, 847 | 0x4C, 848 | 0x00, 849 | 0x35, 850 | 0x88, 851 | 0x22, 852 | 0x0A, 853 | 0x37, 854 | 0x40, 855 | _ARCOM2, 856 | 0xA0, 857 | 0x06, 858 | 0x02, 859 | _COM4, 860 | 0xB7, 861 | 0x0E, 862 | 0x01, 863 | 0x42, 864 | 0x83, 865 | _BANK_SEL, 866 | _BANK_DSP, 867 | _RESET, 868 | _RESET_DVP, 869 | # Set the sensor resolution (UXGA, SVGA, CIF) 870 | _HSIZE8, 871 | 0xC8, 872 | _VSIZE8, 873 | 0x96, 874 | _SIZEL, 875 | 0x00, 876 | # Set the image window size >= output size 877 | _HSIZE, 878 | 0x90, 879 | _VSIZE, 880 | 0x2C, 881 | _XOFFL, 882 | 0x00, 883 | _YOFFL, 884 | 0x00, 885 | _VHYX, 886 | 0x88, 887 | _TEST, 888 | 0x00, 889 | _CTRL2, 890 | _CTRL2_DCW_EN | 0x1D, 891 | _CTRLI, 892 | 0x00, 893 | # _R_DVP_SP, 0x06, 894 | ] 895 | ) 896 | 897 | _ov2640_color_settings = { 898 | OV2640_COLOR_JPEG: bytes( 899 | [ 900 | _BANK_SEL, 901 | _BANK_DSP, 902 | _RESET, 903 | _RESET_JPEG | _RESET_DVP, 904 | _IMAGE_MODE, 905 | _IMAGE_MODE_JPEG_EN | _IMAGE_MODE_HREF_VSYNC, 906 | 0xD7, 907 | 0x03, 908 | 0xE1, 909 | 0x77, 910 | 0xE5, 911 | 0x1F, 912 | 0xD9, 913 | 0x10, 914 | 0xDF, 915 | 0x80, 916 | 0x33, 917 | 0x80, 918 | 0x3C, 919 | 0x10, 920 | 0xEB, 921 | 0x30, 922 | 0xDD, 923 | 0x7F, 924 | _RESET, 925 | 0x00, 926 | ] 927 | ), 928 | OV2640_COLOR_YUV: bytes( 929 | [ 930 | _BANK_SEL, 931 | _BANK_DSP, 932 | _RESET, 933 | _RESET_DVP, 934 | _IMAGE_MODE, 935 | _IMAGE_MODE_YUV422, 936 | 0xD7, 937 | 0x01, 938 | 0xE1, 939 | 0x67, 940 | _RESET, 941 | 0x00, 942 | ] 943 | ), 944 | OV2640_COLOR_RGB: bytes( 945 | [ 946 | _BANK_SEL, 947 | _BANK_DSP, 948 | _RESET, 949 | _RESET_DVP, 950 | _IMAGE_MODE, 951 | _IMAGE_MODE_RGB565, 952 | 0xD7, 953 | 0x03, 954 | 0xE1, 955 | 0x77, 956 | _RESET, 957 | 0x00, 958 | ] 959 | ), 960 | } 961 | 962 | 963 | class _RegBits: 964 | def __init__(self, bank: int, reg: int, shift: int, mask: int) -> None: 965 | self.bank = bank 966 | self.reg = reg 967 | self.shift = shift 968 | self.mask = mask 969 | 970 | def __get__(self, obj: "_SCCBCameraBase", objtype: Optional[Type] = None) -> int: 971 | reg_value = obj._read_bank_register(self.bank, self.reg) 972 | return (reg_value >> self.shift) & self.mask 973 | 974 | def __set__(self, obj: "_SCCBCameraBase", value: Union[bool, int]) -> None: 975 | if value & ~self.mask: 976 | raise ValueError(f"Value 0x{value:02x} does not fit in mask 0x{self.mask:02x}") 977 | reg_value = obj._read_bank_register(self.bank, self.reg) 978 | reg_value &= ~(self.mask << self.shift) 979 | reg_value |= value << self.shift 980 | obj._write_register(self.reg, reg_value) 981 | 982 | 983 | class _SCCBCameraBase: 984 | def __init__(self, i2c_bus: I2C, i2c_address: int) -> None: 985 | self._i2c_device = I2CDevice(i2c_bus, i2c_address) 986 | self._bank = None 987 | 988 | def _get_reg_bits(self, bank: int, reg: int, shift: int, mask: int) -> int: 989 | return (self._read_bank_register(bank, reg) >> shift) & mask 990 | 991 | def _set_reg_bits(self, bank: int, reg: int, shift: int, mask: int, value: int) -> None: 992 | reg_value = self._read_bank_register(bank, reg) 993 | reg_value &= ~(mask << shift) 994 | reg_value |= value << shift 995 | self._write_register(reg, reg_value) 996 | 997 | def _write_list(self, reg_list: List[int]) -> None: 998 | for i in range(0, len(reg_list), 2): 999 | self._write_register(reg_list[i], reg_list[i + 1]) 1000 | time.sleep(0.001) 1001 | 1002 | def _write_bank_register(self, bank: int, reg: int, value: int) -> None: 1003 | if self._bank != bank: 1004 | self._write_register(_BANK_SEL, bank) 1005 | self._write_register(reg, value) 1006 | 1007 | def _read_bank_register(self, bank: int, reg: int) -> int: 1008 | if self._bank != bank: 1009 | self._write_register(_BANK_SEL, bank) 1010 | result = self._read_register(reg) 1011 | return result 1012 | 1013 | def _write_register(self, reg: int, value: int) -> None: 1014 | if reg == _BANK_SEL: 1015 | if self._bank == value: 1016 | return 1017 | self._bank = value 1018 | # print(f"write_register {reg:02x} {value:02x}") 1019 | b = bytearray(2) 1020 | b[0] = reg 1021 | b[1] = value 1022 | with self._i2c_device as i2c: 1023 | i2c.write(b) 1024 | 1025 | def _read_register(self, reg: int) -> int: 1026 | b = bytearray(1) 1027 | b[0] = reg 1028 | with self._i2c_device as i2c: 1029 | i2c.write(b) 1030 | i2c.readinto(b) 1031 | return b[0] 1032 | 1033 | 1034 | class OV2640(_SCCBCameraBase): 1035 | """Library for the OV2640 digital camera""" 1036 | 1037 | test_pattern = _RegBits(_BANK_SENSOR, _COM7, 1, 1) 1038 | gain_ceiling = _RegBits(_BANK_SENSOR, _COM9, 5, 7) 1039 | bpc = _RegBits(_BANK_DSP, _CTRL3, 7, 1) 1040 | wpc = _RegBits(_BANK_DSP, _CTRL3, 6, 1) 1041 | lenc = _RegBits(_BANK_DSP, _CTRL1, 1, 1) 1042 | 1043 | def __init__( 1044 | self, 1045 | i2c_bus: I2C, 1046 | data_pins: Pin, 1047 | clock: Pin, 1048 | vsync: Pin, 1049 | href: Pin, 1050 | shutdown: Optional[Pin] = None, 1051 | reset: Optional[Pin] = None, 1052 | mclk: Optional[Pin] = None, 1053 | mclk_frequency: int = 20_000_000, 1054 | i2c_address: int = 0x30, 1055 | size: int = OV2640_SIZE_QQVGA, 1056 | ): 1057 | """ 1058 | Args: 1059 | i2c_bus (busio.I2C): The I2C bus used to configure the OV2640 1060 | data_pins (List[microcontroller.Pin]): A list of 8 data pins, in order. 1061 | clock (microcontroller.Pin): The pixel clock from the OV2640. 1062 | vsync (microcontroller.Pin): The vsync signal from the OV2640. 1063 | href (microcontroller.Pin): The href signal from the OV2640, \ 1064 | sometimes inaccurately called hsync. 1065 | shutdown (Optional[microcontroller.Pin]): If not None, the shutdown 1066 | signal to the camera, also called the powerdown or enable pin. 1067 | reset (Optional[microcontroller.Pin]): If not None, the reset signal 1068 | to the camera. 1069 | mclk (Optional[microcontroller.Pin]): The pin on which to create a 1070 | master clock signal, or None if the master clock signal is 1071 | already being generated. 1072 | mclk_frequency (int): The frequency of the master clock to generate, \ 1073 | ignored if mclk is None, requred if it is specified 1074 | i2c_address (int): The I2C address of the camera. 1075 | """ 1076 | 1077 | # Initialize the master clock 1078 | if mclk: 1079 | self._mclk_pwm = pwmio.PWMOut(mclk, frequency=mclk_frequency) 1080 | self._mclk_pwm.duty_cycle = 32768 1081 | else: 1082 | self._mclk_pwm = None 1083 | 1084 | if shutdown: 1085 | self._shutdown = digitalio.DigitalInOut(shutdown) 1086 | self._shutdown.switch_to_output(True) 1087 | time.sleep(0.1) 1088 | self._shutdown.switch_to_output(False) 1089 | time.sleep(0.3) 1090 | else: 1091 | self._shutdown = None 1092 | 1093 | if reset: 1094 | self._reset = digitalio.DigitalInOut(reset) 1095 | self._reset.switch_to_output(False) 1096 | time.sleep(0.1) 1097 | self._reset.switch_to_output(True) 1098 | time.sleep(0.1) 1099 | 1100 | super().__init__(i2c_bus, i2c_address) 1101 | 1102 | self._write_bank_register(_BANK_SENSOR, _COM7, _COM7_SRST) 1103 | time.sleep(0.001) 1104 | 1105 | self._write_list(_ov2640_settings_cif) 1106 | 1107 | self._colorspace = OV2640_COLOR_RGB 1108 | self._w = None 1109 | self._h = None 1110 | self._size = None 1111 | self._test_pattern = False 1112 | self.size = size 1113 | 1114 | self._flip_x = False 1115 | self._flip_y = False 1116 | 1117 | self.gain_ceiling = _COM9_AGC_GAIN_2x 1118 | self.bpc = False 1119 | self.wpc = True 1120 | self.lenc = True 1121 | 1122 | # self._sensor_init() 1123 | 1124 | self._imagecapture = imagecapture.ParallelImageCapture( 1125 | data_pins=data_pins, clock=clock, vsync=vsync, href=href 1126 | ) 1127 | 1128 | def capture(self, buf: WriteableBuffer) -> Optional[memoryview]: 1129 | """Capture an image into the buffer. 1130 | 1131 | Args: 1132 | buf (WriteableBuffer): A WritableBuffer to contain the \ 1133 | captured image. Note that this can be a ulab array or a displayio Bitmap. 1134 | """ 1135 | self._imagecapture.capture(buf) 1136 | if self.colorspace == OV2640_COLOR_JPEG: 1137 | eoi = buf.find(b"\xff\xd9") 1138 | if eoi != -1: 1139 | # terminate the JPEG data just after the EOI marker 1140 | return memoryview(buf)[: eoi + 2] 1141 | return None 1142 | 1143 | @property 1144 | def capture_buffer_size(self) -> int: 1145 | """Return the size of capture buffer to use with current resolution & colorspace settings""" 1146 | if self.colorspace == OV2640_COLOR_JPEG: 1147 | return self.width * self.height // 5 1148 | return self.width * self.height * 2 1149 | 1150 | @property 1151 | def mclk_frequency(self) -> Optional[int]: 1152 | """Get the actual frequency the generated mclk, or None""" 1153 | return self._mclk_pwm.frequency if self._mclk_pwm else None 1154 | 1155 | @property 1156 | def width(self) -> int: 1157 | """Get the image width in pixels. A buffer of 2*width*height bytes \ 1158 | stores a whole image.""" 1159 | return self._w 1160 | 1161 | @property 1162 | def height(self) -> int: 1163 | """Get the image height in pixels. A buffer of 2*width*height bytes \ 1164 | stores a whole image.""" 1165 | return self._h 1166 | 1167 | @property 1168 | def colorspace(self) -> bytes: 1169 | """Get or set the colorspace, one of the ``OV2640_COLOR_`` constants.""" 1170 | return self._colorspace 1171 | 1172 | @colorspace.setter 1173 | def colorspace(self, colorspace: bytes) -> None: 1174 | self._colorspace = colorspace 1175 | self._set_size_and_colorspace() 1176 | 1177 | def _set_colorspace(self) -> None: 1178 | colorspace = self._colorspace 1179 | settings = _ov2640_color_settings[colorspace] 1180 | 1181 | self._write_list(settings) 1182 | # written twice? 1183 | self._write_list(settings) 1184 | time.sleep(0.01) 1185 | 1186 | def deinit(self) -> None: 1187 | """Deinitialize the camera""" 1188 | self._imagecapture.deinit() 1189 | if self._mclk_pwm: 1190 | self._mclk_pwm.deinit() 1191 | if self._shutdown: 1192 | self._shutdown.deinit() 1193 | if self._reset: 1194 | self._reset.deinit() 1195 | 1196 | @property 1197 | def size(self) -> int: 1198 | """Get or set the captured image size, one of the ``OV2640_SIZE_`` constants.""" 1199 | return self._size 1200 | 1201 | def _set_size_and_colorspace(self) -> None: 1202 | size = self._size 1203 | width, height, ratio = _resolution_info[size] 1204 | offset_x, offset_y, max_x, max_y = _ratio_table[ratio] 1205 | mode = _OV2640_MODE_UXGA 1206 | if size <= OV2640_SIZE_CIF: 1207 | mode = _OV2640_MODE_CIF 1208 | max_x //= 4 1209 | max_y //= 4 1210 | offset_x //= 4 1211 | offset_y //= 4 1212 | max_y = min(max_y, 296) 1213 | 1214 | elif size <= OV2640_SIZE_SVGA: 1215 | mode = _OV2640_MODE_SVGA 1216 | max_x //= 2 1217 | max_y //= 2 1218 | offset_x //= 2 1219 | offset_y //= 2 1220 | 1221 | self._set_window(mode, offset_x, offset_y, max_x, max_y, width, height) 1222 | 1223 | @size.setter 1224 | def size(self, size: int) -> None: 1225 | self._size = size 1226 | self._set_size_and_colorspace() 1227 | 1228 | def _set_flip(self) -> None: 1229 | bits = 0 1230 | if self._flip_x: 1231 | bits |= _REG04_HFLIP_IMG 1232 | if self._flip_y: 1233 | bits |= _REG04_VFLIP_IMG | _REG04_VREF_EN 1234 | self._write_bank_register(_BANK_SENSOR, _REG04, _REG04_SET(bits)) 1235 | 1236 | @property 1237 | def flip_x(self) -> bool: 1238 | """Get or set the X-flip flag""" 1239 | return self._flip_x 1240 | 1241 | @flip_x.setter 1242 | def flip_x(self, value: bool) -> None: 1243 | self._flip_x = bool(value) 1244 | self._set_flip() 1245 | 1246 | @property 1247 | def flip_y(self) -> bool: 1248 | """Get or set the Y-flip flag""" 1249 | return self._flip_y 1250 | 1251 | @flip_y.setter 1252 | def flip_y(self, value: bool) -> None: 1253 | self._flip_y = bool(value) 1254 | self._set_flip() 1255 | 1256 | @property 1257 | def product_id(self) -> int: 1258 | """Get the product id (PID) register. The expected value is 0x26.""" 1259 | return self._read_bank_register(_BANK_SENSOR, _REG_PID) 1260 | 1261 | @property 1262 | def product_version(self) -> int: 1263 | """Get the version (VER) register. The expected value is 0x41.""" 1264 | return self._read_bank_register(_BANK_SENSOR, _REG_VER) 1265 | 1266 | def _set_window( 1267 | self, 1268 | mode: int, 1269 | offset_x: int, 1270 | offset_y: int, 1271 | max_x: int, 1272 | max_y: int, 1273 | width: int, 1274 | height: int, 1275 | ) -> None: 1276 | self._w = width 1277 | self._h = height 1278 | 1279 | max_x //= 4 1280 | max_y //= 4 1281 | width //= 4 1282 | height //= 4 1283 | 1284 | win_regs = [ 1285 | _BANK_SEL, 1286 | _BANK_DSP, 1287 | _HSIZE, 1288 | max_x & 0xFF, 1289 | _VSIZE, 1290 | max_y & 0xFF, 1291 | _XOFFL, 1292 | offset_x & 0xFF, 1293 | _YOFFL, 1294 | offset_y & 0xFF, 1295 | _VHYX, 1296 | ((max_y >> 1) & 0x80) 1297 | | ((offset_y >> 4) & 0x70) 1298 | | ((max_x >> 5) & 0x08) 1299 | | ((offset_y >> 8) & 0x07), 1300 | _TEST, 1301 | (max_x >> 2) & 0x80, 1302 | _ZMOW, 1303 | (width) & 0xFF, 1304 | _ZMOH, 1305 | (height) & 0xFF, 1306 | _ZMHH, 1307 | ((height >> 6) & 0x04) | ((width >> 8) & 0x03), 1308 | ] 1309 | 1310 | pclk_auto = 0 1311 | pclk_div = 8 1312 | clk_2x = 0 1313 | clk_div = 0 1314 | 1315 | if self._colorspace != OV2640_COLOR_JPEG: 1316 | pclk_auto = 1 1317 | clk_div = 7 1318 | 1319 | if mode == _OV2640_MODE_CIF: 1320 | regs = _ov2640_settings_to_cif 1321 | if self._colorspace != OV2640_COLOR_JPEG: 1322 | clk_div = 3 1323 | elif mode == _OV2640_MODE_SVGA: 1324 | regs = _ov2640_settings_to_svga 1325 | else: 1326 | regs = _ov2640_settings_to_uxga 1327 | pclk_div = 12 1328 | 1329 | clk = clk_div | (clk_2x << 7) 1330 | pclk = pclk_div | (pclk_auto << 7) 1331 | 1332 | self._write_bank_register(_BANK_DSP, _R_BYPASS, _R_BYPASS_DSP_BYPAS) 1333 | self._write_list(regs) 1334 | self._write_list(win_regs) 1335 | self._write_bank_register(_BANK_SENSOR, _CLKRC, clk) 1336 | self._write_bank_register(_BANK_DSP, _R_DVP_SP, pclk) 1337 | self._write_register(_R_BYPASS, _R_BYPASS_DSP_EN) 1338 | time.sleep(0.01) 1339 | 1340 | # Reestablish colorspace 1341 | self._set_colorspace() 1342 | 1343 | # Reestablish test pattern 1344 | if self._test_pattern: 1345 | self.test_pattern = self._test_pattern 1346 | 1347 | @property 1348 | def exposure(self) -> int: 1349 | """The exposure level of the sensor""" 1350 | aec_9_2 = self._get_reg_bits(_BANK_SENSOR, _AEC, 0, 0xFF) 1351 | aec_15_10 = self._get_reg_bits(_BANK_SENSOR, _REG45, 0, 0b111111) 1352 | aec_1_0 = self._get_reg_bits(_BANK_SENSOR, _REG04, 0, 0b11) 1353 | 1354 | return aec_1_0 | (aec_9_2 << 2) | (aec_15_10 << 10) 1355 | 1356 | @exposure.setter 1357 | def exposure(self, exposure: int) -> None: 1358 | aec_1_0 = exposure & 0x11 1359 | aec_9_2 = (exposure >> 2) & 0b11111111 1360 | aec_15_10 = exposure >> 10 1361 | 1362 | self._set_reg_bits(_BANK_SENSOR, _AEC, 0, 0xFF, aec_9_2) 1363 | self._set_reg_bits(_BANK_SENSOR, _REG45, 0, 0b111111, aec_15_10) 1364 | self._set_reg_bits(_BANK_SENSOR, _REG04, 0, 0b11, aec_1_0) 1365 | --------------------------------------------------------------------------------