Removed fancy formatting and generic receiving and transmitting to reduce complexity and size

This commit is contained in:
BlackMark 2016-05-20 17:02:23 +02:00
parent 199f92dcc5
commit 97f0b0608b
8 changed files with 100 additions and 358 deletions

9
.gitmodules vendored
View File

@ -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

@ -1 +0,0 @@
Subproject commit 72c6a6cc88d6cafecf372fe968d3befae037ad9e

View File

@ -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 );
}

@ -1 +0,0 @@
Subproject commit 23189911c6929685e962be9866ff6127938da6a8

View File

@ -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<short int>( liInput );
}
//////////////////////////////////////////////////////////////////////////
void USART0::receive( unsigned short int &ushiReceived )
{
unsigned long int uliInput;
receive( uliInput );
ushiReceived = static_cast<unsigned short int>( uliInput );
}
//////////////////////////////////////////////////////////////////////////
void USART0::receive( int &iReceived )
{
long int liInput;
receive( liInput );
iReceived = static_cast<int>( liInput );
}
//////////////////////////////////////////////////////////////////////////
void USART0::receive( unsigned int &uiReceived )
{
unsigned long int uliInput;
receive( uliInput );
uiReceived = static_cast<unsigned int>( 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<double>( 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<double>( ldData ) );
transmit( m_szConvertBuffer );
}
#ifdef SECOND_USART
//////////////////////////////////////////////////////////////////////////

View File

@ -195,35 +195,15 @@
<Compile Include="clock.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="cppalloc\cppalloc.cpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="cppalloc\cppalloc.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="main.cpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="string\string.cpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="string\string.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="usart.cpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="usart.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="vector\vector.h">
<SubType>compile</SubType>
</Compile>
</ItemGroup>
<ItemGroup>
<Folder Include="cppalloc" />
<Folder Include="vector" />
<Folder Include="string" />
</ItemGroup>
<Import Project="$(AVRSTUDIO_EXE_PATH)\\Vs\\Compiler.targets" />
</Project>

View File

@ -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 <avr/io.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <ctype.h>
#include <string.h>
#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<typename T>
USART0& operator<<( const T &RHS )
{
transmit( RHS );
return *this;
}
//////////////////////////////////////////////////////////////////////////
template<typename T>
USART0& operator>>( T &RHS )
{
receive( RHS );
return *this;
}
void transmitByte( uint8_t ui8Data );
void transmitString( const char *szString );
};
#ifdef SECOND_USART

@ -1 +0,0 @@
Subproject commit 42aed5c824ed24b019ddbe0142454ae5a9375470