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