fork of Camera_LS_Y201 lib supporting the 2MP variant/successor

Fork of Camera_LS_Y201 by Shinichiro Nakamura

Committer:
humlet
Date:
Tue Mar 18 17:28:47 2014 +0000
Revision:
2:ce4d1351bdeb
Parent:
1:43358d40f879
Got it running for the crappy LS-Y201-2MP; quickNdirty

Who changed what in which revision?

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