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