/** \file utilities.h ********************************************************
 * 
 *             Project: Maxim Plug-in Peripheral Modules
 *            Filename: utilities.h
 *         Description: This module contains a collection of general utility
 *                      functions which are not specific to any particular
 *                      module.
 * 
 *    Revision History:
 *\n       4-13-12 Rev 1.0 Seth Messimer   Initial Release
 *\n       7-20-12 Rev 1.4 Nathan Young    Additional functions
 *\n       9-04-12 Rev 1.5 Nathan Young    Multiple PMOD port support
  *\n       10-15-12 Rev 1.6 Nathan Young	OLED display support
 * 
 *  --------------------------------------------------------------------
 * 
 *    This code follows the following naming conventions.
 * 
 * 	  char                   chPmodValue
 * 	  char (array)           sPmodString[16]
 * 	  float                  fPmodValue
 * 	  int                    nPmodValue
 * 	  int (array)            anPmodValue[16]
 * 	  u16                    uPmodValue
 * 	  u8                     uchPmodValue
 * 	  u8 (array)             auchPmodBuffer[16]
 * 	  unsigned int           unPmodValue
 * 	  int *                  punPmodValue
 * 
 * ------------------------------------------------------------------------- */
/*
 * Copyright (C) 2012 Maxim Integrated Products, All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY,  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL MAXIM INTEGRATED PRODUCTS BE LIABLE FOR ANY CLAIM, DAMAGES
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of Maxim Integrated Products
 * shall not be used except as stated in the Maxim Integrated Products
 * Branding Policy.
 *
 * The mere transfer of this software does not imply any licenses
 * of trade secrets, proprietary technology, copyrights, patents,
 * trademarks, maskwork rights, or any other form of intellectual
 * property whatsoever. Maxim Integrated Products retains all ownership rights.
 *
 ***************************************************************************/

#ifndef UTILITIES_H_
#define UTILITIES_H_
#include "xbasic_types.h"   
#include "xspi_l.h"         
#include "stdio.h"          
#include "xiic_l.h"         
#include "xuartlite_i.h"    
#include "xparameters.h"    
#include "xgpio.h"          
#include "xgpio_l.h"        
#include "MAXREFDES4.h"
#endif /* UTILITIES_H_ */

#pragma once

//====================================================================================================
// Function:  SpiRW()
//
// @param unBaseAddr:  			Base address of SPI master.  Find it in "xparameters.h".
// @param unCPHA:				CPHA setting for SPI port.  See IC datasheet.
// @param unCPOL:				CPOL setting for SPI port.  See IC datasheet.
// @param auchWriteBuf:			Buffer containing write data for transfer.  Set to 0 if not writing data.
// @param auchReadBuf:			Buffer into which read data will be placed.  Set to 0 if not reading data.
// @param unNumBytes:			Number of bytes to be read/written.
// @param uchCsActiveHigh:		1 if CS is active high, 0 if CS is active low.
//
// @return:						0 if successful, nonzero otherwise.  Note -- error checking not completed.
//====================================================================================================
int SpiRW(u32 unPeripheralAddressSPI, unsigned int unCPHA, unsigned int unCPOL,
		u8* auchWriteBuf, u8* auchReadBuf, int unNumBytes, u8 uchCsActiveHigh );

void delay(int nStopValue);
void led_knight_rider(XGpio *pLED_GPIO, int nNumberOfTimes);
void max_configure_PMOD_port(u8 uchPmodPortA, u8 uchPmodPortB, u8 uchPmodPortC, u8 uchPmodPortD);
int check_if_i2c_device_is_present();
int number_raised_to_power(int nBase, int nExponent);
int receive_byte_with_timeout(u32 unUartAddress, int nTimeoutInTenthsOfSeconds, u8 *uchRxData);

void set_seven_segment_character(u8 uchDigitNumber, u8 uchValue, u8 uchDecimalActive);
void print_seven_segment_number(float fNumber);
struct maximDateTime *t;
void print_seven_segment_time(struct maximDateTime *t);
int GetLine( char* sInputString, unsigned int unMaxSize );
void print_asterisks(int nQuantity);
void printf_temp(float fTemp, u8 uchPrintCelsius, u8 uchAddCarriageReturn);
void print_seven_segment_temperature(float temp, u8 uchPrintCelsius);
void print_seven_segment_four_digits(char *puchArray);
void increment_active_PMOD_Port();
void max_set_PMOD_port(int nPortNumber, u8 uchPortType);
u8 getUartByte(u32 nUartAddress);
void sendUartByte(u32 unUartAddress, u8 uchByte);
u8 checkUartEmpty(u32 unUartAddress);
void initializeOLED(u8 *pFont);
void sendOLEDSPI(u8 uchDataToWrite);
void clearOLEDBuffer(u8 *pauchBuffer);
void displayOLEDBuffer(u8 *pauchBuffer);
void putCharOLED(int x, int y, char chCharacter);
void printfToBufferOLED(int x, int y,char *chString);
void printfToOLED(int x, int y,char *chString);
void flipAndCopyDisplayBuffer(u8 *pauchSourceBuffer, u8 *pauchDestinationBuffer);
void printOLED_31855(float fInternalTemp,float fProbeTemp, int displayCelsius);
void printOLED_31723(float fTemp);
void printOLED_3231M(struct maximDateTime t, float fTemp);
void printOLED_44000Lux(float fLuxReading);
void printOLED_44000Prox(float fProxReading);

