2 # -*- coding: iso-8859-15 -*-
10 from time import sleep
13 I2C_FILENAME = "/dev/i2c-2"
14 logger = logging.getLogger(__name__)
17 I2C_SLAVE = 0x0703 # Use this slave address
19 __lock = threading.Lock()
22 def __init__(self, addr):
26 parent = inspect.stack()[1][3]
31 parent_owner = "%s (%d), %s()" % (self.__parent_owner[1], self.__parent_owner[2], self.__parent_owner[3])
32 logger.warning("Error: (%s) I2C blocked %fs by %s!", parent, count*0.001, parent_owner)
33 i2c.__parent_owner = inspect.stack()[1]
35 self.dev = os.open(I2C_FILENAME, os.O_RDWR)
38 fcntl.flock(self.dev, fcntl.LOCK_EX)
39 fcntl.ioctl(self.dev, i2c.I2C_SLAVE, addr>>1)
42 num_write = os.write(self.dev, s)
43 if num_write != len(s):
45 raise IOError("write: %d" % (num_write))
48 buf = os.read(self.dev, num)
51 raise IOError("read: %d" % (len(buf)))
64 def i2c_write_reg(addr, reg, buf):
66 s = struct.pack(">B", reg) + buf
71 def i2c_read_reg(addr, reg, num=1):
73 s = struct.pack(">B", reg)
80 if __name__ == "__main__":
84 s = struct.pack(">Bh", int(sys.argv[1]), int(sys.argv[2]))