2 # -*- coding: iso-8859-15 -*-
10 from time import sleep
13 logger = logging.getLogger(__name__)
16 libc = CDLL("libc.so.6")
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 = i2c.libc.open("/dev/i2c-2", os.O_RDWR)
38 err = i2c.libc.ioctl(self.dev, i2c.I2C_SLAVE, addr>>1)
40 raise IOError("ioctl")
43 num_write = i2c.libc.write(self.dev, s, len(s))
44 if num_write != len(s):
46 raise IOError("write: %d" % (num_write))
49 buf = create_string_buffer(num)
50 num_read = i2c.libc.read(self.dev, buf, num)
53 raise IOError("read: %d" % (num_read))
58 i2c.libc.close(self.dev)
60 #i2c.__parent_owner = None
67 def i2c_write_reg(addr, reg, buf):
69 s = struct.pack(">B", reg) + buf
74 def i2c_read_reg(addr, reg, num=1):
76 s = struct.pack(">B", reg)
83 if __name__ == "__main__":
88 s = struct.pack(">Bh", int(sys.argv[1]), int(sys.argv[2]))