#include <stdio.h>
#include <stdlib.h>
//#include <getopt.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
//#include <signal.h>
#include <unistd.h>
//#include <sys/timeb.h>

#include "i2c-dev.h"
#include "i2c-api.h"
#include "i2c-io-api.h"
//#include "Log.h"

//#include "svn-version.h"

// ---- Public Variables ----------------------------------------------------
// ---- Private Constants and Types -----------------------------------------

// ---- Private Variables ---------------------------------------------------

int gI2cAddr    = -1;

#define TRUE    1
#define FALSE   0

// ---- Private Function Prototypes -----------------------------------------

static  int ProcessReadByteCommand( int i2cDev, const uint8_t addr );
static  void ProcessWriteByteCommand( int i2cDev, const uint8_t addr, const uint8_t data );
static  void ProcessRecvByteCommand( int i2cDev );
static  void ProcessSendByteCommand( int i2cDev, const char *dataStr );
static  void Usage( void );

// ---- Functions -----------------------------------------------------------

//***************************************************************************
/**
*   Main entry point
*/

int main( int argc, char **argv )
{
    const char         *i2cDevName = "/dev/i2c-0";
    int                 i2cDev;
    struct timespec r;
    gI2cAddr = strtol("0x60",NULL,0);
    uint8_t bufferRead[2];
    uint8_t bufferWrite = 2;

    // Try to open the i2c device
    if (( i2cDev = open( i2cDevName, O_RDWR )) < 0 )
    {
        printf( "Error  opening '%s': %s\n", i2cDevName, strerror( errno ));
        exit( 1 );
    }

    // Indicate which slave we wish to speak to
//    I2cSetSlaveAddress( i2cDev, gI2cAddr, I2C_NO_CRC );
    ioctl(i2cDev, I2C_SLAVE, gI2cAddr);

    float value;

while(1)
{
    usleep(65000);

//    value = ProcessReadByteCommand( i2cDev, 2);
    write(i2cDev, &bufferWrite, 1);
    read(i2cDev, bufferRead, 2);
    value = bufferRead[0]*256 + bufferRead[1];

    value = value/10.0;

    printf("%.1f deg\n", value);
}
    close( i2cDev );

    return 0;

} // main

//***************************************************************************
/**
*   Issues a read byte command to read a byte from a particular address.
*/

int ProcessReadByteCommand( int i2cDev, const uint8_t addr )
{
    uint8_t dataByte[ I2C_MAX_DATA_LEN ];
    int     rc;
//    int     i;

    if (( rc = I2cReadBytes( i2cDev, addr, dataByte, 2 )) != 0 )
    {
        printf( "I2cReadByte failed: %d\n", rc );
        return -1;
    }

    return dataByte[0] * 256 + dataByte[1];

//    Log( "0x", dataByte[0] );

/*    for ( i = 0; i < gByteCount; i++ ) 
    {
        Log( "%02x", dataByte[i] );
    }
    Log( "\n" );
*/
} // ProcessReadByteCommand

//***************************************************************************
/**
*   Issues a recv byte command to read bytes with no address.
*/

void ProcessRecvByteCommand( int i2cDev )
{
    uint8_t dataByte[ I2C_MAX_DATA_LEN ];
    int     rc;
    int     i;

    if (( rc = I2cReceiveBytes( i2cDev, dataByte, 1 )) != 0 )
    {
        printf( "I2cRecvBytes failed: %d\n", rc );
        return;
    }

//    Log( "0x", dataByte[0] );

    for ( i = 0; i < 1; i++ ) 
    {
//        Log( "%02x", dataByte[i] );
    }
//    Log( "\n" );

} // ProcessRecvByteCommand

//***************************************************************************
/**
*   Issues a write byte command to write a byte to a particular address.
*/

void ProcessWriteByteCommand( int i2cDev, const uint8_t addr, const uint8_t data )
{
    int     rc;
    uint8_t dataByte[ I2C_MAX_DATA_LEN ];
    uint8_t c=1;
    dataByte[0] = data;
    if (( rc = I2cWriteBytes( i2cDev, addr, dataByte, c )) != 0 )
    {
        printf( "I2cWriteBytes failed: %d\n", rc );
        return;
    }
} // ProcessWriteByteCommand

//***************************************************************************
/**
*   Issues a send byte command to write bytes with no address specified.
*/

void ProcessSendByteCommand( int i2cDev, const char *dataStr )
{
    uint8_t dataByte[ I2C_MAX_DATA_LEN ];
    uint8_t bytesParsed;
    int     rc;

    if (( rc = I2cSendBytes( i2cDev, dataByte, bytesParsed )) != 0 )
    {
        printf( "I2cSendBytes failed: %d\n", rc );
        return;
    }

} // ProcessSendByteCommand

//***************************************************************************
/**
*   Usage
*/

void Usage( void )
{
    fprintf( stderr, "Usage: i2c [options] i2c-addr cmd [cmd-arguments]\n" );
    fprintf( stderr, "Send I2C commands\n" );
    fprintf( stderr, "\n" );
    fprintf( stderr, "The following commands are supported:\n" );
    fprintf( stderr, "ReadByte addr         Retrieves byte(s) starting at the indicated address\n" );
    fprintf( stderr, "WriteByte addr data   Write byte(s) starting at the indicated address\n" );
    fprintf( stderr, "ReadByte2 addr        Retrieves two bytes from the indicated address\n" );
    fprintf( stderr, "WriteByte2 addr data  Writes two bytes into the indicated address\n" );
    fprintf( stderr, "ReadByte4 addr        Retrieves four bytes from the indicated address\n" );
    fprintf( stderr, "WriteByte4 addr data  Writes four bytes into the indicated address\n" );
    fprintf( stderr, "RecvByte              Retrieves byte(s)(no address specified)\n" );
    fprintf( stderr, "SendByte data         Writes byte(s)(no address specified)\n" );
    fprintf( stderr, "RecvByte2             Retrieves 2 bytes (no address specified)\n" );
    fprintf( stderr, "SendByte2 data        Writes 2 bytes(no address specified)\n" );
    fprintf( stderr, "RecvByte4             Retrieves 4 bytes (no address specified)\n" );
    fprintf( stderr, "SendByte4 data        Writes 4 bytes(no address specified)\n" );
    fprintf( stderr, "\n" );
    fprintf( stderr, "The above commands can be shortened to rb, wb, rb2, wb2, rb4, wb4, vb, sd, vb2 sb2 vb4, and sb4 \n" );
    fprintf( stderr, "respectively.\n" );
    fprintf( stderr, "\n" );
    fprintf( stderr, "The following options may be used:\n" );
    fprintf( stderr, "--count=n             Specifies how many bytes to read for ReadByte or RecvByte\n" );
    fprintf( stderr, "--version             Prints the SVN version of this program\n" );
    fprintf( stderr, "--verbose             Print additional information\n" );
    fprintf( stderr, "--help                Prints this information\n" );
}
