Description.

Dependents:   RoboPanorama 4180_Project ProjetMacIntel

Fork of Camera_LS_Y201 by Shinichiro Nakamura

Committer:
lndv3
Date:
Fri Oct 12 16:41:24 2012 +0000
Revision:
2:e92ce527b8b0
Parent:
0:f71232252dcf
Commit.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
lndv3 2:e92ce527b8b0 1 /**
lndv3 2:e92ce527b8b0 2 * =============================================================================
lndv3 2:e92ce527b8b0 3 * LS-Y201 device driver class (Version 0.0.1)
lndv3 2:e92ce527b8b0 4 * Reference documents: LinkSprite JPEG Color Camera Serial UART Interface
lndv3 2:e92ce527b8b0 5 * January 2010
lndv3 2:e92ce527b8b0 6 * =============================================================================
lndv3 2:e92ce527b8b0 7 * Copyright (c) 2010 Shinichiro Nakamura (CuBeatSystems)
lndv3 2:e92ce527b8b0 8 *
lndv3 2:e92ce527b8b0 9 * Permission is hereby granted, free of charge, to any person obtaining a copy
lndv3 2:e92ce527b8b0 10 * of this software and associated documentation files (the "Software"), to deal
lndv3 2:e92ce527b8b0 11 * in the Software without restriction, including without limitation the rights
lndv3 2:e92ce527b8b0 12 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
lndv3 2:e92ce527b8b0 13 * copies of the Software, and to permit persons to whom the Software is
lndv3 2:e92ce527b8b0 14 * furnished to do so, subject to the following conditions:
lndv3 2:e92ce527b8b0 15 *
lndv3 2:e92ce527b8b0 16 * The above copyright notice and this permission notice shall be included in
lndv3 2:e92ce527b8b0 17 * all copies or substantial portions of the Software.
lndv3 2:e92ce527b8b0 18 *
lndv3 2:e92ce527b8b0 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
lndv3 2:e92ce527b8b0 20 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
lndv3 2:e92ce527b8b0 21 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
lndv3 2:e92ce527b8b0 22 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
lndv3 2:e92ce527b8b0 23 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
lndv3 2:e92ce527b8b0 24 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
lndv3 2:e92ce527b8b0 25 * THE SOFTWARE.
lndv3 2:e92ce527b8b0 26 * =============================================================================
lndv3 2:e92ce527b8b0 27 */
lndv3 2:e92ce527b8b0 28
lndv3 2:e92ce527b8b0 29 #include "mbed.h"
lndv3 2:e92ce527b8b0 30 #include "SerialBuffered.h"
lndv3 2:e92ce527b8b0 31
lndv3 2:e92ce527b8b0 32 /**
lndv3 2:e92ce527b8b0 33 * Create a buffered serial class.
lndv3 2:e92ce527b8b0 34 *
lndv3 2:e92ce527b8b0 35 * @param tx A pin for transmit.
lndv3 2:e92ce527b8b0 36 * @param rx A pin for receive.
lndv3 2:e92ce527b8b0 37 */
lndv3 2:e92ce527b8b0 38 SerialBuffered::SerialBuffered(PinName tx, PinName rx) : Serial(tx, rx) {
lndv3 2:e92ce527b8b0 39 indexContentStart = 0;
lndv3 2:e92ce527b8b0 40 indexContentEnd = 0;
lndv3 2:e92ce527b8b0 41 timeout = 1;
lndv3 2:e92ce527b8b0 42 attach(this, &SerialBuffered::handleInterrupt);
lndv3 2:e92ce527b8b0 43 }
lndv3 2:e92ce527b8b0 44
lndv3 2:e92ce527b8b0 45 /**
lndv3 2:e92ce527b8b0 46 * Destroy.
lndv3 2:e92ce527b8b0 47 */
lndv3 2:e92ce527b8b0 48 SerialBuffered::~SerialBuffered() {
lndv3 2:e92ce527b8b0 49 }
lndv3 2:e92ce527b8b0 50
lndv3 2:e92ce527b8b0 51 /**
lndv3 2:e92ce527b8b0 52 * Set timeout for getc().
lndv3 2:e92ce527b8b0 53 *
lndv3 2:e92ce527b8b0 54 * @param ms milliseconds. (-1:Disable timeout)
lndv3 2:e92ce527b8b0 55 */
lndv3 2:e92ce527b8b0 56 void SerialBuffered::setTimeout(int ms) {
lndv3 2:e92ce527b8b0 57 timeout = ms;
lndv3 2:e92ce527b8b0 58 }
lndv3 2:e92ce527b8b0 59
lndv3 2:e92ce527b8b0 60 /**
lndv3 2:e92ce527b8b0 61 * Read requested bytes.
lndv3 2:e92ce527b8b0 62 *
lndv3 2:e92ce527b8b0 63 * @param bytes A pointer to a buffer.
lndv3 2:e92ce527b8b0 64 * @param requested Length.
lndv3 2:e92ce527b8b0 65 *
lndv3 2:e92ce527b8b0 66 * @return Readed byte length.
lndv3 2:e92ce527b8b0 67 */
lndv3 2:e92ce527b8b0 68 size_t SerialBuffered::readBytes(uint8_t *bytes, size_t requested) {
lndv3 2:e92ce527b8b0 69 int i = 0;
lndv3 2:e92ce527b8b0 70 while (i < requested) {
lndv3 2:e92ce527b8b0 71 int c = getc();
lndv3 2:e92ce527b8b0 72 if (c < 0) {
lndv3 2:e92ce527b8b0 73 break;
lndv3 2:e92ce527b8b0 74 }
lndv3 2:e92ce527b8b0 75 bytes[i] = c;
lndv3 2:e92ce527b8b0 76 i++;
lndv3 2:e92ce527b8b0 77 }
lndv3 2:e92ce527b8b0 78 return i;
lndv3 2:e92ce527b8b0 79 }
lndv3 2:e92ce527b8b0 80
lndv3 2:e92ce527b8b0 81 /**
lndv3 2:e92ce527b8b0 82 * Get a character.
lndv3 2:e92ce527b8b0 83 *
lndv3 2:e92ce527b8b0 84 * @return A character. (-1:timeout)
lndv3 2:e92ce527b8b0 85 */
lndv3 2:e92ce527b8b0 86 int SerialBuffered::getc() {
lndv3 2:e92ce527b8b0 87 timer.reset();
lndv3 2:e92ce527b8b0 88 timer.start();
lndv3 2:e92ce527b8b0 89 while (indexContentStart == indexContentEnd) {
lndv3 2:e92ce527b8b0 90 wait_ms(1);
lndv3 2:e92ce527b8b0 91 if ((timeout > 0) && (timer.read_ms() > timeout)) {
lndv3 2:e92ce527b8b0 92 /*
lndv3 2:e92ce527b8b0 93 * Timeout occured.
lndv3 2:e92ce527b8b0 94 */
lndv3 2:e92ce527b8b0 95 // printf("Timeout occured.\n");
lndv3 2:e92ce527b8b0 96 return EOF;
lndv3 2:e92ce527b8b0 97 }
lndv3 2:e92ce527b8b0 98 }
lndv3 2:e92ce527b8b0 99 timer.stop();
lndv3 2:e92ce527b8b0 100
lndv3 2:e92ce527b8b0 101 uint8_t result = buffer[indexContentStart++];
lndv3 2:e92ce527b8b0 102 indexContentStart = indexContentStart % BUFFERSIZE;
lndv3 2:e92ce527b8b0 103
lndv3 2:e92ce527b8b0 104 return result;
lndv3 2:e92ce527b8b0 105 }
lndv3 2:e92ce527b8b0 106
lndv3 2:e92ce527b8b0 107 /**
lndv3 2:e92ce527b8b0 108 * Returns 1 if there is a character available to read, 0 otherwise.
lndv3 2:e92ce527b8b0 109 */
lndv3 2:e92ce527b8b0 110 int SerialBuffered::readable() {
lndv3 2:e92ce527b8b0 111 return indexContentStart != indexContentEnd;
lndv3 2:e92ce527b8b0 112 }
lndv3 2:e92ce527b8b0 113
lndv3 2:e92ce527b8b0 114 void SerialBuffered::handleInterrupt() {
lndv3 2:e92ce527b8b0 115 while (Serial::readable()) {
lndv3 2:e92ce527b8b0 116 if (indexContentStart == ((indexContentEnd + 1) % BUFFERSIZE)) {
lndv3 2:e92ce527b8b0 117 /*
lndv3 2:e92ce527b8b0 118 * Buffer overrun occured.
lndv3 2:e92ce527b8b0 119 */
lndv3 2:e92ce527b8b0 120 // printf("Buffer overrun occured.\n");
lndv3 2:e92ce527b8b0 121 Serial::getc();
lndv3 2:e92ce527b8b0 122 } else {
lndv3 2:e92ce527b8b0 123 buffer[indexContentEnd++] = Serial::getc();
lndv3 2:e92ce527b8b0 124 indexContentEnd = indexContentEnd % BUFFERSIZE;
lndv3 2:e92ce527b8b0 125 }
lndv3 2:e92ce527b8b0 126 }
lndv3 2:e92ce527b8b0 127 }