Skip to content

Commit

Permalink
Merge pull request #27 from tannewt/remove_stop
Browse files Browse the repository at this point in the history
Remove stop kwarg and use write_then_readinto.
  • Loading branch information
caternuson committed Aug 23, 2019
2 parents 57b3500 + 52a42af commit 3d784ad
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 27 deletions.
10 changes: 4 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,13 @@ we must implement ``__get__`` and ``__set__``.
.. code-block:: python
def __get__(self, obj, objtype=None):
with obj.i2c_device:
obj.i2c_device.write(self.buffer, end=1, stop=False)
obj.i2c_device.readinto(self.buffer, start=1)
with obj.i2c_device as i2c:
i2c.write_then_readinto(self.buffer, self.buffer, out_end=1, in_start=1)
return bool(self.buffer[1] & self.bit_mask)
def __set__(self, obj, value):
with obj.i2c_device:
obj.i2c_device.write(self.buffer, end=1, stop=False)
obj.i2c_device.readinto(self.buffer, start=1)
with obj.i2c_device as i2c:
i2c.write_then_readinto(self.buffer, self.buffer, out_end=1, in_start=1)
if value:
self.buffer[1] |= self.bit_mask
else:
Expand Down
5 changes: 2 additions & 3 deletions adafruit_register/i2c_bcd_alarm.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,8 @@ def __init__(self, register_address, has_seconds=True, weekday_shared=True, week

def __get__(self, obj, objtype=None):
# Read the alarm register.
with obj.i2c_device:
obj.i2c_device.write(self.buffer, end=1, stop=False)
obj.i2c_device.readinto(self.buffer, start=1)
with obj.i2c_device as i2c:
i2c.write_then_readinto(self.buffer, self.buffer, out_end=1, in_start=1)

frequency = None
i = 1
Expand Down
5 changes: 2 additions & 3 deletions adafruit_register/i2c_bcd_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,8 @@ def __init__(self, register_address, weekday_first=True, weekday_start=1):

def __get__(self, obj, objtype=None):
# Read and return the date and time.
with obj.i2c_device:
obj.i2c_device.write(self.buffer, end=1, stop=False)
obj.i2c_device.readinto(self.buffer, start=1)
with obj.i2c_device as i2c:
i2c.write_then_readinto(self.buffer, self.buffer, out_end=1, in_start=1)
return time.struct_time((_bcd2bin(self.buffer[7]) + 2000,
_bcd2bin(self.buffer[6]),
_bcd2bin(self.buffer[5 - self.weekday_offset]),
Expand Down
4 changes: 2 additions & 2 deletions adafruit_register/i2c_bit.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,13 @@ def __init__(self, register_address, bit, register_width=1, lsb_first=True):
def __get__(self, obj, objtype=None):
with obj.i2c_device as i2c:
i2c.write_then_readinto(self.buffer, self.buffer,
out_end=1, in_start=1, stop=False)
out_end=1, in_start=1)
return bool(self.buffer[self.byte] & self.bit_mask)

def __set__(self, obj, value):
with obj.i2c_device as i2c:
i2c.write_then_readinto(self.buffer, self.buffer,
out_end=1, in_start=1, stop=False)
out_end=1, in_start=1)
if value:
self.buffer[self.byte] |= self.bit_mask
else:
Expand Down
6 changes: 2 additions & 4 deletions adafruit_register/i2c_bits.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ def __init__(self, num_bits, register_address, lowest_bit, # pylint: disable=too

def __get__(self, obj, objtype=None):
with obj.i2c_device as i2c:
i2c.write_then_readinto(self.buffer, self.buffer,
out_end=1, in_start=1, stop=False)
i2c.write_then_readinto(self.buffer, self.buffer, out_end=1, in_start=1)
# read the number of bytes into a single variable
reg = 0
order = range(len(self.buffer)-1, 0, -1)
Expand All @@ -72,8 +71,7 @@ def __get__(self, obj, objtype=None):
def __set__(self, obj, value):
value <<= self.lowest_bit # shift the value over to the right spot
with obj.i2c_device as i2c:
i2c.write_then_readinto(self.buffer, self.buffer,
out_end=1, in_start=1, stop=False)
i2c.write_then_readinto(self.buffer, self.buffer, out_end=1, in_start=1)
reg = 0
order = range(len(self.buffer)-1, 0, -1)
if not self.lsb_first:
Expand Down
6 changes: 2 additions & 4 deletions adafruit_register/i2c_struct.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ def __init__(self, register_address, struct_format):

def __get__(self, obj, objtype=None):
with obj.i2c_device as i2c:
i2c.write_then_readinto(self.buffer, self.buffer,
out_end=1, in_start=1, stop=False)
i2c.write_then_readinto(self.buffer, self.buffer, out_end=1, in_start=1)
return struct.unpack_from(self.format, memoryview(self.buffer)[1:])

def __set__(self, obj, value):
Expand All @@ -83,8 +82,7 @@ def __get__(self, obj, objtype=None):
buf = bytearray(1+struct.calcsize(self.format))
buf[0] = self.address
with obj.i2c_device as i2c:
i2c.write_then_readinto(buf, buf,
out_end=1, in_start=1, stop=False)
i2c.write_then_readinto(buf, buf, out_end=1, in_start=1)
return struct.unpack_from(self.format, buf, 1)[0]

def __set__(self, obj, value):
Expand Down
7 changes: 2 additions & 5 deletions adafruit_register/i2c_struct_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
* Author(s): Scott Shawcroft
"""

try:
import struct
except ImportError:
import ustruct as struct
import struct

__version__ = "0.0.0-auto.0"
__repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Register.git"
Expand Down Expand Up @@ -67,7 +64,7 @@ def __getitem__(self, index):
buf = self._get_buffer(index)
with self.obj.i2c_device as i2c:
i2c.write_then_readinto(buf, buf,
out_end=1, in_start=1, stop=False)
out_end=1, in_start=1)
return struct.unpack_from(self.format, buf, 1) # offset=1

def __setitem__(self, index, value):
Expand Down

0 comments on commit 3d784ad

Please sign in to comment.