たけし みわ
/
y_CameraC1098_ES_01
CameraC1098/SerialBuffered.cpp@0:5bf7e3564c3b, 2012-06-17 (annotated)
- Committer:
- esmiwa
- Date:
- Sun Jun 17 01:15:35 2012 +0000
- Revision:
- 0:5bf7e3564c3b
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
esmiwa | 0:5bf7e3564c3b | 1 | #include "mbed.h" |
esmiwa | 0:5bf7e3564c3b | 2 | #include "SerialBuffered.h" |
esmiwa | 0:5bf7e3564c3b | 3 | |
esmiwa | 0:5bf7e3564c3b | 4 | /** |
esmiwa | 0:5bf7e3564c3b | 5 | * Create a buffered serial class. |
esmiwa | 0:5bf7e3564c3b | 6 | * |
esmiwa | 0:5bf7e3564c3b | 7 | * @param tx A pin for transmit. |
esmiwa | 0:5bf7e3564c3b | 8 | * @param rx A pin for receive. |
esmiwa | 0:5bf7e3564c3b | 9 | */ |
esmiwa | 0:5bf7e3564c3b | 10 | SerialBuffered::SerialBuffered(PinName tx, PinName rx) : Serial(tx, rx) { |
esmiwa | 0:5bf7e3564c3b | 11 | indexContentStart = 0; |
esmiwa | 0:5bf7e3564c3b | 12 | indexContentEnd = 0; |
esmiwa | 0:5bf7e3564c3b | 13 | timeout = 1; |
esmiwa | 0:5bf7e3564c3b | 14 | attach(this, &SerialBuffered::handleInterrupt); |
esmiwa | 0:5bf7e3564c3b | 15 | } |
esmiwa | 0:5bf7e3564c3b | 16 | |
esmiwa | 0:5bf7e3564c3b | 17 | /** |
esmiwa | 0:5bf7e3564c3b | 18 | * Destroy. |
esmiwa | 0:5bf7e3564c3b | 19 | */ |
esmiwa | 0:5bf7e3564c3b | 20 | SerialBuffered::~SerialBuffered() { |
esmiwa | 0:5bf7e3564c3b | 21 | } |
esmiwa | 0:5bf7e3564c3b | 22 | |
esmiwa | 0:5bf7e3564c3b | 23 | /** |
esmiwa | 0:5bf7e3564c3b | 24 | * Set timeout for getc(). |
esmiwa | 0:5bf7e3564c3b | 25 | * |
esmiwa | 0:5bf7e3564c3b | 26 | * @param ms milliseconds. (-1:Disable timeout) |
esmiwa | 0:5bf7e3564c3b | 27 | */ |
esmiwa | 0:5bf7e3564c3b | 28 | void SerialBuffered::setTimeout(int ms) { |
esmiwa | 0:5bf7e3564c3b | 29 | timeout = ms; |
esmiwa | 0:5bf7e3564c3b | 30 | } |
esmiwa | 0:5bf7e3564c3b | 31 | |
esmiwa | 0:5bf7e3564c3b | 32 | /** |
esmiwa | 0:5bf7e3564c3b | 33 | * Read requested bytes. |
esmiwa | 0:5bf7e3564c3b | 34 | * |
esmiwa | 0:5bf7e3564c3b | 35 | * @param bytes A pointer to a buffer. |
esmiwa | 0:5bf7e3564c3b | 36 | * @param requested Length. |
esmiwa | 0:5bf7e3564c3b | 37 | * |
esmiwa | 0:5bf7e3564c3b | 38 | * @return Readed byte length. |
esmiwa | 0:5bf7e3564c3b | 39 | */ |
esmiwa | 0:5bf7e3564c3b | 40 | size_t SerialBuffered::readBytes(uint8_t *bytes, size_t requested) { |
esmiwa | 0:5bf7e3564c3b | 41 | int i = 0; |
esmiwa | 0:5bf7e3564c3b | 42 | while (i < requested) { |
esmiwa | 0:5bf7e3564c3b | 43 | int c = getc(); |
esmiwa | 0:5bf7e3564c3b | 44 | if (c < 0) { |
esmiwa | 0:5bf7e3564c3b | 45 | break; |
esmiwa | 0:5bf7e3564c3b | 46 | } |
esmiwa | 0:5bf7e3564c3b | 47 | bytes[i] = c; |
esmiwa | 0:5bf7e3564c3b | 48 | i++; |
esmiwa | 0:5bf7e3564c3b | 49 | } |
esmiwa | 0:5bf7e3564c3b | 50 | return i; |
esmiwa | 0:5bf7e3564c3b | 51 | } |
esmiwa | 0:5bf7e3564c3b | 52 | |
esmiwa | 0:5bf7e3564c3b | 53 | /** |
esmiwa | 0:5bf7e3564c3b | 54 | * Get a character. |
esmiwa | 0:5bf7e3564c3b | 55 | * |
esmiwa | 0:5bf7e3564c3b | 56 | * @return A character. (-1:timeout) |
esmiwa | 0:5bf7e3564c3b | 57 | */ |
esmiwa | 0:5bf7e3564c3b | 58 | int SerialBuffered::getc() { |
esmiwa | 0:5bf7e3564c3b | 59 | timer.reset(); |
esmiwa | 0:5bf7e3564c3b | 60 | timer.start(); |
esmiwa | 0:5bf7e3564c3b | 61 | while (indexContentStart == indexContentEnd) { |
esmiwa | 0:5bf7e3564c3b | 62 | wait_ms(1); |
esmiwa | 0:5bf7e3564c3b | 63 | if ((timeout > 0) && (timer.read_ms() > timeout)) { |
esmiwa | 0:5bf7e3564c3b | 64 | /* |
esmiwa | 0:5bf7e3564c3b | 65 | * Timeout occured. |
esmiwa | 0:5bf7e3564c3b | 66 | */ |
esmiwa | 0:5bf7e3564c3b | 67 | // printf("Timeout occured.\n"); |
esmiwa | 0:5bf7e3564c3b | 68 | return EOF; |
esmiwa | 0:5bf7e3564c3b | 69 | } |
esmiwa | 0:5bf7e3564c3b | 70 | } |
esmiwa | 0:5bf7e3564c3b | 71 | timer.stop(); |
esmiwa | 0:5bf7e3564c3b | 72 | |
esmiwa | 0:5bf7e3564c3b | 73 | uint8_t result = buffer[indexContentStart++]; |
esmiwa | 0:5bf7e3564c3b | 74 | indexContentStart = indexContentStart % BUFFERSIZE; |
esmiwa | 0:5bf7e3564c3b | 75 | |
esmiwa | 0:5bf7e3564c3b | 76 | return result; |
esmiwa | 0:5bf7e3564c3b | 77 | } |
esmiwa | 0:5bf7e3564c3b | 78 | |
esmiwa | 0:5bf7e3564c3b | 79 | /** |
esmiwa | 0:5bf7e3564c3b | 80 | * Returns 1 if there is a character available to read, 0 otherwise. |
esmiwa | 0:5bf7e3564c3b | 81 | */ |
esmiwa | 0:5bf7e3564c3b | 82 | int SerialBuffered::readable() { |
esmiwa | 0:5bf7e3564c3b | 83 | return indexContentStart != indexContentEnd; |
esmiwa | 0:5bf7e3564c3b | 84 | } |
esmiwa | 0:5bf7e3564c3b | 85 | |
esmiwa | 0:5bf7e3564c3b | 86 | void SerialBuffered::handleInterrupt() { |
esmiwa | 0:5bf7e3564c3b | 87 | while (Serial::readable()) { |
esmiwa | 0:5bf7e3564c3b | 88 | if (indexContentStart == ((indexContentEnd + 1) % BUFFERSIZE)) { |
esmiwa | 0:5bf7e3564c3b | 89 | /* |
esmiwa | 0:5bf7e3564c3b | 90 | * Buffer overrun occured. |
esmiwa | 0:5bf7e3564c3b | 91 | */ |
esmiwa | 0:5bf7e3564c3b | 92 | // printf("Buffer overrun occured.\n"); |
esmiwa | 0:5bf7e3564c3b | 93 | Serial::getc(); |
esmiwa | 0:5bf7e3564c3b | 94 | } else { |
esmiwa | 0:5bf7e3564c3b | 95 | buffer[indexContentEnd++] = Serial::getc(); |
esmiwa | 0:5bf7e3564c3b | 96 | indexContentEnd = indexContentEnd % BUFFERSIZE; |
esmiwa | 0:5bf7e3564c3b | 97 | } |
esmiwa | 0:5bf7e3564c3b | 98 | } |
esmiwa | 0:5bf7e3564c3b | 99 | } |