#include "LED.h"

LED::LED(int number)
{
	this->number = number;
	// much easier with C++11 using to_string(number)
	std::ostringstream s;    // using a stream to contruct the path
	s << LED_PATH << number;   //append LED number to LED_PATH
	path = std::string(s.str());    //convert back from stream to string
}

void LED::writeLED(std::string filename, std::string value)
{
	return;
#if 0
	std::ofstream fs;
	fs.open((path + filename).c_str());
	fs << value;
	fs.close();
#endif
}

void LED::removeTrigger()
{
	writeLED("/trigger", "none");
}

void LED::turnOn()
{
	using namespace std;
	cout << "Turning LED" << number << " on." << endl;
	removeTrigger();
	writeLED("/brightness", "1");
}

void LED::turnOff()
{
	using namespace std;
	cout << "Turning LED" << number << " off." << endl;
	removeTrigger();
	writeLED("/brightness", "0");
}

void LED::flash(std::string delayms = "50")
{
	using namespace std;
	cout << "Making LED" << number << " flash." << endl;
	writeLED("/trigger", "timer");
	writeLED("/delay_on", delayms);
	writeLED("/delay_off", delayms);
}

void LED::outputState()
{
	using namespace std;
	ifstream fs;
	fs.open((path + "/trigger").c_str());
	string line;
	while( getline(fs, line) ) cout << line << endl;
	fs.close();
}

LED::~LED()
{
	using namespace std;
	cout << "destroying the LED with path: " << path << endl;
}