27
27
"""
28
28
from micropython import const
29
29
30
+ try :
31
+ from typing import Optional , Literal
32
+ import circuitpython_typing
33
+ import busio
34
+ except ImportError :
35
+ pass
36
+
30
37
__version__ = "0.0.0-auto.0"
31
38
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_VC0706.git"
32
39
@@ -83,7 +90,7 @@ class VC0706:
83
90
:param int buffer_size: Receive buffer size
84
91
"""
85
92
86
- def __init__ (self , uart , * , buffer_size = 100 ):
93
+ def __init__ (self , uart : busio . UART , * , buffer_size : int = 100 ) -> None :
87
94
self ._uart = uart
88
95
self ._buffer = bytearray (buffer_size )
89
96
self ._frame_ptr = 0
@@ -97,20 +104,20 @@ def __init__(self, uart, *, buffer_size=100):
97
104
raise RuntimeError ("Failed to get response from VC0706, check wiring!" )
98
105
99
106
@property
100
- def version (self ):
107
+ def version (self ) -> str :
101
108
"""Return camera version byte string."""
102
109
# Clear buffer to ensure the end of a string can be found.
103
110
self ._send_command (_GEN_VERSION , b"\x01 " )
104
111
readlen = self ._read_response (self ._buffer , len (self ._buffer ))
105
112
return str (self ._buffer [:readlen ], "ascii" )
106
113
107
114
@property
108
- def baudrate (self ):
115
+ def baudrate (self ) -> Literal [ 9600 , 19200 , 38400 , 57600 , 115200 ] :
109
116
"""Return the currently configured baud rate."""
110
117
return self ._uart .baudrate
111
118
112
119
@baudrate .setter
113
- def baudrate (self , baud ) :
120
+ def baudrate (self , baud : Literal [ 9600 , 19200 , 38400 , 57600 , 115200 ]) -> None :
114
121
"""Set the baudrate to 9600, 19200, 38400, 57600, or 115200."""
115
122
divider = None
116
123
if baud == 9600 :
@@ -130,7 +137,7 @@ def baudrate(self, baud):
130
137
self ._uart .baudrate = baud
131
138
132
139
@property
133
- def image_size (self ):
140
+ def image_size (self ) -> int :
134
141
"""Get the current image size, will return a value of IMAGE_SIZE_640x480,
135
142
IMAGE_SIZE_320x240, or IMAGE_SIZE_160x120.
136
143
"""
@@ -139,7 +146,7 @@ def image_size(self):
139
146
return self ._buffer [5 ]
140
147
141
148
@image_size .setter
142
- def image_size (self , size ) :
149
+ def image_size (self , size : int ) -> bool :
143
150
"""Set the image size to a value of IMAGE_SIZE_640x480, IMAGE_SIZE_320x240, or
144
151
IMAGE_SIZE_160x120.
145
152
"""
@@ -153,8 +160,8 @@ def image_size(self, size):
153
160
)
154
161
155
162
@property
156
- def frame_length (self ):
157
- """Return the length in bytes of the currently capture frame/picture."""
163
+ def frame_length (self ) -> int :
164
+ """Return the length in bytes of the currently captured frame/picture."""
158
165
if not self ._run_command (_GET_FBUF_LEN , b"\x01 \x00 " , 9 ):
159
166
return 0
160
167
frame_length = self ._buffer [5 ]
@@ -166,18 +173,18 @@ def frame_length(self):
166
173
frame_length |= self ._buffer [8 ]
167
174
return frame_length
168
175
169
- def take_picture (self ):
176
+ def take_picture (self ) -> bool :
170
177
"""Tell the camera to take a picture. Returns True if successful."""
171
178
self ._frame_ptr = 0
172
179
return self ._run_command (_FBUF_CTRL , bytes ([0x1 , _STOPCURRENTFRAME ]), 5 )
173
180
174
- def resume_video (self ):
181
+ def resume_video (self ) -> bool :
175
182
"""Tell the camera to resume being a camera after the video has stopped
176
183
(Such as what happens when a picture is taken).
177
184
"""
178
185
return self ._run_command (_FBUF_CTRL , bytes ([0x1 , _RESUMEFRAME ]), 5 )
179
186
180
- def read_picture_into (self , buf ) :
187
+ def read_picture_into (self , buf : circuitpython_typing . WriteableBuffer ) -> int :
181
188
"""Read the next bytes of frame/picture data into the provided buffer.
182
189
Returns the number of bytes written to the buffer (might be less than
183
190
the size of the buffer). Buffer MUST be a multiple of 4 and 100 or
@@ -214,25 +221,27 @@ def read_picture_into(self, buf):
214
221
buf [i ] = self ._buffer [i ]
215
222
return n
216
223
217
- def motion_detected (self ):
224
+ def motion_detected (self ) -> bool :
218
225
"""Read the gesture detection result"""
219
226
self ._read_response (self ._buffer , len (self ._buffer ))
220
227
if not self ._verify_response (_COMM_MOTION_DETECTED ):
221
228
return False
222
229
return True
223
230
224
- def get_motion_detect (self ):
231
+ def get_motion_detect (self ) -> bool :
225
232
"""Query the gesture detection status"""
226
233
return self ._run_command (_COMM_MOTION_STATUS , bytes ([0x00 ]), 6 )
227
234
228
- def set_motion_detect (self , enabled ) :
235
+ def set_motion_detect (self , enabled : bool ) -> bool :
229
236
"""Set gesture detection status.
230
237
231
238
:param bool enabled: False to disable motion detected, True to enable motion detection.
232
239
"""
233
240
return self ._run_command (_COMM_MOTION_CTRL , bytes ([0x01 , enabled ]), 5 )
234
241
235
- def _run_command (self , cmd , args , resplen , flush = True ):
242
+ def _run_command (
243
+ self , cmd : int , args : bytes , resplen : int , flush : bool = True
244
+ ) -> bool :
236
245
if flush :
237
246
self ._read_response (self ._buffer , len (self ._buffer ))
238
247
self ._send_command (cmd , args )
@@ -242,18 +251,22 @@ def _run_command(self, cmd, args, resplen, flush=True):
242
251
return False
243
252
return True
244
253
245
- def _read_response (self , result , numbytes ):
254
+ def _read_response (
255
+ self , result : circuitpython_typing .WriteableBuffer , numbytes : int
256
+ ) -> Optional [int ]:
246
257
return self ._uart .readinto (memoryview (result )[0 :numbytes ])
247
258
248
- def _verify_response (self , cmd ) :
259
+ def _verify_response (self , cmd : int ) -> bool :
249
260
return (
250
261
self ._buffer [0 ] == 0x76
251
262
and self ._buffer [1 ] == _SERIAL
252
263
and self ._buffer [2 ] == cmd & 0xFF
253
264
and self ._buffer [3 ] == 0x00
254
265
)
255
266
256
- def _send_command (self , cmd , args = None ):
267
+ def _send_command (
268
+ self , cmd : int , args : Optional [circuitpython_typing .ReadableBuffer ] = None
269
+ ) -> None :
257
270
self ._command_header [0 ] = 0x56
258
271
self ._command_header [1 ] = _SERIAL
259
272
self ._command_header [2 ] = cmd & 0xFF
0 commit comments