/*DELAY FUNCTIONS
 * 
             LUFA Library
     Copyright (C) Dean Camera, 2012.

  dean [at] fourwalledcubicle [dot] com
           www.lufa-lib.org
*/

// simplified and adapted by AP 2014

//edited for architecture PIC32 by SR 2016

/*
  Copyright 2012  Dean Camera (dean [at] fourwalledcubicle [dot] com)

  Permission to use, copy, modify, distribute, and sell this
  software and its documentation for any purpose is hereby granted
  without fee, provided that the above copyright notice appear in
  all copies and that both that the copyright notice and this
  permission notice and warranty disclaimer appear in supporting
  documentation, and that the name of the author not be used in
  advertising or publicity pertaining to distribution of the
  software without specific, written prior permission.

  The author disclaim all warranties with regard to this
  software, including all implied warranties of merchantability
  and fitness.  In no event shall the author be liable for any
  special, indirect or consequential damages or any damages
  whatsoever resulting from loss of use, data or profits, whether
  in an action of contract, negligence or other tortious action,
  arising out of or in connection with the use or performance of
  this software.
***************************************************************************/

#include <stdint.h>
#include <stdbool.h>
#include <xc.h>


#include "misc.h"
#include "userComm.h"
#include "userGEN.h"
#include "userADC.h"
#include "../mcc_generated_files/pin_manager.h"

static GEN_STATUS status;
static uint8_t  temperature;
volatile static uint16_t counter1ms;

void GEN_UpdateStatus(void)
{
    status.voltage12Good = V12GOOD_GetValue();
    status.genOn = PTCONbits.PTEN;
    if (temperature > 100)
    {
        
        GEN_OverheatSet();                      //temperature of generator is greater then 100°C, stop generator
        GEN_Stop();
    }
    if(DRV_ReadCurrent() > 12000)               //peak current is greater than ~12A, stop generator
    {
        GEN_OverCurrentSet();
        GEN_Stop();
    }
    
}

void GEN_NotReady(void)
{
    status.genReady = 0;
}

void GEN_Ready(void)
{
    status.genReady = 1;
}

void GEN_OpAck(void)
{
    status.opAck = 1;
}

void GEN_OpNack(void)
{
    status.opAck = 0;
}

void GEN_OverheatSet(void)
{
    status.overHeat = 1;
}

void GEN_OverheatClear(void)
{
    status.overHeat = 0;
}

void GEN_OverCurrentSet(void)
{
    status.overCurrent = 1;
}

void GEN_OverCurrentClear(void)
{
    status.overCurrent = 0;
}


uint8_t GEN_GetStatusByte( void)
{
    return status.rawData;
}

void SPI2_CallbackTemp(uint8_t temp)
{
    temperature = temp;
}

uint8_t GEN_GetTemp(void)
{
    return temperature;
}

void TMR1_CallBack(void)
{
    counter1ms++;    
}

bool HasDelayElapsed(uint16_t Delay,uint16_t *DelayCounter)
{
    //static uint32_t DelayCounter;
    if (counter1ms >= *DelayCounter)
    {
        if ((counter1ms - *DelayCounter) >= Delay)
        {
            *DelayCounter = counter1ms;
            return true;
        }
    }
    else
    {
        if (((0xFFFF - *DelayCounter) + counter1ms) >= Delay)
        {
            *DelayCounter = counter1ms;
            return true;
        }
    }

    return false;
}

void DelayReset(uint16_t *DelayCounter)
{
    *DelayCounter = counter1ms;
}