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 "mbed.h"
fyazgan 0:92b7ae8bc9f5 30 #include "SerialBuffered.h"
fyazgan 0:92b7ae8bc9f5 31
fyazgan 0:92b7ae8bc9f5 32 /**
fyazgan 0:92b7ae8bc9f5 33 * Create a buffered serial class.
fyazgan 0:92b7ae8bc9f5 34 *
fyazgan 0:92b7ae8bc9f5 35 * @param tx A pin for transmit.
fyazgan 0:92b7ae8bc9f5 36 * @param rx A pin for receive.
fyazgan 0:92b7ae8bc9f5 37 */
fyazgan 0:92b7ae8bc9f5 38 SerialBuffered::SerialBuffered(PinName tx, PinName rx) : Serial(tx, rx) {
fyazgan 0:92b7ae8bc9f5 39 indexContentStart = 0;
fyazgan 0:92b7ae8bc9f5 40 indexContentEnd = 0;
fyazgan 0:92b7ae8bc9f5 41 timeout = 1;
fyazgan 0:92b7ae8bc9f5 42 attach(this, &SerialBuffered::handleInterrupt);
fyazgan 0:92b7ae8bc9f5 43 }
fyazgan 0:92b7ae8bc9f5 44
fyazgan 0:92b7ae8bc9f5 45 /**
fyazgan 0:92b7ae8bc9f5 46 * Destroy.
fyazgan 0:92b7ae8bc9f5 47 */
fyazgan 0:92b7ae8bc9f5 48 SerialBuffered::~SerialBuffered() {
fyazgan 0:92b7ae8bc9f5 49 }
fyazgan 0:92b7ae8bc9f5 50
fyazgan 0:92b7ae8bc9f5 51 /**
fyazgan 0:92b7ae8bc9f5 52 * Set timeout for getc().
fyazgan 0:92b7ae8bc9f5 53 *
fyazgan 0:92b7ae8bc9f5 54 * @param ms milliseconds. (-1:Disable timeout)
fyazgan 0:92b7ae8bc9f5 55 */
fyazgan 0:92b7ae8bc9f5 56 void SerialBuffered::setTimeout(int ms) {
fyazgan 0:92b7ae8bc9f5 57 timeout = ms;
fyazgan 0:92b7ae8bc9f5 58 }
fyazgan 0:92b7ae8bc9f5 59
fyazgan 0:92b7ae8bc9f5 60 /**
fyazgan 0:92b7ae8bc9f5 61 * Read requested bytes.
fyazgan 0:92b7ae8bc9f5 62 *
fyazgan 0:92b7ae8bc9f5 63 * @param bytes A pointer to a buffer.
fyazgan 0:92b7ae8bc9f5 64 * @param requested Length.
fyazgan 0:92b7ae8bc9f5 65 *
fyazgan 0:92b7ae8bc9f5 66 * @return Readed byte length.
fyazgan 0:92b7ae8bc9f5 67 */
fyazgan 0:92b7ae8bc9f5 68 size_t SerialBuffered::readBytes(uint8_t *bytes, size_t requested) {
fyazgan 0:92b7ae8bc9f5 69 int i = 0;
fyazgan 0:92b7ae8bc9f5 70 while (i < requested) {
fyazgan 0:92b7ae8bc9f5 71 int c = getc();
fyazgan 0:92b7ae8bc9f5 72 if (c < 0) {
fyazgan 0:92b7ae8bc9f5 73 break;
fyazgan 0:92b7ae8bc9f5 74 }
fyazgan 0:92b7ae8bc9f5 75 bytes[i] = c;
fyazgan 0:92b7ae8bc9f5 76 i++;
fyazgan 0:92b7ae8bc9f5 77 }
fyazgan 0:92b7ae8bc9f5 78 return i;
fyazgan 0:92b7ae8bc9f5 79 }
fyazgan 0:92b7ae8bc9f5 80
fyazgan 0:92b7ae8bc9f5 81 /**
fyazgan 0:92b7ae8bc9f5 82 * Get a character.
fyazgan 0:92b7ae8bc9f5 83 *
fyazgan 0:92b7ae8bc9f5 84 * @return A character. (-1:timeout)
fyazgan 0:92b7ae8bc9f5 85 */
fyazgan 0:92b7ae8bc9f5 86 int SerialBuffered::getc() {
fyazgan 0:92b7ae8bc9f5 87 timer.reset();
fyazgan 0:92b7ae8bc9f5 88 timer.start();
fyazgan 0:92b7ae8bc9f5 89 while (indexContentStart == indexContentEnd) {
fyazgan 0:92b7ae8bc9f5 90 wait_ms(1);
fyazgan 0:92b7ae8bc9f5 91 if ((timeout > 0) && (timer.read_ms() > timeout)) {
fyazgan 0:92b7ae8bc9f5 92 /*
fyazgan 0:92b7ae8bc9f5 93 * Timeout occured.
fyazgan 0:92b7ae8bc9f5 94 */
fyazgan 0:92b7ae8bc9f5 95 // printf("Timeout occured.\n");
fyazgan 0:92b7ae8bc9f5 96 return EOF;
fyazgan 0:92b7ae8bc9f5 97 }
fyazgan 0:92b7ae8bc9f5 98 }
fyazgan 0:92b7ae8bc9f5 99 timer.stop();
fyazgan 0:92b7ae8bc9f5 100
fyazgan 0:92b7ae8bc9f5 101 uint8_t result = buffer[indexContentStart++];
fyazgan 0:92b7ae8bc9f5 102 indexContentStart = indexContentStart % BUFFERSIZE;
fyazgan 0:92b7ae8bc9f5 103
fyazgan 0:92b7ae8bc9f5 104 return result;
fyazgan 0:92b7ae8bc9f5 105 }
fyazgan 0:92b7ae8bc9f5 106
fyazgan 0:92b7ae8bc9f5 107 /**
fyazgan 0:92b7ae8bc9f5 108 * Returns 1 if there is a character available to read, 0 otherwise.
fyazgan 0:92b7ae8bc9f5 109 */
fyazgan 0:92b7ae8bc9f5 110 int SerialBuffered::readable() {
fyazgan 0:92b7ae8bc9f5 111 return indexContentStart != indexContentEnd;
fyazgan 0:92b7ae8bc9f5 112 }
fyazgan 0:92b7ae8bc9f5 113
fyazgan 0:92b7ae8bc9f5 114 void SerialBuffered::handleInterrupt() {
fyazgan 0:92b7ae8bc9f5 115 while (Serial::readable()) {
fyazgan 0:92b7ae8bc9f5 116 if (indexContentStart == ((indexContentEnd + 1) % BUFFERSIZE)) {
fyazgan 0:92b7ae8bc9f5 117 /*
fyazgan 0:92b7ae8bc9f5 118 * Buffer overrun occured.
fyazgan 0:92b7ae8bc9f5 119 */
fyazgan 0:92b7ae8bc9f5 120 // printf("Buffer overrun occured.\n");
fyazgan 0:92b7ae8bc9f5 121 Serial::getc();
fyazgan 0:92b7ae8bc9f5 122 } else {
fyazgan 0:92b7ae8bc9f5 123 buffer[indexContentEnd++] = Serial::getc();
fyazgan 0:92b7ae8bc9f5 124 indexContentEnd = indexContentEnd % BUFFERSIZE;
fyazgan 0:92b7ae8bc9f5 125 }
fyazgan 0:92b7ae8bc9f5 126 }
fyazgan 0:92b7ae8bc9f5 127 }