123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- # -*- coding: utf-8-*-
- #
- # This file is part of stdd, the simple time display daemon,
- # written by Helmut Pozimski <helmut@pozimski.eu>,
- # licensed under the 3-clause BSD license
- # This file was originally written by Written by Limor Fried, Kevin Townsend
- # and Mikey Sklar for Adafruit Industries. BSD license,
- # all text above must be included in any redistribution
- import smbus
- # ===========================================================================
- # Adafruit_I2C Class
- # ===========================================================================
- class Adafruit_I2C:
- @staticmethod
- def getPiRevision():
- "Gets the version number of the Raspberry Pi board"
- # Courtesy quick2wire-python-api
- # https://github.com/quick2wire/quick2wire-python-api
- try:
- with open('/proc/cpuinfo', 'r') as f:
- for line in f:
- if line.startswith('Revision'):
- return 1 if line.rstrip()[-1] in ['1', '2'] else 2
- except:
- return 0
- @staticmethod
- def getPiI2CBusNumber():
- # Gets the I2C bus number /dev/i2c#
- return 1 if Adafruit_I2C.getPiRevision() > 1 else 0
- def __init__(self, address, busnum=-1, debug=False):
- self.address = address
- # By default, the correct I2C bus is auto-detected using /proc/cpuinfo
- # Alternatively, you can hard-code the bus version below:
- # self.bus = smbus.SMBus(0); # Force I2C0 (early 256MB Pi's)
- # self.bus = smbus.SMBus(1); # Force I2C1 (512MB Pi's)
- self.bus = smbus.SMBus(
- busnum if busnum >= 0 else Adafruit_I2C.getPiI2CBusNumber())
- self.debug = debug
- def reverseByteOrder(self, data):
- "Reverses the byte order of an int (16-bit) or long (32-bit) value"
- # Courtesy Vishal Sapre
- byteCount = len(hex(data)[2:].replace('L', '')[::2])
- val = 0
- for i in range(byteCount):
- val = (val << 8) | (data & 0xff)
- data >>= 8
- return val
- def errMsg(self):
- print "Error accessing 0x%02X: Check your I2C address" % self.address
- return -1
- def write8(self, reg, value):
- "Writes an 8-bit value to the specified register/address"
- try:
- self.bus.write_byte_data(self.address, reg, value)
- if self.debug:
- print "I2C: Wrote 0x%02X to register 0x%02X" % (value, reg)
- except IOError, err:
- return self.errMsg()
- def write16(self, reg, value):
- "Writes a 16-bit value to the specified register/address pair"
- try:
- self.bus.write_word_data(self.address, reg, value)
- if self.debug:
- print ("I2C: Wrote 0x%02X to register pair 0x%02X,0x%02X" %
- (value, reg, reg+1))
- except IOError, err:
- return self.errMsg()
- def writeList(self, reg, list):
- "Writes an array of bytes using I2C format"
- try:
- if self.debug:
- print "I2C: Writing list to register 0x%02X:" % reg
- print list
- self.bus.write_i2c_block_data(self.address, reg, list)
- except IOError, err:
- return self.errMsg()
- def readList(self, reg, length):
- "Read a list of bytes from the I2C device"
- try:
- results = self.bus.read_i2c_block_data(self.address, reg, length)
- if self.debug:
- print ("I2C: Device 0x % 02X returned the following from \
- reg 0x % 02X" % (self.address, reg))
- print results
- return results
- except IOError, err:
- return self.errMsg()
- def readU8(self, reg):
- "Read an unsigned byte from the I2C device"
- try:
- result = self.bus.read_byte_data(self.address, reg)
- if self.debug:
- print ("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" %
- (self.address, result & 0xFF, reg))
- return result
- except IOError, err:
- return self.errMsg()
- def readS8(self, reg):
- "Reads a signed byte from the I2C device"
- try:
- result = self.bus.read_byte_data(self.address, reg)
- if result > 127: result -= 256
- if self.debug:
- print ("I2C: Device 0x%02X returned 0x%02X from reg 0x%02X" %
- (self.address, result & 0xFF, reg))
- return result
- except IOError, err:
- return self.errMsg()
- def readU16(self, reg):
- "Reads an unsigned 16-bit value from the I2C device"
- try:
- hibyte = self.readU8(reg)
- lobyte = self.readU8(reg+1)
- result = (hibyte << 8) + lobyte
- if (self.debug):
- print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (
- self.address, result & 0xFFFF, reg)
- return result
- except IOError, err:
- return self.errMsg()
- def readS16(self, reg):
- "Reads a signed 16-bit value from the I2C device"
- try:
- hibyte = self.readS8(reg)
- lobyte = self.readU8(reg+1)
- result = (hibyte << 8) + lobyte
- if (self.debug):
- print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (
- self.address, result & 0xFFFF, reg)
- return result
- except IOError, err:
- return self.errMsg()
- def readU16Rev(self, reg):
- "Reads an unsigned 16-bit value from the I2C device with rev byte ord"
- try:
- lobyte = self.readU8(reg)
- hibyte = self.readU8(reg+1)
- result = (hibyte << 8) + lobyte
- if (self.debug):
- print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (
- self.address, result & 0xFFFF, reg)
- return result
- except IOError, err:
- return self.errMsg()
- def readS16Rev(self, reg):
- "Reads a signed 16-bit value from the I2C device with rev byte order"
- try:
- lobyte = self.readS8(reg)
- hibyte = self.readU8(reg+1)
- result = (hibyte << 8) + lobyte
- if (self.debug):
- print "I2C: Device 0x%02X returned 0x%04X from reg 0x%02X" % (
- self.address, result & 0xFFFF, reg)
- return result
- except IOError, err:
- return self.errMsg()
- if __name__ == '__main__':
- try:
- bus = Adafruit_I2C(address=0)
- print "Default I2C bus is accessible"
- except:
- print "Error accessing default I2C bus"
|