|
| 1 | +# The MIT License (MIT) |
| 2 | +# |
| 3 | +# Copyright (c) 2017 Tony DiCola for Adafruit Industries |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in |
| 13 | +# all copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 21 | +# THE SOFTWARE. |
| 22 | +""" |
| 23 | +`adafruit_vc0706` |
| 24 | +==================================================== |
| 25 | +
|
| 26 | +VC0706 serial TTL camera module. Allows basic image capture and download of |
| 27 | +image data from the camera over a serial connection. See examples for demo |
| 28 | +of saving image to a SD card (must be wired up separately). |
| 29 | +
|
| 30 | +* Author(s): Tony DiCola |
| 31 | +""" |
| 32 | +import busio |
| 33 | + |
| 34 | + |
| 35 | +_VC0706_SERIAL = const(0x00) |
| 36 | +_VC0706_RESET = const(0x26) |
| 37 | +_VC0706_GEN_VERSION = const(0x11) |
| 38 | +_VC0706_SET_PORT = const(0x24) |
| 39 | +_VC0706_READ_FBUF = const(0x32) |
| 40 | +_VC0706_GET_FBUF_LEN = const(0x34) |
| 41 | +_VC0706_FBUF_CTRL = const(0x36) |
| 42 | +_VC0706_DOWNSIZE_CTRL = const(0x54) |
| 43 | +_VC0706_DOWNSIZE_STATUS = const(0x55) |
| 44 | +_VC0706_READ_DATA = const(0x30) |
| 45 | +_VC0706_WRITE_DATA = const(0x31) |
| 46 | +_VC0706_COMM_MOTION_CTRL = const(0x37) |
| 47 | +_VC0706_COMM_MOTION_STATUS = const(0x38) |
| 48 | +_VC0706_COMM_MOTION_DETECTED = const(0x39) |
| 49 | +_VC0706_MOTION_CTRL = const(0x42) |
| 50 | +_VC0706_MOTION_STATUS = const(0x43) |
| 51 | +_VC0706_TVOUT_CTRL = const(0x44) |
| 52 | +_VC0706_OSD_ADD_CHAR = const(0x45) |
| 53 | + |
| 54 | +_VC0706_STOPCURRENTFRAME = const(0x0) |
| 55 | +_VC0706_STOPNEXTFRAME = const(0x1) |
| 56 | +_VC0706_RESUMEFRAME = const(0x3) |
| 57 | +_VC0706_STEPFRAME = const(0x2) |
| 58 | + |
| 59 | +VC0706_640x480 = const(0x00) |
| 60 | +VC0706_320x240 = const(0x11) |
| 61 | +VC0706_160x120 = const(0x22) |
| 62 | + |
| 63 | +VC0706_MOTIONCONTROL = const(0x0) |
| 64 | +VC0706_UARTMOTION = const(0x01) |
| 65 | +VC0706_ACTIVATEMOTION = const(0x01) |
| 66 | + |
| 67 | +VC0706_SET_ZOOM = const(0x52) |
| 68 | +VC0706_GET_ZOOM = const(0x53) |
| 69 | + |
| 70 | +_CAMERA_DELAY = const(10) |
| 71 | + |
| 72 | + |
| 73 | +class VC0706: |
| 74 | + |
| 75 | + _COMMAND_HEADER = bytearray(3) |
| 76 | + |
| 77 | + def __init__(self, rx, tx, baudrate=38400, timeout=250, buffer_size=100): |
| 78 | + self._uart = busio.UART(tx, rx, baudrate=baudrate, timeout=timeout) |
| 79 | + self._buffer = bytearray(buffer_size) |
| 80 | + self._frame_ptr = 0 |
| 81 | + if not self._run_command(_VC0706_RESET, bytes([0x00]), 5): |
| 82 | + raise RuntimeError('Failed to get response from VC0706, check wiring!') |
| 83 | + |
| 84 | + @property |
| 85 | + def version(self): |
| 86 | + """Return camera version byte string.""" |
| 87 | + # Clear buffer to ensure the end of a string can be found. |
| 88 | + self._send_command(_VC0706_GEN_VERSION, bytes([0x01])) |
| 89 | + readlen = self._read_response(self._buffer, len(self._buffer)) |
| 90 | + return str(self._buffer[:readlen], 'ascii') |
| 91 | + |
| 92 | + @property |
| 93 | + def image_size(self): |
| 94 | + """Get the current image size, will return a value of VC0706_640x480, |
| 95 | + VC0706_320x240, or VC0706_160x120. |
| 96 | + """ |
| 97 | + if not self._run_command(_VC0706_READ_DATA, bytes([0x4, 0x4, 0x1, 0x00, |
| 98 | + 0x19]), 6): |
| 99 | + raise RuntimeError('Failed to read image size!') |
| 100 | + return self._buffer[5] |
| 101 | + |
| 102 | + @image_size.setter |
| 103 | + def image_size(self, size): |
| 104 | + """Set the image size to a value of VC0706_640x480, VC0706_320x240, or |
| 105 | + VC0706_160x120. |
| 106 | + """ |
| 107 | + if size not in (VC0706_640x480, VC0706_320x240, VC0706_160x120): |
| 108 | + raise ValueError('Size must be one of VC0706_640x480, VC0706_320x240, or VC0706_160x120!') |
| 109 | + return self._run_command(_VC0706_WRITE_DATA, bytes([0x05, 0x04, 0x01, |
| 110 | + 0x00, 0x19, size & 0xFF]), 5) |
| 111 | + |
| 112 | + @property |
| 113 | + def frame_length(self): |
| 114 | + """Return the length in bytes of the currently capture frame/picture. |
| 115 | + """ |
| 116 | + if not self._run_command(_VC0706_GET_FBUF_LEN, bytes([0x01, 0x00]), 9): |
| 117 | + return 0 |
| 118 | + frame_length = self._buffer[5] |
| 119 | + frame_length <<= 8 |
| 120 | + frame_length |= self._buffer[6] |
| 121 | + frame_length <<= 8 |
| 122 | + frame_length |= self._buffer[7] |
| 123 | + frame_length <<= 8 |
| 124 | + frame_length |= self._buffer[8] |
| 125 | + return frame_length |
| 126 | + |
| 127 | + def take_picture(self): |
| 128 | + """Tell the camera to take a picture. Returns True if successful. |
| 129 | + """ |
| 130 | + self._frame_ptr = 0 |
| 131 | + return self._run_command(_VC0706_FBUF_CTRL, bytes([0x1, |
| 132 | + _VC0706_STOPCURRENTFRAME]), 5) |
| 133 | + |
| 134 | + def read_picture_into(self, buf): |
| 135 | + """Read the next bytes of frame/picture data into the provided buffer. |
| 136 | + Returns the number of bytes written to the buffer (might be less than |
| 137 | + the size of the buffer). Buffer MUST be a multiple of 4 and 100 or |
| 138 | + less. Suggested buffer size is 32. |
| 139 | + """ |
| 140 | + n = len(buf) |
| 141 | + if n > 256 or n > (len(self._buffer)-5): |
| 142 | + raise ValueError('Buffer is too large!') |
| 143 | + if n % 4 != 0: |
| 144 | + raise ValueError('Buffer must be a multiple of 4! Try 32.') |
| 145 | + args = bytes([0x0C, 0x0, 0x0A, 0, 0, (self._frame_ptr >> 8) & 0xFF, |
| 146 | + self._frame_ptr & 0xFF, 0, 0, 0, n & 0xFF, |
| 147 | + (_CAMERA_DELAY >> 8) & 0xFF, _CAMERA_DELAY & 0xFF]) |
| 148 | + if not self._run_command(_VC0706_READ_FBUF, args, 5): |
| 149 | + return 0 |
| 150 | + if self._read_response(self._buffer, n+5) == 0: |
| 151 | + return 0 |
| 152 | + self._frame_ptr += n |
| 153 | + for i in range(n): |
| 154 | + buf[i] = self._buffer[i] |
| 155 | + return n |
| 156 | + |
| 157 | + def _run_command(self, cmd, args, resplen, flush=True): |
| 158 | + if flush: |
| 159 | + self._read_response(self._buffer, len(self._buffer)) |
| 160 | + self._send_command(cmd, args) |
| 161 | + if self._read_response(self._buffer, resplen) != resplen: |
| 162 | + return False |
| 163 | + if not self._verify_response(cmd): |
| 164 | + return False |
| 165 | + return True |
| 166 | + |
| 167 | + def _read_response(self, result, numbytes): |
| 168 | + return self._uart.readinto(result, numbytes) |
| 169 | + |
| 170 | + def _verify_response(self, cmd): |
| 171 | + return self._buffer[0] == 0x76 and self._buffer[1] == _VC0706_SERIAL \ |
| 172 | + and self._buffer[2] == cmd & 0xFF and self._buffer[3] == 0x00 |
| 173 | + |
| 174 | + def _send_command(self, cmd, args=None): |
| 175 | + self._COMMAND_HEADER[0] = 0x56 |
| 176 | + self._COMMAND_HEADER[1] = _VC0706_SERIAL |
| 177 | + self._COMMAND_HEADER[2] = cmd & 0xFF |
| 178 | + self._uart.write(self._COMMAND_HEADER) |
| 179 | + if args is not None and len(args) > 0: |
| 180 | + self._uart.write(args) |
0 commit comments