#include <stdio.h>
#include <linux/i2c-dev.h>
#include <fcntl.h>

#define ADDRESS 0x05

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


	int fd;
        fd = open( "/dev/i2c-245", O_RDWR );
	if(fd < 0) {
		perror("Error opening i2c bus");
	}

	if( ioctl( fd, I2C_SLAVE, ADDRESS ) < 0 ) {
                fprintf( stderr, "Failed to set slave address: %m\n" );
                return 2;
        }


	/*
	if( i2c_smbus_write_byte( fd, 0x00 ) < 0 )
        	fprintf( stderr, "Failed to write 0x00 to I2C device: %m\n" );
	if( i2c_smbus_write_byte( fd, 0x00 ) < 0 )
        	fprintf( stderr, "Failed to write 0x00 to I2C device: %m\n" );
	if( i2c_smbus_write_byte( fd, 0x00 ) < 0 )
        	fprintf( stderr, "Failed to write 0x00 to I2C device: %m\n" );
	*/

	int foo = i2c_smbus_read_byte_data( fd, 13);
	if(foo < 0) {
		fprintf(stderr, "failed to read from cmd 13\n");
		perror("read: ");
	} else {
		printf("read: %x\n",foo);
	}

	int bar = i2c_smbus_read_byte_data( fd, 14);
	if(bar < 0) {
		fprintf(stderr, "failed to read from cmd 14\n");
		perror("read: ");
	} else {
		printf("read: %x\n",bar);
	}

	int adcBat = (bar << 8) | (foo);
	printf("Light Left: %d\n",adcBat);


	close(fd);

	return 0;

}
