Correctly hangle regrd with count of 256 or greater.

[#28]
This commit is contained in:
James Bowman 2020-01-07 17:42:36 -08:00
parent cf5f80f3f3
commit f57ca82ee1
2 changed files with 19 additions and 6 deletions

View File

@ -267,7 +267,7 @@ class I2CDriver:
:param dev: 7-bit I2C device address
:param reg: register address 0-255
:param fmt: :py:func:`struct.unpack` format string for the register contents
:param fmt: :py:func:`struct.unpack` format string for the register contents, or an integer byte count
If device 0x75 has a 16-bit register 102, it can be read with:
@ -276,17 +276,23 @@ class I2CDriver:
"""
if isinstance(fmt, str):
n = struct.calcsize(fmt)
self.__ser_w(b'r' + struct.pack("BBB", dev, reg, n))
r = struct.unpack(fmt, self.ser.read(n))
r = struct.unpack(fmt, self.regrd(dev, reg, struct.calcsize(fmt)))
if len(r) == 1:
return r[0]
else:
return r
else:
n = fmt
self.__ser_w(b'r' + struct.pack("BBB", dev, reg, n))
if n <= 256:
self.__ser_w(b'r' + struct.pack("BBB", dev, reg, n & 0xff))
return self.ser.read(n)
else:
self.start(dev, 0)
self.write([reg])
self.start(dev, 1)
r = self.read(n)
self.stop()
return r
def regwr(self, dev, reg, vv):
"""Write a device's register.

View File

@ -111,6 +111,13 @@ class TestDUT(unittest.TestCase):
self.assertEqual(self.lm75_read(i2, 3), 0x5000)
self.stacksame()
def test_regrd256(self):
i2 = self.i2
reg = 3
self.lm75_write(i2, reg, 0x7480)
for n in (127, 128, 129):
self.assertEqual(i2.regrd(0x48, reg, ">" + str(n) + "h"), (0x7480,) * n)
def test_setspeed(self):
i2 = self.init()
self.stack0()