|
@@ -1,194 +0,0 @@
|
|
|
-# -*- 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
|
|
|
-
|
|
|
-"""
|
|
|
-===========================================================================
|
|
|
-AdafruitI2c Class
|
|
|
-===========================================================================
|
|
|
-"""
|
|
|
-
|
|
|
-from __future__ import print_function
|
|
|
-
|
|
|
-from smbus import SMBus
|
|
|
-
|
|
|
-
|
|
|
-class AdafruitI2c(object):
|
|
|
- """ This class provides low level i2c access.
|
|
|
- """
|
|
|
- @staticmethod
|
|
|
- def get_pi_revision():
|
|
|
- """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 file_obj:
|
|
|
- for line in file_obj:
|
|
|
- if line.startswith('Revision'):
|
|
|
- return 1 if line.rstrip()[-1] in ['1', '2'] else 2
|
|
|
- except IOError:
|
|
|
- return 0
|
|
|
-
|
|
|
- @staticmethod
|
|
|
- def get_pi_i2c_bus_number():
|
|
|
- """ Gets the I2C bus number /dev/i2c# """
|
|
|
- return 1 if AdafruitI2c.get_pi_revision() > 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(
|
|
|
- busnum if busnum >= 0 else AdafruitI2c.get_pi_i2c_bus_number())
|
|
|
- self.debug = debug
|
|
|
-
|
|
|
- @staticmethod
|
|
|
- def reverse_byte_order(data):
|
|
|
- """Reverses the byte order of an int (16-bit) or long (32-bit) value"""
|
|
|
- # Courtesy Vishal Sapre
|
|
|
- byte_count = len(hex(data)[2:].replace('L', '')[::2])
|
|
|
- val = 0
|
|
|
- for i in range(byte_count):
|
|
|
- val = (val << 8) | (data & 0xff)
|
|
|
- data >>= 8
|
|
|
- return val
|
|
|
-
|
|
|
- @staticmethod
|
|
|
- def err_msg():
|
|
|
- """Raises an exception."""
|
|
|
- raise Exception("cannot connect to device, check hardware address!")
|
|
|
-
|
|
|
- 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:
|
|
|
- return self.err_msg()
|
|
|
-
|
|
|
- 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:
|
|
|
- return self.err_msg()
|
|
|
-
|
|
|
- def write_list(self, reg, w_list):
|
|
|
- """Writes an array of bytes using I2C format"""
|
|
|
- try:
|
|
|
- if self.debug:
|
|
|
- print("I2C: Writing list to register 0x%02X:" % reg)
|
|
|
- print(w_list)
|
|
|
- self.bus.write_i2c_block_data(self.address, reg, w_list)
|
|
|
- except IOError:
|
|
|
- return self.err_msg()
|
|
|
-
|
|
|
- def read_list(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:
|
|
|
- return self.err_msg()
|
|
|
-
|
|
|
- def read_u8(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:
|
|
|
- return self.err_msg()
|
|
|
-
|
|
|
- def read_s8(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:
|
|
|
- return self.err_msg()
|
|
|
-
|
|
|
- def read_u16(self, reg):
|
|
|
- """Reads an unsigned 16-bit value from the I2C device"""
|
|
|
- try:
|
|
|
- hibyte = self.read_u8(reg)
|
|
|
- lobyte = self.read_u8(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:
|
|
|
- return self.err_msg()
|
|
|
-
|
|
|
- def read_s16(self, reg):
|
|
|
- """Reads a signed 16-bit value from the I2C device"""
|
|
|
- try:
|
|
|
- hibyte = self.read_s8(reg)
|
|
|
- lobyte = self.read_u8(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:
|
|
|
- return self.err_msg()
|
|
|
-
|
|
|
- def read_u16_rev(self, reg):
|
|
|
- """Reads an unsigned 16-bit value from the I2C device with rev byte
|
|
|
- order.
|
|
|
- """
|
|
|
- try:
|
|
|
- lobyte = self.read_u8(reg)
|
|
|
- hibyte = self.read_u8(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:
|
|
|
- return self.err_msg()
|
|
|
-
|
|
|
- def read_s16_rev(self, reg):
|
|
|
- """Reads a signed 16-bit value from the I2C device with rev byte
|
|
|
- order.
|
|
|
- """
|
|
|
- try:
|
|
|
- lobyte = self.read_s8(reg)
|
|
|
- hibyte = self.read_u8(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:
|
|
|
- return self.err_msg()
|
|
|
-
|
|
|
-if __name__ == '__main__':
|
|
|
- try:
|
|
|
- BUS = AdafruitI2c(address=0)
|
|
|
- print("Default I2C bus is accessible")
|
|
|
- except:
|
|
|
- print("Error accessing default I2C bus")
|