35
35
import imagecapture
36
36
import pwmio
37
37
from adafruit_bus_device .i2c_device import I2CDevice
38
-
39
38
from micropython import const
40
39
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
+
41
48
# Supported color formats
42
49
OV7670_COLOR_RGB = 0
43
50
"""RGB565 big-endian"""
503
510
class OV7670 : # pylint: disable=too-many-instance-attributes
504
511
"""Library for the OV7670 digital camera"""
505
512
513
+ # pylint: disable=too-many-arguments
514
+
506
515
def __init__ (
507
516
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 :
519
528
"""
520
529
Args:
521
530
i2c_bus (busio.I2C): The I2C bus used to configure the OV7670
@@ -584,7 +593,7 @@ def __init__(
584
593
data_pins = data_pins , clock = clock , vsync = vsync , href = href
585
594
)
586
595
587
- def capture (self , buf ) :
596
+ def capture (self , buf : WriteableBuffer ) -> None :
588
597
"""Capture an image into the buffer.
589
598
590
599
Args:
@@ -594,7 +603,7 @@ def capture(self, buf):
594
603
self ._imagecapture .capture (buf )
595
604
596
605
@property
597
- def mclk_frequency (self ):
606
+ def mclk_frequency (self ) -> Optional [ int ] :
598
607
"""Get the actual frequency the generated mclk, or None"""
599
608
return self ._mclk_pwm .frequency if self ._mclk_pwm else None
600
609
@@ -611,16 +620,16 @@ def height(self):
611
620
return 480 >> self ._size
612
621
613
622
@property
614
- def colorspace (self ):
623
+ def colorspace (self ) -> int :
615
624
"""Get or set the colorspace, one of the ``OV7670_COLOR_`` constants."""
616
625
return self ._colorspace
617
626
618
627
@colorspace .setter
619
- def colorspace (self , colorspace ) :
628
+ def colorspace (self , colorspace : int ) -> None :
620
629
self ._colorspace = colorspace
621
630
self ._write_list (_OV7670_rgb if colorspace == OV7670_COLOR_RGB else _OV7670_yuv )
622
631
623
- def deinit (self ):
632
+ def deinit (self ) -> None :
624
633
"""Deinitialize the camera"""
625
634
self ._imagecapture .deinit ()
626
635
if self ._mclk_pwm :
@@ -631,7 +640,7 @@ def deinit(self):
631
640
self ._reset .deinit ()
632
641
633
642
@property
634
- def size (self ):
643
+ def size (self ) -> int :
635
644
"""Get or set the captured image size, one of the ``OV7670_SIZE_`` constants."""
636
645
return self ._size
637
646
@@ -642,11 +651,11 @@ def size(self, size):
642
651
643
652
@property
644
653
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."""
646
655
return self ._test_pattern
647
656
648
657
@test_pattern .setter
649
- def test_pattern (self , pattern ) :
658
+ def test_pattern (self , pattern : int ) -> None :
650
659
# Modify only test pattern bits (not scaling bits)
651
660
xsc = self ._read_register (_OV7670_REG_SCALING_XSC ) & ~ 0x80
652
661
ysc = self ._read_register (_OV7670_REG_SCALING_YSC ) & ~ 0x80
@@ -658,7 +667,7 @@ def test_pattern(self, pattern):
658
667
self ._write_register (_OV7670_REG_SCALING_XSC , xsc )
659
668
self ._write_register (_OV7670_REG_SCALING_YSC , ysc )
660
669
661
- def _set_flip (self ):
670
+ def _set_flip (self ) -> None :
662
671
mvfp = self ._read_register (_OV7670_REG_MVFP )
663
672
if self ._flip_x :
664
673
mvfp |= _OV7670_MVFP_MIRROR
@@ -671,60 +680,60 @@ def _set_flip(self):
671
680
self ._write_register (_OV7670_REG_MVFP , mvfp )
672
681
673
682
@property
674
- def flip_x (self ):
683
+ def flip_x (self ) -> bool :
675
684
"""Get or set the X-flip flag"""
676
685
return self ._flip_x
677
686
678
687
@flip_x .setter
679
- def flip_x (self , value ) :
688
+ def flip_x (self , value : bool ) -> None :
680
689
self ._flip_x = bool (value )
681
690
self ._set_flip ()
682
691
683
692
@property
684
- def flip_y (self ):
693
+ def flip_y (self ) -> bool :
685
694
"""Get or set the Y-flip flag"""
686
695
return self ._flip_y
687
696
688
697
@flip_y .setter
689
- def flip_y (self , value ) :
698
+ def flip_y (self , value : bool ) -> None :
690
699
self ._flip_y = bool (value )
691
700
self ._set_flip ()
692
701
693
702
@property
694
- def night (self ):
703
+ def night (self ) -> int :
695
704
"""Get or set the night-vision mode, one of the ``OV7670_NIGHT_MODE_`` constants."""
696
705
return self ._night
697
706
698
707
@night .setter
699
- def night (self , value ) :
708
+ def night (self , value : int ) -> None :
700
709
com11 = self ._read_register (_OV7670_REG_COM11 )
701
710
com11 = (com11 & 0b00011111 ) | value
702
711
self ._write_register (_OV7670_REG_COM11 , com11 )
703
712
self ._night = value
704
713
705
714
@property
706
- def product_id (self ):
715
+ def product_id (self ) -> int :
707
716
"""Get the product id (PID) register. The expected value is 0x76."""
708
717
return self ._read_register (_OV7670_REG_PID )
709
718
710
719
@property
711
- def product_version (self ):
720
+ def product_version (self ) -> int :
712
721
"""Get the version (VER) register. The expected value is 0x73."""
713
722
return self ._read_register (_OV7670_REG_VER )
714
723
715
- def _write_list (self , reg_list ) :
724
+ def _write_list (self , reg_list : bytes ) -> None :
716
725
for i in range (0 , len (reg_list ), 2 ):
717
726
self ._write_register (reg_list [i ], reg_list [i + 1 ])
718
727
time .sleep (0.001 )
719
728
720
- def _write_register (self , reg , value ) :
729
+ def _write_register (self , reg : int , value : int ) -> None :
721
730
b = bytearray (2 )
722
731
b [0 ] = reg
723
732
b [1 ] = value
724
733
with self ._i2c_device as i2c :
725
734
i2c .write (b )
726
735
727
- def _read_register (self , reg ) :
736
+ def _read_register (self , reg : int ) -> int :
728
737
b = bytearray (1 )
729
738
b [0 ] = reg
730
739
with self ._i2c_device as i2c :
@@ -733,8 +742,8 @@ def _read_register(self, reg):
733
742
return b [0 ]
734
743
735
744
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
738
747
739
748
# Enable downsampling if sub-VGA, and zoom if 1:16 scale
740
749
value = _OV7670_COM3_DCWEN if (size > OV7670_SIZE_DIV1 ) else 0
0 commit comments