a program i made a while back to log gps/accelerometer data
Dependencies: FatFileSystem mbed
Diff: SerialBuffered.cpp
- Revision:
- 0:82a02991476c
diff -r 000000000000 -r 82a02991476c SerialBuffered.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/SerialBuffered.cpp Sat Sep 08 20:40:58 2012 +0000 @@ -0,0 +1,155 @@ + +#include "mbed.h" +#include "SerialBuffered.h" +#include <string.h> + +//extern Serial loggerSerial; + +SerialBuffered::SerialBuffered( size_t bufferSize, PinName tx, PinName rx ) : Serial( tx, rx ) { + m_buffSize = 0; + m_contentStart = 0; + m_contentEnd = 0; + m_timeout = 1.0; + ReadStrings = false; + unreadStrings = 0; + + attach( this, &SerialBuffered::handleInterrupt ); + + m_buff = (uint8_t *) malloc( bufferSize ); + if ( m_buff == NULL ) { + //loggerSerial.printf("SerialBuffered - failed to alloc buffer size %d\r\n", (int) bufferSize ); + } else { + m_buffSize = bufferSize; + } +} + + +SerialBuffered::~SerialBuffered() { + if ( m_buff ) + free( m_buff ); + if (str_buff); + free(str_buff); +} + + +void SerialBuffered::setTimeout( float seconds ) { + m_timeout = seconds; +} +//////////////////// +void SerialBuffered::startReadStrings( char endChar) { + term = endChar; + str_buff_pos = 0; + str_wr_buff_pos = 0; + int i; + for (i = 0; i < STRING_BUFFERS; i++) + str_buff[i] = (char *) malloc(m_buffSize); + + ReadStrings = true; +} + +void SerialBuffered::stopReadStrings() { + ReadStrings = false; + +} + +int SerialBuffered::stringsAvailable() { + if (unreadStrings < STRING_BUFFERS) + return unreadStrings; + else + return -1; +} + +char* SerialBuffered::getString() { + unreadStrings--; + if (str_buff_pos >= STRING_BUFFERS) + str_buff_pos = 0; + + return str_buff[str_buff_pos++]; +} +////////////////////////////////////////// + +size_t SerialBuffered::readBytes( uint8_t *bytes, size_t requested ) { + int i = 0; + + for ( ; i < requested; ) { + int c = getc(); + if ( c < 0 ) + break; + bytes[i] = c; + i++; + } + + return i; + +} + + + + + +int SerialBuffered::getc() { + m_timer.reset(); + m_timer.start(); + while ( m_contentStart == m_contentEnd ) { + + + wait_ms( 1 ); + if ( m_timeout > 0 && m_timer.read() > m_timeout ) + return EOF; + } + + m_timer.stop(); + + uint8_t result = m_buff[m_contentStart++]; + m_contentStart = m_contentStart % m_buffSize; + + + return result; +} + + +int SerialBuffered::readable() { + return m_contentStart != m_contentEnd ; +} + + + + +//isr +void SerialBuffered::handleInterrupt() { + if (ReadStrings) { + + str_buff[str_wr_buff_pos][ m_contentEnd ] = Serial::getc(); + if (str_buff[str_wr_buff_pos][ m_contentEnd ] == term) { + str_buff[str_wr_buff_pos][ m_contentEnd + 1 ] = '\0'; + unreadStrings++; + str_wr_buff_pos++; + m_contentEnd = 0; + + if (str_wr_buff_pos >= STRING_BUFFERS) + str_wr_buff_pos = 0; + if (unreadStrings >= STRING_BUFFERS) + unreadStrings = 0; + } else { + m_contentEnd = ++m_contentEnd % m_buffSize; + } + + + } else { + while ( Serial::readable()) { + if ( m_contentStart == (m_contentEnd +1) % m_buffSize) { + // loggerSerial.printf("SerialBuffered - buffer overrun, data lost!\r\n" ); + Serial::getc(); + + } else { + + m_buff[ m_contentEnd ++ ] = Serial::getc(); + m_contentEnd = m_contentEnd % m_buffSize; + + + + } + } + } +} +