/ adafruit_register / i2c_bits.py
i2c_bits.py
  1  # The MIT License (MIT)
  2  #
  3  # Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
  4  #
  5  # Permission is hereby granted, free of charge, to any person obtaining a copy
  6  # of this software and associated documentation files (the "Software"), to deal
  7  # in the Software without restriction, including without limitation the rights
  8  # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9  # copies of the Software, and to permit persons to whom the Software is
 10  # furnished to do so, subject to the following conditions:
 11  #
 12  # The above copyright notice and this permission notice shall be included in
 13  # all copies or substantial portions of the Software.
 14  #
 15  # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16  # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17  # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18  # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19  # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 20  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 21  # THE SOFTWARE.
 22  # pylint: disable=too-few-public-methods
 23  """
 24  `adafruit_register.i2c_bits`
 25  ====================================================
 26  
 27  Multi bit registers
 28  
 29  * Author(s): Scott Shawcroft
 30  """
 31  
 32  __version__ = "0.0.0-auto.0"
 33  __repo__ = "https://github.com/adafruit/Adafruit_CircuitPython_Register.git"
 34  
 35  
 36  class RWBits:
 37      """
 38      Multibit register (less than a full byte) that is readable and writeable.
 39      This must be within a byte register.
 40  
 41      Values are `int` between 0 and 2 ** ``num_bits`` - 1.
 42  
 43      :param int num_bits: The number of bits in the field.
 44      :param int register_address: The register address to read the bit from
 45      :param type lowest_bit: The lowest bits index within the byte at ``register_address``
 46      :param int register_width: The number of bytes in the register. Defaults to 1.
 47      :param bool lsb_first: Is the first byte we read from I2C the LSB? Defaults to true
 48      """
 49  
 50      def __init__(  # pylint: disable=too-many-arguments
 51          self, num_bits, register_address, lowest_bit, register_width=1, lsb_first=True,
 52      ):
 53          self.bit_mask = ((1 << num_bits) - 1) << lowest_bit
 54          # print("bitmask: ",hex(self.bit_mask))
 55          if self.bit_mask >= 1 << (register_width * 8):
 56              raise ValueError("Cannot have more bits than register size")
 57          self.lowest_bit = lowest_bit
 58          self.buffer = bytearray(1 + register_width)
 59          self.buffer[0] = register_address
 60          self.lsb_first = lsb_first
 61  
 62      def __get__(self, obj, objtype=None):
 63          with obj.i2c_device as i2c:
 64              i2c.write_then_readinto(self.buffer, self.buffer, out_end=1, in_start=1)
 65          # read the number of bytes into a single variable
 66          reg = 0
 67          order = range(len(self.buffer) - 1, 0, -1)
 68          if not self.lsb_first:
 69              order = reversed(order)
 70          for i in order:
 71              reg = (reg << 8) | self.buffer[i]
 72          return (reg & self.bit_mask) >> self.lowest_bit
 73  
 74      def __set__(self, obj, value):
 75          value <<= self.lowest_bit  # shift the value over to the right spot
 76          with obj.i2c_device as i2c:
 77              i2c.write_then_readinto(self.buffer, self.buffer, out_end=1, in_start=1)
 78              reg = 0
 79              order = range(len(self.buffer) - 1, 0, -1)
 80              if not self.lsb_first:
 81                  order = range(1, len(self.buffer))
 82              for i in order:
 83                  reg = (reg << 8) | self.buffer[i]
 84              # print("old reg: ", hex(reg))
 85              reg &= ~self.bit_mask  # mask off the bits we're about to change
 86              reg |= value  # then or in our new value
 87              # print("new reg: ", hex(reg))
 88              for i in reversed(order):
 89                  self.buffer[i] = reg & 0xFF
 90                  reg >>= 8
 91              i2c.write(self.buffer)
 92  
 93  
 94  class ROBits(RWBits):
 95      """
 96      Multibit register (less than a full byte) that is read-only. This must be
 97      within a byte register.
 98  
 99      Values are `int` between 0 and 2 ** ``num_bits`` - 1.
100  
101      :param int num_bits: The number of bits in the field.
102      :param int register_address: The register address to read the bit from
103      :param type lowest_bit: The lowest bits index within the byte at ``register_address``
104      :param int register_width: The number of bytes in the register. Defaults to 1.
105      """
106  
107      def __set__(self, obj, value):
108          raise AttributeError()