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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SerialBuffered.cpp Source File

SerialBuffered.cpp

00001 /**
00002  * =============================================================================
00003  * LS-Y201 device driver class (Version 0.0.1)
00004  * Reference documents: LinkSprite JPEG Color Camera Serial UART Interface
00005  *                      January 2010
00006  * =============================================================================
00007  * Copyright (c) 2010 Shinichiro Nakamura (CuBeatSystems)
00008  *
00009  * Permission is hereby granted, free of charge, to any person obtaining a copy
00010  * of this software and associated documentation files (the "Software"), to deal
00011  * in the Software without restriction, including without limitation the rights
00012  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00013  * copies of the Software, and to permit persons to whom the Software is
00014  * furnished to do so, subject to the following conditions:
00015  * 
00016  * The above copyright notice and this permission notice shall be included in
00017  * all copies or substantial portions of the Software.
00018  *
00019  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00020  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00021  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00022  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00023  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00024  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00025  * THE SOFTWARE.
00026  * =============================================================================
00027  */
00028 
00029 #include "mbed.h"
00030 #include "SerialBuffered.h"
00031 
00032 /**
00033  * Create a buffered serial class.
00034  *
00035  * @param tx A pin for transmit.
00036  * @param rx A pin for receive.
00037  */
00038 SerialBuffered::SerialBuffered(PinName tx, PinName rx) : Serial(tx, rx) {
00039     indexContentStart = 0;
00040     indexContentEnd = 0;
00041     timeout = 1;
00042     attach(this, &SerialBuffered::handleInterrupt);
00043 }
00044 
00045 /**
00046  * Destroy.
00047  */
00048 SerialBuffered::~SerialBuffered() {
00049 }
00050 
00051 /**
00052  * Set timeout for getc().
00053  *
00054  * @param ms milliseconds. (-1:Disable timeout)
00055  */
00056 void SerialBuffered::setTimeout(int ms) {
00057     timeout = ms;
00058 }
00059 
00060 /**
00061  * Read requested bytes.
00062  *
00063  * @param bytes A pointer to a buffer.
00064  * @param requested Length.
00065  *
00066  * @return Readed byte length.
00067  */
00068 size_t SerialBuffered::readBytes(uint8_t *bytes, size_t requested) {
00069     int i = 0;
00070     while (i < requested) {
00071         int c = getc();
00072         if (c < 0) {
00073             break;
00074         }
00075         bytes[i] = c;
00076         i++;
00077     }
00078     return i;
00079 }
00080 
00081 /**
00082  * Get a character.
00083  *
00084  * @return A character. (-1:timeout)
00085  */
00086 int SerialBuffered::getc() {
00087     timer.reset();
00088     timer.start();
00089     while (indexContentStart == indexContentEnd) {
00090         wait_ms(1);
00091         if ((timeout > 0) && (timer.read_ms() > timeout)) {
00092             /*
00093              * Timeout occured.
00094              */
00095             // printf("Timeout occured.\n");
00096             return EOF;
00097         }
00098     }
00099     timer.stop();
00100 
00101     uint8_t result = buffer[indexContentStart++];
00102     indexContentStart =  indexContentStart % BUFFERSIZE;
00103 
00104     return result;
00105 }
00106 
00107 /**
00108  * Returns 1 if there is a character available to read, 0 otherwise.
00109  */
00110 int SerialBuffered::readable() {
00111     return indexContentStart != indexContentEnd;
00112 }
00113 
00114 void SerialBuffered::handleInterrupt() {
00115     while (Serial::readable()) {
00116         if (indexContentStart == ((indexContentEnd + 1) % BUFFERSIZE)) {
00117             /*
00118              * Buffer overrun occured.
00119              */
00120             // printf("Buffer overrun occured.\n");
00121             Serial::getc();
00122         } else {
00123             buffer[indexContentEnd++] = Serial::getc();
00124             indexContentEnd = indexContentEnd % BUFFERSIZE;
00125         }
00126     }
00127 }