48
48
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_VC0706.git"
49
49
50
50
# pylint: disable=bad-whitespace
51
- _SERIAL = const (0x00 )
52
- _RESET = const (0x26 )
53
- _GEN_VERSION = const (0x11 )
54
- _SET_PORT = const (0x24 )
55
- _READ_FBUF = const (0x32 )
56
- _GET_FBUF_LEN = const (0x34 )
57
- _FBUF_CTRL = const (0x36 )
58
- _DOWNSIZE_CTRL = const (0x54 )
59
- _DOWNSIZE_STATUS = const (0x55 )
60
- _READ_DATA = const (0x30 )
61
- _WRITE_DATA = const (0x31 )
62
- _COMM_MOTION_CTRL = const (0x37 )
63
- _COMM_MOTION_STATUS = const (0x38 )
51
+ _SERIAL = const (0x00 )
52
+ _RESET = const (0x26 )
53
+ _GEN_VERSION = const (0x11 )
54
+ _SET_PORT = const (0x24 )
55
+ _READ_FBUF = const (0x32 )
56
+ _GET_FBUF_LEN = const (0x34 )
57
+ _FBUF_CTRL = const (0x36 )
58
+ _DOWNSIZE_CTRL = const (0x54 )
59
+ _DOWNSIZE_STATUS = const (0x55 )
60
+ _READ_DATA = const (0x30 )
61
+ _WRITE_DATA = const (0x31 )
62
+ _COMM_MOTION_CTRL = const (0x37 )
63
+ _COMM_MOTION_STATUS = const (0x38 )
64
64
_COMM_MOTION_DETECTED = const (0x39 )
65
- _MOTION_CTRL = const (0x42 )
66
- _MOTION_STATUS = const (0x43 )
67
- _TVOUT_CTRL = const (0x44 )
68
- _OSD_ADD_CHAR = const (0x45 )
65
+ _MOTION_CTRL = const (0x42 )
66
+ _MOTION_STATUS = const (0x43 )
67
+ _TVOUT_CTRL = const (0x44 )
68
+ _OSD_ADD_CHAR = const (0x45 )
69
69
70
- _STOPCURRENTFRAME = const (0x0 )
71
- _STOPNEXTFRAME = const (0x1 )
72
- _RESUMEFRAME = const (0x3 )
73
- _STEPFRAME = const (0x2 )
70
+ _STOPCURRENTFRAME = const (0x0 )
71
+ _STOPNEXTFRAME = const (0x1 )
72
+ _RESUMEFRAME = const (0x3 )
73
+ _STEPFRAME = const (0x2 )
74
74
75
75
# pylint doesn't like the lowercase x but it makes it more readable.
76
76
# pylint: disable=invalid-name
77
- IMAGE_SIZE_640x480 = const (0x00 )
78
- IMAGE_SIZE_320x240 = const (0x11 )
79
- IMAGE_SIZE_160x120 = const (0x22 )
77
+ IMAGE_SIZE_640x480 = const (0x00 )
78
+ IMAGE_SIZE_320x240 = const (0x11 )
79
+ IMAGE_SIZE_160x120 = const (0x22 )
80
80
# pylint: enable=invalid-name
81
- _BAUDRATE_9600 = const (0xAEC8 )
82
- _BAUDRATE_19200 = const (0x56E4 )
83
- _BAUDRATE_38400 = const (0x2AF2 )
84
- _BAUDRATE_57600 = const (0x1C1C )
85
- _BAUDRATE_115200 = const (0x0DA6 )
81
+ _BAUDRATE_9600 = const (0xAEC8 )
82
+ _BAUDRATE_19200 = const (0x56E4 )
83
+ _BAUDRATE_38400 = const (0x2AF2 )
84
+ _BAUDRATE_57600 = const (0x1C1C )
85
+ _BAUDRATE_115200 = const (0x0DA6 )
86
86
87
- _MOTIONCONTROL = const (0x0 )
88
- _UARTMOTION = const (0x01 )
89
- _ACTIVATEMOTION = const (0x01 )
87
+ _MOTIONCONTROL = const (0x0 )
88
+ _UARTMOTION = const (0x01 )
89
+ _ACTIVATEMOTION = const (0x01 )
90
90
91
- __SET_ZOOM = const (0x52 )
92
- __GET_ZOOM = const (0x53 )
91
+ __SET_ZOOM = const (0x52 )
92
+ __GET_ZOOM = const (0x53 )
93
93
94
- _CAMERA_DELAY = const (10 )
94
+ _CAMERA_DELAY = const (10 )
95
95
# pylint: enable=bad-whitespace
96
96
97
97
@@ -100,26 +100,27 @@ class VC0706:
100
100
:param ~busio.UART uart: uart serial or compatible interface
101
101
:param int buffer_size: Receive buffer size
102
102
"""
103
+
103
104
def __init__ (self , uart , * , buffer_size = 100 ):
104
105
self ._uart = uart
105
106
self ._buffer = bytearray (buffer_size )
106
107
self ._frame_ptr = 0
107
108
self ._command_header = bytearray (3 )
108
- for _ in range (2 ): # 2 retries to reset then check resetted baudrate
109
+ for _ in range (2 ): # 2 retries to reset then check resetted baudrate
109
110
for baud in (9600 , 19200 , 38400 , 57600 , 115200 ):
110
111
self ._uart .baudrate = baud
111
- if self ._run_command (_RESET , b' \x00 ' , 5 ):
112
+ if self ._run_command (_RESET , b" \x00 " , 5 ):
112
113
break
113
- else : # for:else rocks! http://book.pythontips.com/en/latest/for_-_else.html
114
- raise RuntimeError (' Failed to get response from VC0706, check wiring!' )
114
+ else : # for:else rocks! http://book.pythontips.com/en/latest/for_-_else.html
115
+ raise RuntimeError (" Failed to get response from VC0706, check wiring!" )
115
116
116
117
@property
117
118
def version (self ):
118
119
"""Return camera version byte string."""
119
120
# Clear buffer to ensure the end of a string can be found.
120
- self ._send_command (_GEN_VERSION , b' \x01 ' )
121
+ self ._send_command (_GEN_VERSION , b" \x01 " )
121
122
readlen = self ._read_response (self ._buffer , len (self ._buffer ))
122
- return str (self ._buffer [:readlen ], ' ascii' )
123
+ return str (self ._buffer [:readlen ], " ascii" )
123
124
124
125
@property
125
126
def baudrate (self ):
@@ -142,7 +143,7 @@ def baudrate(self, baud):
142
143
divider = _BAUDRATE_115200
143
144
else :
144
145
raise ValueError ("Unsupported baud rate" )
145
- args = [0x03 , 0x01 , (divider >> 8 ) & 0xFF , divider & 0xFF ]
146
+ args = [0x03 , 0x01 , (divider >> 8 ) & 0xFF , divider & 0xFF ]
146
147
self ._run_command (_SET_PORT , bytes (args ), 7 )
147
148
self ._uart .baudrate = baud
148
149
@@ -151,10 +152,8 @@ def image_size(self):
151
152
"""Get the current image size, will return a value of IMAGE_SIZE_640x480,
152
153
IMAGE_SIZE_320x240, or IMAGE_SIZE_160x120.
153
154
"""
154
- if not self ._run_command (_READ_DATA ,
155
- b'\0 x04\x04 \x01 \x00 \x19 ' ,
156
- 6 ):
157
- raise RuntimeError ('Failed to read image size!' )
155
+ if not self ._run_command (_READ_DATA , b"\0 x04\x04 \x01 \x00 \x19 " , 6 ):
156
+ raise RuntimeError ("Failed to read image size!" )
158
157
return self ._buffer [5 ]
159
158
160
159
@image_size .setter
@@ -163,15 +162,19 @@ def image_size(self, size):
163
162
IMAGE_SIZE_160x120.
164
163
"""
165
164
if size not in (IMAGE_SIZE_640x480 , IMAGE_SIZE_320x240 , IMAGE_SIZE_160x120 ):
166
- raise ValueError ("Size must be one of IMAGE_SIZE_640x480, IMAGE_SIZE_320x240, or "
167
- "IMAGE_SIZE_160x120!" )
168
- return self ._run_command (_WRITE_DATA , bytes ([0x05 , 0x04 , 0x01 , 0x00 , 0x19 , size & 0xFF ]), 5 )
165
+ raise ValueError (
166
+ "Size must be one of IMAGE_SIZE_640x480, IMAGE_SIZE_320x240, or "
167
+ "IMAGE_SIZE_160x120!"
168
+ )
169
+ return self ._run_command (
170
+ _WRITE_DATA , bytes ([0x05 , 0x04 , 0x01 , 0x00 , 0x19 , size & 0xFF ]), 5
171
+ )
169
172
170
173
@property
171
174
def frame_length (self ):
172
175
"""Return the length in bytes of the currently capture frame/picture.
173
176
"""
174
- if not self ._run_command (_GET_FBUF_LEN , b' \x01 \x00 ' , 9 ):
177
+ if not self ._run_command (_GET_FBUF_LEN , b" \x01 \x00 " , 9 ):
175
178
return 0
176
179
frame_length = self ._buffer [5 ]
177
180
frame_length <<= 8
@@ -195,16 +198,30 @@ def read_picture_into(self, buf):
195
198
less. Suggested buffer size is 32.
196
199
"""
197
200
n = len (buf )
198
- if n > 256 or n > (len (self ._buffer )- 5 ):
199
- raise ValueError (' Buffer is too large!' )
201
+ if n > 256 or n > (len (self ._buffer ) - 5 ):
202
+ raise ValueError (" Buffer is too large!" )
200
203
if n % 4 != 0 :
201
- raise ValueError ('Buffer must be a multiple of 4! Try 32.' )
202
- args = bytes ([0x0C , 0x0 , 0x0A , 0 , 0 , (self ._frame_ptr >> 8 ) & 0xFF ,
203
- self ._frame_ptr & 0xFF , 0 , 0 , 0 , n & 0xFF ,
204
- (_CAMERA_DELAY >> 8 ) & 0xFF , _CAMERA_DELAY & 0xFF ])
204
+ raise ValueError ("Buffer must be a multiple of 4! Try 32." )
205
+ args = bytes (
206
+ [
207
+ 0x0C ,
208
+ 0x0 ,
209
+ 0x0A ,
210
+ 0 ,
211
+ 0 ,
212
+ (self ._frame_ptr >> 8 ) & 0xFF ,
213
+ self ._frame_ptr & 0xFF ,
214
+ 0 ,
215
+ 0 ,
216
+ 0 ,
217
+ n & 0xFF ,
218
+ (_CAMERA_DELAY >> 8 ) & 0xFF ,
219
+ _CAMERA_DELAY & 0xFF ,
220
+ ]
221
+ )
205
222
if not self ._run_command (_READ_FBUF , args , 5 , flush = False ):
206
223
return 0
207
- if self ._read_response (self ._buffer , n + 5 ) == 0 :
224
+ if self ._read_response (self ._buffer , n + 5 ) == 0 :
208
225
return 0
209
226
self ._frame_ptr += n
210
227
for i in range (n ):
@@ -225,8 +242,12 @@ def _read_response(self, result, numbytes):
225
242
return self ._uart .readinto (memoryview (result )[0 :numbytes ])
226
243
227
244
def _verify_response (self , cmd ):
228
- return (self ._buffer [0 ] == 0x76 and self ._buffer [1 ] == _SERIAL and
229
- self ._buffer [2 ] == cmd & 0xFF and self ._buffer [3 ] == 0x00 )
245
+ return (
246
+ self ._buffer [0 ] == 0x76
247
+ and self ._buffer [1 ] == _SERIAL
248
+ and self ._buffer [2 ] == cmd & 0xFF
249
+ and self ._buffer [3 ] == 0x00
250
+ )
230
251
231
252
def _send_command (self , cmd , args = None ):
232
253
self ._command_header [0 ] = 0x56
0 commit comments