106106
107107OV2640_COLOR_RGB = 0
108108OV2640_COLOR_YUV = 1
109+ OV2640_COLOR_JPEG = 2
109110
110111_IMAGE_MODE_Y8_DVP_EN = const (0x40 )
111112_IMAGE_MODE_JPEG_EN = const (0x10 )
886887 ]
887888)
888889
889- # _ov2640_settings_jpeg3 = bytes([
890- # _BANK_SEL, _BANK_DSP,
891- # _RESET, _RESET_JPEG | _RESET_DVP,
892- # _IMAGE_MODE, _IMAGE_MODE_JPEG_EN | _IMAGE_MODE_HREF_VSYNC,
893- # 0xD7, 0x03,
894- # 0xE1, 0x77,
895- # 0xE5, 0x1F,
896- # 0xD9, 0x10,
897- # 0xDF, 0x80,
898- # 0x33, 0x80,
899- # 0x3C, 0x10,
900- # 0xEB, 0x30,
901- # 0xDD, 0x7F,
902- # _RESET, 0x00,
903- # ])
904-
905- _ov2640_settings_yuv422 = bytes (
906- [
907- _BANK_SEL ,
908- _BANK_DSP ,
909- _RESET ,
910- _RESET_DVP ,
911- _IMAGE_MODE ,
912- _IMAGE_MODE_YUV422 ,
913- 0xD7 ,
914- 0x01 ,
915- 0xE1 ,
916- 0x67 ,
917- _RESET ,
918- 0x00 ,
919- ]
920- )
921-
922- _ov2640_settings_rgb565 = bytes (
923- [
924- _BANK_SEL ,
925- _BANK_DSP ,
926- _RESET ,
927- _RESET_DVP ,
928- _IMAGE_MODE ,
929- _IMAGE_MODE_RGB565 ,
930- 0xD7 ,
931- 0x03 ,
932- 0xE1 ,
933- 0x77 ,
934- _RESET ,
935- 0x00 ,
936- ]
937- )
890+ _ov2640_color_settings = {
891+ OV2640_COLOR_JPEG : bytes (
892+ [
893+ _BANK_SEL ,
894+ _BANK_DSP ,
895+ _RESET ,
896+ _RESET_JPEG | _RESET_DVP ,
897+ _IMAGE_MODE ,
898+ _IMAGE_MODE_JPEG_EN | _IMAGE_MODE_HREF_VSYNC ,
899+ 0xD7 ,
900+ 0x03 ,
901+ 0xE1 ,
902+ 0x77 ,
903+ 0xE5 ,
904+ 0x1F ,
905+ 0xD9 ,
906+ 0x10 ,
907+ 0xDF ,
908+ 0x80 ,
909+ 0x33 ,
910+ 0x80 ,
911+ 0x3C ,
912+ 0x10 ,
913+ 0xEB ,
914+ 0x30 ,
915+ 0xDD ,
916+ 0x7F ,
917+ _RESET ,
918+ 0x00 ,
919+ ]
920+ ),
921+ OV2640_COLOR_YUV : bytes (
922+ [
923+ _BANK_SEL ,
924+ _BANK_DSP ,
925+ _RESET ,
926+ _RESET_DVP ,
927+ _IMAGE_MODE ,
928+ _IMAGE_MODE_YUV422 ,
929+ 0xD7 ,
930+ 0x01 ,
931+ 0xE1 ,
932+ 0x67 ,
933+ _RESET ,
934+ 0x00 ,
935+ ]
936+ ),
937+ OV2640_COLOR_RGB : bytes (
938+ [
939+ _BANK_SEL ,
940+ _BANK_DSP ,
941+ _RESET ,
942+ _RESET_DVP ,
943+ _IMAGE_MODE ,
944+ _IMAGE_MODE_RGB565 ,
945+ 0xD7 ,
946+ 0x03 ,
947+ 0xE1 ,
948+ 0x77 ,
949+ _RESET ,
950+ 0x00 ,
951+ ]
952+ ),
953+ }
938954
939955
940956class _RegBits :
@@ -1114,6 +1130,19 @@ def capture(self, buf):
11141130 captured image. Note that this can be a ulab array or a displayio Bitmap.
11151131 """
11161132 self ._imagecapture .capture (buf )
1133+ if self .colorspace == OV2640_COLOR_JPEG :
1134+ eoi = buf .find (b"\xff \xd9 " )
1135+ if eoi != - 1 :
1136+ # terminate the JPEG data just after the EOI marker
1137+ return memoryview (buf )[: eoi + 2 ]
1138+ return None
1139+
1140+ @property
1141+ def capture_buffer_size (self ):
1142+ """Return the size of capture buffer to use with current resolution & colorspace settings"""
1143+ if self .colorspace == OV2640_COLOR_JPEG :
1144+ return self .width * self .height // 5
1145+ return self .width * self .height * 2
11171146
11181147 @property
11191148 def mclk_frequency (self ):
@@ -1140,17 +1169,15 @@ def colorspace(self):
11401169 @colorspace .setter
11411170 def colorspace (self , colorspace ):
11421171 self ._colorspace = colorspace
1143- self ._write_list (
1144- _ov2640_settings_rgb565
1145- if colorspace == OV2640_COLOR_RGB
1146- else _ov2640_settings_yuv422
1147- )
1172+ self ._set_size_and_colorspace ()
1173+
1174+ def _set_colorspace (self ):
1175+ colorspace = self ._colorspace
1176+ settings = _ov2640_color_settings [colorspace ]
1177+
1178+ self ._write_list (settings )
11481179 # written twice?
1149- self ._write_list (
1150- _ov2640_settings_rgb565
1151- if colorspace == OV2640_COLOR_RGB
1152- else _ov2640_settings_yuv422
1153- )
1180+ self ._write_list (settings )
11541181 time .sleep (0.01 )
11551182
11561183 def deinit (self ):
@@ -1168,8 +1195,8 @@ def size(self):
11681195 """Get or set the captured image size, one of the ``OV2640_SIZE_`` constants."""
11691196 return self ._size
11701197
1171- @ size . setter
1172- def size ( self , size ):
1198+ def _set_size_and_colorspace ( self ):
1199+ size = self . _size
11731200 width , height , ratio = _resolution_info [size ]
11741201 offset_x , offset_y , max_x , max_y = _ratio_table [ratio ]
11751202 mode = _OV2640_MODE_UXGA
@@ -1190,7 +1217,11 @@ def size(self, size):
11901217 offset_y //= 2
11911218
11921219 self ._set_window (mode , offset_x , offset_y , max_x , max_y , width , height )
1220+
1221+ @size .setter
1222+ def size (self , size ):
11931223 self ._size = size
1224+ self ._set_size_and_colorspace ()
11941225
11951226 def _set_flip (self ):
11961227 bits = 0
@@ -1267,15 +1298,19 @@ def _set_window(
12671298 ((height >> 6 ) & 0x04 ) | ((width >> 8 ) & 0x03 ),
12681299 ]
12691300
1270- pclk_auto = 1
1301+ pclk_auto = 0
12711302 pclk_div = 8
12721303 clk_2x = 0
1273- clk_div = 7
1304+ clk_div = 0
1305+
1306+ if self ._colorspace != OV2640_COLOR_JPEG :
1307+ pclk_auto = 1
1308+ clk_div = 7
12741309
12751310 if mode == _OV2640_MODE_CIF :
12761311 regs = _ov2640_settings_to_cif
1277- # if pixformat is not jpeg :
1278- clk_div = 3
1312+ if self . _colorspace != OV2640_COLOR_JPEG :
1313+ clk_div = 3
12791314 elif mode == _OV2640_MODE_SVGA :
12801315 regs = _ov2640_settings_to_svga
12811316 else :
@@ -1294,7 +1329,7 @@ def _set_window(
12941329 time .sleep (0.01 )
12951330
12961331 # Reestablish colorspace
1297- self .colorspace = self . _colorspace
1332+ self ._set_colorspace ()
12981333
12991334 # Reestablish test pattern
13001335 if self ._test_pattern :
0 commit comments