#include "Relay.h"
//
CRelay::CRelay(int pin, EStateRelay initstate, bool inverted)
{
    FPin = pin;
    pinMode(FPin, INPUT);
    FInverted = inverted;
    FState = initstate;
}
void CRelay::Open(EStateRelay initstate)
{
    FState = initstate;
    pinMode(FPin, OUTPUT);
    SetState(FState);
}
void CRelay::Close(void)
{
    pinMode(FPin, INPUT);
}
//
EStateRelay CRelay::GetState(void)
{
    return FState;
}
void CRelay::SetState(EStateRelay state)
{
    switch (state)
    {
        case srlOff:
            SetOff();
            break;
        case srlOn:
            SetOn();
            break;
        default:
            SetOff();
            break;
    }
}
//
void CRelay::SetOn(void)
{
    if (FInverted)
    { // Inverted
        digitalWrite(FPin, LOW);        
    }
    else
    { // Normal
        digitalWrite(FPin, HIGH);
    }
    FState = srlOn;
}
void CRelay::SetOff(void)
{
    if (FInverted)
    { // Inverted
        digitalWrite(FPin, HIGH);
    }
    else
    { // Normal
        digitalWrite(FPin, LOW);        
    }
    FState = srlOff;
}
void CRelay::Toggle(void)
{
    switch (FState)
    {
        case srlOff:
            SetOn();
            break;
        case srlOn:
            SetOff();
            break;
        default:
            SetOff();
            break;
    }
}
//
const char* CRelay::GetStateText(void)
{
    switch (FState)
    {
        case srlOn:
            return "On";
        default:
            return "Off";
    }
}
//