Skip to content

Commit 29536e6

Browse files
authored
Merge pull request #17 from patenaud/type_annotations
Added type annotations
2 parents 82101e3 + 49e4b82 commit 29536e6

File tree

1 file changed

+43
-34
lines changed

1 file changed

+43
-34
lines changed

adafruit_ov7670.py

Lines changed: 43 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,16 @@
3535
import imagecapture
3636
import pwmio
3737
from adafruit_bus_device.i2c_device import I2CDevice
38-
3938
from micropython import const
4039

40+
try:
41+
from typing import List, Optional
42+
from busio import I2C
43+
from microcontroller import Pin
44+
from circuitpython_typing import WriteableBuffer
45+
except ImportError:
46+
pass
47+
4148
# Supported color formats
4249
OV7670_COLOR_RGB = 0
4350
"""RGB565 big-endian"""
@@ -503,19 +510,21 @@
503510
class OV7670: # pylint: disable=too-many-instance-attributes
504511
"""Library for the OV7670 digital camera"""
505512

513+
# pylint: disable=too-many-arguments
514+
506515
def __init__(
507516
self,
508-
i2c_bus,
509-
data_pins,
510-
clock,
511-
vsync,
512-
href,
513-
shutdown=None,
514-
reset=None,
515-
mclk=None,
516-
mclk_frequency=16_000_000,
517-
i2c_address=0x21,
518-
): # pylint: disable=too-many-arguments
517+
i2c_bus: I2C,
518+
data_pins: List[Pin],
519+
clock: Pin,
520+
vsync: Pin,
521+
href: Pin,
522+
shutdown: Optional[Pin] = None,
523+
reset: Optional[Pin] = None,
524+
mclk: Optional[Pin] = None,
525+
mclk_frequency: int = 16_000_000,
526+
i2c_address: int = 0x21,
527+
) -> None:
519528
"""
520529
Args:
521530
i2c_bus (busio.I2C): The I2C bus used to configure the OV7670
@@ -584,7 +593,7 @@ def __init__(
584593
data_pins=data_pins, clock=clock, vsync=vsync, href=href
585594
)
586595

587-
def capture(self, buf):
596+
def capture(self, buf: WriteableBuffer) -> None:
588597
"""Capture an image into the buffer.
589598
590599
Args:
@@ -594,7 +603,7 @@ def capture(self, buf):
594603
self._imagecapture.capture(buf)
595604

596605
@property
597-
def mclk_frequency(self):
606+
def mclk_frequency(self) -> Optional[int]:
598607
"""Get the actual frequency the generated mclk, or None"""
599608
return self._mclk_pwm.frequency if self._mclk_pwm else None
600609

@@ -611,16 +620,16 @@ def height(self):
611620
return 480 >> self._size
612621

613622
@property
614-
def colorspace(self):
623+
def colorspace(self) -> int:
615624
"""Get or set the colorspace, one of the ``OV7670_COLOR_`` constants."""
616625
return self._colorspace
617626

618627
@colorspace.setter
619-
def colorspace(self, colorspace):
628+
def colorspace(self, colorspace: int) -> None:
620629
self._colorspace = colorspace
621630
self._write_list(_OV7670_rgb if colorspace == OV7670_COLOR_RGB else _OV7670_yuv)
622631

623-
def deinit(self):
632+
def deinit(self) -> None:
624633
"""Deinitialize the camera"""
625634
self._imagecapture.deinit()
626635
if self._mclk_pwm:
@@ -631,7 +640,7 @@ def deinit(self):
631640
self._reset.deinit()
632641

633642
@property
634-
def size(self):
643+
def size(self) -> int:
635644
"""Get or set the captured image size, one of the ``OV7670_SIZE_`` constants."""
636645
return self._size
637646

@@ -642,11 +651,11 @@ def size(self, size):
642651

643652
@property
644653
def test_pattern(self):
645-
"""Get or set the test pattern, one of the ``OV7670_TES_PATTERN_`` constants."""
654+
"""Get or set the test pattern, one of the ``OV7670_TEST_PATTERN_`` constants."""
646655
return self._test_pattern
647656

648657
@test_pattern.setter
649-
def test_pattern(self, pattern):
658+
def test_pattern(self, pattern: int) -> None:
650659
# Modify only test pattern bits (not scaling bits)
651660
xsc = self._read_register(_OV7670_REG_SCALING_XSC) & ~0x80
652661
ysc = self._read_register(_OV7670_REG_SCALING_YSC) & ~0x80
@@ -658,7 +667,7 @@ def test_pattern(self, pattern):
658667
self._write_register(_OV7670_REG_SCALING_XSC, xsc)
659668
self._write_register(_OV7670_REG_SCALING_YSC, ysc)
660669

661-
def _set_flip(self):
670+
def _set_flip(self) -> None:
662671
mvfp = self._read_register(_OV7670_REG_MVFP)
663672
if self._flip_x:
664673
mvfp |= _OV7670_MVFP_MIRROR
@@ -671,60 +680,60 @@ def _set_flip(self):
671680
self._write_register(_OV7670_REG_MVFP, mvfp)
672681

673682
@property
674-
def flip_x(self):
683+
def flip_x(self) -> bool:
675684
"""Get or set the X-flip flag"""
676685
return self._flip_x
677686

678687
@flip_x.setter
679-
def flip_x(self, value):
688+
def flip_x(self, value: bool) -> None:
680689
self._flip_x = bool(value)
681690
self._set_flip()
682691

683692
@property
684-
def flip_y(self):
693+
def flip_y(self) -> bool:
685694
"""Get or set the Y-flip flag"""
686695
return self._flip_y
687696

688697
@flip_y.setter
689-
def flip_y(self, value):
698+
def flip_y(self, value: bool) -> None:
690699
self._flip_y = bool(value)
691700
self._set_flip()
692701

693702
@property
694-
def night(self):
703+
def night(self) -> int:
695704
"""Get or set the night-vision mode, one of the ``OV7670_NIGHT_MODE_`` constants."""
696705
return self._night
697706

698707
@night.setter
699-
def night(self, value):
708+
def night(self, value: int) -> None:
700709
com11 = self._read_register(_OV7670_REG_COM11)
701710
com11 = (com11 & 0b00011111) | value
702711
self._write_register(_OV7670_REG_COM11, com11)
703712
self._night = value
704713

705714
@property
706-
def product_id(self):
715+
def product_id(self) -> int:
707716
"""Get the product id (PID) register. The expected value is 0x76."""
708717
return self._read_register(_OV7670_REG_PID)
709718

710719
@property
711-
def product_version(self):
720+
def product_version(self) -> int:
712721
"""Get the version (VER) register. The expected value is 0x73."""
713722
return self._read_register(_OV7670_REG_VER)
714723

715-
def _write_list(self, reg_list):
724+
def _write_list(self, reg_list: bytes) -> None:
716725
for i in range(0, len(reg_list), 2):
717726
self._write_register(reg_list[i], reg_list[i + 1])
718727
time.sleep(0.001)
719728

720-
def _write_register(self, reg, value):
729+
def _write_register(self, reg: int, value: int) -> None:
721730
b = bytearray(2)
722731
b[0] = reg
723732
b[1] = value
724733
with self._i2c_device as i2c:
725734
i2c.write(b)
726735

727-
def _read_register(self, reg):
736+
def _read_register(self, reg: int) -> int:
728737
b = bytearray(1)
729738
b[0] = reg
730739
with self._i2c_device as i2c:
@@ -733,8 +742,8 @@ def _read_register(self, reg):
733742
return b[0]
734743

735744
def _frame_control(
736-
self, size, vstart, hstart, edge_offset, pclk_delay
737-
): # pylint: disable=too-many-arguments
745+
self, size: int, vstart: int, hstart: int, edge_offset: int, pclk_delay: int
746+
) -> None: # pylint: disable=too-many-arguments
738747

739748
# Enable downsampling if sub-VGA, and zoom if 1:16 scale
740749
value = _OV7670_COM3_DCWEN if (size > OV7670_SIZE_DIV1) else 0

0 commit comments

Comments
 (0)