|
| 1 | +# Copyright (c) 2014 Adafruit Industries |
| 2 | +# Author: Tony DiCola |
| 3 | +# Based on Adafruit_I2C.py created by Kevin Townsend. |
| 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 | + |
| 23 | +import logging |
| 24 | + |
| 25 | +import smbus |
| 26 | + |
| 27 | + |
| 28 | +logger = logging.getLogger(__name__) |
| 29 | + |
| 30 | + |
| 31 | +def reverseByteOrder(data): |
| 32 | + """Reverses the byte order of an int (16-bit) or long (32-bit) value.""" |
| 33 | + # Courtesy Vishal Sapre |
| 34 | + byteCount = len(hex(data)[2:].replace('L','')[::2]) |
| 35 | + val = 0 |
| 36 | + for i in range(byteCount): |
| 37 | + val = (val << 8) | (data & 0xff) |
| 38 | + data >>= 8 |
| 39 | + return val |
| 40 | + |
| 41 | +def get_default_bus(): |
| 42 | + """Return the default bus number based on the device platform. For a |
| 43 | + Raspberry Pi either bus 0 or 1 (baed on the Pi revision) will be returned. |
| 44 | + For a Beaglebone Black the first user accessible bus, 1, will be returned. |
| 45 | + """ |
| 46 | + return 0 |
| 47 | + |
| 48 | + |
| 49 | +class Device(object): |
| 50 | + """Class for communicating with an I2C device using the smbus library. |
| 51 | + Allows reading and writing 8-bit, 16-bit, and byte array values to registers |
| 52 | + on the device.""" |
| 53 | + logger = logging.getLogger(__name__) |
| 54 | + def __init__(self, address, busnum): |
| 55 | + """Create an instance of the I2C device at the specified address on the |
| 56 | + specified I2C bus number.""" |
| 57 | + self._address = address |
| 58 | + self._bus = smbus.SMBus(busnum) |
| 59 | + self._logger = logging.getLogger('Adafruit_I2C.Device.Bus.{0}.Address.{1:#0X}' \ |
| 60 | + .format(busnum, address)) |
| 61 | + |
| 62 | + def write8(self, register, value): |
| 63 | + """Write an 8-bit value to the specified register.""" |
| 64 | + value = value & 0xFF |
| 65 | + self._bus.write_byte_data(self._address, register, value) |
| 66 | + self._logger.debug("Wrote 0x%02X to register 0x%02X", |
| 67 | + value, register) |
| 68 | + |
| 69 | + def write16(self, register, value): |
| 70 | + """Write a 16-bit value to the specified register.""" |
| 71 | + value = value & 0xFFFF |
| 72 | + self._bus.write_word_data(self._address, register, value) |
| 73 | + self._logger.debug("Wrote 0x%04X to register pair 0x%02X, 0x%02X", |
| 74 | + value, register, register+1) |
| 75 | + |
| 76 | + def writeList(self, register, data): |
| 77 | + """Write bytes to the specified register.""" |
| 78 | + self._bus.write_i2c_block_data(self._address, register, data) |
| 79 | + self._logger.debug("Wrote to register 0x%02X: %s", |
| 80 | + register, data) |
| 81 | + |
| 82 | + def readList(self, register, length): |
| 83 | + """Read a length number of bytes from the specified register. Results |
| 84 | + will be returned as a bytearray.""" |
| 85 | + results = self._bus.read_i2c_block_data(self._address, register, length) |
| 86 | + self._logger.debug("Read the following from register 0x%02X: %s", |
| 87 | + register, results) |
| 88 | + return results |
| 89 | + |
| 90 | + def readU8(self, register): |
| 91 | + """Read an unsigned byte from the specified register.""" |
| 92 | + result = self._bus.read_byte_data(self._address, register) & 0xFF |
| 93 | + self._logger.debug("Read 0x%02X from register 0x%02X", |
| 94 | + result, register) |
| 95 | + return result |
| 96 | + |
| 97 | + def readS8(self, register): |
| 98 | + """Read a signed byte from the specified register.""" |
| 99 | + result = self._bus.read_byte_data(self._address, register) & 0xFF |
| 100 | + if result > 127: |
| 101 | + result -= 256 |
| 102 | + self._logger.debug("Read 0x%02X from register 0x%02X", |
| 103 | + result, register) |
| 104 | + return result |
| 105 | + |
| 106 | + def readU16(self, register): |
| 107 | + """Read an unsigned 16-bit value from the specified register.""" |
| 108 | + result = self._bus.read_word_data(self._address,register) & 0xFFFF |
| 109 | + self._logger.debug("Read 0x%04X from register pair 0x%02X, 0x%02X", |
| 110 | + result, register, register+1) |
| 111 | + return result |
| 112 | + |
| 113 | + def readS16(self, register): |
| 114 | + """Read a signed 16-bit value from the specified register.""" |
| 115 | + result = self._bus.read_word_data(self._address,register) |
| 116 | + if result > 32767: |
| 117 | + result -= 65536 |
| 118 | + self._logger.debug("Read 0x%04X from register pair 0x%02X, 0x%02X", |
| 119 | + result, register, register+1) |
| 120 | + return result |
0 commit comments