#!/usr/bin/python

import sys
import rr
import time
import i2c


class C24AA64:
	def __init__(self, i2c, i2c_addr):
		self.i2c = i2c
		self.i2c_addr = i2c_addr

	def wr_data(self, mem_addr, data):
		i=0
		self.i2c.start(self.i2c_addr, True)
                self.i2c.write((mem_addr >> 8), False)
                self.i2c.write((mem_addr & 0xFF), False)
                #print('24AA64:write: data lenght=%d')%(len(data))
		for i in range(len(data)-1):
                        #print('24AA64:write: i=%d')%(i)
                        print data[i]
                        self.i2c.write(data[i],False)
                i += 1
                #print('24AA64:write:last i=%d')%(i)
                if len(data)==1:
                	self.i2c.write(data[0],True)
                	print data[0]
                	return 0
        
                self.i2c.write(data[i],True)
                print data[i]
		return 0;

	def rd_data(self, mem_addr, size):
		self.i2c.start(self.i2c_addr, True)
		self.i2c.write((mem_addr >> 8), False)
                self.i2c.write((mem_addr & 0xFF), False)
		self.i2c.start(self.i2c_addr, False)
                
                data = []
                #print('24AA64:read: data lenght=%d')%(size)
		for i in range(size-1):
                        data.append(self.i2c.read(False))
                        #print('24AA64:read: i=%d')%(i)
                i += 1
                #print('24AA64:read:last i=%d')%(i)
                data.append(self.i2c.read(True))
		return data;
	
	def dump_file_to_eeprom(self,filename):
		eeprom_content = []
		eeprom_addr = []
		
		print filename
		f = open(filename,"r+")
		for line in f:
			addr,data=line.split()
			eeprom_content=(int(data,2)& 0xFF)
			eeprom_addr=(int(addr,2))
			self.wr_data(eeprom_addr,[eeprom_content])
			time.sleep(.01)
		return 0
		
		
		
		
		
		
