MHVLib
20111011
An efficiency oriented runtime library for AVR microcontrollers
|
00001 /* 00002 * Copyright (c) 2011, Make, Hack, Void Inc 00003 * All rights reserved. 00004 * 00005 * Redistribution and use in source and binary forms, with or without 00006 * modification, are permitted provided that the following conditions are met: 00007 * * Redistributions of source code must retain the above copyright 00008 * notice, this list of conditions and the following disclaimer. 00009 * * Redistributions in binary form must reproduce the above copyright 00010 * notice, this list of conditions and the following disclaimer in the 00011 * documentation and/or other materials provided with the distribution. 00012 * * Neither the name of the Make, Hack, Void nor the 00013 * names of its contributors may be used to endorse or promote products 00014 * derived from this software without specific prior written permission. 00015 * 00016 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 00017 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00018 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00019 * DISCLAIMED. IN NO EVENT SHALL MAKE, HACK, VOID BE LIABLE FOR ANY 00020 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00021 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00022 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00023 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00024 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 00025 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 */ 00027 00028 00029 #include <stdio.h> 00030 #include <avr/pgmspace.h> 00031 #include <stdlib.h> 00032 #include <string.h> 00033 #include <inttypes.h> 00034 00035 #include <MHV_Device_RX.h> 00036 00041 MHV_Device_RX::MHV_Device_RX(MHV_RingBuffer *rxBuffer) { 00042 _rxBuffer = rxBuffer; 00043 _listener = NULL; 00044 } 00045 00053 int MHV_Device_RX::asyncReadLine(char *buffer, uint8_t bufferLength) { 00054 // Peek at the last character & see if its a newline 00055 int last = _rxBuffer->peekHead(); 00056 00057 bool isFull = _rxBuffer->full(); 00058 if (!isFull) { 00059 if ('\r' != last && '\n' != last) { 00060 return -1; 00061 } 00062 } 00063 00064 uint8_t i = 0; 00065 int byte; 00066 while (-1 != (byte = _rxBuffer->consume())) { 00067 if (i == bufferLength - 1) { 00068 buffer[i] = '\0'; 00069 return -2; 00070 } 00071 if ('\r' == byte || '\n' == byte) { 00072 break; 00073 } 00074 buffer[i++] = (char)byte; 00075 } 00076 buffer[i] = '\0'; 00077 00078 if (isFull) { 00079 return -3; 00080 } 00081 return 0; 00082 } 00083 00091 int MHV_Device_RX::busyReadLine(char *buffer, uint8_t bufferLength) { 00092 int rc; 00093 while (-1 == (rc = asyncReadLine(buffer, bufferLength))) {} 00094 return rc; 00095 } 00096 00101 int MHV_Device_RX::read(void) { 00102 return _rxBuffer->consume(); 00103 } 00104 00108 void MHV_Device_RX::flush() { 00109 _rxBuffer->flush(); 00110 } 00111 00116 bool MHV_Device_RX::ready() { 00117 if (_rxBuffer->full()) { 00118 return true; 00119 } 00120 00121 // Peek at the last character & see if its a newline 00122 int last = _rxBuffer->peekHead(); 00123 if ('\r' == last || '\n' == last) { 00124 return true; 00125 } 00126 00127 return false; 00128 } 00129 00134 void MHV_Device_RX::registerListener(MHV_RXListener *listener) { 00135 _listener = listener; 00136 } 00137 00141 void MHV_Device_RX::deregisterListener() { 00142 _listener = NULL; 00143 } 00144 00145 00149 void MHV_Device_RX::handleEvents() { 00150 if (ready()) { 00151 _listener->rxReady(this); 00152 } 00153 } 00154