Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions src/edgepi/peripherals/i2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
I2CDevice
"""


from typing import Union
from periphery import I2C

class I2CDevice():
Expand All @@ -16,27 +16,33 @@ def __init__(self, fd: str = None):
self.fd = fd
self.i2cdev = I2C(fd)

def set_read_msg(self, addr:int = None, msg:list = None):
def set_read_msg(self, addr:Union[int,list] = None, msg:list = None):
'''
set Read message to be sent through I2C.
Attributes:
addr: Register address to read from
addr(int or list): Register address to read from
Msg: list of place holder bytes
Return:
MsgList: list of I2C.Message() objects containing msg to be sent
'''
return [self.i2cdev.Message([addr], read = False), self.i2cdev.Message(msg, read = True)]
list_msg = [self.i2cdev.Message([addr], read = False),
self.i2cdev.Message(msg, read = True)] \
if isinstance(addr, int) else \
[self.i2cdev.Message(addr, read = False),
self.i2cdev.Message(msg, read = True)]
return list_msg

def set_write_msg(self, addr:int = None, msg:list = None):
def set_write_msg(self, addr:Union[int,list] = None, msg:list = None):
'''
set Write message to be sent through I2C.
Attributes:
addr: Register address to write to
addr(int or list): Register address to write to
Msg: list of Msg bytes
Return:
MsgList: list of I2C.Message() objects containing msg to be sent
'''
list_msg = [self.i2cdev.Message([addr]+msg, read = False)]
list_msg = [self.i2cdev.Message([addr]+msg, read = False)] if isinstance(addr, int) else \
[self.i2cdev.Message(addr+msg, read = False)]
return list_msg

def transfer(self, dev_addr: int = None, msg:list = None):
Expand Down
6 changes: 4 additions & 2 deletions src/test_edgepi/unit_tests/test_peripherals/test_i2c.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def test_i2c_init_param(i2c_mock, fd, mock_expects):
[( 30, [31, 32], [[30], False, [31, 32], True]),
( 20, [21, 22], [[20], False, [21, 22], True]),
( 10, [11, 12], [[10], False, [11, 12], True]),
( 0, [1, 2], [[0], False, [1, 2], True])
( 0, [1, 2], [[0], False, [1, 2], True]),
( [1,2], [3, 4], [[1,2], False, [3, 4], True])
])
@patch('edgepi.peripherals.i2c.I2C')
def test_i2c_set_read_msg(i2c_mock, addrs, msg, result):
Expand All @@ -47,7 +48,8 @@ def test_i2c_set_read_msg(i2c_mock, addrs, msg, result):
[( 30, [31, 32], [[30, 31, 32], False]),
( 20, [21, 22], [[20, 21, 22], False]),
( 10, [11, 12], [[10, 11, 12], False]),
( 0, [1, 2], [[0, 1, 2], False])
( 0, [1, 2], [[0, 1, 2], False]),
( [3,4], [1, 2], [[3, 4, 1, 2], False])
])
@patch('edgepi.peripherals.i2c.I2C')
def test_i2c_set_write_msg(i2c_mock, addrs, msg, result):
Expand Down