//c - c++
#include <unistd.h>

//this
#include "SerLEDStatusFlash.H"

void SerLEDStatusFlash :: Activate()
	{
	_flashes=true;
//start thread
	if(pthread_create(&_flash_thread, NULL, (void *(*) (void *))&ThreadEntry, (void *)this)==0)
		{
		pthread_detach(_flash_thread);
		pthread_setschedparam(_flash_thread, SCHED_RR, 0);		
		}
	}


void SerLEDStatusFlash :: DeActivate()
	{
//flsahing thread will exit when _flashes is false
	_flashes=false;
//turn off led
	_switchlock.Lock();
	_device->Off(_led);
	_switchlock.Unlock();
	}

void * SerLEDStatusFlash :: ThreadEntry(SerLEDStatusFlash *cl)
	{
	while(1)
		{
		if(!cl->_flashes) return NULL;
		cl->_switchlock.Lock();
		cl->_device->On(cl->_led);
		cl->_switchlock.Unlock();
		usleep(cl->_on_time);
		if(!cl->_flashes) return NULL;
		cl->_switchlock.Lock();
		cl->_device->Off(cl->_led);
		cl->_switchlock.Unlock();
		usleep(cl->_off_time);
		}
	return NULL;
	}
