|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +import smbus |
| 4 | + |
| 5 | +# =========================================================================== |
| 6 | +# Adafruit_I2C Base Class |
| 7 | +# =========================================================================== |
| 8 | + |
| 9 | +class Adafruit_I2C : |
| 10 | + |
| 11 | + def __init__(self, address, bus=smbus.SMBus(0), debug=False): |
| 12 | + self.address = address |
| 13 | + self.bus = bus |
| 14 | + self.debug = debug |
| 15 | + |
| 16 | + def reverseByteOrder(self, data): |
| 17 | + "Reverses the byte order of an int (16-bit) or long (32-bit) value" |
| 18 | + # Courtesy Vishal Sapre |
| 19 | + dstr = hex(data)[2:].replace('L','') |
| 20 | + byteCount = len(dstr[::2]) |
| 21 | + val = 0 |
| 22 | + for i, n in enumerate(range(byteCount)): |
| 23 | + d = data & 0xFF |
| 24 | + val |= (d << (8 * (byteCount - i - 1))) |
| 25 | + data >>= 8 |
| 26 | + return val |
| 27 | + |
| 28 | + def write8(self, reg, value): |
| 29 | + "Writes an 8-bit value to the specified register/address" |
| 30 | + try: |
| 31 | + self.bus.write_byte_data(self.address, reg, value) |
| 32 | + if (self.debug): |
| 33 | + print("I2C: Wrote 0x%02X to register 0x%02X" % (value, reg)) |
| 34 | + except IOError, err: |
| 35 | + print "Error accessing 0x%02X: Check your I2C address" % self.address |
| 36 | + return -1 |
| 37 | + |
| 38 | + def writeList(self, reg, list): |
| 39 | + "Writes an array of bytes using I2C format" |
| 40 | + try: |
| 41 | + self.bus.write_i2c_block_data(self.address, reg, list) |
| 42 | + except IOError, err: |
| 43 | + print "Error accessing 0x%02X: Check your I2C address" % self.address |
| 44 | + return -1 |
| 45 | + |
| 46 | + def readU8(self, reg): |
| 47 | + "Read an unsigned byte from the I2C device" |
| 48 | + try: |
| 49 | + result = self.bus.read_byte_data(self.address, reg) |
| 50 | + if (self.debug): |
| 51 | + print "I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % (self.address, result & 0xFF, reg) |
| 52 | + return result |
| 53 | + except IOError, err: |
| 54 | + print "Error accessing 0x%02X: Check your I2C address" % self.address |
| 55 | + return -1 |
| 56 | + |
| 57 | + def readS8(self, reg): |
| 58 | + "Reads a signed byte from the I2C device" |
| 59 | + try: |
| 60 | + result = self.bus.read_byte_data(self.address, reg) |
| 61 | + if (self.debug): |
| 62 | + print "I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" % (self.address, result & 0xFF, reg) |
| 63 | + if (result > 127): |
| 64 | + return result - 256 |
| 65 | + else: |
| 66 | + return result |
| 67 | + except IOError, err: |
| 68 | + print "Error accessing 0x%02X: Check your I2C address" % self.address |
| 69 | + return -1 |
| 70 | + |
| 71 | + def readU16(self, reg): |
| 72 | + "Reads an unsigned 16-bit value from the I2C device" |
| 73 | + try: |
| 74 | + hibyte = self.bus.read_byte_data(self.address, reg) |
| 75 | + result = (hibyte << 8) + self.bus.read_byte_data(self.address, reg+1) |
| 76 | + if (self.debug): |
| 77 | + print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg) |
| 78 | + return result |
| 79 | + except IOError, err: |
| 80 | + print "Error accessing 0x%02X: Check your I2C address" % self.address |
| 81 | + return -1 |
| 82 | + |
| 83 | + def readS16(self, reg): |
| 84 | + "Reads a signed 16-bit value from the I2C device" |
| 85 | + try: |
| 86 | + hibyte = self.bus.read_byte_data(self.address, reg) |
| 87 | + if (hibyte > 127): |
| 88 | + hibyte -= 256 |
| 89 | + result = (hibyte << 8) + self.bus.read_byte_data(self.address, reg+1) |
| 90 | + if (self.debug): |
| 91 | + print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (self.address, result & 0xFFFF, reg) |
| 92 | + return result |
| 93 | + except IOError, err: |
| 94 | + print "Error accessing 0x%02X: Check your I2C address" % self.address |
| 95 | + return -1 |
0 commit comments