2 # -*- coding: iso-8859-15 -*-
12 logger = logging.getLogger(__name__)
15 libc = CDLL("libc.so.6")
16 I2C_SLAVE = 0x0703 # Use this slave address
18 __lock = threading.Lock()
21 def __init__(self, addr):
25 parent = inspect.stack()[1][3]
30 parent_owner = "%s (%d), %s()" % (self.__parent_owner[1], self.__parent_owner[2], self.__parent_owner[3])
31 logger.warning("Error: (%s) I2C blocked %fs by %s!", parent, count*0.001, parent_owner)
32 i2c.__parent_owner = inspect.stack()[1]
34 self.dev = i2c.libc.open("/dev/i2c-2", os.O_RDWR)
37 err = i2c.libc.ioctl(self.dev, i2c.I2C_SLAVE, addr>>1)
39 raise IOError("ioctl")
42 num_write = i2c.libc.write(self.dev, s, len(s))
43 if num_write != len(s):
45 raise IOError("write: %d" % (num_write))
48 buf = create_string_buffer(num)
49 num_read = i2c.libc.read(self.dev, buf, num)
52 raise IOError("read: %d" % (num_read))
57 i2c.libc.close(self.dev)
59 #i2c.__parent_owner = None
65 if __name__ == "__main__":
70 s = struct.pack(">Bh", int(sys.argv[1]), int(sys.argv[2]))