Added C++ stream syntax for writing strings

This commit is contained in:
BlackMark 2016-05-21 20:39:37 +02:00
parent 8b081b8c9f
commit 8ccddc483e
2 changed files with 47 additions and 41 deletions

View File

@ -1,7 +1,7 @@
/* /*
* Copyright (c) by BlackMark 2015-2016 * Copyright (c) by BlackMark 2015-2016
* Date 05/01/2016 * Date 21/05/2016
* Version 1.4 * Version 1.5
*/ */
#include <stdio.h> #include <stdio.h>
@ -18,89 +18,89 @@ int main()
array<char, 64> arrBuffer; array<char, 64> arrBuffer;
cUSART.transmitString( "\r\nSizes: \r\n" ); cUSART << "\r\nSizes: \r\n";
sprintf( arrBuffer.data(), "%d\r\n", sizeof( char ) ); sprintf( arrBuffer.data(), "%d\r\n", sizeof( char ) );
cUSART.transmitString( "sizeof( char ) = " ); cUSART << "sizeof( char ) = ";
cUSART.transmitString( arrBuffer.data() ); cUSART << arrBuffer.data();
sprintf( arrBuffer.data(), "%d\r\n", sizeof( unsigned char ) ); sprintf( arrBuffer.data(), "%d\r\n", sizeof( unsigned char ) );
cUSART.transmitString( "sizeof( unsigned char ) = " ); cUSART << "sizeof( unsigned char ) = ";
cUSART.transmitString( arrBuffer.data() ); cUSART << arrBuffer.data();
sprintf( arrBuffer.data(), "%d\r\n", sizeof( short int ) ); sprintf( arrBuffer.data(), "%d\r\n", sizeof( short int ) );
cUSART.transmitString( "sizeof( short int ) = " ); cUSART << "sizeof( short int ) = ";
cUSART.transmitString( arrBuffer.data() ); cUSART << arrBuffer.data();
sprintf( arrBuffer.data(), "%d\r\n", sizeof( unsigned short int ) ); sprintf( arrBuffer.data(), "%d\r\n", sizeof( unsigned short int ) );
cUSART.transmitString( "sizeof( unsigned short int ) = " ); cUSART << "sizeof( unsigned short int ) = ";
cUSART.transmitString( arrBuffer.data() ); cUSART << arrBuffer.data();
sprintf( arrBuffer.data(), "%d\r\n", sizeof( int ) ); sprintf( arrBuffer.data(), "%d\r\n", sizeof( int ) );
cUSART.transmitString( "sizeof( int ) = " ); cUSART << "sizeof( int ) = ";
cUSART.transmitString( arrBuffer.data() ); cUSART << arrBuffer.data();
sprintf( arrBuffer.data(), "%d\r\n", sizeof( unsigned int ) ); sprintf( arrBuffer.data(), "%d\r\n", sizeof( unsigned int ) );
cUSART.transmitString( "sizeof( unsigned int ) = " ); cUSART << "sizeof( unsigned int ) = ";
cUSART.transmitString( arrBuffer.data() ); cUSART << arrBuffer.data();
sprintf( arrBuffer.data(), "%d\r\n", sizeof( long int ) ); sprintf( arrBuffer.data(), "%d\r\n", sizeof( long int ) );
cUSART.transmitString( "sizeof( long int ) = " ); cUSART << "sizeof( long int ) = ";
cUSART.transmitString( arrBuffer.data() ); cUSART << arrBuffer.data();
sprintf( arrBuffer.data(), "%d\r\n", sizeof( unsigned long int ) ); sprintf( arrBuffer.data(), "%d\r\n", sizeof( unsigned long int ) );
cUSART.transmitString( "sizeof( unsigned long int ) = " ); cUSART << "sizeof( unsigned long int ) = ";
cUSART.transmitString( arrBuffer.data() ); cUSART << arrBuffer.data();
sprintf( arrBuffer.data(), "%d\r\n", sizeof( long long int ) ); sprintf( arrBuffer.data(), "%d\r\n", sizeof( long long int ) );
cUSART.transmitString( "sizeof( long long int ) = " ); cUSART << "sizeof( long long int ) = ";
cUSART.transmitString( arrBuffer.data() ); cUSART << arrBuffer.data();
sprintf( arrBuffer.data(), "%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 << "sizeof( unsigned long long int ) = ";
cUSART.transmitString( arrBuffer.data() ); cUSART << arrBuffer.data();
sprintf( arrBuffer.data(), "%d\r\n", sizeof( float ) ); sprintf( arrBuffer.data(), "%d\r\n", sizeof( float ) );
cUSART.transmitString( "sizeof( float ) = " ); cUSART << "sizeof( float ) = ";
cUSART.transmitString( arrBuffer.data() ); cUSART << arrBuffer.data();
sprintf( arrBuffer.data(), "%d\r\n", sizeof( double ) ); sprintf( arrBuffer.data(), "%d\r\n", sizeof( double ) );
cUSART.transmitString( "sizeof( double ) = " ); cUSART << "sizeof( double ) = ";
cUSART.transmitString( arrBuffer.data() ); cUSART << arrBuffer.data();
sprintf( arrBuffer.data(), "%d\r\n", sizeof( long double ) ); sprintf( arrBuffer.data(), "%d\r\n", sizeof( long double ) );
cUSART.transmitString( "sizeof( long double ) = " ); cUSART << "sizeof( long double ) = ";
cUSART.transmitString( arrBuffer.data() ); cUSART << arrBuffer.data();
sprintf( arrBuffer.data(), "%d\r\n\r\n", sizeof( void* ) ); sprintf( arrBuffer.data(), "%d\r\n\r\n", sizeof( void* ) );
cUSART.transmitString( "sizeof( void* ) = " ); cUSART << "sizeof( void* ) = ";
cUSART.transmitString( arrBuffer.data() ); cUSART << arrBuffer.data();
while( true ) while( true )
{ {
sprintf( arrBuffer.data(), "%lu", ui32Counter++ ); sprintf( arrBuffer.data(), "%lu", ui32Counter++ );
cUSART.transmitString( "This has been running for \"" ); cUSART << "This has been running for \"";
cUSART.transmitString( arrBuffer.data() ); cUSART << arrBuffer.data();
cUSART.transmitString( "\" seconds!\r\n" ); cUSART << "\" seconds!\r\n";
if( !cUSART.receiveLine( arrBuffer.data(), arrBuffer.size(), "\r" ) ) if( !cUSART.receiveLine( arrBuffer.data(), arrBuffer.size(), "\r" ) )
{ {
cUSART.transmitString( "Receive error: " ); cUSART << "Receive error: ";
} }
else else
{ {
cUSART.transmitString( "Echo: " ); cUSART << "Echo: ";
} }
cUSART.transmitString( arrBuffer.data() ); cUSART << arrBuffer.data();
cUSART.transmitString( "\r\n" ); cUSART << "\r\n";
uint8_t ui8Byte; uint8_t ui8Byte;
if( cUSART.receiveByte( ui8Byte, 1000 ) ) if( cUSART.receiveByte( ui8Byte, 1000 ) )
{ {
cUSART.transmitByte( ui8Byte ); cUSART.transmitByte( ui8Byte );
cUSART.transmitString( "\r\n" ); cUSART << "\r\n";
} }
} }

View File

@ -1,7 +1,7 @@
/* /*
* Copyright (c) by BlackMark 2015-2016 * Copyright (c) by BlackMark 2015-2016
* Date 20/05/2016 * Date 21/05/2016
* Version 2.6 * Version 2.7
*/ */
#ifndef USART_H #ifndef USART_H
@ -144,6 +144,12 @@ public:
void receiveInterruptHandler(); void receiveInterruptHandler();
void transmitInterruptHandler(); void transmitInterruptHandler();
inline USART0& operator<<( const char *szString )
{
transmitString( szString );
return *this;
}
}; };
#ifdef SECOND_USART #ifdef SECOND_USART