#include <xc.h>
#include "mcc_generated_files/mcc.h"
#include "light.h"
#include "FLash.h"
#include "HEFLash.h"
//------------------------------------------------------------------------------
void main (void)
{
    uint16_t rnd;
    uint16_t add1,add2,add3;
    uint8_t lightup_counter = 0;
    uint8_t charg_volt_counter = 0;
    uint8_t lobat_counter = 0;
    uint8_t ref_timer = 0;
    uint16_t voltage,voltage_old,voltage_led;
    uint16_t x;
    int16_t sample_ref;
    uint8_t charging_flag = 0;
    uint8_t charge_state = 0;
    uint8_t charge_state_old = 0;
    uint16_t litups;
    uint16_t chargings;
    uint8_t easter_egg = 0;
    void (*color)(uint16_t);
    
    SYSTEM_Initialize();
    VREGCONbits.VREGPM = 1;
    WDTCONbits.SWDTEN = 1;
    SLEEP();
    litups = FLASH_read(0x03F0);
    chargings = FLASH_read(0x03F1);
    TEST_SetDigitalOutput();
    TEST_SetLow();
    sample_ref = ADC_GetConversion(1);
    TEST_SetDigitalInput();
    //**************************************************************************
    while(1)
    {   
        WDTCONbits.SWDTEN = 1;
        SLEEP();
        WDTCONbits.SWDTEN = 0;
        ref_timer+=1;
        //----------------------------------------------------------------------
        //enable fvr
        FVRCONbits.FVREN = 1;
        voltage = ADC_GetConversion(0x1F);
        FVRCONbits.FVREN = 0;
        TEST_SetDigitalOutput();
        TEST_SetLow();
        voltage_led = ADC_GetConversion(1);
        if(charging_flag==1)
            TEST_SetHigh();
        else
            TEST_SetDigitalInput();
        if(voltage<240)
        {
            TEST_SetDigitalInput();
            charge_state = TEST_GetValue();
            TEST_SetDigitalOutput();
            TEST_SetHigh();
            if(charge_state!=charge_state_old)
            {
                ref_timer=190;
                lightup_counter=0;
                charge_state_old = charge_state;
                continue;
            }
            charge_state_old = charge_state;
            if(charging_flag==0)
            {
                charg_volt_counter+=1;
                if(charg_volt_counter>5)
                {
                    charging_flag = 1;
                    chargings += 1;
                    pwm_on();
                    if(charge_state==0)
                        color = red;
                    else
                        color = green;
                    for(rnd=100;rnd>0;rnd--)
                    {
                        color(rnd);
                        __delay_ms(8);
                    }
                    color(0);
                    pwm_off();
                    lightup_counter = 0;
                    ref_timer = 190;
                }
            }
        }
        else
        {
            charg_volt_counter = 0;
            charging_flag = 0;
            if(voltage>350)
                lobat_counter+=1;
            if(lobat_counter>100)
            {
                pwm_on();
                green(0);
                blue(0);
                red(50);
                __delay_ms(1000);
                pwm_off();
                SLEEP();
            }          
        }
        if(ref_timer>=190)
        {
            sample_ref = voltage_led;
            ref_timer = 0;
            lightup_counter = 0;
        }
        if(voltage_led>=(sample_ref+4))
            lightup_counter+=1;
        else 
            lightup_counter=0;
        if(lightup_counter>10)
            break;        
    }
    //--------------------------------------------------------------------------
    CLRWDT();
    WDTCONbits.SWDTEN = 0;
    litups+=1;
    litups &= 0x3FFF;
    chargings &= 0x3FFF;
    FLASH_erase(0x03F0);
    FLASH_write(0x03F0,litups,1);
    FLASH_write(0x03F1,chargings,0);
    if((litups&0x007F)==0)
        easter_egg = 1;        
    TEST_SetDigitalInput();
    lfsr(sample_ref-2);
    pwm_on();
    //--------------------------------------------------------------------------
    for(rnd=0;rnd<250;rnd++)
    {
        if(easter_egg==0)
        {
            red(rnd+(rnd<<1));
            green(rnd);
            blue(rnd>>3);
        }
        else
        {
            blue(rnd<<1);
            red(rnd);
            green(rnd>>4);
        }        
        __delay_ms(10);
    }
    while(1)
    {
        while(!PIR1bits.TMR1IF);
        PIR1bits.TMR1IF = 0;
        rnd = lfsr(0);
        add1 = (rnd>>2);
        add2 = (rnd>>3);
        add3 = rnd&0x0003;
        if(easter_egg==0)
        {
            red(765+add1);
            green(210+add2);
            blue(28+add3);
        }
        else
        {
            blue(500+add1);
            red(250+add2);
            green(16+add3);
        }
        FVRCONbits.FVREN = 1;
        voltage = ADC_GetConversion(0x1F);
        FVRCONbits.FVREN = 0;
        if(voltage>350)
            lobat_counter+=1;
        else
            lobat_counter=0;
        if(lobat_counter>100)
        {
            green(0);
            blue(0);
            red(50);
            __delay_ms(1000);
            SLEEP();
        }
    }
}