#include "Led.h"

// Constructor
Led::Led(PinName pin)
    : led(pin)
{
    led.period_ms(10);
    led = 0;
};

// Destructor
Led::~Led()
{
};

void Led::set(bool s)
{
    if(s)
        led = 1;
    else
        led = 0;
};

void Led::setDuty(float duty)
{
    led.write(duty);
};

float Led::getDuty()
{
    return led.read();  // Return the current output duty-cycle setting
}

void Led::graduallyOn()
{
    for(float i=0 ; i<=1; i=i+0.01)
    {
        this->setDuty(i);
        ThisThread::sleep_for(20ms);
    }
    led = 0;
};

PwmOut* Led::getPwmOut()
{
    return &led;
};

void Led::tickerHandler(void)
{
    led = !led;
};

void Led::setBlinkPeriod(float period)
{
    if(period<=0)
    {
        ticker.detach();    // turn off ticker
//        set(false);
        this->set(false);   // turn off led
        return;
    }
 //    ticker.attach(callback(this, &Led::tickerHandler), period);   // deprecated
     auto float_interval = std::chrono::duration<float>(period);
     ticker.attach(callback(this, &Led::tickerHandler), std::chrono::duration_cast<std::chrono::microseconds>(float_interval));
}