Buffering Serial receive data class
Dependents: 10dof 10dof2 TTB_Wallbot Nucleo_L432KC_Quadrature_Decoder_with_ADC_and_DAC
Revision 0:633dd0246854, committed 2013-05-19
- Comitter:
- KentaShimizu
- Date:
- Sun May 19 16:08:07 2013 +0000
- Commit message:
- Buffering Serial receive data class
Changed in this revision
BufferSerial.cpp | Show annotated file Show diff for this revision Revisions of this file |
BufferSerial.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 633dd0246854 BufferSerial.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BufferSerial.cpp Sun May 19 16:08:07 2013 +0000 @@ -0,0 +1,57 @@ +#include "BufferSerial.h" + +BufferSerial :: BufferSerial( PinName tx , PinName rx ) : Serial( tx , rx ) { + _setup( 1 ); +} + +BufferSerial :: BufferSerial( PinName tx , PinName rx , const int& bufferSize ) : Serial( tx , rx ) { + _setup( bufferSize ); +} + +BufferSerial :: BufferSerial( PinName tx , PinName rx , const int& bufferSize , const char* name ) : Serial( tx , rx , name ) { + _setup( bufferSize ); +} + +BufferSerial :: ~BufferSerial() { + delete[] _buf; +} + +void BufferSerial :: _setup( const int& bufferSize ) { + if ( bufferSize > 1 ){ + _buf = new unsigned char[ bufferSize ]; + _size = bufferSize - 1; + }else{ + _buf = new unsigned char[ 2 ]; + _size = 1; + } + _present = 0; + _last = 0; + attach( this, & BufferSerial :: _irq ); +} + +int BufferSerial :: _getShift( const int& value ) { + return value ? ( value - 1 ) : _size ; +} + +void BufferSerial :: _irq( void ) { + int n = _getShift( _last ); + _buf[ n ] = (unsigned char)( Serial :: getc() ); + _last = n; +} + +int BufferSerial :: unreadable( void ) { + return ( _present == _last ); +} + +int BufferSerial :: readable( void ) { + return ! unreadable(); +} + +int BufferSerial :: getc( void ) { + if ( unreadable() ){ + return -1; + }else{ + _present = _getShift( _present ); + return (int)_buf[ _present ]; + } +}
diff -r 000000000000 -r 633dd0246854 BufferSerial.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/BufferSerial.h Sun May 19 16:08:07 2013 +0000 @@ -0,0 +1,32 @@ +/* mbed BufferSerial Library + * Copyright (c) 2013 KentaShimizu + * Version 0.1 (May 18, 2013) + * Released under the MIT License: http://mbed.org/license/mit + */ + +#ifndef _IG_BUFFERSERIAL_20130518 +#define _IG_BUFFERSERIAL_20130518 + +#include "mbed.h" + +class BufferSerial : public Serial { +private: +protected: + int _size; + int _present; + int _last; + unsigned char* _buf; + void _setup( const int& size ); + int _getShift( const int& value ); + void _irq( void ); +public: + BufferSerial( PinName tx , PinName rx ); + BufferSerial( PinName tx , PinName rx , const int& bufferSize ); + BufferSerial( PinName tx , PinName rx , const int& bufferSize , const char* name ); + virtual ~BufferSerial(); + virtual int getc( void ); + virtual int unreadable( void ); + virtual int readable( void ); +}; + +#endif