/*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.
***************************************************************************/

// DOM-IGNORE-BEGIN
/*******************************************************************************

 *******************************************************************************/
// DOM-IGNORE-END


// *****************************************************************************
// *****************************************************************************
// Section: Included Files 
// *****************************************************************************
// *****************************************************************************
#include <xc.h>
#include "misc.h"
#include "../mcc_generated_files/pin_manager.h"
#include "userTemp.h"

// *****************************************************************************
// *****************************************************************************
// Section: Global Data Definitions
// *****************************************************************************
// *****************************************************************************

// *****************************************************************************

static uint32_t delayTempCheck;


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

    return false;
}

void DelayReset(uint32_t *DelayCounter)
{
    *DelayCounter = _CP0_GET_COUNT();
}



bool User_TempCheck(uint8_t * temp)
{
    
    if (HasDelayElapsed(12000*500,&delayTempCheck))
    {
        *temp = User_ReadTemperature();
        return true;
    }
    return false;
}



void User_TempCheckInit(void)
{
    DelayReset(&delayTempCheck);
}



/*******************************************************************************
 End of File
 */
