test

Dependencies:   Nanopb iSerial mbed BaseJpegDecode FatFileSystem SDFileSystem RingBuffer Camera_LS_Y201

Committer:
cgraham
Date:
Thu Sep 18 15:21:47 2014 +0000
Revision:
0:d69efd0ee139
test

Who changed what in which revision?

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