#include "Button.h"
//
CButton::CButton(int pin, bool inverted, long unsigned period)
{
    FPin = pin;
    pinMode(FPin, INPUT);
    FState = sbtUnpressed;
    FInverted = inverted;
    FTimeStart = 0L;
    FTimePeriod = period;
}
void CButton::Open()
{
    pinMode(FPin, INPUT);
}
void CButton::Close(void)
{
    pinMode(FPin, INPUT);
}
//
EStateButton CButton::ReadState(void)
{
    if (FInverted)
    { // Inverted 
        if (LOW == digitalRead(FPin))
        { // LOW (reset period!)
            FTimeStart = millis();
            FState = sbtPressed;
        } else
        { // HIGH
            if ((FTimeStart + FTimePeriod) <= millis())
            {
                FState = sbtUnpressed;
            }
        }        
    }
    else
    { // Normal
        if (LOW == digitalRead(FPin))
        {
            if ((FTimeStart + FTimePeriod) <= millis())
            {
                FState = sbtUnpressed;
            }
        } else
        { // HIGH (reset period!)
            FTimeStart = millis();
            FState = sbtPressed;
        }        
    }
    return FState;
}
//
const char* CButton::GetStateText(void)
{
    switch (FState)
    {
        case sbtPressed:
            return "Pressed";
        default:
            return "Unpressed";
    }
}
//
EStateButton CButton::Execute(void)
{
    return ReadState();
}
//
//---------------------------------------------------------
//
//---------------------------------------------------------
CButtonNormallyOpened::CButtonNormallyOpened(int pin)
: CButton(pin, false)
{
}

void CButtonNormallyOpened::Open(void)
{
}

void CButtonNormallyOpened::Close(void)
{

}
//
//---------------------------------------------------------
//
//---------------------------------------------------------
CButtonNormallyClosed::CButtonNormallyClosed(int pin)
: CButton(pin, true)
{
}

void CButtonNormallyClosed::Open(void)
{
}

void CButtonNormallyClosed::Close(void)
{

}
