Skip to content

Commit b691b2c

Browse files
Move implementation from __i2c_rdwr__() function in Micropython/CircuitPython to write_read_block()
1 parent c0be657 commit b691b2c

File tree

2 files changed

+19
-53
lines changed

2 files changed

+19
-53
lines changed

qwiic_i2c/circuitpy_i2c.py

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,21 @@ def write_block(self, address, commandCode, value):
300300
return self.writeBlock(address, commandCode, value)
301301

302302
def writeReadBlock(self, address, writeBytes, readNBytes):
303-
return self.__i2c_rdwr__(address, writeBytes, readNBytes)
303+
read_buffer = bytearray(readNBytes)
304+
305+
if not self._i2cbus.try_lock():
306+
raise Exception("Unable to lock I2C bus")
307+
308+
try:
309+
self._i2cbus.writeto_then_readfrom(address, bytes(writeBytes), read_buffer)
310+
except Exception as e:
311+
self._i2cbus.unlock()
312+
raise e
313+
else:
314+
self._i2cbus.unlock()
315+
316+
return list(read_buffer)
317+
304318

305319
def write_read_block(self, address, writeBytes, readNBytes):
306320
return self.writeReadBlock(address, writeBytes, readNBytes)
@@ -347,33 +361,4 @@ def scan(self):
347361
self._i2cbus.unlock()
348362

349363
return devices
350-
351-
#-----------------------------------------------------------------------
352-
# Custom method for reading +8-bit register using `i2c_msg` from `smbus2`
353-
#
354-
# Designed to have same operation as the __i2c_rdwr method in linux_i2c.py
355-
def __i2c_rdwr__(self, address, write_message, read_nbytes):
356-
"""
357-
Custom method used for 16-bit (or greater) register reads
358-
:param address: 7-bit address
359-
:param write_message: list with register(s) to read
360-
:param read_nbytes: number of bytes to be read
361-
362-
:return: response of read transaction
363-
:rtype: list
364-
"""
365-
read_buffer = bytearray(read_nbytes)
366-
367-
if not self._i2cbus.try_lock():
368-
raise Exception("Unable to lock I2C bus")
369-
370-
try:
371-
self._i2cbus.writeto_then_readfrom(address, bytes(write_message), read_buffer)
372-
except Exception as e:
373-
self._i2cbus.unlock()
374-
raise e
375-
else:
376-
self._i2cbus.unlock()
377-
378-
return list(read_buffer)
379-
364+

qwiic_i2c/micropython_i2c.py

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,9 @@ def write_block(self, address, commandCode, value):
171171
return self.writeBlock(address, commandCode, value)
172172

173173
def writeReadBlock(self, address, writeBytes, readNBytes):
174-
return self.__i2c_rdwr__(address, writeBytes, readNBytes)
174+
# micropython I2C doesn't have a corresponding "i2c_rdwr" function like smbus2, so we will make our own by passing stop=False to not send stop bits between repeated transfers
175+
self._i2cbus.writeto(address, bytes(writeBytes), False)
176+
return self._i2cbus.readfrom(address, readNBytes)
175177

176178
def write_read_block(self, address, writeBytes, readNBytes):
177179
return self.writeReadBlock(address, writeBytes, readNBytes)
@@ -198,24 +200,3 @@ def ping(self, devAddress):
198200
def scan(self):
199201
""" Returns a list of addresses for the devices connected to the I2C bus."""
200202
return self._i2cbus.scan()
201-
202-
#-----------------------------------------------------------------------
203-
# Custom method for reading +8-bit register using `i2c_msg` from `smbus2`
204-
#
205-
# Designed to have same operation as the __i2c_rdwr method in linux_i2c.py
206-
def __i2c_rdwr__(self, address, write_message, read_nbytes):
207-
"""
208-
Custom method used for 16-bit (or greater) register reads
209-
:param address: 7-bit address
210-
:param write_message: list with register(s) to read
211-
:param read_nbytes: number of bytes to be read
212-
213-
:return: response of read transaction
214-
:rtype: list
215-
"""
216-
217-
# micropython I2C doesn't have a corresponding "i2c_rdwr" function like smbus2, so we will make our own by passing stop=False to not send stop bits between repeated transfers
218-
self._i2cbus.writeto(address, bytes(write_message), False)
219-
return self._i2cbus.readfrom(address, read_nbytes)
220-
221-

0 commit comments

Comments
 (0)