MODIFIED BY FEVZI YAZGAN BOUDRATE AND IMAGESIZE FUNCTION ADDED ALSO IT SENDS THE IMAGE AT ONE TIME TO THE MBED. SO IT IS FASTER TO SEND IMAGE AT A ONE TIME:)

Dependents:   10_Camera_LS_Y201_TestProgram

Committer:
fyazgan
Date:
Wed Mar 02 05:43:09 2011 +0000
Revision:
0:92b7ae8bc9f5

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fyazgan 0:92b7ae8bc9f5 1 /**
fyazgan 0:92b7ae8bc9f5 2 * =============================================================================
fyazgan 0:92b7ae8bc9f5 3 * LS-Y201 device driver class (Version 0.0.1)
fyazgan 0:92b7ae8bc9f5 4 * Reference documents: LinkSprite JPEG Color Camera Serial UART Interface
fyazgan 0:92b7ae8bc9f5 5 * January 2010
fyazgan 0:92b7ae8bc9f5 6 * =============================================================================
fyazgan 0:92b7ae8bc9f5 7 * Copyright (c) 2010 Shinichiro Nakamura (CuBeatSystems)
fyazgan 0:92b7ae8bc9f5 8 *
fyazgan 0:92b7ae8bc9f5 9 * Permission is hereby granted, free of charge, to any person obtaining a copy
fyazgan 0:92b7ae8bc9f5 10 * of this software and associated documentation files (the "Software"), to deal
fyazgan 0:92b7ae8bc9f5 11 * in the Software without restriction, including without limitation the rights
fyazgan 0:92b7ae8bc9f5 12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
fyazgan 0:92b7ae8bc9f5 13 * copies of the Software, and to permit persons to whom the Software is
fyazgan 0:92b7ae8bc9f5 14 * furnished to do so, subject to the following conditions:
fyazgan 0:92b7ae8bc9f5 15 *
fyazgan 0:92b7ae8bc9f5 16 * The above copyright notice and this permission notice shall be included in
fyazgan 0:92b7ae8bc9f5 17 * all copies or substantial portions of the Software.
fyazgan 0:92b7ae8bc9f5 18 *
fyazgan 0:92b7ae8bc9f5 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
fyazgan 0:92b7ae8bc9f5 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
fyazgan 0:92b7ae8bc9f5 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
fyazgan 0:92b7ae8bc9f5 22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
fyazgan 0:92b7ae8bc9f5 23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
fyazgan 0:92b7ae8bc9f5 24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
fyazgan 0:92b7ae8bc9f5 25 * THE SOFTWARE.
fyazgan 0:92b7ae8bc9f5 26 * =============================================================================
fyazgan 0:92b7ae8bc9f5 27 */
fyazgan 0:92b7ae8bc9f5 28
fyazgan 0:92b7ae8bc9f5 29 #include "Camera_LS_Y201.h"
fyazgan 0:92b7ae8bc9f5 30
fyazgan 0:92b7ae8bc9f5 31 /**
fyazgan 0:92b7ae8bc9f5 32 * Create.
fyazgan 0:92b7ae8bc9f5 33 *
fyazgan 0:92b7ae8bc9f5 34 * @param tx Transmitter.
fyazgan 0:92b7ae8bc9f5 35 * @param rx Receiver.
fyazgan 0:92b7ae8bc9f5 36 */
fyazgan 0:92b7ae8bc9f5 37 Camera_LS_Y201::Camera_LS_Y201(PinName tx, PinName rx,Bauds bs) : serial(tx, rx) {
fyazgan 0:92b7ae8bc9f5 38 switch (bs) {
fyazgan 0:92b7ae8bc9f5 39 case Default:
fyazgan 0:92b7ae8bc9f5 40 serial.baud(38400);
fyazgan 0:92b7ae8bc9f5 41 break;
fyazgan 0:92b7ae8bc9f5 42 case Bauds57600:
fyazgan 0:92b7ae8bc9f5 43 serial.baud(57600);
fyazgan 0:92b7ae8bc9f5 44 break;
fyazgan 0:92b7ae8bc9f5 45 case Bauds115200:
fyazgan 0:92b7ae8bc9f5 46 serial.baud(115200);
fyazgan 0:92b7ae8bc9f5 47 break;
fyazgan 0:92b7ae8bc9f5 48 case Bauds1228800:
fyazgan 0:92b7ae8bc9f5 49 serial.baud(1228800);
fyazgan 0:92b7ae8bc9f5 50 break;
fyazgan 0:92b7ae8bc9f5 51 }
fyazgan 0:92b7ae8bc9f5 52 }
fyazgan 0:92b7ae8bc9f5 53
fyazgan 0:92b7ae8bc9f5 54 /**
fyazgan 0:92b7ae8bc9f5 55 * Dispose.
fyazgan 0:92b7ae8bc9f5 56 */
fyazgan 0:92b7ae8bc9f5 57 Camera_LS_Y201::~Camera_LS_Y201() {
fyazgan 0:92b7ae8bc9f5 58 }
fyazgan 0:92b7ae8bc9f5 59
fyazgan 0:92b7ae8bc9f5 60 /**
fyazgan 0:92b7ae8bc9f5 61 * Reset module.
fyazgan 0:92b7ae8bc9f5 62 *
fyazgan 0:92b7ae8bc9f5 63 * @return Error code.
fyazgan 0:92b7ae8bc9f5 64 */
fyazgan 0:92b7ae8bc9f5 65 Camera_LS_Y201::ErrorCode Camera_LS_Y201::reset() {
fyazgan 0:92b7ae8bc9f5 66 uint8_t send[4] = {
fyazgan 0:92b7ae8bc9f5 67 0x56,
fyazgan 0:92b7ae8bc9f5 68 0x00,
fyazgan 0:92b7ae8bc9f5 69 0x26,
fyazgan 0:92b7ae8bc9f5 70 0x00
fyazgan 0:92b7ae8bc9f5 71 };
fyazgan 0:92b7ae8bc9f5 72 uint8_t recv[4];
fyazgan 0:92b7ae8bc9f5 73
fyazgan 0:92b7ae8bc9f5 74 waitIdle();
fyazgan 0:92b7ae8bc9f5 75 if (!sendBytes(send, sizeof(send), 200 * 1000)) {
fyazgan 0:92b7ae8bc9f5 76 return SendError;
fyazgan 0:92b7ae8bc9f5 77 }
fyazgan 0:92b7ae8bc9f5 78 if (!recvBytes(recv, sizeof(recv), 200 * 1000)) {
fyazgan 0:92b7ae8bc9f5 79 return RecvError;
fyazgan 0:92b7ae8bc9f5 80 }
fyazgan 0:92b7ae8bc9f5 81 if ((recv[0] == 0x76)
fyazgan 0:92b7ae8bc9f5 82 && (recv[1] == 0x00)
fyazgan 0:92b7ae8bc9f5 83 && (recv[2] == 0x26)
fyazgan 0:92b7ae8bc9f5 84 && (recv[3] == 0x00)) {
fyazgan 0:92b7ae8bc9f5 85 ErrorCode r = waitInitEnd();
fyazgan 0:92b7ae8bc9f5 86 if (r != NoError) {
fyazgan 0:92b7ae8bc9f5 87 return r;
fyazgan 0:92b7ae8bc9f5 88 }
fyazgan 0:92b7ae8bc9f5 89 wait(4);
fyazgan 0:92b7ae8bc9f5 90 return NoError;
fyazgan 0:92b7ae8bc9f5 91 } else {
fyazgan 0:92b7ae8bc9f5 92 return UnexpectedReply;
fyazgan 0:92b7ae8bc9f5 93 }
fyazgan 0:92b7ae8bc9f5 94 }
fyazgan 0:92b7ae8bc9f5 95
fyazgan 0:92b7ae8bc9f5 96 /**
fyazgan 0:92b7ae8bc9f5 97 * Set image size.
fyazgan 0:92b7ae8bc9f5 98 *
fyazgan 0:92b7ae8bc9f5 99 * @param is Image size.
fyazgan 0:92b7ae8bc9f5 100 * @return Error code.
fyazgan 0:92b7ae8bc9f5 101 */
fyazgan 0:92b7ae8bc9f5 102 Camera_LS_Y201::ErrorCode Camera_LS_Y201::setImageSize(ImageSize is) {
fyazgan 0:92b7ae8bc9f5 103 uint8_t send[9] = {
fyazgan 0:92b7ae8bc9f5 104 0x56,
fyazgan 0:92b7ae8bc9f5 105 0x00,
fyazgan 0:92b7ae8bc9f5 106 0x31,
fyazgan 0:92b7ae8bc9f5 107 0x05,
fyazgan 0:92b7ae8bc9f5 108 0x04,
fyazgan 0:92b7ae8bc9f5 109 0x01,
fyazgan 0:92b7ae8bc9f5 110 0x00,
fyazgan 0:92b7ae8bc9f5 111 0x19,
fyazgan 0:92b7ae8bc9f5 112 0x00 // 0x11:320x240, 0x00:640x480, 0x22:160x120
fyazgan 0:92b7ae8bc9f5 113 };
fyazgan 0:92b7ae8bc9f5 114 uint8_t recv[5];
fyazgan 0:92b7ae8bc9f5 115 switch (is) {
fyazgan 0:92b7ae8bc9f5 116 case ImageSize160x120:
fyazgan 0:92b7ae8bc9f5 117 send[8] = 0x22;
fyazgan 0:92b7ae8bc9f5 118 break;
fyazgan 0:92b7ae8bc9f5 119 case ImageSize320x280:
fyazgan 0:92b7ae8bc9f5 120 send[8] = 0x11;
fyazgan 0:92b7ae8bc9f5 121 break;
fyazgan 0:92b7ae8bc9f5 122 case ImageSize640x480:
fyazgan 0:92b7ae8bc9f5 123 send[8] = 0x00;
fyazgan 0:92b7ae8bc9f5 124 break;
fyazgan 0:92b7ae8bc9f5 125 default:
fyazgan 0:92b7ae8bc9f5 126 return InvalidArguments;
fyazgan 0:92b7ae8bc9f5 127 }
fyazgan 0:92b7ae8bc9f5 128 if (!sendBytes(send, sizeof(send), 200 * 1000)) {
fyazgan 0:92b7ae8bc9f5 129 return SendError;
fyazgan 0:92b7ae8bc9f5 130 }
fyazgan 0:92b7ae8bc9f5 131 if (!recvBytes(recv, sizeof(recv), 200 * 1000)) {
fyazgan 0:92b7ae8bc9f5 132 return RecvError;
fyazgan 0:92b7ae8bc9f5 133 }
fyazgan 0:92b7ae8bc9f5 134 if ((recv[0] == 0x76)
fyazgan 0:92b7ae8bc9f5 135 && (recv[1] == 0x00)
fyazgan 0:92b7ae8bc9f5 136 && (recv[2] == 0x31)
fyazgan 0:92b7ae8bc9f5 137 && (recv[3] == 0x00)
fyazgan 0:92b7ae8bc9f5 138 && (recv[4] == 0x00)) {
fyazgan 0:92b7ae8bc9f5 139 wait(1);
fyazgan 0:92b7ae8bc9f5 140 return reset();
fyazgan 0:92b7ae8bc9f5 141 } else {
fyazgan 0:92b7ae8bc9f5 142 return UnexpectedReply;
fyazgan 0:92b7ae8bc9f5 143 }
fyazgan 0:92b7ae8bc9f5 144 }
fyazgan 0:92b7ae8bc9f5 145
fyazgan 0:92b7ae8bc9f5 146 /**
fyazgan 0:92b7ae8bc9f5 147 * Take picture.
fyazgan 0:92b7ae8bc9f5 148 *
fyazgan 0:92b7ae8bc9f5 149 * @return Error code.
fyazgan 0:92b7ae8bc9f5 150 */
fyazgan 0:92b7ae8bc9f5 151 Camera_LS_Y201::ErrorCode Camera_LS_Y201::takePicture() {
fyazgan 0:92b7ae8bc9f5 152 uint8_t send[5] = {
fyazgan 0:92b7ae8bc9f5 153 0x56,
fyazgan 0:92b7ae8bc9f5 154 0x00,
fyazgan 0:92b7ae8bc9f5 155 0x36,
fyazgan 0:92b7ae8bc9f5 156 0x01,
fyazgan 0:92b7ae8bc9f5 157 0x00
fyazgan 0:92b7ae8bc9f5 158 };
fyazgan 0:92b7ae8bc9f5 159 uint8_t recv[5];
fyazgan 0:92b7ae8bc9f5 160
fyazgan 0:92b7ae8bc9f5 161 if (!sendBytes(send, sizeof(send), 200 * 1000)) {
fyazgan 0:92b7ae8bc9f5 162 return SendError;
fyazgan 0:92b7ae8bc9f5 163 }
fyazgan 0:92b7ae8bc9f5 164 if (!recvBytes(recv, sizeof(recv), 200 * 1000)) {
fyazgan 0:92b7ae8bc9f5 165 return RecvError;
fyazgan 0:92b7ae8bc9f5 166 }
fyazgan 0:92b7ae8bc9f5 167
fyazgan 0:92b7ae8bc9f5 168 if ((recv[0] == 0x76)
fyazgan 0:92b7ae8bc9f5 169 && (recv[1] == 0x00)
fyazgan 0:92b7ae8bc9f5 170 && (recv[2] == 0x36)
fyazgan 0:92b7ae8bc9f5 171 && (recv[3] == 0x00)
fyazgan 0:92b7ae8bc9f5 172 && (recv[4] == 0x00)) {
fyazgan 0:92b7ae8bc9f5 173 /*
fyazgan 0:92b7ae8bc9f5 174 * I think the camera need a time for operating.
fyazgan 0:92b7ae8bc9f5 175 * But there is no any comments on the documents.
fyazgan 0:92b7ae8bc9f5 176 */
fyazgan 0:92b7ae8bc9f5 177 wait_ms(100);
fyazgan 0:92b7ae8bc9f5 178 return NoError;
fyazgan 0:92b7ae8bc9f5 179 } else {
fyazgan 0:92b7ae8bc9f5 180 return UnexpectedReply;
fyazgan 0:92b7ae8bc9f5 181 }
fyazgan 0:92b7ae8bc9f5 182 }
fyazgan 0:92b7ae8bc9f5 183
fyazgan 0:92b7ae8bc9f5 184 /**
fyazgan 0:92b7ae8bc9f5 185 * Read jpeg file size.
fyazgan 0:92b7ae8bc9f5 186 *
fyazgan 0:92b7ae8bc9f5 187 * @param fileSize File size.
fyazgan 0:92b7ae8bc9f5 188 * @return Error code.
fyazgan 0:92b7ae8bc9f5 189 */
fyazgan 0:92b7ae8bc9f5 190 Camera_LS_Y201::ErrorCode Camera_LS_Y201::readJpegFileSize(int *fileSize) {
fyazgan 0:92b7ae8bc9f5 191 uint8_t send[5] = {
fyazgan 0:92b7ae8bc9f5 192 0x56,
fyazgan 0:92b7ae8bc9f5 193 0x00,
fyazgan 0:92b7ae8bc9f5 194 0x34,
fyazgan 0:92b7ae8bc9f5 195 0x01,
fyazgan 0:92b7ae8bc9f5 196 0x00
fyazgan 0:92b7ae8bc9f5 197 };
fyazgan 0:92b7ae8bc9f5 198 uint8_t recv[9];
fyazgan 0:92b7ae8bc9f5 199
fyazgan 0:92b7ae8bc9f5 200 if (!sendBytes(send, sizeof(send), 200 * 1000)) {
fyazgan 0:92b7ae8bc9f5 201 return SendError;
fyazgan 0:92b7ae8bc9f5 202 }
fyazgan 0:92b7ae8bc9f5 203 if (!recvBytes(recv, sizeof(recv), 200 * 1000)) {
fyazgan 0:92b7ae8bc9f5 204 return RecvError;
fyazgan 0:92b7ae8bc9f5 205 }
fyazgan 0:92b7ae8bc9f5 206
fyazgan 0:92b7ae8bc9f5 207 if ((recv[0] == 0x76)
fyazgan 0:92b7ae8bc9f5 208 && (recv[1] == 0x00)
fyazgan 0:92b7ae8bc9f5 209 && (recv[2] == 0x34)
fyazgan 0:92b7ae8bc9f5 210 && (recv[3] == 0x00)
fyazgan 0:92b7ae8bc9f5 211 && (recv[4] == 0x04)
fyazgan 0:92b7ae8bc9f5 212 && (recv[5] == 0x00)
fyazgan 0:92b7ae8bc9f5 213 && (recv[6] == 0x00)) {
fyazgan 0:92b7ae8bc9f5 214 *fileSize = ((recv[7] & 0x00ff) << 8)
fyazgan 0:92b7ae8bc9f5 215 | ((recv[8] & 0x00ff) << 0);
fyazgan 0:92b7ae8bc9f5 216 return NoError;
fyazgan 0:92b7ae8bc9f5 217 } else {
fyazgan 0:92b7ae8bc9f5 218 return UnexpectedReply;
fyazgan 0:92b7ae8bc9f5 219 }
fyazgan 0:92b7ae8bc9f5 220 }
fyazgan 0:92b7ae8bc9f5 221
fyazgan 0:92b7ae8bc9f5 222 /**
fyazgan 0:92b7ae8bc9f5 223 * Read jpeg file content.
fyazgan 0:92b7ae8bc9f5 224 *
fyazgan 0:92b7ae8bc9f5 225 * @param func A pointer to a call back function.
fyazgan 0:92b7ae8bc9f5 226 * @return Error code.
fyazgan 0:92b7ae8bc9f5 227 */
fyazgan 0:92b7ae8bc9f5 228 Camera_LS_Y201::ErrorCode Camera_LS_Y201::readJpegFileContent(void (*func)( int total, uint8_t *buf, size_t siz)) {
fyazgan 0:92b7ae8bc9f5 229 uint8_t send[16] = {
fyazgan 0:92b7ae8bc9f5 230 0x56,
fyazgan 0:92b7ae8bc9f5 231 0x00,
fyazgan 0:92b7ae8bc9f5 232 0x32,
fyazgan 0:92b7ae8bc9f5 233 0x0C,
fyazgan 0:92b7ae8bc9f5 234 0x00,
fyazgan 0:92b7ae8bc9f5 235 0x0A,
fyazgan 0:92b7ae8bc9f5 236 0x00,
fyazgan 0:92b7ae8bc9f5 237 0x00,
fyazgan 0:92b7ae8bc9f5 238 0x00, // MH
fyazgan 0:92b7ae8bc9f5 239 0x00, // ML
fyazgan 0:92b7ae8bc9f5 240 0x00,
fyazgan 0:92b7ae8bc9f5 241 0x00,
fyazgan 0:92b7ae8bc9f5 242 0x00, // KH
fyazgan 0:92b7ae8bc9f5 243 0x00, // KL
fyazgan 0:92b7ae8bc9f5 244 0x00, // XX
fyazgan 0:92b7ae8bc9f5 245 0x00 // XX
fyazgan 0:92b7ae8bc9f5 246 };
fyazgan 0:92b7ae8bc9f5 247
fyazgan 0:92b7ae8bc9f5 248 /*
fyazgan 0:92b7ae8bc9f5 249 * Get the data size.
fyazgan 0:92b7ae8bc9f5 250 */
fyazgan 0:92b7ae8bc9f5 251 int siz_total = 0;
fyazgan 0:92b7ae8bc9f5 252 ErrorCode r = readJpegFileSize(&siz_total);
fyazgan 0:92b7ae8bc9f5 253 if (r != NoError) {
fyazgan 0:92b7ae8bc9f5 254 return r;
fyazgan 0:92b7ae8bc9f5 255 }
fyazgan 0:92b7ae8bc9f5 256
fyazgan 0:92b7ae8bc9f5 257 int pac=(((siz_total/8))+1)*8;
fyazgan 0:92b7ae8bc9f5 258 uint8_t body[pac];
fyazgan 0:92b7ae8bc9f5 259 uint16_t m = 0; // Staring address.
fyazgan 0:92b7ae8bc9f5 260 uint16_t k = sizeof(body); // Packet size.
fyazgan 0:92b7ae8bc9f5 261 uint16_t x = 10; // Interval time. XX XX * 0.01m[sec]
fyazgan 0:92b7ae8bc9f5 262 bool end = false;
fyazgan 0:92b7ae8bc9f5 263
fyazgan 0:92b7ae8bc9f5 264 do {
fyazgan 0:92b7ae8bc9f5 265 send[8] = (m >> 8) & 0xff;
fyazgan 0:92b7ae8bc9f5 266 send[9] = (m >> 0) & 0xff;
fyazgan 0:92b7ae8bc9f5 267 send[12] = (k >> 8) & 0xff;
fyazgan 0:92b7ae8bc9f5 268 send[13] = (k >> 0) & 0xff;
fyazgan 0:92b7ae8bc9f5 269 send[14] = (x >> 8) & 0xff;
fyazgan 0:92b7ae8bc9f5 270 send[15] = (x >> 0) & 0xff;
fyazgan 0:92b7ae8bc9f5 271 /*
fyazgan 0:92b7ae8bc9f5 272 * Send a command.
fyazgan 0:92b7ae8bc9f5 273 */
fyazgan 0:92b7ae8bc9f5 274 if (!sendBytes(send, sizeof(send), 200 * 1000)) {
fyazgan 0:92b7ae8bc9f5 275 return SendError;
fyazgan 0:92b7ae8bc9f5 276 }
fyazgan 0:92b7ae8bc9f5 277 /*
fyazgan 0:92b7ae8bc9f5 278 * Read the header of the response.
fyazgan 0:92b7ae8bc9f5 279 */
fyazgan 0:92b7ae8bc9f5 280 uint8_t header[5];
fyazgan 0:92b7ae8bc9f5 281 if (!recvBytes(header, sizeof(header), 2 * 1000 * 1000)) {
fyazgan 0:92b7ae8bc9f5 282 return RecvError;
fyazgan 0:92b7ae8bc9f5 283 }
fyazgan 0:92b7ae8bc9f5 284 /*
fyazgan 0:92b7ae8bc9f5 285 * Check the response and fetch an image data.
fyazgan 0:92b7ae8bc9f5 286 */
fyazgan 0:92b7ae8bc9f5 287 if ((header[0] == 0x76)
fyazgan 0:92b7ae8bc9f5 288 && (header[1] == 0x00)
fyazgan 0:92b7ae8bc9f5 289 && (header[2] == 0x32)
fyazgan 0:92b7ae8bc9f5 290 && (header[3] == 0x00)
fyazgan 0:92b7ae8bc9f5 291 && (header[4] == 0x00)) {
fyazgan 0:92b7ae8bc9f5 292 if (!recvBytes(body, sizeof(body), 2 * 1000 * 1000)) {
fyazgan 0:92b7ae8bc9f5 293 return RecvError;
fyazgan 0:92b7ae8bc9f5 294 }
fyazgan 0:92b7ae8bc9f5 295 if (func != NULL) {
fyazgan 0:92b7ae8bc9f5 296 func(siz_total, body, sizeof(body));
fyazgan 0:92b7ae8bc9f5 297 }
fyazgan 0:92b7ae8bc9f5 298 for (int i = 1; i < sizeof(body); i++) {
fyazgan 0:92b7ae8bc9f5 299 if ((body[i - 1] == 0xFF) && (body[i - 0] == 0xD9)) {
fyazgan 0:92b7ae8bc9f5 300 end = true;
fyazgan 0:92b7ae8bc9f5 301 }
fyazgan 0:92b7ae8bc9f5 302 }
fyazgan 0:92b7ae8bc9f5 303 } else {
fyazgan 0:92b7ae8bc9f5 304 return UnexpectedReply;
fyazgan 0:92b7ae8bc9f5 305 }
fyazgan 0:92b7ae8bc9f5 306 /*
fyazgan 0:92b7ae8bc9f5 307 * Read the footer of the response.
fyazgan 0:92b7ae8bc9f5 308 */
fyazgan 0:92b7ae8bc9f5 309 uint8_t footer[5];
fyazgan 0:92b7ae8bc9f5 310 if (!recvBytes(footer, sizeof(footer), 2 * 1000 * 1000)) {
fyazgan 0:92b7ae8bc9f5 311 return RecvError;
fyazgan 0:92b7ae8bc9f5 312 }
fyazgan 0:92b7ae8bc9f5 313
fyazgan 0:92b7ae8bc9f5 314 m += sizeof(body);
fyazgan 0:92b7ae8bc9f5 315 } while (!end);
fyazgan 0:92b7ae8bc9f5 316 return NoError;
fyazgan 0:92b7ae8bc9f5 317 }
fyazgan 0:92b7ae8bc9f5 318
fyazgan 0:92b7ae8bc9f5 319 /**
fyazgan 0:92b7ae8bc9f5 320 * Stop taking pictures.
fyazgan 0:92b7ae8bc9f5 321 *
fyazgan 0:92b7ae8bc9f5 322 * @return Error code.
fyazgan 0:92b7ae8bc9f5 323 */
fyazgan 0:92b7ae8bc9f5 324 Camera_LS_Y201::ErrorCode Camera_LS_Y201::stopTakingPictures() {
fyazgan 0:92b7ae8bc9f5 325 uint8_t send[5] = {
fyazgan 0:92b7ae8bc9f5 326 0x56,
fyazgan 0:92b7ae8bc9f5 327 0x00,
fyazgan 0:92b7ae8bc9f5 328 0x36,
fyazgan 0:92b7ae8bc9f5 329 0x01,
fyazgan 0:92b7ae8bc9f5 330 0x03
fyazgan 0:92b7ae8bc9f5 331 };
fyazgan 0:92b7ae8bc9f5 332 uint8_t recv[5];
fyazgan 0:92b7ae8bc9f5 333
fyazgan 0:92b7ae8bc9f5 334 if (!sendBytes(send, sizeof(send), 200 * 1000)) {
fyazgan 0:92b7ae8bc9f5 335 return SendError;
fyazgan 0:92b7ae8bc9f5 336 }
fyazgan 0:92b7ae8bc9f5 337 if (!recvBytes(recv, sizeof(recv), 200 * 1000)) {
fyazgan 0:92b7ae8bc9f5 338 return RecvError;
fyazgan 0:92b7ae8bc9f5 339 }
fyazgan 0:92b7ae8bc9f5 340
fyazgan 0:92b7ae8bc9f5 341 if ((recv[0] == 0x76)
fyazgan 0:92b7ae8bc9f5 342 && (recv[1] == 0x00)
fyazgan 0:92b7ae8bc9f5 343 && (recv[2] == 0x36)
fyazgan 0:92b7ae8bc9f5 344 && (recv[3] == 0x00)
fyazgan 0:92b7ae8bc9f5 345 && (recv[4] == 0x00)) {
fyazgan 0:92b7ae8bc9f5 346 /*
fyazgan 0:92b7ae8bc9f5 347 * I think the camera need a time for operating.
fyazgan 0:92b7ae8bc9f5 348 * But there is no any comments on the documents.
fyazgan 0:92b7ae8bc9f5 349 */
fyazgan 0:92b7ae8bc9f5 350 wait_ms(100);
fyazgan 0:92b7ae8bc9f5 351 return NoError;
fyazgan 0:92b7ae8bc9f5 352 } else {
fyazgan 0:92b7ae8bc9f5 353 return UnexpectedReply;
fyazgan 0:92b7ae8bc9f5 354 }
fyazgan 0:92b7ae8bc9f5 355 }
fyazgan 0:92b7ae8bc9f5 356
fyazgan 0:92b7ae8bc9f5 357 /**
fyazgan 0:92b7ae8bc9f5 358 * Wait init end codes.
fyazgan 0:92b7ae8bc9f5 359 *
fyazgan 0:92b7ae8bc9f5 360 * @return True if the data sended.
fyazgan 0:92b7ae8bc9f5 361 */
fyazgan 0:92b7ae8bc9f5 362 Camera_LS_Y201::ErrorCode Camera_LS_Y201::waitInitEnd() {
fyazgan 0:92b7ae8bc9f5 363 static const char *PWR_ON_MSG = "Init end\x0d\x0a";
fyazgan 0:92b7ae8bc9f5 364 for (int i = 0; i < strlen(PWR_ON_MSG); i++) {
fyazgan 0:92b7ae8bc9f5 365 static const int MAXCNT = 128;
fyazgan 0:92b7ae8bc9f5 366 int cnt = 0;
fyazgan 0:92b7ae8bc9f5 367 uint8_t c = 0x00;
fyazgan 0:92b7ae8bc9f5 368 do {
fyazgan 0:92b7ae8bc9f5 369 if (!recvBytes(&c, sizeof(c), 500 * 1000)) {
fyazgan 0:92b7ae8bc9f5 370 return Timeout;
fyazgan 0:92b7ae8bc9f5 371 }
fyazgan 0:92b7ae8bc9f5 372
fyazgan 0:92b7ae8bc9f5 373 /*
fyazgan 0:92b7ae8bc9f5 374 * What is the version of the camera.
fyazgan 0:92b7ae8bc9f5 375 * You can check the version with this code.
fyazgan 0:92b7ae8bc9f5 376 *
fyazgan 0:92b7ae8bc9f5 377 * VC0703 1.00
fyazgan 0:92b7ae8bc9f5 378 * 3o ctrl in
fyazgan 0:92b7ae8bc9f5 379 * Init end
fyazgan 0:92b7ae8bc9f5 380 */
fyazgan 0:92b7ae8bc9f5 381 #if 0
fyazgan 0:92b7ae8bc9f5 382 printf("%c", c);
fyazgan 0:92b7ae8bc9f5 383 #endif
fyazgan 0:92b7ae8bc9f5 384
fyazgan 0:92b7ae8bc9f5 385 cnt++;
fyazgan 0:92b7ae8bc9f5 386 if (MAXCNT < cnt) {
fyazgan 0:92b7ae8bc9f5 387 return UnexpectedReply;
fyazgan 0:92b7ae8bc9f5 388 }
fyazgan 0:92b7ae8bc9f5 389 } while (c != PWR_ON_MSG[i]);
fyazgan 0:92b7ae8bc9f5 390 }
fyazgan 0:92b7ae8bc9f5 391 return NoError;
fyazgan 0:92b7ae8bc9f5 392 }
fyazgan 0:92b7ae8bc9f5 393
fyazgan 0:92b7ae8bc9f5 394 /**
fyazgan 0:92b7ae8bc9f5 395 * Send bytes to camera module.
fyazgan 0:92b7ae8bc9f5 396 *
fyazgan 0:92b7ae8bc9f5 397 * @param buf Pointer to the data buffer.
fyazgan 0:92b7ae8bc9f5 398 * @param len Length of the data buffer.
fyazgan 0:92b7ae8bc9f5 399 *
fyazgan 0:92b7ae8bc9f5 400 * @return True if the data sended.
fyazgan 0:92b7ae8bc9f5 401 */
fyazgan 0:92b7ae8bc9f5 402 bool Camera_LS_Y201::sendBytes(uint8_t *buf, size_t len, int timeout_us) {
fyazgan 0:92b7ae8bc9f5 403 for (uint32_t i = 0; i < (uint32_t)len; i++) {
fyazgan 0:92b7ae8bc9f5 404 int cnt = 0;
fyazgan 0:92b7ae8bc9f5 405 while (!serial.writeable()) {
fyazgan 0:92b7ae8bc9f5 406 wait_us(1);
fyazgan 0:92b7ae8bc9f5 407 cnt++;
fyazgan 0:92b7ae8bc9f5 408 if (timeout_us < cnt) {
fyazgan 0:92b7ae8bc9f5 409 return false;
fyazgan 0:92b7ae8bc9f5 410 }
fyazgan 0:92b7ae8bc9f5 411 }
fyazgan 0:92b7ae8bc9f5 412 serial.putc(buf[i]);
fyazgan 0:92b7ae8bc9f5 413 }
fyazgan 0:92b7ae8bc9f5 414 return true;
fyazgan 0:92b7ae8bc9f5 415 }
fyazgan 0:92b7ae8bc9f5 416
fyazgan 0:92b7ae8bc9f5 417 /**
fyazgan 0:92b7ae8bc9f5 418 * Receive bytes from camera module.
fyazgan 0:92b7ae8bc9f5 419 *
fyazgan 0:92b7ae8bc9f5 420 * @param buf Pointer to the data buffer.
fyazgan 0:92b7ae8bc9f5 421 * @param len Length of the data buffer.
fyazgan 0:92b7ae8bc9f5 422 *
fyazgan 0:92b7ae8bc9f5 423 * @return True if the data received.
fyazgan 0:92b7ae8bc9f5 424 */
fyazgan 0:92b7ae8bc9f5 425 bool Camera_LS_Y201::recvBytes(uint8_t *buf, size_t len, int timeout_us) {
fyazgan 0:92b7ae8bc9f5 426 for (uint32_t i = 0; i < (uint32_t)len; i++) {
fyazgan 0:92b7ae8bc9f5 427 int cnt = 0;
fyazgan 0:92b7ae8bc9f5 428 while (!serial.readable()) {
fyazgan 0:92b7ae8bc9f5 429 wait_us(1);
fyazgan 0:92b7ae8bc9f5 430 cnt++;
fyazgan 0:92b7ae8bc9f5 431 if (timeout_us < cnt) {
fyazgan 0:92b7ae8bc9f5 432 return false;
fyazgan 0:92b7ae8bc9f5 433 }
fyazgan 0:92b7ae8bc9f5 434 }
fyazgan 0:92b7ae8bc9f5 435 buf[i] = serial.getc();
fyazgan 0:92b7ae8bc9f5 436 }
fyazgan 0:92b7ae8bc9f5 437 return true;
fyazgan 0:92b7ae8bc9f5 438 }
fyazgan 0:92b7ae8bc9f5 439
fyazgan 0:92b7ae8bc9f5 440 /**
fyazgan 0:92b7ae8bc9f5 441 * Wait received.
fyazgan 0:92b7ae8bc9f5 442 *
fyazgan 0:92b7ae8bc9f5 443 * @return True if the data received.
fyazgan 0:92b7ae8bc9f5 444 */
fyazgan 0:92b7ae8bc9f5 445 bool Camera_LS_Y201::waitRecv() {
fyazgan 0:92b7ae8bc9f5 446 while (!serial.readable()) {
fyazgan 0:92b7ae8bc9f5 447 }
fyazgan 0:92b7ae8bc9f5 448 return true;
fyazgan 0:92b7ae8bc9f5 449 }
fyazgan 0:92b7ae8bc9f5 450
fyazgan 0:92b7ae8bc9f5 451 /**
fyazgan 0:92b7ae8bc9f5 452 * Wait idle state.
fyazgan 0:92b7ae8bc9f5 453 */
fyazgan 0:92b7ae8bc9f5 454 bool Camera_LS_Y201::waitIdle() {
fyazgan 0:92b7ae8bc9f5 455 while (serial.readable()) {
fyazgan 0:92b7ae8bc9f5 456 serial.getc();
fyazgan 0:92b7ae8bc9f5 457 }
fyazgan 0:92b7ae8bc9f5 458 return true;
fyazgan 0:92b7ae8bc9f5 459 }
fyazgan 0:92b7ae8bc9f5 460 /**
fyazgan 0:92b7ae8bc9f5 461 * Set Baudrate
fyazgan 0:92b7ae8bc9f5 462 *
fyazgan 0:92b7ae8bc9f5 463 * @param is baudrate.
fyazgan 0:92b7ae8bc9f5 464 * @return Error code.
fyazgan 0:92b7ae8bc9f5 465 */
fyazgan 0:92b7ae8bc9f5 466 Camera_LS_Y201::ErrorCode Camera_LS_Y201::setBaudrate(Baudrate bt) {
fyazgan 0:92b7ae8bc9f5 467 uint8_t send[7] = {
fyazgan 0:92b7ae8bc9f5 468 0x56,
fyazgan 0:92b7ae8bc9f5 469 0x00,
fyazgan 0:92b7ae8bc9f5 470 0x24,
fyazgan 0:92b7ae8bc9f5 471 0x03,
fyazgan 0:92b7ae8bc9f5 472 0x01,
fyazgan 0:92b7ae8bc9f5 473 };
fyazgan 0:92b7ae8bc9f5 474 uint8_t recv[5];
fyazgan 0:92b7ae8bc9f5 475 switch (bt) {
fyazgan 0:92b7ae8bc9f5 476 case Baud57600:
fyazgan 0:92b7ae8bc9f5 477 send[5] = 0x1C;
fyazgan 0:92b7ae8bc9f5 478 send[6] = 0x4C;
fyazgan 0:92b7ae8bc9f5 479 break;
fyazgan 0:92b7ae8bc9f5 480 case Baud115200:
fyazgan 0:92b7ae8bc9f5 481 send[5] = 0x0D;
fyazgan 0:92b7ae8bc9f5 482 send[6] = 0xA6;
fyazgan 0:92b7ae8bc9f5 483 break;
fyazgan 0:92b7ae8bc9f5 484 case Baud1228800:
fyazgan 0:92b7ae8bc9f5 485 send[5] = 0x02;
fyazgan 0:92b7ae8bc9f5 486 send[6] = 0x00;
fyazgan 0:92b7ae8bc9f5 487 break;
fyazgan 0:92b7ae8bc9f5 488 default:
fyazgan 0:92b7ae8bc9f5 489 return InvalidArguments;
fyazgan 0:92b7ae8bc9f5 490 }
fyazgan 0:92b7ae8bc9f5 491 if (!sendBytes(send, sizeof(send), 200 * 1000)) {
fyazgan 0:92b7ae8bc9f5 492 return SendError;
fyazgan 0:92b7ae8bc9f5 493 }
fyazgan 0:92b7ae8bc9f5 494 if (!recvBytes(recv, sizeof(recv), 200 * 1000)) {
fyazgan 0:92b7ae8bc9f5 495 return RecvError;
fyazgan 0:92b7ae8bc9f5 496 }
fyazgan 0:92b7ae8bc9f5 497 if ((recv[0] == 0x76)
fyazgan 0:92b7ae8bc9f5 498 && (recv[1] == 0x00)
fyazgan 0:92b7ae8bc9f5 499 && (recv[2] == 0x24)
fyazgan 0:92b7ae8bc9f5 500 && (recv[3] == 0x00)
fyazgan 0:92b7ae8bc9f5 501 && (recv[4] == 0x00)) {
fyazgan 0:92b7ae8bc9f5 502 wait(1);
fyazgan 0:92b7ae8bc9f5 503 return reset();
fyazgan 0:92b7ae8bc9f5 504 } else {
fyazgan 0:92b7ae8bc9f5 505 return UnexpectedReply;
fyazgan 0:92b7ae8bc9f5 506 }
fyazgan 0:92b7ae8bc9f5 507 }