Skip to content

Add read word data #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 15 additions & 10 deletions usmbus/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ class SMBus(I2C):
Hopefully this will allow you to run code that was targeted at
py-smbus unmodified on micropython.

Use it like you would the machine.I2C class:
Use it like you would the machine.I2C class:

import usmbus.SMBus

bus = SMBus(1, pins=('G15','G10'), baudrate=100000)
bus = SMBus(id=0, scl=machine.Pin(15), sda=machine.Pin(10), freq=100000)
bus.read_byte_data(addr, register)
... etc
"""
"""

def read_byte_data(self, addr, register):
""" Read a single byte from register of device at addr
Expand All @@ -45,11 +45,13 @@ def write_i2c_block_data(self, addr, register, data):
""" Write multiple bytes of data to register of device at addr
Returns None """
# writeto_mem() expects something it can treat as a buffer
if isinstance(data, int):
data = bytes([data])
if not isinstance(data, bytes):
if not isinstance(data, list):
data = [data]
data = bytes(data)
return self.writeto_mem(addr, register, data)

# The follwing haven't been implemented, but could be.
# The following haven't been implemented, but could be.
def read_byte(self, *args, **kwargs):
""" Not yet implemented """
raise RuntimeError("Not yet implemented")
Expand All @@ -58,11 +60,14 @@ def write_byte(self, *args, **kwargs):
""" Not yet implemented """
raise RuntimeError("Not yet implemented")

def read_word_data(self, *args, **kwargs):
""" Not yet implemented """
raise RuntimeError("Not yet implemented")
def read_word_data(self, addr, register) -> int:
""" Read a single word (2 bytes) from a given register;
little-endian decode that word as int
Returns int
"""

return int.from_bytes(self.i2c.readfrom_mem(addr, register, 2), 'little')

def write_word_data(self, *args, **kwargs):
""" Not yet implemented """
raise RuntimeError("Not yet implemented")