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