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