#include "light.h"
#include "mcc_generated_files/mcc.h"
//------------------------------------------------------------------------------
void red (uint16_t dc)
{
    PWM1_LoadDutyValue(dc);
}
//------------------------------------------------------------------------------
void green (uint16_t dc)
{
    PWM2_LoadDutyValue(dc);
}
//------------------------------------------------------------------------------
void blue (uint16_t dc)
{
    PWM3_LoadDutyValue(dc);
}
//------------------------------------------------------------------------------
//-----------------------------  
//10-bitovy Fibonacciho LFSR pre generovanie pseudonahodnych hodnot PWM duty cycle
//pri prvom volani funkcie je potrebne zadat pociatocnu nenulovu hodnotu (seed) - funkcia uz teraz vrati pseudonahodne cislo
//pri kazdom dalsom volani treba pre spravnu funkciu zadat do parametra 0
//XOR-ovane su 10-ty a 7-my bit (maximum length - overene)
uint16_t lfsr (uint16_t seed)
{
	static uint16_t sr;
	uint16_t x1,x2,x3;
	
	//testuje ci ide o inicializaciu alebo o generovanie cisla
	//upravuje pripadny seed na 10-bitove cislo
	seed &= 0x03FF;
	if(sr == 0)
	{
		if(seed == 0)
			return 0;
		else
			sr = seed;
	}
	
	x1 = sr & 0x0200;
	x2 = (sr & 0x0040)<<3;
	x3 = x1 ^ x2;
	sr = sr<<1;
	sr = sr & 0x03FF;
	sr = sr | (x3>>9);	

	return sr;
}
//------------------------------------------------------------------------------
void pwm_on(void)
{
    PWM1CONbits.PWM1EN = 1;
    PWM2CONbits.PWM2EN = 1;
    PWM3CONbits.PWM3EN = 1;
    T2CONbits.TMR2ON = 1;
    T1CONbits.TMR1ON = 1;
}
//------------------------------------------------------------------------------
void pwm_off(void)
{
    PWM1CONbits.PWM1EN = 0;
    PWM2CONbits.PWM2EN = 0;
    PWM3CONbits.PWM3EN = 0;
    T2CONbits.TMR2ON = 0;
    T1CONbits.TMR1ON = 0;
}