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
Revision 0:92b7ae8bc9f5, committed 2011-03-02
- Comitter:
- fyazgan
- Date:
- Wed Mar 02 05:43:09 2011 +0000
- Commit message:
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Camera_LS_Y201.cpp Wed Mar 02 05:43:09 2011 +0000 @@ -0,0 +1,507 @@ +/** + * ============================================================================= + * LS-Y201 device driver class (Version 0.0.1) + * Reference documents: LinkSprite JPEG Color Camera Serial UART Interface + * January 2010 + * ============================================================================= + * Copyright (c) 2010 Shinichiro Nakamura (CuBeatSystems) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * ============================================================================= + */ + +#include "Camera_LS_Y201.h" + +/** + * Create. + * + * @param tx Transmitter. + * @param rx Receiver. + */ +Camera_LS_Y201::Camera_LS_Y201(PinName tx, PinName rx,Bauds bs) : serial(tx, rx) { + switch (bs) { + case Default: + serial.baud(38400); + break; + case Bauds57600: + serial.baud(57600); + break; + case Bauds115200: + serial.baud(115200); + break; + case Bauds1228800: + serial.baud(1228800); + break; + } +} + +/** + * Dispose. + */ +Camera_LS_Y201::~Camera_LS_Y201() { +} + +/** + * Reset module. + * + * @return Error code. + */ +Camera_LS_Y201::ErrorCode Camera_LS_Y201::reset() { + uint8_t send[4] = { + 0x56, + 0x00, + 0x26, + 0x00 + }; + uint8_t recv[4]; + + waitIdle(); + if (!sendBytes(send, sizeof(send), 200 * 1000)) { + return SendError; + } + if (!recvBytes(recv, sizeof(recv), 200 * 1000)) { + return RecvError; + } + if ((recv[0] == 0x76) + && (recv[1] == 0x00) + && (recv[2] == 0x26) + && (recv[3] == 0x00)) { + ErrorCode r = waitInitEnd(); + if (r != NoError) { + return r; + } + wait(4); + return NoError; + } else { + return UnexpectedReply; + } +} + +/** + * Set image size. + * + * @param is Image size. + * @return Error code. + */ +Camera_LS_Y201::ErrorCode Camera_LS_Y201::setImageSize(ImageSize is) { + uint8_t send[9] = { + 0x56, + 0x00, + 0x31, + 0x05, + 0x04, + 0x01, + 0x00, + 0x19, + 0x00 // 0x11:320x240, 0x00:640x480, 0x22:160x120 + }; + uint8_t recv[5]; + switch (is) { + case ImageSize160x120: + send[8] = 0x22; + break; + case ImageSize320x280: + send[8] = 0x11; + break; + case ImageSize640x480: + send[8] = 0x00; + break; + default: + return InvalidArguments; + } + if (!sendBytes(send, sizeof(send), 200 * 1000)) { + return SendError; + } + if (!recvBytes(recv, sizeof(recv), 200 * 1000)) { + return RecvError; + } + if ((recv[0] == 0x76) + && (recv[1] == 0x00) + && (recv[2] == 0x31) + && (recv[3] == 0x00) + && (recv[4] == 0x00)) { + wait(1); + return reset(); + } else { + return UnexpectedReply; + } +} + +/** + * Take picture. + * + * @return Error code. + */ +Camera_LS_Y201::ErrorCode Camera_LS_Y201::takePicture() { + uint8_t send[5] = { + 0x56, + 0x00, + 0x36, + 0x01, + 0x00 + }; + uint8_t recv[5]; + + if (!sendBytes(send, sizeof(send), 200 * 1000)) { + return SendError; + } + if (!recvBytes(recv, sizeof(recv), 200 * 1000)) { + return RecvError; + } + + if ((recv[0] == 0x76) + && (recv[1] == 0x00) + && (recv[2] == 0x36) + && (recv[3] == 0x00) + && (recv[4] == 0x00)) { + /* + * I think the camera need a time for operating. + * But there is no any comments on the documents. + */ + wait_ms(100); + return NoError; + } else { + return UnexpectedReply; + } +} + +/** + * Read jpeg file size. + * + * @param fileSize File size. + * @return Error code. + */ +Camera_LS_Y201::ErrorCode Camera_LS_Y201::readJpegFileSize(int *fileSize) { + uint8_t send[5] = { + 0x56, + 0x00, + 0x34, + 0x01, + 0x00 + }; + uint8_t recv[9]; + + if (!sendBytes(send, sizeof(send), 200 * 1000)) { + return SendError; + } + if (!recvBytes(recv, sizeof(recv), 200 * 1000)) { + return RecvError; + } + + if ((recv[0] == 0x76) + && (recv[1] == 0x00) + && (recv[2] == 0x34) + && (recv[3] == 0x00) + && (recv[4] == 0x04) + && (recv[5] == 0x00) + && (recv[6] == 0x00)) { + *fileSize = ((recv[7] & 0x00ff) << 8) + | ((recv[8] & 0x00ff) << 0); + return NoError; + } else { + return UnexpectedReply; + } +} + +/** + * Read jpeg file content. + * + * @param func A pointer to a call back function. + * @return Error code. + */ +Camera_LS_Y201::ErrorCode Camera_LS_Y201::readJpegFileContent(void (*func)( int total, uint8_t *buf, size_t siz)) { + uint8_t send[16] = { + 0x56, + 0x00, + 0x32, + 0x0C, + 0x00, + 0x0A, + 0x00, + 0x00, + 0x00, // MH + 0x00, // ML + 0x00, + 0x00, + 0x00, // KH + 0x00, // KL + 0x00, // XX + 0x00 // XX + }; + + /* + * Get the data size. + */ + int siz_total = 0; + ErrorCode r = readJpegFileSize(&siz_total); + if (r != NoError) { + return r; + } + + int pac=(((siz_total/8))+1)*8; + uint8_t body[pac]; + uint16_t m = 0; // Staring address. + uint16_t k = sizeof(body); // Packet size. + uint16_t x = 10; // Interval time. XX XX * 0.01m[sec] + bool end = false; + + do { + send[8] = (m >> 8) & 0xff; + send[9] = (m >> 0) & 0xff; + send[12] = (k >> 8) & 0xff; + send[13] = (k >> 0) & 0xff; + send[14] = (x >> 8) & 0xff; + send[15] = (x >> 0) & 0xff; + /* + * Send a command. + */ + if (!sendBytes(send, sizeof(send), 200 * 1000)) { + return SendError; + } + /* + * Read the header of the response. + */ + uint8_t header[5]; + if (!recvBytes(header, sizeof(header), 2 * 1000 * 1000)) { + return RecvError; + } + /* + * Check the response and fetch an image data. + */ + if ((header[0] == 0x76) + && (header[1] == 0x00) + && (header[2] == 0x32) + && (header[3] == 0x00) + && (header[4] == 0x00)) { + if (!recvBytes(body, sizeof(body), 2 * 1000 * 1000)) { + return RecvError; + } + if (func != NULL) { + func(siz_total, body, sizeof(body)); + } + for (int i = 1; i < sizeof(body); i++) { + if ((body[i - 1] == 0xFF) && (body[i - 0] == 0xD9)) { + end = true; + } + } + } else { + return UnexpectedReply; + } + /* + * Read the footer of the response. + */ + uint8_t footer[5]; + if (!recvBytes(footer, sizeof(footer), 2 * 1000 * 1000)) { + return RecvError; + } + + m += sizeof(body); + } while (!end); + return NoError; +} + +/** + * Stop taking pictures. + * + * @return Error code. + */ +Camera_LS_Y201::ErrorCode Camera_LS_Y201::stopTakingPictures() { + uint8_t send[5] = { + 0x56, + 0x00, + 0x36, + 0x01, + 0x03 + }; + uint8_t recv[5]; + + if (!sendBytes(send, sizeof(send), 200 * 1000)) { + return SendError; + } + if (!recvBytes(recv, sizeof(recv), 200 * 1000)) { + return RecvError; + } + + if ((recv[0] == 0x76) + && (recv[1] == 0x00) + && (recv[2] == 0x36) + && (recv[3] == 0x00) + && (recv[4] == 0x00)) { + /* + * I think the camera need a time for operating. + * But there is no any comments on the documents. + */ + wait_ms(100); + return NoError; + } else { + return UnexpectedReply; + } +} + +/** + * Wait init end codes. + * + * @return True if the data sended. + */ +Camera_LS_Y201::ErrorCode Camera_LS_Y201::waitInitEnd() { + static const char *PWR_ON_MSG = "Init end\x0d\x0a"; + for (int i = 0; i < strlen(PWR_ON_MSG); i++) { + static const int MAXCNT = 128; + int cnt = 0; + uint8_t c = 0x00; + do { + if (!recvBytes(&c, sizeof(c), 500 * 1000)) { + return Timeout; + } + + /* + * What is the version of the camera. + * You can check the version with this code. + * + * VC0703 1.00 + * 3o ctrl in + * Init end + */ +#if 0 + printf("%c", c); +#endif + + cnt++; + if (MAXCNT < cnt) { + return UnexpectedReply; + } + } while (c != PWR_ON_MSG[i]); + } + return NoError; +} + +/** + * Send bytes to camera module. + * + * @param buf Pointer to the data buffer. + * @param len Length of the data buffer. + * + * @return True if the data sended. + */ +bool Camera_LS_Y201::sendBytes(uint8_t *buf, size_t len, int timeout_us) { + for (uint32_t i = 0; i < (uint32_t)len; i++) { + int cnt = 0; + while (!serial.writeable()) { + wait_us(1); + cnt++; + if (timeout_us < cnt) { + return false; + } + } + serial.putc(buf[i]); + } + return true; +} + +/** + * Receive bytes from camera module. + * + * @param buf Pointer to the data buffer. + * @param len Length of the data buffer. + * + * @return True if the data received. + */ +bool Camera_LS_Y201::recvBytes(uint8_t *buf, size_t len, int timeout_us) { + for (uint32_t i = 0; i < (uint32_t)len; i++) { + int cnt = 0; + while (!serial.readable()) { + wait_us(1); + cnt++; + if (timeout_us < cnt) { + return false; + } + } + buf[i] = serial.getc(); + } + return true; +} + +/** + * Wait received. + * + * @return True if the data received. + */ +bool Camera_LS_Y201::waitRecv() { + while (!serial.readable()) { + } + return true; +} + +/** + * Wait idle state. + */ +bool Camera_LS_Y201::waitIdle() { + while (serial.readable()) { + serial.getc(); + } + return true; +} +/** + * Set Baudrate + * + * @param is baudrate. + * @return Error code. + */ +Camera_LS_Y201::ErrorCode Camera_LS_Y201::setBaudrate(Baudrate bt) { + uint8_t send[7] = { + 0x56, + 0x00, + 0x24, + 0x03, + 0x01, + }; + uint8_t recv[5]; + switch (bt) { + case Baud57600: + send[5] = 0x1C; + send[6] = 0x4C; + break; + case Baud115200: + send[5] = 0x0D; + send[6] = 0xA6; + break; + case Baud1228800: + send[5] = 0x02; + send[6] = 0x00; + break; + default: + return InvalidArguments; + } + if (!sendBytes(send, sizeof(send), 200 * 1000)) { + return SendError; + } + if (!recvBytes(recv, sizeof(recv), 200 * 1000)) { + return RecvError; + } + if ((recv[0] == 0x76) + && (recv[1] == 0x00) + && (recv[2] == 0x24) + && (recv[3] == 0x00) + && (recv[4] == 0x00)) { + wait(1); + return reset(); + } else { + return UnexpectedReply; + } +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Camera_LS_Y201.h Wed Mar 02 05:43:09 2011 +0000 @@ -0,0 +1,190 @@ +/** + * ============================================================================= + * LS-Y201 device driver class (Version 0.0.1) + * Reference documents: LinkSprite JPEG Color Camera Serial UART Interface + * January 2010 + * ============================================================================= + * Copyright (c) 2010 Shinichiro Nakamura (CuBeatSystems) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * ============================================================================= + */ + +#ifndef LS_Y201_H +#define LS_Y201_H + +#include "mbed.h" +#include "SerialBuffered.h" + +/** + * Camera + */ +class Camera_LS_Y201 { +public: + + /** + * Image size. + */ + enum Bauds { + Default, + Bauds57600, + Bauds115200, + Bauds1228800 + }; + + /** + * Create. + * + * @param tx Transmitter. + * @param rx Receiver. + */ + Camera_LS_Y201(PinName tx, PinName rx, Bauds bs); + + /** + * Dispose. + */ + ~Camera_LS_Y201(); + + /** + * Error code. + */ + enum ErrorCode { + NoError = 0, + UnexpectedReply, + Timeout, + SendError, + RecvError, + InvalidArguments + }; + + /** + * Image size. + */ + enum ImageSize { + ImageSize160x120, /**< 160x120. */ + ImageSize320x280, /**< 320x280. */ + ImageSize640x480 /**< 640x480. */ + }; + + /** + * Reset module. + * + * @return Error code. + */ + ErrorCode reset(); + + /** + * Set image size. + * + * @param is Image size. + * @return Error code. + */ + ErrorCode setImageSize(ImageSize is); + + /** + * Take picture. + * + * @return Error code. + */ + ErrorCode takePicture(); + + /** + * Read jpeg file size. + * + * @param fileSize File size. + * @return Error code. + */ + ErrorCode readJpegFileSize(int *fileSize); + + /** + * Read jpeg file content. + * + * @param func A pointer to a call back function. + * @return Error code. + */ + ErrorCode readJpegFileContent(void (*func)(int total, uint8_t *buf, size_t siz)); + + /** + * Stop taking pictures. + * + * @return Error code. + */ + ErrorCode stopTakingPictures(); + + /** + * Baudrate + */ + enum Baudrate { + Baud57600, + Baud115200, + Baud1228800 + }; + + /** + * @return Error code. + */ + ErrorCode setBaudrate(Baudrate bt); + +private: + SerialBuffered serial; + + /** + * Wait init end codes. + * + * @return Error code. + */ + ErrorCode waitInitEnd(); + + /** + * Send bytes to camera module. + * + * @param buf Pointer to the data buffer. + * @param len Length of the data buffer. + * + * @return True if the data sended. + */ + bool sendBytes(uint8_t *buf, size_t len, int timeout_us); + + /** + * Receive bytes from camera module. + * + * @param buf Pointer to the data buffer. + * @param len Length of the data buffer. + * + * @return True if the data received. + */ + bool recvBytes(uint8_t *buf, size_t len, int timeout_us); + + /** + * Wait received. + * + * @return True if the data received. + */ + bool waitRecv(); + + /** + * Wait idle state. + * + * @return True if it succeed. + */ + bool waitIdle(); + +}; + +#endif
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SerialBuffered.cpp Wed Mar 02 05:43:09 2011 +0000 @@ -0,0 +1,127 @@ +/** + * ============================================================================= + * LS-Y201 device driver class (Version 0.0.1) + * Reference documents: LinkSprite JPEG Color Camera Serial UART Interface + * January 2010 + * ============================================================================= + * Copyright (c) 2010 Shinichiro Nakamura (CuBeatSystems) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * ============================================================================= + */ + +#include "mbed.h" +#include "SerialBuffered.h" + +/** + * Create a buffered serial class. + * + * @param tx A pin for transmit. + * @param rx A pin for receive. + */ +SerialBuffered::SerialBuffered(PinName tx, PinName rx) : Serial(tx, rx) { + indexContentStart = 0; + indexContentEnd = 0; + timeout = 1; + attach(this, &SerialBuffered::handleInterrupt); +} + +/** + * Destroy. + */ +SerialBuffered::~SerialBuffered() { +} + +/** + * Set timeout for getc(). + * + * @param ms milliseconds. (-1:Disable timeout) + */ +void SerialBuffered::setTimeout(int ms) { + timeout = ms; +} + +/** + * Read requested bytes. + * + * @param bytes A pointer to a buffer. + * @param requested Length. + * + * @return Readed byte length. + */ +size_t SerialBuffered::readBytes(uint8_t *bytes, size_t requested) { + int i = 0; + while (i < requested) { + int c = getc(); + if (c < 0) { + break; + } + bytes[i] = c; + i++; + } + return i; +} + +/** + * Get a character. + * + * @return A character. (-1:timeout) + */ +int SerialBuffered::getc() { + timer.reset(); + timer.start(); + while (indexContentStart == indexContentEnd) { + wait_ms(1); + if ((timeout > 0) && (timer.read_ms() > timeout)) { + /* + * Timeout occured. + */ + // printf("Timeout occured.\n"); + return EOF; + } + } + timer.stop(); + + uint8_t result = buffer[indexContentStart++]; + indexContentStart = indexContentStart % BUFFERSIZE; + + return result; +} + +/** + * Returns 1 if there is a character available to read, 0 otherwise. + */ +int SerialBuffered::readable() { + return indexContentStart != indexContentEnd; +} + +void SerialBuffered::handleInterrupt() { + while (Serial::readable()) { + if (indexContentStart == ((indexContentEnd + 1) % BUFFERSIZE)) { + /* + * Buffer overrun occured. + */ + // printf("Buffer overrun occured.\n"); + Serial::getc(); + } else { + buffer[indexContentEnd++] = Serial::getc(); + indexContentEnd = indexContentEnd % BUFFERSIZE; + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SerialBuffered.h Wed Mar 02 05:43:09 2011 +0000 @@ -0,0 +1,89 @@ +/** + * ============================================================================= + * LS-Y201 device driver class (Version 0.0.1) + * Reference documents: LinkSprite JPEG Color Camera Serial UART Interface + * January 2010 + * ============================================================================= + * Copyright (c) 2010 Shinichiro Nakamura (CuBeatSystems) + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * ============================================================================= + */ + +#ifndef _SERIAL_BUFFERED_H_ +#define _SERIAL_BUFFERED_H_ + +/** + * Buffered serial class. + */ +class SerialBuffered : public Serial { +public: + /** + * Create a buffered serial class. + * + * @param tx A pin for transmit. + * @param rx A pin for receive. + */ + SerialBuffered(PinName tx, PinName rx); + + /** + * Destroy. + */ + virtual ~SerialBuffered(); + + /** + * Get a character. + * + * @return A character. (-1:timeout) + */ + int getc(); + + /** + * Returns 1 if there is a character available to read, 0 otherwise. + */ + int readable(); + + /** + * Set timeout for getc(). + * + * @param ms milliseconds. (-1:Disable timeout) + */ + void setTimeout(int ms); + + /** + * Read requested bytes. + * + * @param bytes A pointer to a buffer. + * @param requested Length. + * + * @return Readed byte length. + */ + size_t readBytes(uint8_t *bytes, size_t requested); + +private: + void handleInterrupt(); + static const int BUFFERSIZE = 4096; + uint8_t buffer[BUFFERSIZE]; // points at a circular buffer, containing data from m_contentStart, for m_contentSize bytes, wrapping when you get to the end + uint16_t indexContentStart; // index of first bytes of content + uint16_t indexContentEnd; // index of bytes after last byte of content + int timeout; + Timer timer; +}; + +#endif