Smart coffee machine with facial recognition and remote control
Dependencies: Camera_LS_Y201 EthernetInterface EthernetNetIf HTTPClient SRF05 TextLCD mbed-rtos mbed-src
SerialBuffered.cpp
00001 /** 00002 * ============================================================================= 00003 * LS-Y201 device driver class (Version 0.0.1) 00004 * Reference documents: LinkSprite JPEG Color Camera Serial UART Interface 00005 * January 2010 00006 * ============================================================================= 00007 * Copyright (c) 2010 Shinichiro Nakamura (CuBeatSystems) 00008 * 00009 * Permission is hereby granted, free of charge, to any person obtaining a copy 00010 * of this software and associated documentation files (the "Software"), to deal 00011 * in the Software without restriction, including without limitation the rights 00012 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 00013 * copies of the Software, and to permit persons to whom the Software is 00014 * furnished to do so, subject to the following conditions: 00015 * 00016 * The above copyright notice and this permission notice shall be included in 00017 * all copies or substantial portions of the Software. 00018 * 00019 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 00020 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 00021 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 00022 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 00023 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 00024 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 00025 * THE SOFTWARE. 00026 * ============================================================================= 00027 */ 00028 00029 #include <RawSerial.h> 00030 #include "mbed.h" 00031 #include "SerialBuffered.h" 00032 00033 /** 00034 * Create a buffered serial class. 00035 * 00036 * @param tx A pin for transmit. 00037 * @param rx A pin for receive. 00038 */ 00039 SerialBuffered::SerialBuffered(PinName tx, PinName rx) : RawSerial(tx, rx) { 00040 indexContentStart = 0; 00041 indexContentEnd = 0; 00042 timeout = 1; 00043 attach(this, &SerialBuffered::handleInterrupt); 00044 } 00045 00046 /** 00047 * Destroy. 00048 */ 00049 SerialBuffered::~SerialBuffered() { 00050 } 00051 00052 /** 00053 * Set timeout for getc(). 00054 * 00055 * @param ms milliseconds. (-1:Disable timeout) 00056 */ 00057 void SerialBuffered::setTimeout(int ms) { 00058 timeout = ms; 00059 } 00060 00061 /** 00062 * Read requested bytes. 00063 * 00064 * @param bytes A pointer to a buffer. 00065 * @param requested Length. 00066 * 00067 * @return Readed byte length. 00068 */ 00069 size_t SerialBuffered::readBytes(uint8_t *bytes, size_t requested) { 00070 int i = 0; 00071 while (i < requested) { 00072 int c = getc(); 00073 if (c < 0) { 00074 break; 00075 } 00076 bytes[i] = c; 00077 i++; 00078 } 00079 return i; 00080 } 00081 00082 /** 00083 * Get a character. 00084 * 00085 * @return A character. (-1:timeout) 00086 */ 00087 int SerialBuffered::getc() { 00088 timer.reset(); 00089 timer.start(); 00090 while (indexContentStart == indexContentEnd) { 00091 wait_ms(1); 00092 if ((timeout > 0) && (timer.read_ms() > timeout)) { 00093 /* 00094 * Timeout occured. 00095 */ 00096 // printf("Timeout occured.\n"); 00097 return EOF; 00098 } 00099 } 00100 timer.stop(); 00101 00102 uint8_t result = buffer[indexContentStart++]; 00103 indexContentStart = indexContentStart % BUFFERSIZE; 00104 00105 return result; 00106 } 00107 00108 /** 00109 * Returns 1 if there is a character available to read, 0 otherwise. 00110 */ 00111 int SerialBuffered::readable() { 00112 return indexContentStart != indexContentEnd; 00113 } 00114 00115 void SerialBuffered::handleInterrupt() { 00116 while (RawSerial::readable()) { 00117 if (indexContentStart == ((indexContentEnd + 1) % BUFFERSIZE)) { 00118 /* 00119 * Buffer overrun occured. 00120 */ 00121 // printf("Buffer overrun occured.\n"); 00122 RawSerial::getc(); 00123 } else { 00124 buffer[indexContentEnd++] = RawSerial::getc(); 00125 indexContentEnd = indexContentEnd % BUFFERSIZE; 00126 } 00127 } 00128 }
Generated on Wed Jul 13 2022 03:22:24 by 1.7.2