#!/usr/bin/python

import sys
import rr
import time
import i2c


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

#	def wr_data(self, data_msb, data_lsb):
#		self.i2c.start(self.i2c_addr, True)	
#                self.i2c.write((data_msb & 0xFF), False)
#                self.i2c.write((data_lsb & 0xFF), True)
#		return 0;
		
	def wr_data(self,data):
		data_wr_msb = ((0xF00 & data) >> 8) 
    		data_wr_lsb = (0x0FF & data) 
		self.i2c.start(self.i2c_addr, True)	
                self.i2c.write((data_wr_msb & 0x0F), False)
                self.i2c.write((data_wr_lsb & 0xFF), True)
		return 0;
		
	def power_down(self):
		data_wr_msb = ((1 & 0xF)<<4) + ((0xF00 & 0) >> 8) 
    		data_wr_lsb = (0x0FF & 0) 
		self.i2c.start(self.i2c_addr, True)	
                self.i2c.write((data_wr_msb & 0xFF), False)
                self.i2c.write((data_wr_lsb & 0xFF), True)
		return "DAC powered down";
		
		

	def rd_data(self):
		self.i2c.start(self.i2c_addr, False)
                data = []
                #print('24AA64:read: data lenght=%d')%(size)
		data.append(self.i2c.read(False))
                data.append(self.i2c.read(True))
                data_return = (((data[0] <<8) + data[1])>>2)
                return data_return;
