From 97f0b0608b2e4d59735972213bdc68a6a1bfe9a9 Mon Sep 17 00:00:00 2001 From: BlackMark Date: Fri, 20 May 2016 17:02:23 +0200 Subject: [PATCH] Removed fancy formatting and generic receiving and transmitting to reduce complexity and size --- .gitmodules | 9 -- usart/cppalloc | 1 - usart/main.cpp | 109 +++++++++++++------ usart/string | 1 - usart/usart.cpp | 254 ++++---------------------------------------- usart/usart.cppproj | 20 ---- usart/usart.h | 63 ++--------- usart/vector | 1 - 8 files changed, 100 insertions(+), 358 deletions(-) delete mode 100644 .gitmodules delete mode 160000 usart/cppalloc delete mode 160000 usart/string delete mode 160000 usart/vector diff --git a/.gitmodules b/.gitmodules deleted file mode 100644 index 940a38b..0000000 --- a/.gitmodules +++ /dev/null @@ -1,9 +0,0 @@ -[submodule "usart/cppalloc"] - path = usart/cppalloc - url = git@blackmark.me:cppalloc.git -[submodule "usart/string"] - path = usart/string - url = git@blackmark.me:string.git -[submodule "usart/vector"] - path = usart/vector - url = git@blackmark.me:vector.git diff --git a/usart/cppalloc b/usart/cppalloc deleted file mode 160000 index 72c6a6c..0000000 --- a/usart/cppalloc +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 72c6a6cc88d6cafecf372fe968d3befae037ad9e diff --git a/usart/main.cpp b/usart/main.cpp index 3d7201e..fbf9773 100644 --- a/usart/main.cpp +++ b/usart/main.cpp @@ -15,44 +15,85 @@ int main() uint32_t ui32Counter = 0; - cUSART << "\r\nSizes: \r\n"; - cUSART << "sizeof( char ) = " << sizeof( char ) << "\r\n"; - cUSART << "sizeof( unsigned char ) = " << sizeof( unsigned char ) << "\r\n"; - cUSART << "sizeof( short int ) = " << sizeof( short int ) << "\r\n"; - cUSART << "sizeof( unsigned short int ) = " << sizeof( unsigned short int ) << "\r\n"; - cUSART << "sizeof( int ) = " << sizeof( int ) << "\r\n"; - cUSART << "sizeof( unsigned int ) = " << sizeof( unsigned int ) << "\r\n"; - cUSART << "sizeof( long int ) = " << sizeof( long int ) << "\r\n"; - cUSART << "sizeof( unsigned long int ) = " << sizeof( unsigned long int ) << "\r\n"; - cUSART << "sizeof( long long int ) = " << sizeof( long long int ) << "\r\n"; - cUSART << "sizeof( unsigned long long int ) = " << sizeof( unsigned long long int ) << "\r\n"; - cUSART << "sizeof( float ) = " << sizeof( float ) << "\r\n"; - cUSART << "sizeof( double ) = " << sizeof( double ) << "\r\n"; - cUSART << "sizeof( long double ) = " << sizeof( long double ) << "\r\n\r\n"; - cUSART << "sizeof( void* ) = " << sizeof( void* ) << "\r\n"; - cUSART << "sizeof( unsigned char* ) = " << sizeof( unsigned char* ) << "\r\n"; - cUSART << "sizeof( unsigned int* ) = " << sizeof( unsigned int* ) << "\r\n\r\n"; + constexpr size_t sizeBufferSize = 64; + char szBuffer[sizeBufferSize]; + + cUSART.transmitString( "\r\nSizes: \r\n" ); + + sprintf( szBuffer, "%d\r\n", sizeof( char ) ); + cUSART.transmitString( "sizeof( char ) = " ); + cUSART.transmitString( szBuffer ); + + sprintf( szBuffer, "%d\r\n", sizeof( unsigned char ) ); + cUSART.transmitString( "sizeof( unsigned char ) = " ); + cUSART.transmitString( szBuffer ); + + sprintf( szBuffer, "%d\r\n", sizeof( short int ) ); + cUSART.transmitString( "sizeof( short int ) = " ); + cUSART.transmitString( szBuffer ); + + sprintf( szBuffer, "%d\r\n", sizeof( unsigned short int ) ); + cUSART.transmitString( "sizeof( unsigned short int ) = " ); + cUSART.transmitString( szBuffer ); + + sprintf( szBuffer, "%d\r\n", sizeof( int ) ); + cUSART.transmitString( "sizeof( int ) = " ); + cUSART.transmitString( szBuffer ); + + sprintf( szBuffer, "%d\r\n", sizeof( unsigned int ) ); + cUSART.transmitString( "sizeof( unsigned int ) = " ); + cUSART.transmitString( szBuffer ); + + sprintf( szBuffer, "%d\r\n", sizeof( long int ) ); + cUSART.transmitString( "sizeof( long int ) = " ); + cUSART.transmitString( szBuffer ); + + sprintf( szBuffer, "%d\r\n", sizeof( unsigned long int ) ); + cUSART.transmitString( "sizeof( unsigned long int ) = " ); + cUSART.transmitString( szBuffer ); + + sprintf( szBuffer, "%d\r\n", sizeof( long long int ) ); + cUSART.transmitString( "sizeof( long long int ) = " ); + cUSART.transmitString( szBuffer ); + + sprintf( szBuffer, "%d\r\n", sizeof( unsigned long long int ) ); + cUSART.transmitString( "sizeof( unsigned long long int ) = " ); + cUSART.transmitString( szBuffer ); + + sprintf( szBuffer, "%d\r\n", sizeof( float ) ); + cUSART.transmitString( "sizeof( float ) = " ); + cUSART.transmitString( szBuffer ); + + sprintf( szBuffer, "%d\r\n", sizeof( double ) ); + cUSART.transmitString( "sizeof( double ) = " ); + cUSART.transmitString( szBuffer ); + + sprintf( szBuffer, "%d\r\n", sizeof( long double ) ); + cUSART.transmitString( "sizeof( long double ) = " ); + cUSART.transmitString( szBuffer ); + + sprintf( szBuffer, "%d\r\n\r\n", sizeof( void* ) ); + cUSART.transmitString( "sizeof( void* ) = " ); + cUSART.transmitString( szBuffer ); while( true ) { - cUSART << "This has been running for \"" << ui32Counter++ << "\" seconds!\r\n\r\n"; + sprintf( szBuffer, "%lu", ui32Counter++ ); + cUSART.transmitString( "This has been running for \"" ); + cUSART.transmitString( szBuffer ); + cUSART.transmitString( "\" seconds!\r\n" ); - cUSART << "Please enter a number: "; - - int iNumber; - cUSART >> iNumber; - - cUSART << "\r\nYou entered: " << iNumber << "\r\n\r\n"; - - cUSART << "Please enter a decimal number: "; - - double dNumber; - cUSART >> dNumber; - - cUSART << "\r\nYou entered: " << dNumber << "\r\n\r\n"; - - unsigned char uchByte; - cUSART.receiveByte( uchByte, 1000 ); + if( !cUSART.receiveLine( szBuffer, sizeBufferSize, "\r" ) ) + { + cUSART.transmitString( "Receive error: " ); + } + else + { + cUSART.transmitString( "Echo: " ); + } + + cUSART.transmitString( szBuffer ); + cUSART.transmitString( "\r\n" ); _delay_ms( 1000 ); } diff --git a/usart/string b/usart/string deleted file mode 160000 index 2318991..0000000 --- a/usart/string +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 23189911c6929685e962be9866ff6127938da6a8 diff --git a/usart/usart.cpp b/usart/usart.cpp index e558ed6..313ce27 100644 --- a/usart/usart.cpp +++ b/usart/usart.cpp @@ -219,7 +219,7 @@ void USART0::init( uint32_t ui32BaudRate /* = 9600 */, uint8_t ui8DataBits /* = } ////////////////////////////////////////////////////////////////////////// -bool USART0::receiveByte( unsigned char &chData, uint32_t ui32DelayMS ) +bool USART0::receiveByte( uint8_t &ui8Data, uint32_t ui32DelayMS ) { const uint8_t ui8ClockCyclesPerIteration = 6; double dDelayS = ui32DelayMS / 1000.0; @@ -230,7 +230,7 @@ bool USART0::receiveByte( unsigned char &chData, uint32_t ui32DelayMS ) { if( ( *m_vui8pUCSRA & ( 1 << RXC_D ) ) ) { - chData = *m_vui8pUDR; + ui8Data = *m_vui8pUDR; return true; } } while( --ui32Iterations > 0 ); @@ -239,7 +239,7 @@ bool USART0::receiveByte( unsigned char &chData, uint32_t ui32DelayMS ) } ////////////////////////////////////////////////////////////////////////// -unsigned char USART0::receiveByte() +uint8_t USART0::receiveByte() { while( !( *m_vui8pUCSRA & ( 1 << RXC_D ) ) ); @@ -247,259 +247,41 @@ unsigned char USART0::receiveByte() } ////////////////////////////////////////////////////////////////////////// -string USART0::receiveLine( string strLineTerminator /* = "\r\n" */, size_t sizeMaxSize /* = 1024 */ ) +bool USART0::receiveLine( char *szBuffer, size_t sizeLength, const char *szLineTerminator /* = "\r\n" */, size_t sizeMaxSize /* = 1024 */ ) { - string strReceived; - - while( strReceived.length() < sizeMaxSize ) + size_t sizeReceived = 0; + + while( sizeReceived < sizeMaxSize - 1 && sizeReceived < sizeLength - 1 ) { - strReceived += receiveByte(); + szBuffer[sizeReceived++] = receiveByte(); + szBuffer[sizeReceived] = '\0'; - size_t sizeLineTerminator = strReceived.rfind( strLineTerminator ); - - if( sizeLineTerminator != string::npos ) + if( strstr( szBuffer, szLineTerminator ) ) { - strReceived = strReceived.substr( 0, sizeLineTerminator ); - break; + return true; } } - - return strReceived; + + return false; } ////////////////////////////////////////////////////////////////////////// -void USART0::receive( char &chReceived ) -{ - chReceived = receiveByte(); -} - -////////////////////////////////////////////////////////////////////////// -void USART0::receive( unsigned char &uchReceived ) -{ - uchReceived = receiveByte(); -} - -////////////////////////////////////////////////////////////////////////// -void USART0::receive( short int &shiReceived ) -{ - long int liInput; - receive( liInput ); - - shiReceived = static_cast( liInput ); -} - -////////////////////////////////////////////////////////////////////////// -void USART0::receive( unsigned short int &ushiReceived ) -{ - unsigned long int uliInput; - receive( uliInput ); - - ushiReceived = static_cast( uliInput ); -} - -////////////////////////////////////////////////////////////////////////// -void USART0::receive( int &iReceived ) -{ - long int liInput; - receive( liInput ); - - iReceived = static_cast( liInput ); -} - -////////////////////////////////////////////////////////////////////////// -void USART0::receive( unsigned int &uiReceived ) -{ - unsigned long int uliInput; - receive( uliInput ); - - uiReceived = static_cast( uliInput ); -} - -////////////////////////////////////////////////////////////////////////// -void USART0::receive( long int &liReceived ) -{ - string strInput; - unsigned char uchRead; - - while( !isspace( uchRead = receiveByte() ) ) - { - strInput += uchRead; - } - - liReceived = strtol( strInput.c_str(), nullptr, 10 ); -} - -////////////////////////////////////////////////////////////////////////// -void USART0::receive( unsigned long int &uliReceived ) -{ - string strInput; - unsigned char uchRead; - - while( !isspace( uchRead = receiveByte() ) ) - { - strInput += uchRead; - } - - uliReceived = strtoul( strInput.c_str(), nullptr, 10 ); -} - -////////////////////////////////////////////////////////////////////////// -void USART0::receive( long long int &lliReceived ) -{ - long int liInput; - receive( liInput ); - - lliReceived = liInput; -} - -////////////////////////////////////////////////////////////////////////// -void USART0::receive( unsigned long long int &ulliReceived ) -{ - unsigned long int uliInput; - receive( uliInput ); - - ulliReceived = uliInput; -} - -////////////////////////////////////////////////////////////////////////// -void USART0::receive( float &fReceived ) -{ - double dInput; - receive( dInput ); - - fReceived = dInput; -} - -////////////////////////////////////////////////////////////////////////// -void USART0::receive( double &dReceived ) -{ - string strInput; - unsigned char uchRead; - - while( !isspace( uchRead = receiveByte() ) ) - { - strInput += uchRead; - } - - dReceived = strtod( strInput.c_str(), nullptr ); -} - -////////////////////////////////////////////////////////////////////////// -void USART0::receive( long double &ldReceived ) -{ - double dInput; - receive( dInput ); - - ldReceived = dInput; -} - -////////////////////////////////////////////////////////////////////////// -void USART0::transmitByte( unsigned char byteData ) +void USART0::transmitByte( uint8_t ui8Data ) { while( !( *m_vui8pUCSRA & ( 1 << UDRE_D ) ) ); - *m_vui8pUDR = byteData; + *m_vui8pUDR = ui8Data; } ////////////////////////////////////////////////////////////////////////// -void USART0::transmit( string strData ) +void USART0::transmitString( const char *szString ) { - for( size_t i = 0; i < strData.length(); ++i ) + while( *szString != '\0' ) { - transmitByte( strData[i] ); + transmitByte( *szString++ ); } } -////////////////////////////////////////////////////////////////////////// -void USART0::transmit( char chData ) -{ - transmitByte( chData ); -} - -////////////////////////////////////////////////////////////////////////// -void USART0::transmit( unsigned char uchData ) -{ - sprintf( m_szConvertBuffer, "%hhu", uchData ); - transmit( m_szConvertBuffer ); -} - -////////////////////////////////////////////////////////////////////////// -void USART0::transmit( short int shiData ) -{ - sprintf( m_szConvertBuffer, "%hd", shiData ); - transmit( m_szConvertBuffer ); -} - -////////////////////////////////////////////////////////////////////////// -void USART0::transmit( unsigned short int ushiData ) -{ - sprintf( m_szConvertBuffer, "%hu", ushiData ); - transmit( m_szConvertBuffer ); -} - -////////////////////////////////////////////////////////////////////////// -void USART0::transmit( int iData ) -{ - sprintf( m_szConvertBuffer, "%d", iData ); - transmit( m_szConvertBuffer ); -} - -////////////////////////////////////////////////////////////////////////// -void USART0::transmit( unsigned int uiData ) -{ - sprintf( m_szConvertBuffer, "%u", uiData ); - transmit( m_szConvertBuffer ); -} - -////////////////////////////////////////////////////////////////////////// -void USART0::transmit( long int liData ) -{ - sprintf( m_szConvertBuffer, "%ld", liData ); - transmit( m_szConvertBuffer ); -} - -////////////////////////////////////////////////////////////////////////// -void USART0::transmit( unsigned long int uliData ) -{ - sprintf( m_szConvertBuffer, "%lu", uliData ); - transmit( m_szConvertBuffer ); -} - -////////////////////////////////////////////////////////////////////////// -void USART0::transmit( long long int lliData ) -{ - sprintf( m_szConvertBuffer, "%lld", lliData ); - transmit( m_szConvertBuffer ); -} - -////////////////////////////////////////////////////////////////////////// -void USART0::transmit( unsigned long long int ulliData ) -{ - sprintf( m_szConvertBuffer, "%llu", ulliData ); - transmit( m_szConvertBuffer ); -} - -////////////////////////////////////////////////////////////////////////// -void USART0::transmit( float fData ) -{ - sprintf( m_szConvertBuffer, "%f", static_cast( fData ) ); - transmit( m_szConvertBuffer ); -} - -////////////////////////////////////////////////////////////////////////// -void USART0::transmit( double dData ) -{ - sprintf( m_szConvertBuffer, "%f", dData ); - transmit( m_szConvertBuffer ); -} - -////////////////////////////////////////////////////////////////////////// -void USART0::transmit( long double ldData ) -{ - sprintf( m_szConvertBuffer, "%f", static_cast( ldData ) ); - transmit( m_szConvertBuffer ); -} - #ifdef SECOND_USART ////////////////////////////////////////////////////////////////////////// diff --git a/usart/usart.cppproj b/usart/usart.cppproj index 5fddd74..13483d3 100644 --- a/usart/usart.cppproj +++ b/usart/usart.cppproj @@ -195,35 +195,15 @@ compile - - compile - - - compile - compile - - compile - - - compile - compile compile - - compile - - - - - - \ No newline at end of file diff --git a/usart/usart.h b/usart/usart.h index f98a001..4ac3d57 100644 --- a/usart/usart.h +++ b/usart/usart.h @@ -1,20 +1,16 @@ /* * Copyright (c) by BlackMark 2015-2016 * Date 20/05/2016 -* Version 2.4 +* Version 2.5 */ #ifndef USART_H #define USART_H #include -#include -#include #include -#include +#include #include "clock.h" -#include "string/string.h" -#include "vector/vector.h" #if defined (__AVR_ATmega168A__) || defined (__AVR_ATmega328P__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega1284P__) #define USART_SPI @@ -91,7 +87,6 @@ protected: volatile uint8_t *m_vui8pUDR; private: - char m_szConvertBuffer[64]; uint8_t readUCSRC(); void setUCSRC( uint8_t ui8UCSRC ); @@ -109,56 +104,12 @@ public: void init( uint32_t ui32BaudRate = 9600, uint8_t ui8DataBits = 8, Parity enmParity = Parity::DISABLED, StopBit enmStopBits = StopBit::ONE, Mode enmMode = Mode::ASYNCHRONOUS ); - bool receiveByte( unsigned char &chData, uint32_t ui32DelayMS ); - unsigned char receiveByte(); - string receiveLine( string strLineTerminator = "\r\n", size_t sizeMaxSize = 1024 ); + bool receiveByte( uint8_t &ui8Data, uint32_t ui32DelayMS ); + uint8_t receiveByte(); + bool receiveLine( char *szBuffer, size_t sizeLength, const char *szLineTerminator = "\r\n", size_t sizeMaxSize = 512 ); - void receive( char &chReceived ); - void receive( unsigned char &uchReceived ); - void receive( short int &shiReceived ); - void receive( unsigned short int &ushiReceived ); - void receive( int &iReceived ); - void receive( unsigned int &uiReceived ); - void receive( long int &liReceived ); - void receive( unsigned long int &uliReceived ); - void receive( long long int &lliReceived ); - void receive( unsigned long long int &ulliReceived ); - void receive( float &fReceived ); - void receive( double &dReceived ); - void receive( long double &ldReceived ); - - void transmitByte( unsigned char byteData ); - void transmit( string strData ); - - void transmit( char chData ); - void transmit( unsigned char uchData ); - void transmit( short int shiData ); - void transmit( unsigned short int ushiData ); - void transmit( int iData ); - void transmit( unsigned int uiData ); - void transmit( long int liData ); - void transmit( unsigned long int uliData ); - void transmit( long long int lliData ); - void transmit( unsigned long long int ulliData ); - void transmit( float fData ); - void transmit( double dData ); - void transmit( long double ldData ); - - ////////////////////////////////////////////////////////////////////////// - template - USART0& operator<<( const T &RHS ) - { - transmit( RHS ); - return *this; - } - - ////////////////////////////////////////////////////////////////////////// - template - USART0& operator>>( T &RHS ) - { - receive( RHS ); - return *this; - } + void transmitByte( uint8_t ui8Data ); + void transmitString( const char *szString ); }; #ifdef SECOND_USART diff --git a/usart/vector b/usart/vector deleted file mode 160000 index 42aed5c..0000000 --- a/usart/vector +++ /dev/null @@ -1 +0,0 @@ -Subproject commit 42aed5c824ed24b019ddbe0142454ae5a9375470