42
42
* Adafruit CircuitPython firmware for the ESP8622 and M0-based boards:
43
43
https://github.com/adafruit/circuitpython/releases
44
44
"""
45
- import busio
46
45
from micropython import const
47
46
48
47
__version__ = "0.0.0-auto.0"
79
78
IMAGE_SIZE_320x240 = const (0x11 )
80
79
IMAGE_SIZE_160x120 = const (0x22 )
81
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 )
82
86
83
87
_MOTIONCONTROL = const (0x0 )
84
88
_UARTMOTION = const (0x01 )
93
97
94
98
class VC0706 :
95
99
"""Driver for VC0706 serial TTL camera module.
96
-
97
- :param ~microcontroller.Pin rx: Receive pin
98
- :param ~microcontroller.Pin tx: Transmit pin
99
- :param int baudrate: Serial connection speed
100
- :param int timeout: Read timeout in seconds
100
+ :param ~busio.UART uart: uart serial or compatible interface
101
101
:param int buffer_size: Receive buffer size
102
102
"""
103
- def __init__ (self , rx , tx , * , baudrate = 38400 , timeout = 250 , buffer_size = 100 ):
104
- self ._uart = busio . UART ( tx , rx , baudrate = baudrate , timeout = timeout )
103
+ def __init__ (self , uart , * , buffer_size = 100 ):
104
+ self ._uart = uart
105
105
self ._buffer = bytearray (buffer_size )
106
106
self ._frame_ptr = 0
107
107
self ._command_header = bytearray (3 )
108
- if not self ._run_command (_RESET , bytes ([0x00 ]), 5 ):
109
- raise RuntimeError ('Failed to get response from VC0706, check wiring!' )
108
+ for _ in range (2 ): # 2 retries to reset then check resetted baudrate
109
+ for baud in (9600 , 19200 , 38400 , 57600 , 115200 ):
110
+ self ._uart .baudrate = baud
111
+ if self ._run_command (_RESET , b'\x00 ' , 5 ):
112
+ 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!' )
110
115
111
116
@property
112
117
def version (self ):
113
118
"""Return camera version byte string."""
114
119
# Clear buffer to ensure the end of a string can be found.
115
- self ._send_command (_GEN_VERSION , bytes ([ 0x01 ]) )
120
+ self ._send_command (_GEN_VERSION , b' \x01 ' )
116
121
readlen = self ._read_response (self ._buffer , len (self ._buffer ))
117
122
return str (self ._buffer [:readlen ], 'ascii' )
118
123
124
+ @property
125
+ def baudrate (self ):
126
+ """Return the currently configured baud rate."""
127
+ return self ._uart .baudrate
128
+
129
+ @baudrate .setter
130
+ def baudrate (self , baud ):
131
+ """Set the baudrate to 9600, 19200, 38400, 57600, or 115200. """
132
+ divider = None
133
+ if baud == 9600 :
134
+ divider = _BAUDRATE_9600
135
+ elif baud == 19200 :
136
+ divider = _BAUDRATE_19200
137
+ elif baud == 38400 :
138
+ divider = _BAUDRATE_38400
139
+ elif baud == 57600 :
140
+ divider = _BAUDRATE_57600
141
+ elif baud == 115200 :
142
+ divider = _BAUDRATE_115200
143
+ else :
144
+ raise ValueError ("Unsupported baud rate" )
145
+ args = [0x03 , 0x01 , (divider >> 8 ) & 0xFF , divider & 0xFF ]
146
+ self ._run_command (_SET_PORT , bytes (args ), 7 )
147
+ self ._uart .baudrate = baud
148
+
119
149
@property
120
150
def image_size (self ):
121
151
"""Get the current image size, will return a value of IMAGE_SIZE_640x480,
122
152
IMAGE_SIZE_320x240, or IMAGE_SIZE_160x120.
123
153
"""
124
154
if not self ._run_command (_READ_DATA ,
125
- bytes ([ 0x4 , 0x4 , 0x1 , 0x00 , 0x19 ]) ,
155
+ b' \0 x04 \x04 \x01 \x00 \x19 ' ,
126
156
6 ):
127
157
raise RuntimeError ('Failed to read image size!' )
128
158
return self ._buffer [5 ]
@@ -141,7 +171,7 @@ def image_size(self, size):
141
171
def frame_length (self ):
142
172
"""Return the length in bytes of the currently capture frame/picture.
143
173
"""
144
- if not self ._run_command (_GET_FBUF_LEN , bytes ([ 0x01 , 0x00 ]) , 9 ):
174
+ if not self ._run_command (_GET_FBUF_LEN , b' \x01 \x00 ' , 9 ):
145
175
return 0
146
176
frame_length = self ._buffer [5 ]
147
177
frame_length <<= 8
@@ -172,7 +202,7 @@ def read_picture_into(self, buf):
172
202
args = bytes ([0x0C , 0x0 , 0x0A , 0 , 0 , (self ._frame_ptr >> 8 ) & 0xFF ,
173
203
self ._frame_ptr & 0xFF , 0 , 0 , 0 , n & 0xFF ,
174
204
(_CAMERA_DELAY >> 8 ) & 0xFF , _CAMERA_DELAY & 0xFF ])
175
- if not self ._run_command (_READ_FBUF , args , 5 ):
205
+ if not self ._run_command (_READ_FBUF , args , 5 , flush = False ):
176
206
return 0
177
207
if self ._read_response (self ._buffer , n + 5 ) == 0 :
178
208
return 0
@@ -192,7 +222,7 @@ def _run_command(self, cmd, args, resplen, flush=True):
192
222
return True
193
223
194
224
def _read_response (self , result , numbytes ):
195
- return self ._uart .readinto (result , numbytes )
225
+ return self ._uart .readinto (memoryview ( result )[ 0 : numbytes ] )
196
226
197
227
def _verify_response (self , cmd ):
198
228
return (self ._buffer [0 ] == 0x76 and self ._buffer [1 ] == _SERIAL and
0 commit comments