Skip to content

Commit cf51ea2

Browse files
authored
Merge pull request #27 from FoamyGuy/type_info
adding type information
2 parents c13715b + ac3e9d1 commit cf51ea2

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

adafruit_vc0706.py

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@
2727
"""
2828
from micropython import const
2929

30+
try:
31+
from typing import Optional, Literal
32+
import circuitpython_typing
33+
import busio
34+
except ImportError:
35+
pass
36+
3037
__version__ = "0.0.0-auto.0"
3138
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_VC0706.git"
3239

@@ -83,7 +90,7 @@ class VC0706:
8390
:param int buffer_size: Receive buffer size
8491
"""
8592

86-
def __init__(self, uart, *, buffer_size=100):
93+
def __init__(self, uart: busio.UART, *, buffer_size: int = 100) -> None:
8794
self._uart = uart
8895
self._buffer = bytearray(buffer_size)
8996
self._frame_ptr = 0
@@ -97,20 +104,20 @@ def __init__(self, uart, *, buffer_size=100):
97104
raise RuntimeError("Failed to get response from VC0706, check wiring!")
98105

99106
@property
100-
def version(self):
107+
def version(self) -> str:
101108
"""Return camera version byte string."""
102109
# Clear buffer to ensure the end of a string can be found.
103110
self._send_command(_GEN_VERSION, b"\x01")
104111
readlen = self._read_response(self._buffer, len(self._buffer))
105112
return str(self._buffer[:readlen], "ascii")
106113

107114
@property
108-
def baudrate(self):
115+
def baudrate(self) -> Literal[9600, 19200, 38400, 57600, 115200]:
109116
"""Return the currently configured baud rate."""
110117
return self._uart.baudrate
111118

112119
@baudrate.setter
113-
def baudrate(self, baud):
120+
def baudrate(self, baud: Literal[9600, 19200, 38400, 57600, 115200]) -> None:
114121
"""Set the baudrate to 9600, 19200, 38400, 57600, or 115200."""
115122
divider = None
116123
if baud == 9600:
@@ -130,7 +137,7 @@ def baudrate(self, baud):
130137
self._uart.baudrate = baud
131138

132139
@property
133-
def image_size(self):
140+
def image_size(self) -> int:
134141
"""Get the current image size, will return a value of IMAGE_SIZE_640x480,
135142
IMAGE_SIZE_320x240, or IMAGE_SIZE_160x120.
136143
"""
@@ -139,7 +146,7 @@ def image_size(self):
139146
return self._buffer[5]
140147

141148
@image_size.setter
142-
def image_size(self, size):
149+
def image_size(self, size: int) -> bool:
143150
"""Set the image size to a value of IMAGE_SIZE_640x480, IMAGE_SIZE_320x240, or
144151
IMAGE_SIZE_160x120.
145152
"""
@@ -153,8 +160,8 @@ def image_size(self, size):
153160
)
154161

155162
@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."""
158165
if not self._run_command(_GET_FBUF_LEN, b"\x01\x00", 9):
159166
return 0
160167
frame_length = self._buffer[5]
@@ -166,18 +173,18 @@ def frame_length(self):
166173
frame_length |= self._buffer[8]
167174
return frame_length
168175

169-
def take_picture(self):
176+
def take_picture(self) -> bool:
170177
"""Tell the camera to take a picture. Returns True if successful."""
171178
self._frame_ptr = 0
172179
return self._run_command(_FBUF_CTRL, bytes([0x1, _STOPCURRENTFRAME]), 5)
173180

174-
def resume_video(self):
181+
def resume_video(self) -> bool:
175182
"""Tell the camera to resume being a camera after the video has stopped
176183
(Such as what happens when a picture is taken).
177184
"""
178185
return self._run_command(_FBUF_CTRL, bytes([0x1, _RESUMEFRAME]), 5)
179186

180-
def read_picture_into(self, buf):
187+
def read_picture_into(self, buf: circuitpython_typing.WriteableBuffer) -> int:
181188
"""Read the next bytes of frame/picture data into the provided buffer.
182189
Returns the number of bytes written to the buffer (might be less than
183190
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):
214221
buf[i] = self._buffer[i]
215222
return n
216223

217-
def motion_detected(self):
224+
def motion_detected(self) -> bool:
218225
"""Read the gesture detection result"""
219226
self._read_response(self._buffer, len(self._buffer))
220227
if not self._verify_response(_COMM_MOTION_DETECTED):
221228
return False
222229
return True
223230

224-
def get_motion_detect(self):
231+
def get_motion_detect(self) -> bool:
225232
"""Query the gesture detection status"""
226233
return self._run_command(_COMM_MOTION_STATUS, bytes([0x00]), 6)
227234

228-
def set_motion_detect(self, enabled):
235+
def set_motion_detect(self, enabled: bool) -> bool:
229236
"""Set gesture detection status.
230237
231238
:param bool enabled: False to disable motion detected, True to enable motion detection.
232239
"""
233240
return self._run_command(_COMM_MOTION_CTRL, bytes([0x01, enabled]), 5)
234241

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:
236245
if flush:
237246
self._read_response(self._buffer, len(self._buffer))
238247
self._send_command(cmd, args)
@@ -242,18 +251,22 @@ def _run_command(self, cmd, args, resplen, flush=True):
242251
return False
243252
return True
244253

245-
def _read_response(self, result, numbytes):
254+
def _read_response(
255+
self, result: circuitpython_typing.WriteableBuffer, numbytes: int
256+
) -> Optional[int]:
246257
return self._uart.readinto(memoryview(result)[0:numbytes])
247258

248-
def _verify_response(self, cmd):
259+
def _verify_response(self, cmd: int) -> bool:
249260
return (
250261
self._buffer[0] == 0x76
251262
and self._buffer[1] == _SERIAL
252263
and self._buffer[2] == cmd & 0xFF
253264
and self._buffer[3] == 0x00
254265
)
255266

256-
def _send_command(self, cmd, args=None):
267+
def _send_command(
268+
self, cmd: int, args: Optional[circuitpython_typing.ReadableBuffer] = None
269+
) -> None:
257270
self._command_header[0] = 0x56
258271
self._command_header[1] = _SERIAL
259272
self._command_header[2] = cmd & 0xFF

0 commit comments

Comments
 (0)