adafruit-circuitpython-ov5647 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,138 @@
1
+ Metadata-Version: 2.4
2
+ Name: adafruit-circuitpython-ov5647
3
+ Version: 1.0.0
4
+ Summary: CircuitPython driver for the OV5647 camera module
5
+ Author-email: Adafruit Industries <circuitpython@adafruit.com>
6
+ License-Expression: GPL-2.0-only
7
+ Project-URL: Homepage, https://github.com/adafruit/Adafruit_CircuitPython_OV5647
8
+ Keywords: adafruit,blinka,circuitpython,micropython,ov5647
9
+ Classifier: Intended Audience :: Developers
10
+ Classifier: Topic :: Software Development :: Libraries
11
+ Classifier: Topic :: Software Development :: Embedded Systems
12
+ Classifier: Topic :: System :: Hardware
13
+ Classifier: Programming Language :: Python :: 3
14
+ Description-Content-Type: text/x-rst
15
+ License-File: LICENSE
16
+ Requires-Dist: Adafruit-Blinka
17
+ Requires-Dist: adafruit-circuitpython-busdevice
18
+ Provides-Extra: optional
19
+ Dynamic: license-file
20
+
21
+ Introduction
22
+ ============
23
+
24
+
25
+ .. image:: https://readthedocs.org/projects/adafruit-circuitpython-ov5647/badge/?version=latest
26
+ :target: https://docs.circuitpython.org/projects/ov5647/en/latest/
27
+ :alt: Documentation Status
28
+
29
+
30
+ .. image:: https://raw.githubusercontent.com/adafruit/Adafruit_CircuitPython_Bundle/main/badges/adafruit_discord.svg
31
+ :target: https://adafru.it/discord
32
+ :alt: Discord
33
+
34
+
35
+ .. image:: https://github.com/adafruit/Adafruit_CircuitPython_OV5647/workflows/Build%20CI/badge.svg
36
+ :target: https://github.com/adafruit/Adafruit_CircuitPython_OV5647/actions
37
+ :alt: Build Status
38
+
39
+
40
+ .. image:: https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json
41
+ :target: https://github.com/astral-sh/ruff
42
+ :alt: Code Style: Ruff
43
+
44
+ CircuitPython driver for the OV5647 camera module
45
+
46
+
47
+ Dependencies
48
+ =============
49
+ This driver depends on:
50
+
51
+ * `Adafruit CircuitPython <https://github.com/adafruit/circuitpython>`_
52
+ * `Bus Device <https://github.com/adafruit/Adafruit_CircuitPython_BusDevice>`_
53
+
54
+ Please ensure all dependencies are available on the CircuitPython filesystem.
55
+ This is easily achieved by downloading
56
+ `the Adafruit library and driver bundle <https://circuitpython.org/libraries>`_
57
+ or individual libraries can be installed using
58
+ `circup <https://github.com/adafruit/circup>`_.
59
+
60
+
61
+ Installing from PyPI
62
+ =====================
63
+
64
+ On supported GNU/Linux systems like the Raspberry Pi, you can install the driver locally `from
65
+ PyPI <https://pypi.org/project/adafruit-circuitpython-ov5647/>`_.
66
+ To install for current user:
67
+
68
+ .. code-block:: shell
69
+
70
+ pip3 install adafruit-circuitpython-ov5647
71
+
72
+ To install system-wide (this may be required in some cases):
73
+
74
+ .. code-block:: shell
75
+
76
+ sudo pip3 install adafruit-circuitpython-ov5647
77
+
78
+ To install in a virtual environment in your current project:
79
+
80
+ .. code-block:: shell
81
+
82
+ mkdir project-name && cd project-name
83
+ python3 -m venv .venv
84
+ source .env/bin/activate
85
+ pip3 install adafruit-circuitpython-ov5647
86
+
87
+ Installing to a Connected CircuitPython Device with Circup
88
+ ==========================================================
89
+
90
+ Make sure that you have ``circup`` installed in your Python environment.
91
+ Install it with the following command if necessary:
92
+
93
+ .. code-block:: shell
94
+
95
+ pip3 install circup
96
+
97
+ With ``circup`` installed and your CircuitPython device connected use the
98
+ following command to install:
99
+
100
+ .. code-block:: shell
101
+
102
+ circup install adafruit_ov5647
103
+
104
+ Or the following command to update an existing version:
105
+
106
+ .. code-block:: shell
107
+
108
+ circup update
109
+
110
+ Usage Example
111
+ =============
112
+
113
+ .. code-block:: python
114
+
115
+ import board
116
+ import mipicsi
117
+ import videocore
118
+ import adafruit_ov5647
119
+
120
+ sensor = adafruit_ov5647.OV5647(board.I2C(), reset_pin=board.CAMERA_RESET)
121
+ camera = mipicsi.Camera(sensor, output_format=mipicsi.PixelFormat.RGB565)
122
+ frame = bytearray(sensor.width * sensor.height * 2)
123
+ video = videocore.VideoBuffer(frame)
124
+ video.play(camera)
125
+
126
+ Documentation
127
+ =============
128
+ API documentation for this library can be found on `Read the Docs <https://docs.circuitpython.org/projects/ov5647/en/latest/>`_.
129
+
130
+ For information on building library documentation, please check out
131
+ `this guide <https://learn.adafruit.com/creating-and-sharing-a-circuitpython-library/sharing-our-docs-on-readthedocs#sphinx-5-1>`_.
132
+
133
+ Contributing
134
+ ============
135
+
136
+ Contributions are welcome! Please read our `Code of Conduct
137
+ <https://github.com/adafruit/Adafruit_CircuitPython_OV5647/blob/HEAD/CODE_OF_CONDUCT.md>`_
138
+ before contributing to help this project stay welcoming.
@@ -0,0 +1,6 @@
1
+ adafruit_ov5647.py,sha256=i60c5hWBQ_Eiq3Ejq4scsyILseOviVQQ-2kR0UkqVvs,19241
2
+ adafruit_circuitpython_ov5647-1.0.0.dist-info/licenses/LICENSE,sha256=qvE1Ry-BxbSg3Kk2flu16XUAMrW-vlRCs25MCkdDDfM,17337
3
+ adafruit_circuitpython_ov5647-1.0.0.dist-info/METADATA,sha256=vSPQT51x1kIpj93rQjbPZH-tiFnGdXTZNZKmFn_H46U,4398
4
+ adafruit_circuitpython_ov5647-1.0.0.dist-info/WHEEL,sha256=mOdXR9C3wMVov7cSY8PY-XCzo3SmmWCHvGQr5csJoao,91
5
+ adafruit_circuitpython_ov5647-1.0.0.dist-info/top_level.txt,sha256=rVRyIyVd-dX7ik5tlyyiPxWXi_94ulyQXhVR0x-I5v0,16
6
+ adafruit_circuitpython_ov5647-1.0.0.dist-info/RECORD,,
@@ -0,0 +1,5 @@
1
+ Wheel-Version: 1.0
2
+ Generator: setuptools (84.2.1)
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
5
+
@@ -0,0 +1,117 @@
1
+ GNU GENERAL PUBLIC LICENSE
2
+ Version 2, June 1991
3
+
4
+ Copyright (C) 1989, 1991 Free Software Foundation, Inc.
5
+ 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
6
+
7
+ Everyone is permitted to copy and distribute verbatim copies of this license document, but changing it is not allowed.
8
+
9
+ Preamble
10
+
11
+ The licenses for most software are designed to take away your freedom to share and change it. By contrast, the GNU General Public License is intended to guarantee your freedom to share and change free software--to make sure the software is free for all its users. This General Public License applies to most of the Free Software Foundation's software and to any other program whose authors commit to using it. (Some other Free Software Foundation software is covered by the GNU Lesser General Public License instead.) You can apply it to your programs, too.
12
+
13
+ When we speak of free software, we are referring to freedom, not price. Our General Public Licenses are designed to make sure that you have the freedom to distribute copies of free software (and charge for this service if you wish), that you receive source code or can get it if you want it, that you can change the software or use pieces of it in new free programs; and that you know you can do these things.
14
+
15
+ To protect your rights, we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights. These restrictions translate to certain responsibilities for you if you distribute copies of the software, or if you modify it.
16
+
17
+ For example, if you distribute copies of such a program, whether gratis or for a fee, you must give the recipients all the rights that you have. You must make sure that they, too, receive or can get the source code. And you must show them these terms so they know their rights.
18
+
19
+ We protect your rights with two steps: (1) copyright the software, and (2) offer you this license which gives you legal permission to copy, distribute and/or modify the software.
20
+
21
+ Also, for each author's protection and ours, we want to make certain that everyone understands that there is no warranty for this free software. If the software is modified by someone else and passed on, we want its recipients to know that what they have is not the original, so that any problems introduced by others will not reflect on the original authors' reputations.
22
+
23
+ Finally, any free program is threatened constantly by software patents. We wish to avoid the danger that redistributors of a free program will individually obtain patent licenses, in effect making the program proprietary. To prevent this, we have made it clear that any patent must be licensed for everyone's free use or not licensed at all.
24
+
25
+ The precise terms and conditions for copying, distribution and modification follow.
26
+
27
+ TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
28
+
29
+ 0. This License applies to any program or other work which contains a notice placed by the copyright holder saying it may be distributed under the terms of this General Public License. The "Program", below, refers to any such program or work, and a "work based on the Program" means either the Program or any derivative work under copyright law: that is to say, a work containing the Program or a portion of it, either verbatim or with modifications and/or translated into another language. (Hereinafter, translation is included without limitation in the term "modification".) Each licensee is addressed as "you".
30
+
31
+ Activities other than copying, distribution and modification are not covered by this License; they are outside its scope. The act of running the Program is not restricted, and the output from the Program is covered only if its contents constitute a work based on the Program (independent of having been made by running the Program). Whether that is true depends on what the Program does.
32
+
33
+ 1. You may copy and distribute verbatim copies of the Program's source code as you receive it, in any medium, provided that you conspicuously and appropriately publish on each copy an appropriate copyright notice and disclaimer of warranty; keep intact all the notices that refer to this License and to the absence of any warranty; and give any other recipients of the Program a copy of this License along with the Program.
34
+
35
+ You may charge a fee for the physical act of transferring a copy, and you may at your option offer warranty protection in exchange for a fee.
36
+
37
+ 2. You may modify your copy or copies of the Program or any portion of it, thus forming a work based on the Program, and copy and distribute such modifications or work under the terms of Section 1 above, provided that you also meet all of these conditions:
38
+
39
+ a) You must cause the modified files to carry prominent notices stating that you changed the files and the date of any change.
40
+
41
+ b) You must cause any work that you distribute or publish, that in whole or in part contains or is derived from the Program or any part thereof, to be licensed as a whole at no charge to all third parties under the terms of this License.
42
+
43
+ c) If the modified program normally reads commands interactively when run, you must cause it, when started running for such interactive use in the most ordinary way, to print or display an announcement including an appropriate copyright notice and a notice that there is no warranty (or else, saying that you provide a warranty) and that users may redistribute the program under these conditions, and telling the user how to view a copy of this License. (Exception: if the Program itself is interactive but does not normally print such an announcement, your work based on the Program is not required to print an announcement.)
44
+
45
+ These requirements apply to the modified work as a whole. If identifiable sections of that work are not derived from the Program, and can be reasonably considered independent and separate works in themselves, then this License, and its terms, do not apply to those sections when you distribute them as separate works. But when you distribute the same sections as part of a whole which is a work based on the Program, the distribution of the whole must be on the terms of this License, whose permissions for other licensees extend to the entire whole, and thus to each and every part regardless of who wrote it.
46
+
47
+ Thus, it is not the intent of this section to claim rights or contest your rights to work written entirely by you; rather, the intent is to exercise the right to control the distribution of derivative or collective works based on the Program.
48
+
49
+ In addition, mere aggregation of another work not based on the Program with the Program (or with a work based on the Program) on a volume of a storage or distribution medium does not bring the other work under the scope of this License.
50
+
51
+ 3. You may copy and distribute the Program (or a work based on it, under Section 2) in object code or executable form under the terms of Sections 1 and 2 above provided that you also do one of the following:
52
+
53
+ a) Accompany it with the complete corresponding machine-readable source code, which must be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or,
54
+
55
+ b) Accompany it with a written offer, valid for at least three years, to give any third party, for a charge no more than your cost of physically performing source distribution, a complete machine-readable copy of the corresponding source code, to be distributed under the terms of Sections 1 and 2 above on a medium customarily used for software interchange; or,
56
+
57
+ c) Accompany it with the information you received as to the offer to distribute corresponding source code. (This alternative is allowed only for noncommercial distribution and only if you received the program in object code or executable form with such an offer, in accord with Subsection b above.)
58
+
59
+ The source code for a work means the preferred form of the work for making modifications to it. For an executable work, complete source code means all the source code for all modules it contains, plus any associated interface definition files, plus the scripts used to control compilation and installation of the executable. However, as a special exception, the source code distributed need not include anything that is normally distributed (in either source or binary form) with the major components (compiler, kernel, and so on) of the operating system on which the executable runs, unless that component itself accompanies the executable.
60
+
61
+ If distribution of executable or object code is made by offering access to copy from a designated place, then offering equivalent access to copy the source code from the same place counts as distribution of the source code, even though third parties are not compelled to copy the source along with the object code.
62
+
63
+ 4. You may not copy, modify, sublicense, or distribute the Program except as expressly provided under this License. Any attempt otherwise to copy, modify, sublicense or distribute the Program is void, and will automatically terminate your rights under this License. However, parties who have received copies, or rights, from you under this License will not have their licenses terminated so long as such parties remain in full compliance.
64
+
65
+ 5. You are not required to accept this License, since you have not signed it. However, nothing else grants you permission to modify or distribute the Program or its derivative works. These actions are prohibited by law if you do not accept this License. Therefore, by modifying or distributing the Program (or any work based on the Program), you indicate your acceptance of this License to do so, and all its terms and conditions for copying, distributing or modifying the Program or works based on it.
66
+
67
+ 6. Each time you redistribute the Program (or any work based on the Program), the recipient automatically receives a license from the original licensor to copy, distribute or modify the Program subject to these terms and conditions. You may not impose any further restrictions on the recipients' exercise of the rights granted herein. You are not responsible for enforcing compliance by third parties to this License.
68
+
69
+ 7. If, as a consequence of a court judgment or allegation of patent infringement or for any other reason (not limited to patent issues), conditions are imposed on you (whether by court order, agreement or otherwise) that contradict the conditions of this License, they do not excuse you from the conditions of this License. If you cannot distribute so as to satisfy simultaneously your obligations under this License and any other pertinent obligations, then as a consequence you may not distribute the Program at all. For example, if a patent license would not permit royalty-free redistribution of the Program by all those who receive copies directly or indirectly through you, then the only way you could satisfy both it and this License would be to refrain entirely from distribution of the Program.
70
+
71
+ If any portion of this section is held invalid or unenforceable under any particular circumstance, the balance of the section is intended to apply and the section as a whole is intended to apply in other circumstances.
72
+
73
+ It is not the purpose of this section to induce you to infringe any patents or other property right claims or to contest validity of any such claims; this section has the sole purpose of protecting the integrity of the free software distribution system, which is implemented by public license practices. Many people have made generous contributions to the wide range of software distributed through that system in reliance on consistent application of that system; it is up to the author/donor to decide if he or she is willing to distribute software through any other system and a licensee cannot impose that choice.
74
+
75
+ This section is intended to make thoroughly clear what is believed to be a consequence of the rest of this License.
76
+
77
+ 8. If the distribution and/or use of the Program is restricted in certain countries either by patents or by copyrighted interfaces, the original copyright holder who places the Program under this License may add an explicit geographical distribution limitation excluding those countries, so that distribution is permitted only in or among countries not thus excluded. In such case, this License incorporates the limitation as if written in the body of this License.
78
+
79
+ 9. The Free Software Foundation may publish revised and/or new versions of the General Public License from time to time. Such new versions will be similar in spirit to the present version, but may differ in detail to address new problems or concerns.
80
+
81
+ Each version is given a distinguishing version number. If the Program specifies a version number of this License which applies to it and "any later version", you have the option of following the terms and conditions either of that version or of any later version published by the Free Software Foundation. If the Program does not specify a version number of this License, you may choose any version ever published by the Free Software Foundation.
82
+
83
+ 10. If you wish to incorporate parts of the Program into other free programs whose distribution conditions are different, write to the author to ask for permission. For software which is copyrighted by the Free Software Foundation, write to the Free Software Foundation; we sometimes make exceptions for this. Our decision will be guided by the two goals of preserving the free status of all derivatives of our free software and of promoting the sharing and reuse of software generally.
84
+
85
+ NO WARRANTY
86
+
87
+ 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
88
+
89
+ 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
90
+
91
+ END OF TERMS AND CONDITIONS
92
+
93
+ How to Apply These Terms to Your New Programs
94
+
95
+ If you develop a new program, and you want it to be of the greatest possible use to the public, the best way to achieve this is to make it free software which everyone can redistribute and change under these terms.
96
+
97
+ To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively convey the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found.
98
+
99
+ one line to give the program's name and an idea of what it does. Copyright (C) yyyy name of author
100
+
101
+ This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
102
+
103
+ This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
104
+
105
+ You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. Also add information on how to contact you by electronic and paper mail.
106
+
107
+ If the program is interactive, make it output a short notice like this when it starts in an interactive mode:
108
+
109
+ Gnomovision version 69, Copyright (C) year name of author Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. This is free software, and you are welcome to redistribute it under certain conditions; type `show c' for details.
110
+
111
+ The hypothetical commands `show w' and `show c' should show the appropriate parts of the General Public License. Of course, the commands you use may be called something other than `show w' and `show c'; they could even be mouse-clicks or menu items--whatever suits your program.
112
+
113
+ You should also get your employer (if you work as a programmer) or your school, if any, to sign a "copyright disclaimer" for the program, if necessary. Here is a sample; alter the names:
114
+
115
+ Yoyodyne, Inc., hereby disclaims all copyright interest in the program `Gnomovision' (which makes passes at compilers) written by James Hacker.
116
+
117
+ signature of Ty Coon, 1 April 1989 Ty Coon, President of Vice
@@ -0,0 +1 @@
1
+ adafruit_ov5647
adafruit_ov5647.py ADDED
@@ -0,0 +1,649 @@
1
+ # SPDX-FileCopyrightText: Copyright (c) 2026 Liz Clark for Adafruit Industries
2
+ # SPDX-FileCopyrightText: Copyright (C) 2016, Synopsys, Inc.
3
+ # SPDX-FileCopyrightText: Copyright (C) 2011 Sylwester Nawrocki
4
+ # SPDX-FileCopyrightText: Copyright (C) 2006-7 Jonathan Corbet
5
+ #
6
+ # SPDX-License-Identifier: GPL-2.0-only
7
+ """
8
+ `adafruit_ov5647`
9
+ ================================================================================
10
+
11
+ CircuitPython driver for the OV5647 camera module, as used on the
12
+ Raspberry Pi Camera v1.3. For use with `mipicsi.Camera`.
13
+
14
+ * Author(s): Liz Clark
15
+
16
+ Implementation Notes
17
+ --------------------
18
+
19
+ **Software and Dependencies:**
20
+
21
+ * Adafruit CircuitPython firmware for the supported boards:
22
+ https://circuitpython.org/downloads
23
+
24
+
25
+ * Adafruit's Bus Device library: https://github.com/adafruit/Adafruit_CircuitPython_BusDevice
26
+
27
+ **Notes:**
28
+
29
+ * Register tables, power-up, mode and stream on/off sequences, and the
30
+ controls follow the Linux kernel driver drivers/media/i2c/ov5647.c
31
+ """
32
+
33
+ import time
34
+
35
+ import mipicsi
36
+ from adafruit_bus_device.i2c_device import I2CDevice
37
+ from digitalio import DigitalInOut
38
+ from micropython import const
39
+
40
+ try:
41
+ from typing import Optional
42
+
43
+ import busio
44
+ from microcontroller import Pin
45
+ except ImportError:
46
+ pass
47
+
48
+ __version__ = "1.0.0"
49
+ __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_OV5647.git"
50
+
51
+ _I2C_ADDRESS = const(0x36)
52
+ _CHIP_ID = const(0x5647)
53
+
54
+ _SW_STANDBY = const(0x0100)
55
+ _SW_RESET = const(0x0103)
56
+ _CHIP_ID_HIGH = const(0x300A)
57
+ _CHIP_ID_LOW = const(0x300B)
58
+ _PAD_OUT = const(0x300D)
59
+ _EXPOSURE = const(0x3500) # 20 bits; the low 4 are fractions of a line
60
+ _AEC_AGC = const(0x3503) # bit 0: manual exposure, bit 1: manual gain
61
+ _GAIN = const(0x350A) # 10 bits
62
+ _HTS = const(0x380C) # 16 bits
63
+ _VTS = const(0x380E) # 16 bits
64
+ _OUTPUT_WIDTH = const(0x3808) # 16 bits
65
+ _OUTPUT_HEIGHT = const(0x380A) # 16 bits
66
+ _X_OFFSET = const(0x3810) # 16 bits
67
+ _Y_OFFSET = const(0x3812) # 16 bits
68
+ _TIMING_TC_V = const(0x3820) # bit 1: vertical flip
69
+ _TIMING_TC_H = const(0x3821) # bit 1: in-built horizontal mirror
70
+ _FRAME_OFF_NUMBER = const(0x4202)
71
+ _MIPI_CTRL00 = const(0x4800)
72
+ _MIPI_CTRL14 = const(0x4814) # bits 7:6: virtual channel
73
+ _ISPCTRL3D = const(0x503D) # test pattern
74
+
75
+ # MIPI_CTRL00 bits
76
+ _CLOCK_LANE_GATE = const(0x20)
77
+ _LINE_SYNC_ENABLE = const(0x10)
78
+ _BUS_IDLE = const(0x04)
79
+ _CLOCK_LANE_DISABLE = const(0x01)
80
+
81
+ # Register tables from the Linux ov5647 driver, unchanged.
82
+ _REGS_OE_ENABLE = (
83
+ (0x3000, 0x0F),
84
+ (0x3001, 0xFF),
85
+ (0x3002, 0xE4),
86
+ )
87
+
88
+ _REGS_OE_DISABLE = (
89
+ (0x3000, 0x00),
90
+ (0x3001, 0x00),
91
+ (0x3002, 0x00),
92
+ )
93
+
94
+ _REGS_COMMON = (
95
+ (0x0100, 0x00),
96
+ (0x0103, 0x01),
97
+ (0x3034, 0x1A),
98
+ (0x3035, 0x21),
99
+ (0x303C, 0x11),
100
+ (0x3106, 0xF5),
101
+ (0x3827, 0xEC),
102
+ (0x370C, 0x03),
103
+ (0x5000, 0x06),
104
+ (0x5003, 0x08),
105
+ (0x5A00, 0x08),
106
+ (0x3000, 0x00),
107
+ (0x3001, 0x00),
108
+ (0x3002, 0x00),
109
+ (0x3016, 0x08),
110
+ (0x3017, 0xE0),
111
+ (0x3018, 0x44),
112
+ (0x301C, 0xF8),
113
+ (0x301D, 0xF0),
114
+ (0x3A18, 0x00),
115
+ (0x3A19, 0xF8),
116
+ (0x3C01, 0x80),
117
+ (0x3B07, 0x0C),
118
+ (0x3630, 0x2E),
119
+ (0x3632, 0xE2),
120
+ (0x3633, 0x23),
121
+ (0x3634, 0x44),
122
+ (0x3636, 0x06),
123
+ (0x3620, 0x64),
124
+ (0x3621, 0xE0),
125
+ (0x3600, 0x37),
126
+ (0x3704, 0xA0),
127
+ (0x3703, 0x5A),
128
+ (0x3715, 0x78),
129
+ (0x3717, 0x01),
130
+ (0x3731, 0x02),
131
+ (0x370B, 0x60),
132
+ (0x3705, 0x1A),
133
+ (0x3F05, 0x02),
134
+ (0x3F06, 0x10),
135
+ (0x3F01, 0x0A),
136
+ (0x3A08, 0x01),
137
+ (0x3A0F, 0x58),
138
+ (0x3A10, 0x50),
139
+ (0x3A1B, 0x58),
140
+ (0x3A1E, 0x50),
141
+ (0x3A11, 0x60),
142
+ (0x3A1F, 0x28),
143
+ (0x4001, 0x02),
144
+ (0x4000, 0x09),
145
+ (0x3503, 0x03),
146
+ )
147
+
148
+ _REGS_2592X1944 = (
149
+ (0x3036, 0x69),
150
+ (0x3821, 0x02),
151
+ (0x3820, 0x00),
152
+ (0x3612, 0x5B),
153
+ (0x3618, 0x04),
154
+ (0x5002, 0x41),
155
+ (0x3814, 0x11),
156
+ (0x3815, 0x11),
157
+ (0x3708, 0x64),
158
+ (0x3709, 0x12),
159
+ (0x3800, 0x00),
160
+ (0x3801, 0x00),
161
+ (0x3802, 0x00),
162
+ (0x3803, 0x00),
163
+ (0x3804, 0x0A),
164
+ (0x3805, 0x3F),
165
+ (0x3806, 0x07),
166
+ (0x3807, 0xA3),
167
+ (0x3808, 0x0A),
168
+ (0x3809, 0x20),
169
+ (0x380A, 0x07),
170
+ (0x380B, 0x98),
171
+ (0x3811, 0x10),
172
+ (0x3813, 0x06),
173
+ (0x3A09, 0x28),
174
+ (0x3A0A, 0x00),
175
+ (0x3A0B, 0xF6),
176
+ (0x3A0D, 0x08),
177
+ (0x3A0E, 0x06),
178
+ (0x4004, 0x04),
179
+ (0x4837, 0x19),
180
+ (0x4800, 0x24),
181
+ (0x0100, 0x01),
182
+ )
183
+
184
+ _REGS_1920X1080 = (
185
+ (0x3036, 0x69),
186
+ (0x3821, 0x02),
187
+ (0x3820, 0x00),
188
+ (0x3612, 0x5B),
189
+ (0x3618, 0x04),
190
+ (0x5002, 0x41),
191
+ (0x3814, 0x11),
192
+ (0x3815, 0x11),
193
+ (0x3708, 0x64),
194
+ (0x3709, 0x12),
195
+ (0x3800, 0x01),
196
+ (0x3801, 0x5C),
197
+ (0x3802, 0x01),
198
+ (0x3803, 0xB2),
199
+ (0x3804, 0x08),
200
+ (0x3805, 0xE3),
201
+ (0x3806, 0x05),
202
+ (0x3807, 0xF1),
203
+ (0x3808, 0x07),
204
+ (0x3809, 0x80),
205
+ (0x380A, 0x04),
206
+ (0x380B, 0x38),
207
+ (0x3811, 0x04),
208
+ (0x3813, 0x02),
209
+ (0x3A09, 0x4B),
210
+ (0x3A0A, 0x01),
211
+ (0x3A0B, 0x13),
212
+ (0x3A0D, 0x04),
213
+ (0x3A0E, 0x03),
214
+ (0x4004, 0x04),
215
+ (0x4837, 0x19),
216
+ (0x4800, 0x34),
217
+ (0x0100, 0x01),
218
+ )
219
+
220
+ _REGS_1296X972 = (
221
+ (0x3036, 0x69),
222
+ (0x3821, 0x03),
223
+ (0x3820, 0x41),
224
+ (0x3612, 0x59),
225
+ (0x3618, 0x00),
226
+ (0x5002, 0x41),
227
+ (0x3800, 0x00),
228
+ (0x3801, 0x00),
229
+ (0x3802, 0x00),
230
+ (0x3803, 0x00),
231
+ (0x3804, 0x0A),
232
+ (0x3805, 0x3F),
233
+ (0x3806, 0x07),
234
+ (0x3807, 0xA3),
235
+ (0x3808, 0x05),
236
+ (0x3809, 0x10),
237
+ (0x380A, 0x03),
238
+ (0x380B, 0xCC),
239
+ (0x3811, 0x0C),
240
+ (0x3813, 0x06),
241
+ (0x3814, 0x31),
242
+ (0x3815, 0x31),
243
+ (0x3A09, 0x28),
244
+ (0x3A0A, 0x00),
245
+ (0x3A0B, 0xF6),
246
+ (0x3A0D, 0x08),
247
+ (0x3A0E, 0x06),
248
+ (0x4004, 0x04),
249
+ (0x4837, 0x16),
250
+ (0x4800, 0x24),
251
+ (0x350A, 0x00),
252
+ (0x350B, 0x10),
253
+ (0x3500, 0x00),
254
+ (0x3501, 0x1A),
255
+ (0x3502, 0xF0),
256
+ (0x3212, 0xA0),
257
+ (0x0100, 0x01),
258
+ )
259
+
260
+ _REGS_640X480 = (
261
+ (0x3036, 0x46),
262
+ (0x3821, 0x03),
263
+ (0x3820, 0x41),
264
+ (0x3612, 0x59),
265
+ (0x3618, 0x00),
266
+ (0x3814, 0x35),
267
+ (0x3815, 0x35),
268
+ (0x3708, 0x64),
269
+ (0x3709, 0x52),
270
+ (0x3800, 0x00),
271
+ (0x3801, 0x10),
272
+ (0x3802, 0x00),
273
+ (0x3803, 0x00),
274
+ (0x3804, 0x0A),
275
+ (0x3805, 0x2F),
276
+ (0x3806, 0x07),
277
+ (0x3807, 0x9F),
278
+ (0x3808, 0x02),
279
+ (0x3809, 0x80),
280
+ (0x380A, 0x01),
281
+ (0x380B, 0xE0),
282
+ (0x3A09, 0x2E),
283
+ (0x3A0A, 0x00),
284
+ (0x3A0B, 0xFB),
285
+ (0x3A0D, 0x02),
286
+ (0x3A0E, 0x01),
287
+ (0x4004, 0x02),
288
+ (0x4800, 0x34),
289
+ (0x0100, 0x01),
290
+ )
291
+
292
+
293
+ class _ModeInfo:
294
+ """One sensor mode: native size, frame timing and MIPI lane bit rate"""
295
+
296
+ def __init__(self, name, size, timing, lane_bit_rate_mbps, regs):
297
+ self.name = name
298
+ self.width, self.height = size
299
+ self.hts, self.vts = timing # total line length and frame height
300
+ self.lane_bit_rate_mbps = lane_bit_rate_mbps
301
+ self.regs = regs
302
+
303
+ def __repr__(self):
304
+ return "Mode." + self.name
305
+
306
+
307
+ class Mode:
308
+ """Sensor modes. All send RAW10 over 2 MIPI data lanes."""
309
+
310
+ R640X480 = _ModeInfo("R640X480", (640, 480), (1852, 504), 292, _REGS_640X480)
311
+ """640 x 480, 2x2 binned and subsampled, full field of view, about 60
312
+ frames per second. The default."""
313
+
314
+ R1296X972 = _ModeInfo("R1296X972", (1296, 972), (1896, 1435), 438, _REGS_1296X972)
315
+ """1296 x 972, 2x2 binned, full field of view"""
316
+
317
+ R1920X1080 = _ModeInfo("R1920X1080", (1920, 1080), (2416, 1104), 438, _REGS_1920X1080)
318
+ """1920 x 1080, center crop of the full resolution, 30 frames per second"""
319
+
320
+ R2592X1944 = _ModeInfo("R2592X1944", (2592, 1944), (2844, 1968), 438, _REGS_2592X1944)
321
+ """2592 x 1944, full 5 megapixel resolution. One RGB565 frame is about
322
+ 10 MB."""
323
+
324
+
325
+ class TestPattern:
326
+ """Built-in test patterns, for `OV5647.test_pattern`"""
327
+
328
+ NONE = 0x00
329
+ """The camera image"""
330
+ COLOR_BARS = 0x80
331
+ """Vertical color bars"""
332
+ COLOR_SQUARES = 0x82
333
+ """A grid of colored squares"""
334
+ RANDOM = 0x81
335
+ """Random data"""
336
+
337
+
338
+ # Bayer order for each combination of [flip][mirror], as in the Linux
339
+ # driver's ov5647_get_mbus_code().
340
+ _BAYER_ORDERS = (
341
+ (mipicsi.BayerOrder.GBRG, mipicsi.BayerOrder.BGGR),
342
+ (mipicsi.BayerOrder.RGGB, mipicsi.BayerOrder.GRBG),
343
+ )
344
+
345
+
346
+ class OV5647:
347
+ """Driver for the OV5647 camera sensor.
348
+
349
+ :param ~busio.I2C i2c: The I2C bus the sensor is connected to
350
+ :param ~microcontroller.Pin reset_pin: The camera's enable/power-down pin,
351
+ such as ``board.CAMERA_RESET``. It is pulsed low, then held high.
352
+ :param Mode mode: The initial sensor mode
353
+ :param int address: The sensor's I2C address
354
+ """
355
+
356
+ def __init__(
357
+ self,
358
+ i2c: busio.I2C,
359
+ *,
360
+ reset_pin: Optional[Pin] = None,
361
+ mode: _ModeInfo = Mode.R640X480,
362
+ address: int = _I2C_ADDRESS,
363
+ ) -> None:
364
+ self._reset = None
365
+ if reset_pin is not None:
366
+ self._reset = DigitalInOut(reset_pin)
367
+ self._reset.switch_to_output(value=False)
368
+ time.sleep(0.01)
369
+ self._reset.value = True
370
+ time.sleep(0.02) # datasheet: 20 ms before the first I2C access
371
+ self._device = I2CDevice(i2c, address)
372
+ self._buf = bytearray(3)
373
+
374
+ self._streaming = False
375
+ self._mode = mode
376
+ self._width = mode.width
377
+ self._height = mode.height
378
+ self._mirror = False
379
+ self._flip = False
380
+ self._test_pattern = TestPattern.NONE
381
+ self._auto_exposure = True
382
+ self._exposure = min(mode.vts - 4, 1000)
383
+ self._analog_gain = 32
384
+
385
+ # Power on: enable the outputs, then stop streaming so the MIPI lanes
386
+ # rest in LP-11 until the camera starts playing.
387
+ self._write_list(_REGS_OE_ENABLE)
388
+ self._stop()
389
+
390
+ # Detect
391
+ self._write(_SW_RESET, 0x01)
392
+ chip_id = (self._read(_CHIP_ID_HIGH) << 8) | self._read(_CHIP_ID_LOW)
393
+ if chip_id != _CHIP_ID:
394
+ raise RuntimeError(f"Expected OV5647, got chip ID 0x{chip_id:04X}")
395
+ self._write(_SW_RESET, 0x00)
396
+
397
+ def _read(self, reg: int) -> int:
398
+ buf = self._buf
399
+ buf[0] = reg >> 8
400
+ buf[1] = reg & 0xFF
401
+ with self._device as dev:
402
+ dev.write_then_readinto(buf, buf, out_end=2, in_start=2)
403
+ return buf[2]
404
+
405
+ def _write(self, reg: int, value: int) -> None:
406
+ buf = self._buf
407
+ buf[0] = reg >> 8
408
+ buf[1] = reg & 0xFF
409
+ buf[2] = value
410
+ with self._device as dev:
411
+ dev.write(buf)
412
+
413
+ def _write_list(self, regs) -> None:
414
+ for reg, value in regs:
415
+ self._write(reg, value)
416
+ if reg == _SW_RESET and value == 0x01:
417
+ time.sleep(0.005) # let the software reset finish
418
+
419
+ def _write16(self, reg: int, value: int) -> None:
420
+ self._write(reg, value >> 8)
421
+ self._write(reg + 1, value & 0xFF)
422
+
423
+ def _read16(self, reg: int) -> int:
424
+ return (self._read(reg) << 8) | self._read(reg + 1)
425
+
426
+ def _update_bits(self, reg: int, mask: int, value: int) -> None:
427
+ self._write(reg, (self._read(reg) & ~mask & 0xFF) | (value & mask))
428
+
429
+ def _check_not_streaming(self) -> None:
430
+ if self._streaming:
431
+ raise RuntimeError("Stop the video before changing the sensor's mode")
432
+
433
+ @property
434
+ def mode(self) -> _ModeInfo:
435
+ """The sensor mode, one of the `Mode` values. Setting it resets
436
+ `width` and `height` to the mode's full size, and `exposure` to the
437
+ mode's default. Can't be changed while streaming."""
438
+ return self._mode
439
+
440
+ @mode.setter
441
+ def mode(self, mode: _ModeInfo) -> None:
442
+ self._check_not_streaming()
443
+ self._mode = mode
444
+ self._width = mode.width
445
+ self._height = mode.height
446
+ self._exposure = min(mode.vts - 4, 1000)
447
+
448
+ @property
449
+ def width(self) -> int:
450
+ """Frame width in pixels. Set it to crop a centered window narrower
451
+ than the mode's full width (even values only). Can't be changed while
452
+ streaming."""
453
+ return self._width
454
+
455
+ @width.setter
456
+ def width(self, width: int) -> None:
457
+ self._check_not_streaming()
458
+ if not 2 <= width <= self._mode.width or width % 2:
459
+ raise ValueError(f"width must be even and at most {self._mode.width}")
460
+ self._width = width
461
+
462
+ @property
463
+ def height(self) -> int:
464
+ """Frame height in pixels. Set it to crop a centered window shorter
465
+ than the mode's full height (even values only). Can't be changed while
466
+ streaming."""
467
+ return self._height
468
+
469
+ @height.setter
470
+ def height(self, height: int) -> None:
471
+ self._check_not_streaming()
472
+ if not 2 <= height <= self._mode.height or height % 2:
473
+ raise ValueError(f"height must be even and at most {self._mode.height}")
474
+ self._height = height
475
+
476
+ @property
477
+ def pixel_format(self) -> mipicsi.PixelFormat:
478
+ """The `mipicsi.PixelFormat` the sensor transmits: always RAW10"""
479
+ return mipicsi.PixelFormat.RAW10
480
+
481
+ @property
482
+ def bayer_order(self) -> mipicsi.BayerOrder:
483
+ """The `mipicsi.BayerOrder` of the RAW data, which depends on
484
+ `mirror` and `flip`"""
485
+ return _BAYER_ORDERS[self._flip][self._mirror]
486
+
487
+ @property
488
+ def data_lanes(self) -> int:
489
+ """The number of MIPI data lanes: always 2"""
490
+ return 2
491
+
492
+ @property
493
+ def lane_bit_rate_mbps(self) -> int:
494
+ """The bit rate of each MIPI data lane in the current mode"""
495
+ return self._mode.lane_bit_rate_mbps
496
+
497
+ @property
498
+ def mirror(self) -> bool:
499
+ """True to mirror the image left to right. Can't be changed while
500
+ streaming, because it changes the `bayer_order`."""
501
+ return self._mirror
502
+
503
+ @mirror.setter
504
+ def mirror(self, value: bool) -> None:
505
+ self._check_not_streaming()
506
+ self._mirror = bool(value)
507
+
508
+ @property
509
+ def flip(self) -> bool:
510
+ """True to flip the image upside down. `mirror` and `flip` together
511
+ rotate the image 180 degrees. Can't be changed while streaming,
512
+ because it changes the `bayer_order`."""
513
+ return self._flip
514
+
515
+ @flip.setter
516
+ def flip(self, value: bool) -> None:
517
+ self._check_not_streaming()
518
+ self._flip = bool(value)
519
+
520
+ @property
521
+ def test_pattern(self) -> int:
522
+ """A `TestPattern` value to send instead of the camera image, or
523
+ `TestPattern.NONE` for the image"""
524
+ return self._test_pattern
525
+
526
+ @test_pattern.setter
527
+ def test_pattern(self, value: int) -> None:
528
+ self._test_pattern = value
529
+ if self._streaming:
530
+ self._write(_ISPCTRL3D, value)
531
+
532
+ @property
533
+ def auto_exposure(self) -> bool:
534
+ """True (the default) for the sensor's automatic exposure and gain.
535
+ When False, `exposure` and `analog_gain` set them directly."""
536
+ return self._auto_exposure
537
+
538
+ @auto_exposure.setter
539
+ def auto_exposure(self, value: bool) -> None:
540
+ self._auto_exposure = bool(value)
541
+ if self._streaming:
542
+ self._write_exposure()
543
+
544
+ @property
545
+ def exposure(self) -> int:
546
+ """Exposure time in lines, 4 up to the mode's frame height plus
547
+ blanking. With `auto_exposure` it is only the starting point."""
548
+ return self._exposure
549
+
550
+ @exposure.setter
551
+ def exposure(self, value: int) -> None:
552
+ if not 4 <= value <= self._mode.vts - 4:
553
+ raise ValueError(f"exposure must be 4 to {self._mode.vts - 4} lines")
554
+ self._exposure = value
555
+ if self._streaming:
556
+ self._write_exposure()
557
+
558
+ @property
559
+ def analog_gain(self) -> int:
560
+ """Analog gain, 16 (1x) to 1023 (about 64x). The default is 32 (2x).
561
+ With `auto_exposure` it is only the starting point."""
562
+ return self._analog_gain
563
+
564
+ @analog_gain.setter
565
+ def analog_gain(self, value: int) -> None:
566
+ if not 16 <= value <= 1023:
567
+ raise ValueError("analog_gain must be 16 to 1023")
568
+ self._analog_gain = value
569
+ if self._streaming:
570
+ self._write_exposure()
571
+
572
+ def _write_exposure(self) -> None:
573
+ lines = self._exposure << 4
574
+ self._write(_EXPOSURE, (lines >> 16) & 0x0F)
575
+ self._write(_EXPOSURE + 1, (lines >> 8) & 0xFF)
576
+ self._write(_EXPOSURE + 2, lines & 0xFF)
577
+ self._write16(_GAIN, self._analog_gain & 0x3FF)
578
+ self._write(_AEC_AGC, 0x00 if self._auto_exposure else 0x03)
579
+
580
+ @property
581
+ def streaming(self) -> bool:
582
+ """True while the sensor is streaming frames (read-only)"""
583
+ return self._streaming
584
+
585
+ def _start(self) -> None:
586
+ """Program the current mode and start streaming. Called by
587
+ `mipicsi.Camera`; user code should not call it."""
588
+ if self._streaming:
589
+ return # idempotent, as mipicsi expects
590
+ mode = self._mode
591
+
592
+ # Set mode: common registers, mode registers, virtual channel 0
593
+ self._write_list(_REGS_COMMON)
594
+ self._write_list(mode.regs)
595
+ self._update_bits(_MIPI_CTRL14, 0xC0, 0x00)
596
+ if not self._read(_SW_STANDBY) & 0x01:
597
+ self._write(_SW_STANDBY, 0x01)
598
+
599
+ # Controls, as the Linux driver applies them at stream start
600
+ self._write16(_HTS, mode.hts)
601
+ self._write16(_VTS, mode.vts)
602
+ self._write_exposure()
603
+ self._write(_ISPCTRL3D, self._test_pattern)
604
+ # The sensor's mirror bit is set for the unmirrored image
605
+ self._update_bits(_TIMING_TC_H, 0x02, 0x00 if self._mirror else 0x02)
606
+ self._update_bits(_TIMING_TC_V, 0x02, 0x02 if self._flip else 0x00)
607
+
608
+ # Crop a centered window by shrinking the output size and moving the
609
+ # window offset. Offsets stay even so the Bayer order doesn't change.
610
+ if (self._width, self._height) != (mode.width, mode.height):
611
+ x_offset = self._read16(_X_OFFSET) + ((mode.width - self._width) // 2 & ~1)
612
+ y_offset = self._read16(_Y_OFFSET) + ((mode.height - self._height) // 2 & ~1)
613
+ self._write16(_OUTPUT_WIDTH, self._width)
614
+ self._write16(_OUTPUT_HEIGHT, self._height)
615
+ self._write16(_X_OFFSET, x_offset)
616
+ self._write16(_Y_OFFSET, y_offset)
617
+
618
+ # Stream on: non-continuous clock with line sync, as on a Raspberry Pi
619
+ self._write(_MIPI_CTRL00, _BUS_IDLE | _CLOCK_LANE_GATE | _LINE_SYNC_ENABLE)
620
+ self._write(_FRAME_OFF_NUMBER, 0x00)
621
+ self._write(_PAD_OUT, 0x00)
622
+ self._streaming = True
623
+
624
+ def _stop(self) -> None:
625
+ """Stop streaming. Called by `mipicsi.Camera`; user code should not
626
+ call it."""
627
+ self._write(_MIPI_CTRL00, _CLOCK_LANE_GATE | _BUS_IDLE | _CLOCK_LANE_DISABLE)
628
+ self._write(_FRAME_OFF_NUMBER, 0x0F)
629
+ self._write(_PAD_OUT, 0x01)
630
+ self._streaming = False
631
+
632
+ def deinit(self) -> None:
633
+ """Stop streaming, power the sensor down and release the reset pin"""
634
+ try:
635
+ self._stop()
636
+ self._write_list(_REGS_OE_DISABLE)
637
+ self._update_bits(_SW_STANDBY, 0x01, 0x00)
638
+ except OSError:
639
+ pass # the sensor may already be powered off
640
+ if self._reset is not None:
641
+ self._reset.value = False
642
+ self._reset.deinit()
643
+ self._reset = None
644
+
645
+ def __enter__(self) -> "OV5647":
646
+ return self
647
+
648
+ def __exit__(self, exception_type, exception_value, traceback) -> None:
649
+ self.deinit()