/*
 *  rppic example c code for the Raspberry pi
 * 
 * 
 * Address 0x08 ( 0x10 /2 )
 * Original IIC code By James Henderson, 2012.
 * https://www.robot-electronics.co.uk/
 * 
 * modified by Doug Rice
 * used to debug IIC code on PIC16F886.
 * 
 * Enable i2c on the raspberry pi using the GUI. 
 * 
 * This is no longer needed if you enable i2c on the Raspberry Pi using the GUI. 
 * 
 * comment out i2c in black list in /etc/modprobe.d and reboot 
 * 
 * then run:
 * 
 *   sudo modprobe i2c-dev
 *   sudo chmod o+rw /dev/ic*
 * 
 * Build using:
 * gcc rppic.c 
 * 
 * run using:
 * ./a.out
 * 
 * pi@raspberrypi:~/Desktop $ ./a.out
**** RPPIC test program ****
  read count 00,01,02 
 read count 03,04,05 
 read count 06,07,08 
 read count 09,0A,0B 
 read count 0C,0D,0E 
 read count 0F,10,11 

*/

/*
*******************************************************************************
SEC 1.0 - includes
*******************************************************************************
*/

#include <stdio.h>
#include <stdlib.h>
#include <linux/i2c-dev.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

/*
*******************************************************************************
SEC 2.0 - variables
*******************************************************************************
*/
	int read_ip;
	int fd;
	int fd2;
	int count;
								// File descrition
	//char *fileName = "/dev/i2c-0";
	// I needed to change the device
	char *fileName = "/dev/i2c-1";


/*
*******************************************************************************
SEC 3.0 - ProtoTypes
*******************************************************************************
*/
int iis_start( int address );
int iis_close(  );
int disp_buff( char * buff );
 
/*
*******************************************************************************
SEC 4.0 - functions
*******************************************************************************
*/

int iis_start( int address ){
  	  if ((fd = open(fileName, O_RDWR)) < 0) {					// Open port for reading and writing
		printf("Failed to open i2c port\n");
                printf(" run: \n");
                printf(" sudo modprobe i2c-dev  \n");
                printf(" sudo chmod o+rw /dev/i2c* \n");
		exit(1);
	  }
	  if (ioctl(fd, I2C_SLAVE, address) < 0) {					// Set the port options and set the address of the device we wish to speak to
		printf("Unable to get bus access to talk to slave\n");
		exit(1);
	  }
}

int iis_close(  ){
  close(fd );
}


/*
*******************************************************************************
SEC 10.0 - main
*******************************************************************************
*/

int main(int argc, char **argv)
{
	char buff[100];
    int  buffIndex;
    int  readCnt;

    char ipChar;
	int  count;
	int keys;
	int lastKeys;
	int events;

	printf("**** RPPIC test program ****\n");

	printf( " " );
	buff[0] = 0x00;
	buff[1] = 0x00;
	int  address = 0x08;
//while(1){
	count++;


	count =0xff;
	buff[0] =count;
	buff[1] =count;



	iis_start( address   );
	write(fd,&buff[0], 1);
	iis_close( address   );
	usleep(10000);
//}


while(1){
        buff[0] = 0x08;
 	buff[1] = 0x04;
	buff[2] = 0x02;
	buff[3] = 0x11;
  	buff[4] = 0x00;

	for( count = 0 ; count < 3 ; count++){
		
	  iis_start( address   );
          usleep(1000);
	  write(fd, &buff[0], 1 );
          usleep(1000);
	  iis_close( address   );
	  usleep(5000);
	
          iis_start( address   );
          usleep(1000);
	  write(fd, &buff[1], 1 );
          usleep(1000);
	  iis_close( address   );
	  usleep(5000);

	  iis_start( address   );
          usleep(1000);
	  write(fd, &buff[2], 1 );
          usleep(1000);
	  iis_close( address   );
	  usleep(5000);

	  printf(" read count",count );	

 	  iis_start( address );
	  read(fd,&buff[8],3);
	  iis_close( address );
	  usleep(100);
	  printf(" %02X,%02X,%02X \n", buff[8], buff[9], buff[10]    ); 
	  usleep(1000);

    }	       

  }

}
