Skip to content

Commit

Permalink
Make sure writeto_mem() gets something that supports buffer protocol.
Browse files Browse the repository at this point in the history
  • Loading branch information
Geoff Lee committed Apr 20, 2018
1 parent a1f2277 commit 539d595
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions usmbus/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
""" Provides an SMBus class for use on micropython """

try:
from machine import I2C
except ImportError:
Expand All @@ -7,20 +9,20 @@

class SMBus(I2C):
""" Provides an 'SMBus' module which supports some of the py-smbus
i2c methods, as well as being a subclass of machine.I2C
i2c methods, as well as being a subclass of machine.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:
import usmbus.SMBus
Use it like you would the machine.I2C class:
import usmbus.SMBus
bus = SMBus(1, pins=('G15','G10'), baudrate=100000)
bus.read_byte_data(addr, register)
... etc
... etc
"""

def read_byte_data(self, addr, register):
""" Read a single byte from register of device at addr
Returns a single byte """
Expand All @@ -32,29 +34,35 @@ def read_i2c_block_data(self, addr, register, length):
return self.readfrom_mem(addr, register, length)

def write_byte_data(self, addr, register, data):
""" Write a single byte of data to register of device at addr
""" Write a single byte from buffer `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])
return self.writeto_mem(addr, register, data)

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])
return self.writeto_mem(addr, register, data)

# The follwing haven't been implemented, but could be.
def read_byte(*args, **kwargs):
def read_byte(self, *args, **kwargs):
""" Not yet implemented """
raise RuntimeError("Not yet implemented")

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

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

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

0 comments on commit 539d595

Please sign in to comment.