#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( 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( 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 ); }