Rewrote inout library to allow compile time port lookup and inline optimization

This commit is contained in:
BlackMark 2018-04-24 18:59:12 +02:00
parent b4fca1e7ce
commit 093c0a6a02
2 changed files with 380 additions and 439 deletions

225
inout.cpp
View File

@ -1,225 +0,0 @@
#include "inout.h"
//////////////////////////////////////////////////////////////////////////
volatile uint8_t* InOut::getPort( Pin enmPin, Type enmType )
{
volatile uint8_t *vpui8Port = nullptr;
if( enmPin == Pin::P_NONE )
{
return vpui8Port;
}
uint8_t ui8Port = ( static_cast<uint16_t>( enmPin ) >> 4 ) & 0x0F;
switch( ui8Port )
{
case 0:
{
if( enmType == Type::T_PIN )
{
vpui8Port = PORT_PINA;
}
else if( enmType == Type::T_DDR )
{
vpui8Port = PORT_DDRA;
}
else if( enmType == Type::T_PORT )
{
vpui8Port = PORT_PORTA;
}
break;
}
case 1:
{
if( enmType == Type::T_PIN )
{
vpui8Port = PORT_PINB;
}
else if( enmType == Type::T_DDR )
{
vpui8Port = PORT_DDRB;
}
else if( enmType == Type::T_PORT )
{
vpui8Port = PORT_PORTB;
}
break;
}
case 2:
{
if( enmType == Type::T_PIN )
{
vpui8Port = PORT_PINC;
}
else if( enmType == Type::T_DDR )
{
vpui8Port = PORT_DDRC;
}
else if( enmType == Type::T_PORT )
{
vpui8Port = PORT_PORTC;
}
break;
}
case 3:
{
if( enmType == Type::T_PIN )
{
vpui8Port = PORT_PIND;
}
else if( enmType == Type::T_DDR )
{
vpui8Port = PORT_DDRD;
}
else if( enmType == Type::T_PORT )
{
vpui8Port = PORT_PORTD;
}
break;
}
}
return vpui8Port;
}
//////////////////////////////////////////////////////////////////////////
uint8_t InOut::getPin( Pin enmPin )
{
return static_cast<uint16_t>( enmPin ) & 0x0F;
}
//////////////////////////////////////////////////////////////////////////
void InOut::setPinDirection( Pin enmPin, Dir enmDir, bool bPullup )
{
if( enmPin == Pin::P_NONE )
{
return;
}
volatile uint8_t *vpui8PortDir = getPort( enmPin, Type::T_DDR );
volatile uint8_t *vpui8PortOut = getPort( enmPin, Type::T_PORT );
uint8_t ui8Pin = getPin( enmPin );
setPinDirection( vpui8PortDir, ui8Pin, enmDir );
if( enmDir == Dir::D_IN )
{
writePin( vpui8PortOut, ui8Pin, bPullup );
}
}
//////////////////////////////////////////////////////////////////////////
bool InOut::readPin( Pin enmPin )
{
if( enmPin == Pin::P_NONE )
{
return false;
}
volatile uint8_t *vpui8Port = getPort( enmPin, Type::T_PIN );
uint8_t ui8Pin = getPin( enmPin );
return readPin( vpui8Port, ui8Pin );
}
//////////////////////////////////////////////////////////////////////////
void InOut::writePin( Pin enmPin, bool bValue )
{
if( enmPin == Pin::P_NONE )
{
return;
}
volatile uint8_t *vpui8Port = getPort( enmPin, Type::T_PORT );
uint8_t ui8Pin = getPin( enmPin );
writePin( vpui8Port, ui8Pin, bValue );
}
//////////////////////////////////////////////////////////////////////////
uint8_t InOut::readPort( Pin enmPortPin )
{
if( enmPortPin == Pin::P_NONE )
{
return 0;
}
volatile uint8_t *vpui8Port = getPort( enmPortPin, Type::T_PIN );
return readPort( vpui8Port );
}
//////////////////////////////////////////////////////////////////////////
void InOut::writePort( Pin enmPortPin, uint8_t ui8Value )
{
if( enmPortPin == Pin::P_NONE )
{
return;
}
volatile uint8_t *vpui8Port = getPort( enmPortPin, Type::T_PORT );
writePort( vpui8Port, ui8Value );
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
InOutPin::InOutPin()
{
setPin( InOut::Pin::P_NONE );
}
//////////////////////////////////////////////////////////////////////////
InOutPin::InOutPin( InOut::Pin enmPin )
{
setPin( enmPin );
}
//////////////////////////////////////////////////////////////////////////
InOutPin::~InOutPin()
{
setDirection( InOut::Dir::D_IN, false );
}
//////////////////////////////////////////////////////////////////////////
void InOutPin::setPin( InOut::Pin enmPin )
{
m_vpui8Input = InOut::getPort( enmPin, InOut::Type::T_PIN );
m_vpui8Dir = InOut::getPort( enmPin, InOut::Type::T_DDR );
m_vpui8Output = InOut::getPort( enmPin, InOut::Type::T_PORT );
m_ui8Pin = InOut::getPin( enmPin );
}
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
InOutPort::InOutPort()
{
setPort( InOut::Pin::P_NONE );
}
//////////////////////////////////////////////////////////////////////////
InOutPort::InOutPort( InOut::Pin enmPin )
{
setPort( enmPin );
}
//////////////////////////////////////////////////////////////////////////
InOutPort::~InOutPort()
{
setDirection( InOut::Dir::D_IN, false );
}
//////////////////////////////////////////////////////////////////////////
void InOutPort::setPort( InOut::Pin enmPortPin )
{
m_vpui8Input = InOut::getPort( enmPortPin, InOut::Type::T_PIN );
m_vpui8Dir = InOut::getPort( enmPortPin, InOut::Type::T_DDR );
m_vpui8Output = InOut::getPort( enmPortPin, InOut::Type::T_PORT );
}

594
inout.h
View File

@ -1,298 +1,464 @@
/* /*
* Copyright (c) by BlackMark 2015-2017 * Copyright (c) by BlackMark 2015-2018
* Date 17/04/2017 * Date 24/04/2018
* Version 2.7 * Version 2.8
*/ */
#ifndef INOUT_H #ifndef INOUT_H
#define INOUT_H #define INOUT_H
//////////////////////////////////////////////////////////////////////////
#include <stdint.h> #include <stdint.h>
#include <avr/io.h> #include <avr/io.h>
//////////////////////////////////////////////////////////////////////////
#define AVR_DIP40 defined (__AVR_ATmega32A__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega1284P__) #define AVR_DIP40 defined (__AVR_ATmega32A__) || defined (__AVR_ATmega644P__) || defined (__AVR_ATmega1284P__)
#define AVR_DIP28 defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega168A__) || defined (__AVR_ATmega328P__) #define AVR_DIP28 defined (__AVR_ATmega8__) || defined (__AVR_ATmega8A__) || defined (__AVR_ATmega168A__) || defined (__AVR_ATmega328P__)
#define AVR_DIP8 defined (__AVR_ATtiny13A__) || defined (__AVR_ATtiny85__) #define AVR_DIP8 defined (__AVR_ATtiny13A__) || defined (__AVR_ATtiny85__)
#if AVR_DIP40 #if AVR_DIP40
#define PORT_PINA &PINA static constexpr volatile uint8_t *IO_PINA = &PINA;
#define PORT_DDRA &DDRA static constexpr volatile uint8_t *IO_DDRA = &DDRA;
#define PORT_PORTA &PORTA static constexpr volatile uint8_t *IO_PORTA = &PORTA;
#else #else
#define PORT_PINA nullptr static constexpr volatile uint8_t *IO_PINA = nullptr;
#define PORT_DDRA nullptr static constexpr volatile uint8_t *IO_DDRA = nullptr;
#define PORT_PORTA nullptr static constexpr volatile uint8_t *IO_PORTA = nullptr;
#endif #endif
#if AVR_DIP40 || AVR_DIP28 || AVR_DIP8 #if AVR_DIP40 || AVR_DIP28 || AVR_DIP8
#define PORT_PINB &PINB static constexpr volatile uint8_t *IO_PINB = &PINB;
#define PORT_DDRB &DDRB static constexpr volatile uint8_t *IO_DDRB = &DDRB;
#define PORT_PORTB &PORTB static constexpr volatile uint8_t *IO_PORTB = &PORTB;
#else #else
#define PORT_PINB nullptr static constexpr volatile uint8_t *IO_PINB = nullptr;
#define PORT_DDRB nullptr static constexpr volatile uint8_t *IO_DDRB = nullptr;
#define PORT_PORTB nullptr static constexpr volatile uint8_t *IO_PORTB = nullptr;
#endif #endif
#if AVR_DIP40 || AVR_DIP28 #if AVR_DIP40 || AVR_DIP28
#define PORT_PINC &PINC static constexpr volatile uint8_t *IO_PINC = &PINC;
#define PORT_DDRC &DDRC static constexpr volatile uint8_t *IO_DDRC = &DDRC;
#define PORT_PORTC &PORTC static constexpr volatile uint8_t *IO_PORTC = &PORTC;
#else #else
#define PORT_PINC nullptr static constexpr volatile uint8_t *IO_PINC = nullptr;
#define PORT_DDRC nullptr static constexpr volatile uint8_t *IO_DDRC = nullptr;
#define PORT_PORTC nullptr static constexpr volatile uint8_t *IO_PORTC = nullptr;
#endif #endif
#if AVR_DIP40 || AVR_DIP28 #if AVR_DIP40 || AVR_DIP28
#define PORT_PIND &PIND static constexpr volatile uint8_t *IO_PIND = &PIND;
#define PORT_DDRD &DDRD static constexpr volatile uint8_t *IO_DDRD = &DDRD;
#define PORT_PORTD &PORTD static constexpr volatile uint8_t *IO_PORTD = &PORTD;
#else #else
#define PORT_PIND nullptr static constexpr volatile uint8_t *IO_PIND = nullptr;
#define PORT_DDRD nullptr static constexpr volatile uint8_t *IO_DDRD = nullptr;
#define PORT_PORTD nullptr static constexpr volatile uint8_t *IO_PORTD = nullptr;
#endif #endif
class InOut /************************************************************************/
//////////////////////////////////////////////////////////////////////////
struct InOut
{ {
public:
enum class Pin enum class Pin
{ {
P_NONE = -1,
#if AVR_DIP40 #if AVR_DIP40
P_A0 = 0x00, A0 = 0x00,
P_A1 = 0x01, A1 = 0x01,
P_A2 = 0x02, A2 = 0x02,
P_A3 = 0x03, A3 = 0x03,
P_A4 = 0x04, A4 = 0x04,
P_A5 = 0x05, A5 = 0x05,
P_A6 = 0x06, A6 = 0x06,
P_A7 = 0x07, A7 = 0x07,
#endif #endif
#if AVR_DIP40 || AVR_DIP28 || AVR_DIP8 #if AVR_DIP40 || AVR_DIP28 || AVR_DIP8
P_B0 = 0x10, B0 = 0x10,
P_B1 = 0x11, B1 = 0x11,
P_B2 = 0x12, B2 = 0x12,
P_B3 = 0x13, B3 = 0x13,
P_B4 = 0x14, B4 = 0x14,
P_B5 = 0x15, B5 = 0x15,
#endif #endif
#if AVR_DIP40 || AVR_DIP28 #if AVR_DIP40 || AVR_DIP28
P_B6 = 0x16, B6 = 0x16,
P_B7 = 0x17, B7 = 0x17,
P_C0 = 0x20, C0 = 0x20,
P_C1 = 0x21, C1 = 0x21,
P_C2 = 0x22, C2 = 0x22,
P_C3 = 0x23, C3 = 0x23,
P_C4 = 0x24, C4 = 0x24,
P_C5 = 0x25, C5 = 0x25,
P_C6 = 0x26, C6 = 0x26,
#endif #endif
#if AVR_DIP40 #if AVR_DIP40
P_C7 = 0x27, C7 = 0x27,
#endif #endif
#if AVR_DIP40 || AVR_DIP28 #if AVR_DIP40 || AVR_DIP28
P_D0 = 0x30, D0 = 0x30,
P_D1 = 0x31, D1 = 0x31,
P_D2 = 0x32, D2 = 0x32,
P_D3 = 0x33, D3 = 0x33,
P_D4 = 0x34, D4 = 0x34,
P_D5 = 0x35, D5 = 0x35,
P_D6 = 0x36, D6 = 0x36,
P_D7 = 0x37, D7 = 0x37,
#endif
};
enum class Port
{
#if AVR_DIP40
A = Pin::A0,
#endif
#if AVR_DIP40 || AVR_DIP28 || AVR_DIP8
B = Pin::B0,
#endif
#if AVR_DIP40 || AVR_DIP28
C = Pin::C0,
D = Pin::D0,
#endif #endif
}; };
enum class Dir enum class Dir
{ {
D_IN = 0, IN,
D_OUT = 1 OUT
}; };
enum class Type enum class Type
{ {
T_PIN = 0, PIN,
T_DDR = 1, DDR,
T_PORT = 2 PORT
}; };
static volatile uint8_t* getPort( Pin enmPin, Type enmType ); static constexpr volatile uint8_t* getPort( Pin enmPin, Type enmType );
static uint8_t getPin( Pin enmPin ); static constexpr uint8_t getPin( Pin enmPin );
static void setPinDirection( Pin enmPin, Dir enmDir, bool bPullup ); static inline void setPinDirection( volatile uint8_t *vpui8Port, uint8_t ui8Pin, Dir enmDir );
static bool readPin( Pin enmPin ); static inline bool readPin( volatile uint8_t *vpui8Port, uint8_t ui8Pin );
static void writePin( Pin enmPin, bool bValue ); static inline void writePin( volatile uint8_t *vpui8Port, uint8_t ui8Pin, bool bValue );
static uint8_t readPort( Pin enmPortPin ); static inline void setPortDirection( volatile uint8_t *vpui8Port, Dir enmDir );
static void writePort( Pin enmPortPin, uint8_t ui8Value ); static inline uint8_t readPort( volatile uint8_t *vpui8Port );
static inline void writePort( volatile uint8_t *vpui8Port, uint8_t ui8Value );
////////////////////////////////////////////////////////////////////////// static inline void setPinDirection( Pin enmPin, Dir enmDir );
static inline void setPinDirection( volatile uint8_t *vpui8Port, uint8_t ui8Pin, Dir enmDir ) static inline void setPinPullup( Pin enmPin, bool bPullup );
{ static inline bool readPin( Pin enmPin );
if( enmDir == Dir::D_OUT ) static inline void writePin( Pin enmPin, bool bValue );
{
*vpui8Port |= ( 1 << ui8Pin );
}
else
{
*vpui8Port &= ~( 1 << ui8Pin );
}
}
////////////////////////////////////////////////////////////////////////// static inline void setPortDirection( Port enmPort, Dir enmDir );
static inline bool readPin( volatile uint8_t *vpui8Port, uint8_t ui8Pin ) static inline void setPortPullup( Port enmPort, bool bPullup );
{ static inline uint8_t readPort( Port enmPort );
if( ( ( *vpui8Port ) >> ui8Pin ) & 1 ) static inline void writePort( Port enmPort, uint8_t ui8Value );
{
return true;
}
return false;
}
//////////////////////////////////////////////////////////////////////////
static inline void writePin( volatile uint8_t *vpui8Port, uint8_t ui8Pin, bool bValue )
{
if( bValue )
{
*vpui8Port |= ( 1 << ui8Pin );
}
else
{
*vpui8Port &= ~( 1 << ui8Pin );
}
}
//////////////////////////////////////////////////////////////////////////
static inline void setPortDirection( volatile uint8_t *vpui8Port, Dir enmDir )
{
*vpui8Port = ( ( enmDir == InOut::Dir::D_OUT ) ? ( 0xFF ) : ( 0x00 ) );
}
//////////////////////////////////////////////////////////////////////////
static inline uint8_t readPort( volatile uint8_t *vpui8Port )
{
return *vpui8Port;
}
//////////////////////////////////////////////////////////////////////////
static inline void writePort( volatile uint8_t *vpui8Port, uint8_t ui8Value )
{
*vpui8Port = ui8Value;
}
}; };
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
constexpr volatile uint8_t* InOut::getPort( Pin enmPin, Type enmType )
{
volatile uint8_t *vpui8Port = nullptr;
uint8_t ui8Port = static_cast<uint16_t>( enmPin ) >> 4 & 0x0F;
switch( ui8Port )
{
case 0:
{
if( enmType == Type::PIN )
vpui8Port = IO_PINA;
else if( enmType == Type::DDR )
vpui8Port = IO_DDRA;
else if( enmType == Type::PORT )
vpui8Port = IO_PORTA;
break;
}
case 1:
{
if( enmType == Type::PIN )
vpui8Port = IO_PINB;
else if( enmType == Type::DDR )
vpui8Port = IO_DDRB;
else if( enmType == Type::PORT )
vpui8Port = IO_PORTB;
break;
}
case 2:
{
if( enmType == Type::PIN )
vpui8Port = IO_PINC;
else if( enmType == Type::DDR )
vpui8Port = IO_DDRC;
else if( enmType == Type::PORT )
vpui8Port = IO_PORTC;
break;
}
case 3:
{
if( enmType == Type::PIN )
vpui8Port = IO_PIND;
else if( enmType == Type::DDR )
vpui8Port = IO_DDRD;
else if( enmType == Type::PORT )
vpui8Port = IO_PORTD;
break;
}
}
return vpui8Port;
}
//////////////////////////////////////////////////////////////////////////
constexpr uint8_t InOut::getPin( Pin enmPin )
{
return static_cast<uint16_t>( enmPin ) & 0x0F;
}
//////////////////////////////////////////////////////////////////////////
inline void InOut::setPinDirection( volatile uint8_t *vpui8Port, uint8_t ui8Pin, Dir enmDir )
{
if( enmDir == Dir::OUT )
*vpui8Port |= ( 1 << ui8Pin );
else
*vpui8Port &= ~( 1 << ui8Pin );
}
//////////////////////////////////////////////////////////////////////////
inline bool InOut::readPin( volatile uint8_t *vpui8Port, uint8_t ui8Pin )
{
if( *vpui8Port >> ui8Pin & 1 )
return true;
return false;
}
//////////////////////////////////////////////////////////////////////////
inline void InOut::writePin( volatile uint8_t *vpui8Port, uint8_t ui8Pin, bool bValue )
{
if( bValue )
*vpui8Port |= ( 1 << ui8Pin );
else
*vpui8Port &= ~( 1 << ui8Pin );
}
//////////////////////////////////////////////////////////////////////////
inline void InOut::setPortDirection( volatile uint8_t *vpui8Port, Dir enmDir )
{
*vpui8Port = ( enmDir == Dir::OUT ) ? 0xFF : 0x00;
}
//////////////////////////////////////////////////////////////////////////
inline uint8_t InOut::readPort( volatile uint8_t *vpui8Port )
{
return *vpui8Port;
}
//////////////////////////////////////////////////////////////////////////
inline void InOut::writePort( volatile uint8_t *vpui8Port, uint8_t ui8Value )
{
*vpui8Port = ui8Value;
}
//////////////////////////////////////////////////////////////////////////
inline void InOut::setPinDirection( Pin enmPin, Dir enmDir )
{
volatile uint8_t *vpui8PortDir = getPort( enmPin, Type::DDR );
uint8_t ui8Pin = getPin( enmPin );
setPinDirection( vpui8PortDir, ui8Pin, enmDir );
}
//////////////////////////////////////////////////////////////////////////
inline void InOut::setPinPullup( Pin enmPin, bool bPullup )
{
volatile uint8_t *vpui8PortOut = getPort( enmPin, Type::PORT );
uint8_t ui8Pin = getPin( enmPin );
writePin( vpui8PortOut, ui8Pin, bPullup );
}
//////////////////////////////////////////////////////////////////////////
inline bool InOut::readPin( Pin enmPin )
{
volatile uint8_t *vpui8Port = getPort( enmPin, Type::PIN );
uint8_t ui8Pin = getPin( enmPin );
return readPin( vpui8Port, ui8Pin );
}
//////////////////////////////////////////////////////////////////////////
inline void InOut::writePin( Pin enmPin, bool bValue )
{
volatile uint8_t *vpui8Port = getPort( enmPin, Type::PORT );
uint8_t ui8Pin = getPin( enmPin );
writePin( vpui8Port, ui8Pin, bValue );
}
//////////////////////////////////////////////////////////////////////////
inline void InOut::setPortDirection( Port enmPort, Dir enmDir )
{
volatile uint8_t *vpui8PortDir = getPort( static_cast<Pin>( enmPort ), Type::DDR );
setPortDirection( vpui8PortDir, enmDir );
}
//////////////////////////////////////////////////////////////////////////
inline void InOut::setPortPullup( Port enmPort, bool bPullup )
{
volatile uint8_t *vpui8PortOut = getPort( static_cast<Pin>( enmPort ), Type::PORT );
writePort( vpui8PortOut, bPullup ? 0xFF : 0x00 );
}
//////////////////////////////////////////////////////////////////////////
inline uint8_t InOut::readPort( Port enmPort )
{
volatile uint8_t *vpui8Port = getPort( static_cast<Pin>( enmPort ), Type::PIN );
return readPort( vpui8Port );
}
//////////////////////////////////////////////////////////////////////////
inline void InOut::writePort( Port enmPort, uint8_t ui8Value )
{
volatile uint8_t *vpui8Port = getPort( static_cast<Pin>( enmPort ), Type::PORT );
writePort( vpui8Port, ui8Value );
}
/************************************************************************/
//////////////////////////////////////////////////////////////////////////
class InOutPin class InOutPin
{ {
private: private:
volatile uint8_t *m_vpui8Input; volatile uint8_t *m_pvui8PIN;
volatile uint8_t *m_vpui8Dir; volatile uint8_t *m_pvui8DDR;
volatile uint8_t *m_vpui8Output; volatile uint8_t *m_pvui8PORT;
uint8_t m_ui8Pin; uint8_t m_ui8Pin;
public: public:
InOutPin(); inline InOutPin();
InOutPin( InOut::Pin enmPin ); inline InOutPin( InOut::Pin enmPin );
~InOutPin();
void setPin( InOut::Pin enmPin ); inline void setPin( InOut::Pin enmPin );
////////////////////////////////////////////////////////////////////////// inline void setDirection( InOut::Dir enmDir );
inline void setDirection( InOut::Dir enmDir, bool bPullup ) inline void setPullup( bool bPullup );
{
if( !m_vpui8Dir || !m_vpui8Output )
{
return;
}
InOut::setPinDirection( m_vpui8Dir, m_ui8Pin, enmDir ); inline bool read();
inline void write( bool bValue );
if( enmDir == InOut::Dir::D_IN )
{
InOut::writePin( m_vpui8Output, m_ui8Pin, bPullup );
}
}
//////////////////////////////////////////////////////////////////////////
inline bool read()
{
if( !m_vpui8Input )
{
return false;
}
return InOut::readPin( m_vpui8Input, m_ui8Pin );
}
//////////////////////////////////////////////////////////////////////////
inline void write( bool bValue )
{
if( !m_vpui8Output )
{
return;
}
InOut::writePin( m_vpui8Output, m_ui8Pin, bValue );
}
}; };
////////////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////
inline InOutPin::InOutPin() :
m_pvui8PIN( nullptr ), m_pvui8DDR( nullptr ), m_pvui8PORT( nullptr ), m_ui8Pin( 0xFF )
{}
//////////////////////////////////////////////////////////////////////////
inline InOutPin::InOutPin( InOut::Pin enmPin ) :
m_pvui8PIN( InOut::getPort( enmPin, InOut::Type::PIN ) ),
m_pvui8DDR( InOut::getPort( enmPin, InOut::Type::DDR ) ),
m_pvui8PORT( InOut::getPort( enmPin, InOut::Type::PORT ) ),
m_ui8Pin( InOut::getPin( enmPin ) )
{}
//////////////////////////////////////////////////////////////////////////
inline void InOutPin::setPin( InOut::Pin enmPin )
{
m_pvui8PIN = InOut::getPort( enmPin, InOut::Type::PIN );
m_pvui8DDR = InOut::getPort( enmPin, InOut::Type::DDR );
m_pvui8PORT = InOut::getPort( enmPin, InOut::Type::PORT );
m_ui8Pin = InOut::getPin( enmPin );
}
//////////////////////////////////////////////////////////////////////////
inline void InOutPin::setDirection( InOut::Dir enmDir )
{
InOut::setPinDirection( m_pvui8DDR, m_ui8Pin, enmDir );
}
//////////////////////////////////////////////////////////////////////////
inline void InOutPin::setPullup( bool bPullup )
{
InOut::writePin( m_pvui8PORT, m_ui8Pin, bPullup );
}
//////////////////////////////////////////////////////////////////////////
inline bool InOutPin::read()
{
return InOut::readPin( m_pvui8PIN, m_ui8Pin );
}
//////////////////////////////////////////////////////////////////////////
inline void InOutPin::write( bool bValue )
{
InOut::writePin( m_pvui8PORT, m_ui8Pin, bValue );
}
/************************************************************************/
//////////////////////////////////////////////////////////////////////////
class InOutPort class InOutPort
{ {
private: private:
volatile uint8_t *m_vpui8Input; volatile uint8_t *m_pvui8PIN;
volatile uint8_t *m_vpui8Dir; volatile uint8_t *m_pvui8DDR;
volatile uint8_t *m_vpui8Output; volatile uint8_t *m_pvui8PORT;
public: public:
InOutPort(); inline InOutPort();
InOutPort( InOut::Pin enmPortPin ); inline InOutPort( InOut::Port enmPort );
~InOutPort();
void setPort( InOut::Pin enmPortPin ); inline void setPort( InOut::Port enmPort );
////////////////////////////////////////////////////////////////////////// inline void setDirection( InOut::Dir enmDir );
inline void setDirection( InOut::Dir enmDir, bool bPullup ) inline void setPullup( bool bPullup );
{
if( !m_vpui8Dir || !m_vpui8Output )
{
return;
}
InOut::setPortDirection( m_vpui8Dir, enmDir ); inline uint8_t read();
inline void write( uint8_t ui8Value );
if( enmDir == InOut::Dir::D_IN )
{
InOut::writePort( m_vpui8Output, ( ( bPullup ) ? ( 0xFF ) : ( 0x00 ) ) );
}
}
//////////////////////////////////////////////////////////////////////////
inline uint8_t read()
{
if( !m_vpui8Input )
{
return 0;
}
return InOut::readPort( m_vpui8Input );
}
//////////////////////////////////////////////////////////////////////////
inline void write( uint8_t ui8Value )
{
if( !m_vpui8Output )
{
return;
}
InOut::writePort( m_vpui8Output, ui8Value );
}
}; };
//////////////////////////////////////////////////////////////////////////
inline InOutPort::InOutPort() :
m_pvui8PIN( nullptr ), m_pvui8DDR( nullptr ), m_pvui8PORT( nullptr )
{}
//////////////////////////////////////////////////////////////////////////
inline InOutPort::InOutPort( InOut::Port enmPort ) :
m_pvui8PIN( InOut::getPort( static_cast<InOut::Pin>( enmPort ), InOut::Type::PIN ) ),
m_pvui8DDR( InOut::getPort( static_cast<InOut::Pin>( enmPort ), InOut::Type::DDR ) ),
m_pvui8PORT( InOut::getPort( static_cast<InOut::Pin>( enmPort ), InOut::Type::PORT ) )
{}
//////////////////////////////////////////////////////////////////////////
inline void InOutPort::setPort( InOut::Port enmPort )
{
m_pvui8PIN = InOut::getPort( static_cast<InOut::Pin>( enmPort ), InOut::Type::PIN );
m_pvui8DDR = InOut::getPort( static_cast<InOut::Pin>( enmPort ), InOut::Type::DDR );
m_pvui8PORT = InOut::getPort( static_cast<InOut::Pin>( enmPort ), InOut::Type::PORT );
}
//////////////////////////////////////////////////////////////////////////
inline void InOutPort::setDirection( InOut::Dir enmDir )
{
InOut::setPortDirection( m_pvui8DDR, enmDir );
}
//////////////////////////////////////////////////////////////////////////
inline void InOutPort::setPullup( bool bPullup )
{
InOut::writePort( m_pvui8PORT, bPullup ? 0xFF : 0x00 );
}
//////////////////////////////////////////////////////////////////////////
inline uint8_t InOutPort::read()
{
return InOut::readPort( m_pvui8PIN );
}
//////////////////////////////////////////////////////////////////////////
inline void InOutPort::write( uint8_t ui8Value )
{
InOut::writePort( m_pvui8PORT, ui8Value );
}
#endif #endif