V.06 11/3

Dependencies:   FT6206 SDFileSystem SPI_TFT_ILI9341 TFT_fonts

Fork of ATT_AWS_IoT_demo by attiot

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MODSERIAL.cpp Source File

MODSERIAL.cpp

00001 /*
00002     Copyright (c) 2010 Andy Kirkham
00003  
00004     Permission is hereby granted, free of charge, to any person obtaining a copy
00005     of this software and associated documentation files (the "Software"), to deal
00006     in the Software without restriction, including without limitation the rights
00007     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008     copies of the Software, and to permit persons to whom the Software is
00009     furnished to do so, subject to the following conditions:
00010  
00011     The above copyright notice and this permission notice shall be included in
00012     all copies or substantial portions of the Software.
00013  
00014     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020     THE SOFTWARE.
00021     
00022     @file          MODSERIAL.h 
00023     @purpose       Extends Serial to provide fully buffered IO
00024     @version       1.6
00025     @date          Nov 2010
00026     @author        Andy Kirkham    
00027 */
00028 
00029 #include "MODSERIAL.h"
00030 #include "MACROS.h"
00031 
00032 namespace AjK {
00033 
00034 MODSERIAL::MODSERIAL( PinName tx, PinName rx, const char* name ) : Serial( tx, rx, name )
00035 {
00036     init( MODSERIAL_DEFAULT_TX_BUFFER_SIZE, MODSERIAL_DEFAULT_RX_BUFFER_SIZE, rx );
00037 }
00038 
00039 MODSERIAL::MODSERIAL( PinName tx, PinName rx, int bufferSize, const char* name ) : Serial( tx, rx, name )
00040 {
00041     init( bufferSize, bufferSize, rx );
00042 }
00043 
00044 MODSERIAL::MODSERIAL( PinName tx, PinName rx, int txSize, int rxSize, const char* name ) : Serial( tx, rx, name )
00045 {
00046     init( txSize, rxSize, rx );
00047 }
00048 
00049 MODSERIAL::~MODSERIAL()
00050 {
00051     NVIC_DisableIRQ(_IRQ);
00052     if ( buffer[0] != NULL) free((char *)buffer[0] );
00053     if ( buffer[1] != NULL) free((char *)buffer[1] );    
00054 }
00055 
00056 bool MODSERIAL::txBufferFull( void ) 
00057 { 
00058     return MODSERIAL_TX_BUFFER_FULL; 
00059 }
00060 
00061 bool MODSERIAL::rxBufferFull( void ) 
00062 { 
00063     return MODSERIAL_RX_BUFFER_FULL; 
00064 }
00065 
00066 bool MODSERIAL::txBufferEmpty( void ) 
00067 { 
00068     return MODSERIAL_TX_BUFFER_EMPTY; 
00069 }
00070 
00071 bool MODSERIAL::rxBufferEmpty( void ) 
00072 { 
00073     return MODSERIAL_RX_BUFFER_EMPTY; 
00074 }
00075 
00076 
00077 int MODSERIAL::rxDiscardLastChar( void )
00078 {
00079     // This function can only be called indirectly from
00080     // an rxCallback function. Therefore, we know we 
00081     // just placed a char into the buffer.
00082     char c = buffer[RxIrq][buffer_in[RxIrq]];
00083     
00084     if (buffer_count[RxIrq]) {        
00085         buffer_count[RxIrq]--;
00086         buffer_in[RxIrq]--;
00087         if (buffer_in[RxIrq] < 0) {
00088             buffer_in[RxIrq] = buffer_size[RxIrq] - 1;
00089         }
00090     }
00091     
00092     return (int)c;
00093 }
00094 
00095 
00096 bool MODSERIAL::claim (FILE *stream) {
00097 #if 0
00098     if ( _name == NULL) {
00099         error("claim requires a name to be given in the instantiator of the MODSERIAL instance!\r\n");
00100     }
00101     
00102     //Add '/' before name:
00103     char *path = new char[strlen(_name) + 2];
00104     sprintf(path, "/%s", _name);
00105     
00106     if (freopen(path, "w", stream) == NULL) {
00107         // Failed, should not happen
00108         return false;
00109     }
00110     
00111     delete(path);
00112     
00113     //No buffering
00114     setvbuf(stdout, NULL, _IONBF, buffer_size[TxIrq]);
00115 #endif
00116 
00117     return true;
00118 } 
00119 
00120 
00121 
00122 }; // namespace AjK ends
00123