From 8b081b8c9f91f1473e80566f773d8710e03cc173 Mon Sep 17 00:00:00 2001 From: BlackMark Date: Sat, 21 May 2016 19:11:04 +0200 Subject: [PATCH] Replaced c array buffer with c++ array class buffer --- .gitmodules | 3 ++ usart/array | 1 + usart/main.cpp | 68 ++++++++++++++++++++++----------------------- usart/usart.cpp | 4 +-- usart/usart.cppproj | 6 ++++ usart/usart.h | 2 +- 6 files changed, 47 insertions(+), 37 deletions(-) create mode 100644 .gitmodules create mode 160000 usart/array diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..c7e0c08 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "usart/array"] + path = usart/array + url = git@blackmark.me:array.git diff --git a/usart/array b/usart/array new file mode 160000 index 0000000..634564a --- /dev/null +++ b/usart/array @@ -0,0 +1 @@ +Subproject commit 634564a7bb6f34305dbc6f68993a34a4419899a6 diff --git a/usart/main.cpp b/usart/main.cpp index 577d594..cdbfbe0 100644 --- a/usart/main.cpp +++ b/usart/main.cpp @@ -7,6 +7,7 @@ #include #include "clock.h" #include "usart.h" +#include "array/array.h" int main() { @@ -15,75 +16,74 @@ int main() uint32_t ui32Counter = 0; - constexpr size_t sizeBUFFER_SIZE = 64; - char szBuffer[sizeBUFFER_SIZE]; + array arrBuffer; cUSART.transmitString( "\r\nSizes: \r\n" ); - sprintf( szBuffer, "%d\r\n", sizeof( char ) ); + sprintf( arrBuffer.data(), "%d\r\n", sizeof( char ) ); cUSART.transmitString( "sizeof( char ) = " ); - cUSART.transmitString( szBuffer ); + cUSART.transmitString( arrBuffer.data() ); - sprintf( szBuffer, "%d\r\n", sizeof( unsigned char ) ); + sprintf( arrBuffer.data(), "%d\r\n", sizeof( unsigned char ) ); cUSART.transmitString( "sizeof( unsigned char ) = " ); - cUSART.transmitString( szBuffer ); + cUSART.transmitString( arrBuffer.data() ); - sprintf( szBuffer, "%d\r\n", sizeof( short int ) ); + sprintf( arrBuffer.data(), "%d\r\n", sizeof( short int ) ); cUSART.transmitString( "sizeof( short int ) = " ); - cUSART.transmitString( szBuffer ); + cUSART.transmitString( arrBuffer.data() ); - sprintf( szBuffer, "%d\r\n", sizeof( unsigned short int ) ); + sprintf( arrBuffer.data(), "%d\r\n", sizeof( unsigned short int ) ); cUSART.transmitString( "sizeof( unsigned short int ) = " ); - cUSART.transmitString( szBuffer ); + cUSART.transmitString( arrBuffer.data() ); - sprintf( szBuffer, "%d\r\n", sizeof( int ) ); + sprintf( arrBuffer.data(), "%d\r\n", sizeof( int ) ); cUSART.transmitString( "sizeof( int ) = " ); - cUSART.transmitString( szBuffer ); + cUSART.transmitString( arrBuffer.data() ); - sprintf( szBuffer, "%d\r\n", sizeof( unsigned int ) ); + sprintf( arrBuffer.data(), "%d\r\n", sizeof( unsigned int ) ); cUSART.transmitString( "sizeof( unsigned int ) = " ); - cUSART.transmitString( szBuffer ); + cUSART.transmitString( arrBuffer.data() ); - sprintf( szBuffer, "%d\r\n", sizeof( long int ) ); + sprintf( arrBuffer.data(), "%d\r\n", sizeof( long int ) ); cUSART.transmitString( "sizeof( long int ) = " ); - cUSART.transmitString( szBuffer ); + cUSART.transmitString( arrBuffer.data() ); - sprintf( szBuffer, "%d\r\n", sizeof( unsigned long int ) ); + sprintf( arrBuffer.data(), "%d\r\n", sizeof( unsigned long int ) ); cUSART.transmitString( "sizeof( unsigned long int ) = " ); - cUSART.transmitString( szBuffer ); + cUSART.transmitString( arrBuffer.data() ); - sprintf( szBuffer, "%d\r\n", sizeof( long long int ) ); + sprintf( arrBuffer.data(), "%d\r\n", sizeof( long long int ) ); cUSART.transmitString( "sizeof( long long int ) = " ); - cUSART.transmitString( szBuffer ); + cUSART.transmitString( arrBuffer.data() ); - sprintf( szBuffer, "%d\r\n", sizeof( unsigned long long int ) ); + sprintf( arrBuffer.data(), "%d\r\n", sizeof( unsigned long long int ) ); cUSART.transmitString( "sizeof( unsigned long long int ) = " ); - cUSART.transmitString( szBuffer ); + cUSART.transmitString( arrBuffer.data() ); - sprintf( szBuffer, "%d\r\n", sizeof( float ) ); + sprintf( arrBuffer.data(), "%d\r\n", sizeof( float ) ); cUSART.transmitString( "sizeof( float ) = " ); - cUSART.transmitString( szBuffer ); + cUSART.transmitString( arrBuffer.data() ); - sprintf( szBuffer, "%d\r\n", sizeof( double ) ); + sprintf( arrBuffer.data(), "%d\r\n", sizeof( double ) ); cUSART.transmitString( "sizeof( double ) = " ); - cUSART.transmitString( szBuffer ); + cUSART.transmitString( arrBuffer.data() ); - sprintf( szBuffer, "%d\r\n", sizeof( long double ) ); + sprintf( arrBuffer.data(), "%d\r\n", sizeof( long double ) ); cUSART.transmitString( "sizeof( long double ) = " ); - cUSART.transmitString( szBuffer ); + cUSART.transmitString( arrBuffer.data() ); - sprintf( szBuffer, "%d\r\n\r\n", sizeof( void* ) ); + sprintf( arrBuffer.data(), "%d\r\n\r\n", sizeof( void* ) ); cUSART.transmitString( "sizeof( void* ) = " ); - cUSART.transmitString( szBuffer ); + cUSART.transmitString( arrBuffer.data() ); while( true ) { - sprintf( szBuffer, "%lu", ui32Counter++ ); + sprintf( arrBuffer.data(), "%lu", ui32Counter++ ); cUSART.transmitString( "This has been running for \"" ); - cUSART.transmitString( szBuffer ); + cUSART.transmitString( arrBuffer.data() ); cUSART.transmitString( "\" seconds!\r\n" ); - if( !cUSART.receiveLine( szBuffer, sizeBUFFER_SIZE, "\r" ) ) + if( !cUSART.receiveLine( arrBuffer.data(), arrBuffer.size(), "\r" ) ) { cUSART.transmitString( "Receive error: " ); } @@ -92,7 +92,7 @@ int main() cUSART.transmitString( "Echo: " ); } - cUSART.transmitString( szBuffer ); + cUSART.transmitString( arrBuffer.data() ); cUSART.transmitString( "\r\n" ); uint8_t ui8Byte; diff --git a/usart/usart.cpp b/usart/usart.cpp index 01f1893..52a0a5a 100644 --- a/usart/usart.cpp +++ b/usart/usart.cpp @@ -291,11 +291,11 @@ bool USART0::receiveByte( uint8_t &ui8Data, uint16_t ui16DelayMS ) } ////////////////////////////////////////////////////////////////////////// -bool USART0::receiveLine( char *szBuffer, size_t sizeLength, const char *szLineTerminator /* = "\r\n" */, size_t sizeMaxSize /* = 1024 */ ) +bool USART0::receiveLine( char *szBuffer, size_t sizeBufferLength, const char *szLineTerminator /* = "\r\n" */, size_t sizeMaxSize /* = 512 */ ) { size_t sizeReceived = 0; - while( sizeReceived < sizeMaxSize - 1 && sizeReceived < sizeLength - 1 ) + while( sizeReceived < sizeMaxSize - 1 && sizeReceived < sizeBufferLength - 1 ) { uint8_t ui8ReceiveByte; diff --git a/usart/usart.cppproj b/usart/usart.cppproj index 13483d3..2021324 100644 --- a/usart/usart.cppproj +++ b/usart/usart.cppproj @@ -192,6 +192,9 @@ echo "C:\avrdude-6.2\avrdude.exe" -v -p$(avrdevice) %%* -Uflash:w:"$(OutputDirectory)\$(Name).hex":i > "$(MSBuildProjectDirectory)\avrdude.bat" + + compile + compile @@ -205,5 +208,8 @@ compile + + + \ No newline at end of file diff --git a/usart/usart.h b/usart/usart.h index 7a412c0..399214c 100644 --- a/usart/usart.h +++ b/usart/usart.h @@ -137,7 +137,7 @@ public: bool receiveByte( uint8_t &ui8Data ); bool receiveByte( uint8_t &ui8Data, uint16_t ui16DelayMS ); - bool receiveLine( char *szBuffer, size_t sizeLength, const char *szLineTerminator = "\r\n", size_t sizeMaxSize = 512 ); + bool receiveLine( char *szBuffer, size_t sizeBufferLength, const char *szLineTerminator = "\r\n", size_t sizeMaxSize = 512 ); void transmitByte( uint8_t ui8Data ); void transmitString( const char *szString );