

#include <unistd.h>
#include <cstdlib>
#include <fcntl.h>
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#include <string>
#include <iostream>
#include <stdint.h>


#define ADDRESS 0x10
#define DEVNAME "/dev/i2c-1"

#define START 0x00

#define LENGTH 6144

using namespace std;

void readblock( int I2CFile, int addr );

int main( int argc, char **argv )
{


	int I2CFile;
	
	if( ( I2CFile = open( DEVNAME, O_RDWR ) ) <= 0 )
	{	
		cout << "Cant open I2C driver on bus" << endl;
		return( 1 );
	}

	if( ioctl( I2CFile, I2C_SLAVE, ADDRESS ) < 0 )
	{
		cout << "Cant open I2C slave address" << endl;
		return( 1 );
	}
	
	// byte 0 is the high byte, byte 1 is the low byte of the address.

	for( int i = 0; i < LENGTH; i+=16 )
	{
		readblock( I2CFile, i );
	}


}		

void readblock( int I2CFile, int addr )
{
	char obuf[2] = { ( addr >> 8 ), addr };
	write( I2CFile, obuf, 2 );
	char inbuf[16];
	read( I2CFile, inbuf, 16 );

	cout << hex << addr << ": ";
	for( int i = 0; i < 4; i++ )
	{
		cout.width( 2 );
		cout.fill('0');
		cout << hex << (int)inbuf[ ( i * 4 ) ];
		cout.width( 2 );
		cout.fill('0');
		cout << hex << (int)inbuf[ ( i * 4 ) + 1];
		cout.width( 2 );
		cout.fill('0');
		cout << hex << (int)inbuf[ ( i * 4 ) + 2];
		cout.width( 2 );
		cout.fill('0');
		cout << hex << (int)inbuf[ ( i * 4 ) + 3] << " ";


	}
	cout << endl;
}
