Initial commit

This commit is contained in:
BlackMark 2016-05-19 21:43:10 +02:00
commit 6ac7d71530
13 changed files with 1745 additions and 0 deletions

9
.gitattributes vendored Normal file
View File

@ -0,0 +1,9 @@
*.h eol=lf
*.hpp eol=lf
*.c eol=lf
*.cpp eol=lf
.git* eol=lf
*.vcxproj* eol=crlf
*.cppproj eol=crlf
*.sln eol=crlf
*.atsln eol=crlf

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
.vs
Release
Debug
*.componentinfo.xml
avrdude.bat

22
USART.atsln Normal file
View File

@ -0,0 +1,22 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Atmel Studio Solution File, Format Version 11.00
VisualStudioVersion = 14.0.23107.0
MinimumVisualStudioVersion = 10.0.40219.1
Project("{E66E83B9-2572-4076-B26E-6BE79FF3018A}") = "USART", "USART\USART.cppproj", "{DCE6C7E3-EE26-4D79-826B-08594B9AD897}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|AVR = Debug|AVR
Release|AVR = Release|AVR
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Debug|AVR.ActiveCfg = Debug|AVR
{DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Debug|AVR.Build.0 = Debug|AVR
{DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Release|AVR.ActiveCfg = Release|AVR
{DCE6C7E3-EE26-4D79-826B-08594B9AD897}.Release|AVR.Build.0 = Release|AVR
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal

14
USART/Clock.h Normal file
View File

@ -0,0 +1,14 @@
/*
* Copyright (c) by BlackMark 2015
* Date 24/11/2015
* Version 1.1
*/
#ifndef CLOCK_H
#define CLOCK_H
#define F_CPU 16000000
#include <util/delay.h>
#endif

586
USART/USART.cpp Normal file
View File

@ -0,0 +1,586 @@
#include "USART.h"
//////////////////////////////////////////////////////////////////////////
uint8_t USART0::readUCSRC()
{
uint8_t ui8UCSRC;
#ifdef USART_SHAREDIO
ui8UCSRC = UBRRH;
ui8UCSRC = UCSRC;
#else
ui8UCSRC = *m_vui8pUCSRC;
#endif
return ui8UCSRC;
}
//////////////////////////////////////////////////////////////////////////
void USART0::setUCSRC( uint8_t ui8UCSRC )
{
#ifdef USART_SHAREDIO
*m_vui8pUCSRC = ( 1 << URSEL ) | ui8UCSRC;
#else
*m_vui8pUCSRC = ui8UCSRC;
#endif
}
//////////////////////////////////////////////////////////////////////////
void USART0::setRXState( bool bEnable )
{
if( bEnable )
{
*m_vui8pUCSRB |= ( 1 << m_ui8RXEN );
}
else
{
*m_vui8pUCSRB &= ~( 1 << m_ui8RXEN );
}
}
//////////////////////////////////////////////////////////////////////////
void USART0::setTXState( bool bEnable )
{
if( bEnable )
{
*m_vui8pUCSRB |= ( 1 << m_ui8TXEN );
}
else
{
*m_vui8pUCSRB &= ~( 1 << m_ui8TXEN );
}
}
//////////////////////////////////////////////////////////////////////////
void USART0::setBaudRate( uint32_t ui32BaudRate )
{
uint16_t ui16UBRR = ( ( F_CPU / ( 16 * ui32BaudRate ) ) - 1 );
*m_vui8pUBRRH = static_cast<uint8_t>( ui16UBRR >> 8 );
*m_vui8pUBRRL = static_cast<uint8_t>( ui16UBRR );
}
//////////////////////////////////////////////////////////////////////////
void USART0::setDataBits( uint8_t ui8DataBits )
{
uint8_t ui8UCSRC = readUCSRC();
if( ui8DataBits < 5 )
{
ui8DataBits = 5;
}
else if( ui8DataBits > 9 )
{
ui8DataBits = 9;
}
if( ui8DataBits <= 8 )
{
bool bZeroBit = ( ui8DataBits - 5 ) & 1;
bool bOneBit = ( ( ui8DataBits - 5 ) >> 1 ) & 1;
if( bZeroBit )
{
ui8UCSRC |= ( 1 << m_ui8UCSZ0 );
}
else
{
ui8UCSRC &= ~( 1 << m_ui8UCSZ0 );
}
if( bOneBit )
{
ui8UCSRC |= ( 1 << m_ui8UCSZ1 );
}
else
{
ui8UCSRC &= ~( 1 << m_ui8UCSZ1 );
}
*m_vui8pUCSRB &= ~( 1 << m_ui8UCSZ2 );
}
else
{
ui8UCSRC |= ( 1 << m_ui8UCSZ1 ) | ( 1 << m_ui8UCSZ0 );
*m_vui8pUCSRB |= ( 1 << m_ui8UCSZ2 );
}
setUCSRC( ui8UCSRC );
}
//////////////////////////////////////////////////////////////////////////
void USART0::setParity( Parity enmParity )
{
uint8_t ui8UCSRC = readUCSRC();
if( enmParity == Parity::DISABLED )
{
ui8UCSRC &= ~( ( 1 << m_ui8UPM1 ) | ( 1 << m_ui8UPM0 ) );
}
else if( enmParity == Parity::ODD )
{
ui8UCSRC |= ( ( 1 << m_ui8UPM1 ) | ( 1 << m_ui8UPM0 ) );
}
else if( enmParity == Parity::EVEN )
{
ui8UCSRC &= ~( ( 1 << m_ui8UPM0 ) );
ui8UCSRC |= ( ( 1 << m_ui8UPM1 ) );
}
setUCSRC( ui8UCSRC );
}
//////////////////////////////////////////////////////////////////////////
void USART0::setStopBits( StopBit enmStopBits )
{
uint8_t ui8UCSRC = readUCSRC();
if( enmStopBits == StopBit::ONE )
{
ui8UCSRC &= ~( 1 << m_ui8USBS );
}
else if( enmStopBits == StopBit::TWO )
{
ui8UCSRC |= ( 1 << m_ui8USBS );
}
setUCSRC( ui8UCSRC );
}
//////////////////////////////////////////////////////////////////////////
void USART0::setMode( Mode enmMode )
{
uint8_t ui8UCSRC = readUCSRC();
#ifdef USART_SPI
if( enmMode == Mode::ASYNCHRONOUS )
{
ui8UCSRC &= ~( ( 1 << m_ui8UMSEL1 ) | ( 1 << m_ui8UMSEL0 ) );
}
else if( enmMode == Mode::SYNCHRONOUS )
{
ui8UCSRC &= ~( 1 << m_ui8UMSEL1 );
ui8UCSRC |= ( 1 << m_ui8UMSEL0 );
}
else if( enmMode == Mode::MASTERSPI )
{
ui8UCSRC |= ( ( 1 << m_ui8UMSEL1 ) | ( 1 << m_ui8UMSEL0 ) );
}
#else
if( enmMode == Mode::ASYNCHRONOUS )
{
ui8UCSRC &= ~( 1 << m_ui8UMSEL );
}
else if( enmMode == Mode::SYNCHRONOUS )
{
ui8UCSRC |= ( 1 << m_ui8UMSEL );
}
#endif
setUCSRC( ui8UCSRC );
}
//////////////////////////////////////////////////////////////////////////
USART0::USART0() : USART0( 9600, 8, Parity::DISABLED, StopBit::ONE, Mode::ASYNCHRONOUS )
{}
//////////////////////////////////////////////////////////////////////////
USART0::USART0( uint32_t ui32BaudRate, uint8_t ui8DataBits, Parity enmParity, StopBit enmStopBits, Mode enmMode /* = Mode::ASYNCHRONOUS */ )
{
#ifdef USART_SHAREDIO
m_vui8pUCSRA = &UCSRA;
m_vui8pUCSRB = &UCSRB;
m_vui8pUCSRC = &UCSRC;
m_vui8pUBRRH = &UBRRH;
m_vui8pUBRRL = &UBRRL;
m_vui8pUDR = &UDR;
m_ui8RXEN = RXEN;
m_ui8TXEN = TXEN;
m_ui8UCSZ0 = UCSZ0;
m_ui8UCSZ1 = UCSZ1;
m_ui8UCSZ2 = UCSZ2;
m_ui8UPM0 = UPM0;
m_ui8UPM1 = UPM1;
m_ui8USBS = USBS;
m_ui8RXC = RXC;
m_ui8UDRE = UDRE;
#endif
#ifndef USART_SHAREDIO
m_vui8pUCSRA = &UCSR0A;
m_vui8pUCSRB = &UCSR0B;
m_vui8pUCSRC = &UCSR0C;
m_vui8pUBRRH = &UBRR0H;
m_vui8pUBRRL = &UBRR0L;
m_vui8pUDR = &UDR0;
m_ui8RXEN = RXEN0;
m_ui8TXEN = TXEN0;
m_ui8UCSZ0 = UCSZ00;
m_ui8UCSZ1 = UCSZ01;
m_ui8UCSZ2 = UCSZ02;
m_ui8UPM0 = UPM00;
m_ui8UPM1 = UPM01;
m_ui8USBS = USBS0;
m_ui8RXC = RXC0;
m_ui8UDRE = UDRE0;
#endif
#ifdef USART_SPI
m_ui8UMSEL0 = UMSEL00;
m_ui8UMSEL1 = UMSEL01;
#else
m_ui8UMSEL = UMSEL;
#endif
init( ui32BaudRate, ui8DataBits, enmParity, enmStopBits, enmMode );
}
//////////////////////////////////////////////////////////////////////////
USART0::~USART0()
{}
//////////////////////////////////////////////////////////////////////////
void USART0::init( uint32_t ui32BaudRate, uint8_t ui8DataBits, Parity enmParity, StopBit enmStopBits, Mode enmMode /* = Mode::ASYNCHRONOUS */ )
{
setBaudRate( ui32BaudRate );
setDataBits( ui8DataBits );
setParity( enmParity );
setStopBits( enmStopBits );
setMode( enmMode );
setRXState( true );
setTXState( true );
}
//////////////////////////////////////////////////////////////////////////
bool USART0::receiveByte( unsigned char &chData, uint32_t ui32DelayMS )
{
const uint8_t ui8ClockCyclesPerIteration = 6;
double dDelayS = ui32DelayMS / 1000.0;
uint32_t ui32Iterations = static_cast<uint32_t>( ( dDelayS * F_CPU ) / ui8ClockCyclesPerIteration );
do
{
if( ( *m_vui8pUCSRA & ( 1 << m_ui8RXC ) ) )
{
chData = *m_vui8pUDR;
return true;
}
} while( --ui32Iterations > 0 );
return false;
}
//////////////////////////////////////////////////////////////////////////
unsigned char USART0::receiveByte()
{
while( !( *m_vui8pUCSRA & ( 1 << m_ui8RXC ) ) );
return *m_vui8pUDR;
}
//////////////////////////////////////////////////////////////////////////
string USART0::receiveLine( string strLineTerminator /* = "\r\n" */, size_t sizeMaxSize /* = 1024 */ )
{
string strReceived;
while( strReceived.length() < sizeMaxSize )
{
strReceived += receiveByte();
size_t sizeLineTerminator = strReceived.rfind( strLineTerminator );
if( sizeLineTerminator != string::npos )
{
strReceived = strReceived.substr( 0, sizeLineTerminator );
break;
}
}
return strReceived;
}
//////////////////////////////////////////////////////////////////////////
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 )
{
while( !( *m_vui8pUCSRA & ( 1 << m_ui8UDRE ) ) );
*m_vui8pUDR = byteData;
}
//////////////////////////////////////////////////////////////////////////
void USART0::transmit( string strData )
{
for( size_t i = 0; i < strData.length(); ++i )
{
transmitByte( strData[i] );
}
}
//////////////////////////////////////////////////////////////////////////
void USART0::transmit( char chData )
{
transmitByte( chData );
}
//////////////////////////////////////////////////////////////////////////
void USART0::transmit( unsigned char uchData )
{
vector<char> vecBuffer( 4 );
sprintf( vecBuffer.data(), "%hhu", uchData );
transmit( vecBuffer.data() );
}
//////////////////////////////////////////////////////////////////////////
void USART0::transmit( short int shiData )
{
vector<char> vecBuffer( 6 );
sprintf( vecBuffer.data(), "%hd", shiData );
transmit( vecBuffer.data() );
}
//////////////////////////////////////////////////////////////////////////
void USART0::transmit( unsigned short int ushiData )
{
vector<char> vecBuffer( 6 );
sprintf( vecBuffer.data(), "%hu", ushiData );
transmit( vecBuffer.data() );
}
//////////////////////////////////////////////////////////////////////////
void USART0::transmit( int iData )
{
vector<char> vecBuffer( 6 );
sprintf( vecBuffer.data(), "%d", iData );
transmit( vecBuffer.data() );
}
//////////////////////////////////////////////////////////////////////////
void USART0::transmit( unsigned int uiData )
{
vector<char> vecBuffer( 6 );
sprintf( vecBuffer.data(), "%u", uiData );
transmit( vecBuffer.data() );
}
//////////////////////////////////////////////////////////////////////////
void USART0::transmit( long int liData )
{
vector<char> vecBuffer( 11 );
sprintf( vecBuffer.data(), "%ld", liData );
transmit( vecBuffer.data() );
}
//////////////////////////////////////////////////////////////////////////
void USART0::transmit( unsigned long int uliData )
{
vector<char> vecBuffer( 11 );
sprintf( vecBuffer.data(), "%lu", uliData );
transmit( vecBuffer.data() );
}
//////////////////////////////////////////////////////////////////////////
void USART0::transmit( long long int lliData )
{
vector<char> vecBuffer( 21 );
sprintf( vecBuffer.data(), "%lld", lliData );
transmit( vecBuffer.data() );
}
//////////////////////////////////////////////////////////////////////////
void USART0::transmit( unsigned long long int ulliData )
{
vector<char> vecBuffer( 21 );
sprintf( vecBuffer.data(), "%llu", ulliData );
transmit( vecBuffer.data() );
}
//////////////////////////////////////////////////////////////////////////
void USART0::transmit( float fData )
{
vector<char> vecBuffer( 64 );
sprintf( vecBuffer.data(), "%f", static_cast<double>( fData ) );
transmit( vecBuffer.data() );
}
//////////////////////////////////////////////////////////////////////////
void USART0::transmit( double dData )
{
vector<char> vecBuffer( 64 );
sprintf( vecBuffer.data(), "%f", dData );
transmit( vecBuffer.data() );
}
//////////////////////////////////////////////////////////////////////////
void USART0::transmit( long double ldData )
{
vector<char> vecBuffer( 64 );
sprintf( vecBuffer.data(), "%f", static_cast<double>( ldData ) );
transmit( vecBuffer.data() );
}
#ifdef SECOND_USART
//////////////////////////////////////////////////////////////////////////
USART1::USART1() : USART1( 9600, 8, Parity::DISABLED, StopBit::ONE, Mode::ASYNCHRONOUS )
{}
//////////////////////////////////////////////////////////////////////////
USART1::USART1( uint32_t ui32BaudRate, uint8_t ui8DataBits, Parity enmParity, StopBit enmStopBits, Mode enmMode /* = Mode::ASYNCHRONOUS */ )
{
m_vui8pUCSRA = &UCSR1A;
m_vui8pUCSRB = &UCSR1B;
m_vui8pUCSRC = &UCSR1C;
m_vui8pUBRRH = &UBRR1H;
m_vui8pUBRRL = &UBRR1L;
m_vui8pUDR = &UDR1;
m_ui8RXEN = RXEN1;
m_ui8TXEN = TXEN1;
m_ui8UCSZ0 = UCSZ10;
m_ui8UCSZ1 = UCSZ11;
m_ui8UCSZ2 = UCSZ12;
m_ui8UPM0 = UPM10;
m_ui8UPM1 = UPM11;
m_ui8USBS = USBS1;
m_ui8RXC = RXC1;
m_ui8UDRE = UDRE1;
m_ui8UMSEL0 = UMSEL10;
m_ui8UMSEL1 = UMSEL11;
init( ui32BaudRate, ui8DataBits, enmParity, enmStopBits, enmMode );
}
//////////////////////////////////////////////////////////////////////////
USART1::~USART1()
{}
#endif

158
USART/USART.cppproj Normal file
View File

@ -0,0 +1,158 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="14.0">
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
<ProjectVersion>7.0</ProjectVersion>
<ToolchainName>com.Atmel.AVRGCC8.CPP</ToolchainName>
<ProjectGuid>dce6c7e3-ee26-4d79-826b-08594b9ad897</ProjectGuid>
<avrdevice>ATmega328P</avrdevice>
<avrdeviceseries>none</avrdeviceseries>
<OutputType>Executable</OutputType>
<Language>CPP</Language>
<OutputFileName>$(MSBuildProjectName)</OutputFileName>
<OutputFileExtension>.elf</OutputFileExtension>
<OutputDirectory>$(MSBuildProjectDirectory)\$(Configuration)</OutputDirectory>
<AssemblyName>USART</AssemblyName>
<Name>USART</Name>
<RootNamespace>USART</RootNamespace>
<ToolchainFlavour>Native</ToolchainFlavour>
<KeepTimersRunning>true</KeepTimersRunning>
<OverrideVtor>false</OverrideVtor>
<CacheFlash>true</CacheFlash>
<ProgFlashFromRam>true</ProgFlashFromRam>
<RamSnippetAddress>0x20000000</RamSnippetAddress>
<UncachedRange />
<preserveEEPROM>true</preserveEEPROM>
<OverrideVtorValue>exception_table</OverrideVtorValue>
<BootSegment>2</BootSegment>
<eraseonlaunchrule>0</eraseonlaunchrule>
<AsfFrameworkConfig>
<framework-data xmlns="">
<options />
<configurations />
<files />
<documentation help="" />
<offline-documentation help="" />
<dependencies>
<content-extension eid="atmel.asf" uuidref="Atmel.ASF" version="3.31.0" />
</dependencies>
</framework-data>
</AsfFrameworkConfig>
<avrtool>com.atmel.avrdbg.tool.stk500</avrtool>
<avrtoolserialnumber />
<avrdeviceexpectedsignature>0x1E950F</avrdeviceexpectedsignature>
<com_atmel_avrdbg_tool_stk500>
<ToolOptions>
<InterfaceProperties>
<IspClock>1843200</IspClock>
</InterfaceProperties>
<InterfaceName>ISP</InterfaceName>
</ToolOptions>
<ToolType>com.atmel.avrdbg.tool.stk500</ToolType>
<ToolNumber>
</ToolNumber>
<ToolName>STK500</ToolName>
</com_atmel_avrdbg_tool_stk500>
<avrtoolinterface>ISP</avrtoolinterface>
<avrtoolinterfaceclock>1843200</avrtoolinterfaceclock>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<ToolchainSettings>
<AvrGccCpp>
<avrgcc.common.Device>-mmcu=atmega328p -B "%24(PackRepoDir)\atmel\ATmega_DFP\1.0.106\gcc\dev\atmega328p"</avrgcc.common.Device>
<avrgcc.common.outputfiles.hex>True</avrgcc.common.outputfiles.hex>
<avrgcc.common.outputfiles.lss>True</avrgcc.common.outputfiles.lss>
<avrgcc.common.outputfiles.eep>True</avrgcc.common.outputfiles.eep>
<avrgcc.common.outputfiles.srec>True</avrgcc.common.outputfiles.srec>
<avrgcc.common.outputfiles.usersignatures>False</avrgcc.common.outputfiles.usersignatures>
<avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>
<avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>
<avrgcc.compiler.directories.IncludePaths><ListValues><Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.0.106\include</Value></ListValues></avrgcc.compiler.directories.IncludePaths>
<avrgcc.compiler.optimization.PackStructureMembers>True</avrgcc.compiler.optimization.PackStructureMembers>
<avrgcc.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcc.compiler.optimization.AllocateBytesNeededForEnum>
<avrgcc.compiler.warnings.AllWarnings>True</avrgcc.compiler.warnings.AllWarnings>
<avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>
<avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>
<avrgcccpp.compiler.directories.IncludePaths><ListValues><Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.0.106\include</Value></ListValues></avrgcccpp.compiler.directories.IncludePaths>
<avrgcccpp.compiler.optimization.PackStructureMembers>True</avrgcccpp.compiler.optimization.PackStructureMembers>
<avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>
<avrgcccpp.compiler.warnings.AllWarnings>True</avrgcccpp.compiler.warnings.AllWarnings>
<avrgcccpp.compiler.warnings.Pedantic>True</avrgcccpp.compiler.warnings.Pedantic>
<avrgcccpp.compiler.miscellaneous.OtherFlags>-Wextra -std=c++11</avrgcccpp.compiler.miscellaneous.OtherFlags>
<avrgcccpp.linker.libraries.Libraries><ListValues><Value>libm</Value></ListValues></avrgcccpp.linker.libraries.Libraries>
<avrgcccpp.assembler.general.IncludePaths><ListValues><Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.0.106\include</Value></ListValues></avrgcccpp.assembler.general.IncludePaths>
<avrgcc.compiler.symbols.DefSymbols><ListValues><Value>NDEBUG</Value></ListValues></avrgcc.compiler.symbols.DefSymbols>
<avrgcc.compiler.optimization.level>Optimize for size (-Os)</avrgcc.compiler.optimization.level>
<avrgcccpp.compiler.symbols.DefSymbols><ListValues><Value>NDEBUG</Value></ListValues></avrgcccpp.compiler.symbols.DefSymbols>
<avrgcccpp.compiler.optimization.level>Optimize for size (-Os)</avrgcccpp.compiler.optimization.level>
</AvrGccCpp>
</ToolchainSettings>
<PreBuildEvent>echo "C:\avrdude-6.2\avrdude.exe" -v -p$(avrdevice) %%* -Uflash:w:"$(OutputDirectory)\$(Name).hex":i &gt; "$(MSBuildProjectDirectory)\avrdude.bat"</PreBuildEvent>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<ToolchainSettings>
<AvrGccCpp>
<avrgcc.common.Device>-mmcu=atmega328p -B "%24(PackRepoDir)\atmel\ATmega_DFP\1.0.106\gcc\dev\atmega328p"</avrgcc.common.Device>
<avrgcc.common.outputfiles.hex>True</avrgcc.common.outputfiles.hex>
<avrgcc.common.outputfiles.lss>True</avrgcc.common.outputfiles.lss>
<avrgcc.common.outputfiles.eep>True</avrgcc.common.outputfiles.eep>
<avrgcc.common.outputfiles.srec>True</avrgcc.common.outputfiles.srec>
<avrgcc.common.outputfiles.usersignatures>False</avrgcc.common.outputfiles.usersignatures>
<avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcc.compiler.general.ChangeDefaultCharTypeUnsigned>
<avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcc.compiler.general.ChangeDefaultBitFieldUnsigned>
<avrgcc.compiler.directories.IncludePaths><ListValues><Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.0.106\include</Value></ListValues></avrgcc.compiler.directories.IncludePaths>
<avrgcc.compiler.optimization.PackStructureMembers>True</avrgcc.compiler.optimization.PackStructureMembers>
<avrgcc.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcc.compiler.optimization.AllocateBytesNeededForEnum>
<avrgcc.compiler.warnings.AllWarnings>True</avrgcc.compiler.warnings.AllWarnings>
<avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultCharTypeUnsigned>
<avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>True</avrgcccpp.compiler.general.ChangeDefaultBitFieldUnsigned>
<avrgcccpp.compiler.directories.IncludePaths><ListValues><Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.0.106\include</Value></ListValues></avrgcccpp.compiler.directories.IncludePaths>
<avrgcccpp.compiler.optimization.PackStructureMembers>True</avrgcccpp.compiler.optimization.PackStructureMembers>
<avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>True</avrgcccpp.compiler.optimization.AllocateBytesNeededForEnum>
<avrgcccpp.compiler.warnings.AllWarnings>True</avrgcccpp.compiler.warnings.AllWarnings>
<avrgcccpp.compiler.warnings.Pedantic>True</avrgcccpp.compiler.warnings.Pedantic>
<avrgcccpp.compiler.miscellaneous.OtherFlags>-Wextra -std=c++11</avrgcccpp.compiler.miscellaneous.OtherFlags>
<avrgcccpp.linker.libraries.Libraries><ListValues><Value>libm</Value></ListValues></avrgcccpp.linker.libraries.Libraries>
<avrgcccpp.assembler.general.IncludePaths><ListValues><Value>%24(PackRepoDir)\atmel\ATmega_DFP\1.0.106\include</Value></ListValues></avrgcccpp.assembler.general.IncludePaths>
<avrgcc.compiler.symbols.DefSymbols><ListValues><Value>DEBUG</Value></ListValues></avrgcc.compiler.symbols.DefSymbols>
<avrgcc.compiler.optimization.level>Optimize (-O1)</avrgcc.compiler.optimization.level>
<avrgcc.compiler.optimization.DebugLevel>Default (-g2)</avrgcc.compiler.optimization.DebugLevel>
<avrgcccpp.compiler.symbols.DefSymbols><ListValues><Value>DEBUG</Value></ListValues></avrgcccpp.compiler.symbols.DefSymbols>
<avrgcccpp.compiler.optimization.level>Optimize (-O1)</avrgcccpp.compiler.optimization.level>
<avrgcccpp.compiler.optimization.DebugLevel>Default (-g2)</avrgcccpp.compiler.optimization.DebugLevel>
<avrgcccpp.assembler.debugging.DebugLevel>Default (-Wa,-g)</avrgcccpp.assembler.debugging.DebugLevel>
</AvrGccCpp>
</ToolchainSettings>
<PreBuildEvent>echo "C:\avrdude-6.2\avrdude.exe" -v -p$(avrdevice) %%* -Uflash:w:"$(OutputDirectory)\$(Name).hex":i &gt; "$(MSBuildProjectDirectory)\avrdude.bat"</PreBuildEvent>
</PropertyGroup>
<ItemGroup>
<Compile Include="Clock.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="cppalloc.cpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="cppalloc.h">
<SubType>compile</SubType>
</Compile>
<Compile Include="main.cpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="string.cpp">
<SubType>compile</SubType>
</Compile>
<Compile Include="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.h">
<SubType>compile</SubType>
</Compile>
</ItemGroup>
<Import Project="$(AVRSTUDIO_EXE_PATH)\\Vs\\Compiler.targets" />
</Project>

163
USART/USART.h Normal file
View File

@ -0,0 +1,163 @@
/*
* Copyright (c) by BlackMark 2015-2016
* Date 05/01/2016
* Version 2.2
*/
#ifndef USART_H
#define USART_H
#include <avr/io.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <ctype.h>
#include "Clock.h"
#include "string.h"
#include "vector.h"
#if defined (__AVR_ATmega168A__) || defined (__AVR_ATmega328P__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega1284P__)
#define USART_SPI
#endif
#if defined (__AVR_ATmega32A__) || (__AVR_ATmega8__)
#define USART_SHAREDIO
#endif
#if defined (__AVR_ATmega1284P__)
#define SECOND_USART
#endif
class USART0
{
public:
enum class Mode
{
ASYNCHRONOUS = 0,
SYNCHRONOUS = 1,
#ifdef USART_SPI
MASTERSPI = 2
#endif
};
enum class Parity
{
DISABLED = 0,
ODD = 1,
EVEN = 2
};
enum class StopBit
{
ONE = 1,
TWO = 2
};
protected:
volatile uint8_t *m_vui8pUCSRA;
volatile uint8_t *m_vui8pUCSRB;
volatile uint8_t *m_vui8pUCSRC;
volatile uint8_t *m_vui8pUBRRH;
volatile uint8_t *m_vui8pUBRRL;
volatile uint8_t *m_vui8pUDR;
uint8_t m_ui8RXEN;
uint8_t m_ui8TXEN;
uint8_t m_ui8UCSZ0;
uint8_t m_ui8UCSZ1;
uint8_t m_ui8UCSZ2;
uint8_t m_ui8UPM0;
uint8_t m_ui8UPM1;
uint8_t m_ui8USBS;
uint8_t m_ui8RXC;
uint8_t m_ui8UDRE;
#ifdef USART_SPI
uint8_t m_ui8UMSEL0;
uint8_t m_ui8UMSEL1;
#else
uint8_t m_ui8UMSEL;
#endif
private:
uint8_t readUCSRC();
void setUCSRC( uint8_t ui8UCSRC );
void setRXState( bool bEnable );
void setTXState( bool bEnable );
void setBaudRate( uint32_t ui32BaudRate );
void setDataBits( uint8_t ui8DataBits );
void setParity( Parity enmParity );
void setStopBits( StopBit enmStopBits );
void setMode( Mode enmMode );
public:
USART0();
USART0( uint32_t ui32BaudRate, uint8_t ui8DataBits, Parity enmParity, StopBit enmStopBits, Mode enmMode = Mode::ASYNCHRONOUS );
~USART0();
void init( uint32_t ui32BaudRate, uint8_t ui8DataBits, Parity enmParity, StopBit enmStopBits, 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 );
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;
}
};
#ifdef SECOND_USART
class USART1 : public USART0
{
public:
USART1();
USART1( uint32_t ui32BaudRate, uint8_t ui8DataBits, Parity enmParity, StopBit enmStopBits, Mode enmMode = Mode::ASYNCHRONOUS );
~USART1();
};
#endif
#endif

25
USART/cppalloc.cpp Normal file
View File

@ -0,0 +1,25 @@
#include "cppalloc.h"
//////////////////////////////////////////////////////////////////////////
void* operator new( size_t sizeSize )
{
return malloc( sizeSize );
}
//////////////////////////////////////////////////////////////////////////
void operator delete( void* pAddress )
{
free( pAddress );
}
//////////////////////////////////////////////////////////////////////////
void* operator new[]( size_t sizeSize )
{
return malloc( sizeSize );
}
//////////////////////////////////////////////////////////////////////////
void operator delete[]( void* pAddress )
{
free( pAddress );
}

18
USART/cppalloc.h Normal file
View File

@ -0,0 +1,18 @@
/*
* Copyright (c) by BlackMark 2015
* Date 24/11/2015
* Version 1.3
*/
#ifndef CPPALLOC_H
#define CPPALLOC_H
#include <stdlib.h>
void* operator new( size_t sizeSize );
void operator delete( void* pAddress );
void* operator new[]( size_t sizeSize );
void operator delete[]( void* pAddress );
#endif

54
USART/main.cpp Normal file
View File

@ -0,0 +1,54 @@
/*
* Copyright (c) by BlackMark 2015-2016
* Date 05/01/2016
* Version 1.4
*/
#include <stdio.h>
#include "Clock.h"
#include "USART.h"
int main()
{
USART0 cUSART;
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";
while( true )
{
cUSART << "This has been running for \"" << ui32Counter++ << "\" seconds!\r\n\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";
_delay_ms( 1000 );
}
return 0;
}

448
USART/string.cpp Normal file
View File

@ -0,0 +1,448 @@
#include "string.h"
//////////////////////////////////////////////////////////////////////////
string::string() :
m_sizeLength( 0 ), m_sizeCapacity( 1 ), m_szString( nullptr )
{
m_szString = new char[m_sizeCapacity];
if( !m_szString )
{
return;
}
m_szString[m_sizeLength] = '\0';
}
//////////////////////////////////////////////////////////////////////////
string::string( const string &strString ) :
m_sizeLength( strString.m_sizeLength ), m_sizeCapacity( strString.m_sizeCapacity ), m_szString( nullptr )
{
m_szString = new char[m_sizeCapacity];
if( !m_szString )
{
return;
}
memcpy( m_szString, strString.m_szString, m_sizeCapacity * sizeof( char ) );
}
//////////////////////////////////////////////////////////////////////////
string::string( const char* szString ) :
m_sizeLength( strlen( szString ) ), m_sizeCapacity( strlen( szString ) + 1 ), m_szString( nullptr )
{
m_szString = new char[m_sizeCapacity];
if( !m_szString )
{
return;
}
memcpy( m_szString, szString, m_sizeLength * sizeof( char ) );
m_szString[m_sizeLength] = '\0';
}
//////////////////////////////////////////////////////////////////////////
string::string( char chCharacter ) :
m_sizeLength( 1 ), m_sizeCapacity( 2 ), m_szString( nullptr )
{
m_szString = new char[m_sizeCapacity];
if( !m_szString )
{
return;
}
m_szString[0] = chCharacter;
m_szString[m_sizeLength] = '\0';
}
//////////////////////////////////////////////////////////////////////////
string::string( size_t sizeLength, char chCharacter ) :
m_sizeLength( sizeLength ), m_sizeCapacity( sizeLength + 1 ), m_szString( nullptr )
{
m_szString = new char[m_sizeCapacity];
if( !m_szString )
{
return;
}
for( size_t i = 0; i < sizeLength; ++i )
{
m_szString[i] = chCharacter;
}
m_szString[m_sizeLength] = '\0';
}
//////////////////////////////////////////////////////////////////////////
string::~string()
{
delete [] m_szString;
}
//////////////////////////////////////////////////////////////////////////
string &string::append( const string& strString )
{
if( m_sizeLength + strString.m_sizeLength >= m_sizeCapacity )
{
char* szCopyString = new char[m_sizeLength + strString.m_sizeLength + 1];
if( !szCopyString )
{
return *this;
}
memcpy( szCopyString, m_szString, m_sizeLength * sizeof( char ) );
delete [] m_szString;
m_sizeCapacity = m_sizeLength + strString.m_sizeLength + 1;
m_szString = szCopyString;
}
memcpy( m_szString + m_sizeLength, strString.m_szString, strString.m_sizeLength * sizeof( char ) );
m_sizeLength = m_sizeLength + strString.m_sizeLength;
m_szString[m_sizeLength] = '\0';
return *this;
}
//////////////////////////////////////////////////////////////////////////
char& string::at( size_t sizeIndex )
{
if( sizeIndex >= m_sizeLength )
{
return m_szString[m_sizeLength - 1];
}
return m_szString[sizeIndex];
}
//////////////////////////////////////////////////////////////////////////
const char& string::at( size_t sizeIndex ) const
{
if( sizeIndex >= m_sizeLength )
{
return m_szString[m_sizeLength - 1];
}
return m_szString[sizeIndex];
}
//////////////////////////////////////////////////////////////////////////
size_t string::capacity() const
{
return m_sizeCapacity;
}
//////////////////////////////////////////////////////////////////////////
void string::clear()
{
m_sizeLength = 0;
m_szString[m_sizeLength] = '\0';
}
//////////////////////////////////////////////////////////////////////////
const char* string::c_str() const
{
return m_szString;
}
//////////////////////////////////////////////////////////////////////////
const char* string::data() const
{
return m_szString;
}
//////////////////////////////////////////////////////////////////////////
bool string::empty() const
{
if( m_sizeLength == 0 )
{
return true;
}
return false;
}
//////////////////////////////////////////////////////////////////////////
size_t string::find( const string &strString, size_t sizePos /* = 0 */ ) const
{
if( strString.empty() || sizePos >= m_sizeLength )
{
return npos;
}
for( size_t i = sizePos; i < m_sizeLength; ++i )
{
if( i + strString.length() > m_sizeLength )
{
return npos;
}
if( m_szString[i] == strString[0] )
{
size_t sizeFound;
for( sizeFound = 1; sizeFound < strString.size(); ++sizeFound )
{
if( m_szString[i + sizeFound] != strString[sizeFound] )
{
break;
}
}
if( sizeFound == strString.size() )
{
return i;
}
}
}
return npos;
}
//////////////////////////////////////////////////////////////////////////
size_t string::length() const
{
return m_sizeLength;
}
//////////////////////////////////////////////////////////////////////////
void string::push_back( char chCharacter )
{
if( m_sizeLength + 1 >= m_sizeCapacity )
{
char* szCopyString = new char[m_sizeLength + 2];
if( !szCopyString )
{
return;
}
memcpy( szCopyString, m_szString, m_sizeLength * sizeof( char ) );
delete [] m_szString;
m_sizeCapacity = m_sizeLength + 2;
m_szString = szCopyString;
}
m_szString[m_sizeLength++] = chCharacter;
m_szString[m_sizeLength] = '\0';
}
//////////////////////////////////////////////////////////////////////////
void string::reserve( size_t sizeCapacity /* = 0 */ )
{
if( sizeCapacity > m_sizeCapacity )
{
char* szCopyString = new char[sizeCapacity];
if( !szCopyString )
{
return;
}
memcpy( szCopyString, m_szString, m_sizeLength * sizeof( char ) );
delete [] m_szString;
szCopyString[m_sizeLength] = '\0';
m_sizeCapacity = sizeCapacity;
m_szString = szCopyString;
}
}
//////////////////////////////////////////////////////////////////////////
void string::resize( size_t sizeLength, char chCharacter )
{
if( sizeLength < m_sizeLength )
{
m_sizeLength = sizeLength;
m_szString[m_sizeLength] = '\0';
}
else if( sizeLength > m_sizeLength )
{
if( sizeLength + 1 > m_sizeCapacity )
{
char* szCopyString = new char[sizeLength + 1];
if( !szCopyString )
{
return;
}
memcpy( szCopyString, m_szString, m_sizeLength * sizeof( char ) );
delete [] m_szString;
for( size_t i = m_sizeLength; i < sizeLength; ++i )
{
szCopyString[i] = chCharacter;
}
szCopyString[sizeLength] = '\0';
m_sizeLength = sizeLength;
m_sizeCapacity = m_sizeLength + 1;
m_szString = szCopyString;
}
else
{
for( size_t i = m_sizeLength; i < sizeLength; ++i )
{
m_szString[i] = chCharacter;
}
m_szString[sizeLength] = '\0';
m_sizeLength = sizeLength;
}
}
}
//////////////////////////////////////////////////////////////////////////
size_t string::rfind( const string &strString, size_t sizePos /* = npos */ ) const
{
if( sizePos == npos )
{
sizePos = m_sizeLength - 1;
}
if( strString.empty() || sizePos >= m_sizeLength )
{
return npos;
}
for( size_t i = sizePos + 1; i-- > 0; )
{
if( i - ( strString.length() - 1 ) > m_sizeLength )
{
return npos;
}
if( m_szString[i] == strString[strString.length() - 1] )
{
size_t sizeFound;
for( sizeFound = strString.length() - 1; sizeFound-- > 0; )
{
if( m_szString[i - ( strString.length() - ( sizeFound + 1 ) )] != strString[sizeFound] )
{
break;
}
}
if( sizeFound == npos )
{
return i - ( strString.length() - 1 );
}
}
}
return npos;
}
//////////////////////////////////////////////////////////////////////////
size_t string::size() const
{
return m_sizeLength;
}
//////////////////////////////////////////////////////////////////////////
string string::substr( size_t sizePos /* = 0 */, size_t sizeLength /* = npos */ ) const
{
if( sizePos >= m_sizeLength )
{
return string();
}
string strSubString;
for( size_t i = sizePos; i < sizePos + sizeLength; ++i )
{
if( i >= m_sizeLength )
{
break;
}
strSubString += m_szString[i];
}
return strSubString;
}
//////////////////////////////////////////////////////////////////////////
string& string::operator=( const string &strRHS )
{
char* szCopyString = new char[strRHS.m_sizeCapacity];
if( !szCopyString )
{
return *this;
}
m_sizeLength = strRHS.m_sizeLength;
m_sizeCapacity = strRHS.m_sizeCapacity;
memcpy( szCopyString, strRHS.m_szString, m_sizeCapacity * sizeof( char ) );
delete [] m_szString;
m_szString = szCopyString;
return *this;
}
//////////////////////////////////////////////////////////////////////////
char &string::operator[]( size_t sizeIndex )
{
return m_szString[sizeIndex];
}
//////////////////////////////////////////////////////////////////////////
const char &string::operator[]( size_t sizeIndex ) const
{
return m_szString[sizeIndex];
}
//////////////////////////////////////////////////////////////////////////
string string::operator+( const string &strRHS ) const
{
string strReturn = *this;
strReturn.append( strRHS );
return strReturn;
}
//////////////////////////////////////////////////////////////////////////
string &string::operator+=( const string &strRHS )
{
append( strRHS );
return *this;
}
//////////////////////////////////////////////////////////////////////////
bool string::operator==( const string &strRHS ) const
{
if( size() != strRHS.size() )
{
return false;
}
for( size_t i = 0; i < size(); ++i )
{
if( m_szString[i] != strRHS.m_szString[i] )
{
return false;
}
}
return true;
}
//////////////////////////////////////////////////////////////////////////
bool string::operator!=( const string &strRHS ) const
{
return !( *this == strRHS );
}

55
USART/string.h Normal file
View File

@ -0,0 +1,55 @@
/*
* Copyright (c) by BlackMark 2015
* Date 26/11/2015
* Version 1.4
*/
#ifndef STRING_H
#define STRING_H
#include <string.h>
class string
{
private:
size_t m_sizeLength;
size_t m_sizeCapacity;
char* m_szString;
public:
static const size_t npos = -1;
string();
string( const string &strString );
string( const char* szString );
string( char chCharacter );
string( size_t sizeLength, char chCharacter );
~string();
string& append( const string &strString );
char& at( size_t sizeIndex );
const char& at( size_t sizeIndex ) const;
size_t capacity() const;
void clear();
const char* c_str() const;
const char* data() const;
bool empty() const;
size_t find( const string &strString, size_t sizePos = 0 ) const;
size_t length() const;
void push_back( char chCharacter );
void reserve( size_t sizeCapacity = 0 );
void resize( size_t sizeLength, char chCharacter = '\0' );
size_t rfind( const string &strString, size_t sizePos = npos ) const;
size_t size() const;
string substr( size_t sizePos = 0, size_t sizeLength = npos ) const;
string& operator=( const string &strRHS );
char& operator[]( size_t sizeIndex );
const char& operator[]( size_t sizeIndex ) const;
string operator+( const string &strRHS ) const;
string& operator+=( const string &strRHS );
bool operator==( const string &strRHS ) const;
bool operator!=( const string &strRHS ) const;
};
#endif

188
USART/vector.h Normal file
View File

@ -0,0 +1,188 @@
/*
* Copyright (c) by BlackMark 2015
* Date 06/12/2015
* Version 1.6
*/
#ifndef VECTOR_H
#define VECTOR_H
#include <stdlib.h>
#include <string.h>
#include "cppalloc.h"
template<typename T>
class vector
{
private:
size_t m_sizeSize;
size_t m_sizeCapacity;
T* m_pData;
//////////////////////////////////////////////////////////////////////////
void extend()
{
m_sizeCapacity = ( ( m_sizeCapacity > 0 ) ? ( 2 * m_sizeCapacity ) : ( 1 ) );
T *pCopyData = new T[m_sizeCapacity];
memcpy( pCopyData, m_pData, m_sizeSize * sizeof( T ) );
delete [] m_pData;
m_pData = pCopyData;
}
public:
//////////////////////////////////////////////////////////////////////////
vector() :
m_sizeSize( 0 ), m_sizeCapacity( 0 ), m_pData( nullptr )
{}
//////////////////////////////////////////////////////////////////////////
vector( size_t sizeSize, const T &Value = T() ) :
m_sizeSize( sizeSize ), m_sizeCapacity( sizeSize ), m_pData( nullptr )
{
m_pData = new T[m_sizeCapacity];
for( size_t i = 0; i < m_sizeSize; ++i )
{
m_pData[i] = Value;
}
}
//////////////////////////////////////////////////////////////////////////
vector( const vector &vecVector ) :
m_sizeSize( vecVector.m_sizeSize ), m_sizeCapacity( vecVector.m_sizeCapacity ), m_pData( nullptr )
{
m_pData = new T[m_sizeCapacity];
memcpy( m_pData, vecVector.m_pData, m_sizeSize * sizeof( T ) );
}
//////////////////////////////////////////////////////////////////////////
vector( vector &&vecVector ) :
m_sizeSize( vecVector.m_sizeSize ), m_sizeCapacity( vecVector.m_sizeCapacity ), m_pData( vecVector.m_pData )
{
vecVector.m_sizeSize = 0;
vecVector.m_sizeCapacity = 0;
vecVector.m_pData = nullptr;
}
//////////////////////////////////////////////////////////////////////////
~vector()
{
delete [] m_pData;
}
//////////////////////////////////////////////////////////////////////////
vector& operator=( const vector &vecRHS )
{
m_sizeSize = vecRHS.m_sizeSize;
m_sizeCapacity = vecRHS.m_sizeCapacity;
m_pData = new T[m_sizeCapacity];
memcpy( m_pData, vecRHS.m_pData, m_sizeSize * sizeof( T ) );
return *this;
}
//////////////////////////////////////////////////////////////////////////
vector& operator=( vector &&vecRHS )
{
m_sizeSize = vecRHS.m_sizeSize;
m_sizeCapacity = vecRHS.m_sizeCapacity;
delete [] m_pData;
m_pData = vecRHS.m_pData;
vecRHS.m_sizeSize = 0;
vecRHS.m_sizeCapacity = 0;
vecRHS.m_pData = nullptr;
return *this;
}
//////////////////////////////////////////////////////////////////////////
T* data()
{
return m_pData;
}
//////////////////////////////////////////////////////////////////////////
const T* data() const
{
return m_pData;
}
//////////////////////////////////////////////////////////////////////////
void clear()
{
delete [] m_pData;
m_pData = nullptr;
m_sizeSize = 0;
m_sizeCapacity = 0;
}
//////////////////////////////////////////////////////////////////////////
void push_back( T const &Data )
{
if( m_sizeSize == m_sizeCapacity )
{
extend();
}
m_pData[m_sizeSize++] = Data;
}
//////////////////////////////////////////////////////////////////////////
void resize( size_t sizeSize, T val = T() )
{
if( sizeSize != m_sizeSize )
{
m_sizeCapacity = sizeSize;
T *pCopyData = new T[m_sizeCapacity];
memcpy( pCopyData, m_pData, ( ( sizeSize < m_sizeSize ) ? ( sizeSize ) : ( m_sizeSize ) ) * sizeof( T ) );
delete [] m_pData;
m_pData = pCopyData;
if( sizeSize > m_sizeSize )
{
for( size_t i = m_sizeSize; i < sizeSize; ++i )
{
m_pData[i] = val;
}
}
m_sizeSize = sizeSize;
}
}
//////////////////////////////////////////////////////////////////////////
size_t size() const
{
return m_sizeSize;
}
//////////////////////////////////////////////////////////////////////////
bool empty() const
{
if( m_sizeSize == 0 )
{
return true;
}
return false;
}
//////////////////////////////////////////////////////////////////////////
const T& operator[]( size_t sizeIndex ) const
{
return m_pData[sizeIndex];
}
//////////////////////////////////////////////////////////////////////////
T& operator[]( size_t sizeIndex )
{
return m_pData[sizeIndex];
}
};
#endif