Python Adafruit_GPIO.I2C Examples

The following are 17 code examples of Adafruit_GPIO.I2C(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module Adafruit_GPIO , or try the search function .
Example #1
Source File: TCS34725.py    From Adafruit_Python_TCS34725 with MIT License 6 votes vote down vote up
def __init__(self, integration_time=TCS34725_INTEGRATIONTIME_2_4MS,
                 gain=TCS34725_GAIN_4X, address=TCS34725_ADDRESS, i2c=None, **kwargs):
        """Initialize the TCS34725 sensor."""
        # Setup I2C interface for the device.
        if i2c is None:
            import Adafruit_GPIO.I2C as I2C
            i2c = I2C
        self._device = i2c.get_i2c_device(address, **kwargs)
        # Make sure we're connected to the sensor.
        chip_id = self._readU8(TCS34725_ID)
        if chip_id != 0x44:
            raise RuntimeError('Failed to read TCS34725 chip ID, check your wiring.')
        # Set default integration time and gain.
        self.set_integration_time(integration_time)
        self.set_gain(gain)
        # Enable the device (by default, the device is in power down mode on bootup).
        self.enable() 
Example #2
Source File: PCA95xx.py    From Adafruit_Python_GPIO with MIT License 6 votes vote down vote up
def __init__(self, address=0x20, busnum=None, i2c=None, num_gpios=16, **kwargs):
        address = int(address)
        self.__name__ = "PCA955"
        # Create I2C device.
        i2c = i2c or I2C
        busnum = busnum or i2c.get_default_bus()
        self._device = i2c.get_i2c_device(address, busnum, **kwargs)
        self.num_gpios = num_gpios
        
        if self.num_gpios <= 8:
            self.iodir = self._device.readU8(CONFIG_PORT)
            self.outputvalue = self._device.readU8(OUTPUT_PORT)

        elif self.num_gpios > 8 and self.num_gpios <= 16:
            self.iodir = self._device.readU16(CONFIG_PORT<< 1)
            self.outputvalue = self._device.readU16(OUTPUT_PORT << 1) 
Example #3
Source File: PCF8574.py    From Adafruit_Python_GPIO with MIT License 6 votes vote down vote up
def __init__(self, address=0x27, busnum=None, i2c=None, **kwargs):
        address = int(address)
        self.__name__ = \
            "PCF8574" if address in range(0x20, 0x28) else \
            "PCF8574A" if address in range(0x38, 0x40) else \
            "Bad address for PCF8574(A): 0x%02X not in range [0x20..0x27, 0x38..0x3F]" % address
        if self.__name__[0] != 'P':
            raise ValueError(self.__name__)
        # Create I2C device.
        i2c = i2c or I2C
        busnum = busnum or i2c.get_default_bus()
        self._device = i2c.get_i2c_device(address, busnum, **kwargs)
        # Buffer register values so they can be changed without reading.
        self.iodir = 0xFF  # Default direction to all inputs is in
        self.gpio = 0x00
        self._write_pins() 
Example #4
Source File: MCP230xx.py    From Adafruit_Python_GPIO with MIT License 6 votes vote down vote up
def __init__(self, address, i2c=None, **kwargs):
        """Initialize MCP230xx at specified I2C address and bus number.  If bus
        is not specified it will default to the appropriate platform detected bus.
        """
        # Create I2C device.
        if i2c is None:
            import Adafruit_GPIO.I2C as I2C
            i2c = I2C
        self._device = i2c.get_i2c_device(address, **kwargs)
        # Assume starting in ICON.BANK = 0 mode (sequential access).
        # Compute how many bytes are needed to store count of GPIO.
        self.gpio_bytes = int(math.ceil(self.NUM_GPIO/8.0))
        # Buffer register values so they can be changed without reading.
        self.iodir = [0xFF]*self.gpio_bytes  # Default direction to all inputs.
        self.gppu = [0x00]*self.gpio_bytes  # Default to pullups disabled.
        self.gpio = [0x00]*self.gpio_bytes
        # Write current direction and pullup buffer state.
        self.write_iodir()
        self.write_gppu() 
Example #5
Source File: LSM303.py    From Adafruit_Python_LSM303 with MIT License 6 votes vote down vote up
def __init__(self, hires=True, accel_address=LSM303_ADDRESS_ACCEL,
                 mag_address=LSM303_ADDRESS_MAG, i2c=None, **kwargs):
        """Initialize the LSM303 accelerometer & magnetometer.  The hires
        boolean indicates if high resolution (12-bit) mode vs. low resolution
        (10-bit, faster and lower power) mode should be used.
        """
        # Setup I2C interface for accelerometer and magnetometer.
        if i2c is None:
            import Adafruit_GPIO.I2C as I2C
            i2c = I2C
        self._accel = i2c.get_i2c_device(accel_address, **kwargs)
        self._mag = i2c.get_i2c_device(mag_address, **kwargs)
        # Enable the accelerometer
        self._accel.write8(LSM303_REGISTER_ACCEL_CTRL_REG1_A, 0x27)
        # Select hi-res (12-bit) or low-res (10-bit) output mode.
        # Low-res mode uses less power and sustains a higher update rate,
        # output is padded to compatible 12-bit units.
        if hires:
            self._accel.write8(LSM303_REGISTER_ACCEL_CTRL_REG4_A, 0b00001000)
        else:
            self._accel.write8(LSM303_REGISTER_ACCEL_CTRL_REG4_A, 0)
        # Enable the magnetometer
        self._mag.write8(LSM303_REGISTER_MAG_MR_REG_M, 0x00) 
Example #6
Source File: BMP085.py    From Adafruit_Python_BMP with MIT License 5 votes vote down vote up
def __init__(self, mode=BMP085_STANDARD, address=BMP085_I2CADDR, i2c=None, **kwargs):
        self._logger = logging.getLogger('Adafruit_BMP.BMP085')
        # Check that mode is valid.
        if mode not in [BMP085_ULTRALOWPOWER, BMP085_STANDARD, BMP085_HIGHRES, BMP085_ULTRAHIGHRES]:
            raise ValueError('Unexpected mode value {0}.  Set mode to one of BMP085_ULTRALOWPOWER, BMP085_STANDARD, BMP085_HIGHRES, or BMP085_ULTRAHIGHRES'.format(mode))
        self._mode = mode
        # Create I2C device.
        if i2c is None:
            import Adafruit_GPIO.I2C as I2C
            i2c = I2C
        self._device = i2c.get_i2c_device(address, **kwargs)
        # Load calibration values.
        self._load_calibration() 
Example #7
Source File: Adafruit_CCS811.py    From Adafruit_CCS811_python with MIT License 5 votes vote down vote up
def __init__(self, mode=CCS811_DRIVE_MODE_1SEC, address=CCS811_ADDRESS, i2c=None, **kwargs):
		self._logger = logging.getLogger('Adafruit_CCS811.CCS811')
		# Check that mode is valid.
		if mode not in [CCS811_DRIVE_MODE_IDLE, CCS811_DRIVE_MODE_1SEC, CCS811_DRIVE_MODE_10SEC, CCS811_DRIVE_MODE_60SEC, CCS811_DRIVE_MODE_250MS]:
			raise ValueError('Unexpected mode value {0}.  Set mode to one of CCS811_DRIVE_MODE_IDLE, CCS811_DRIVE_MODE_1SEC, CCS811_DRIVE_MODE_10SEC, CCS811_DRIVE_MODE_60SEC or CCS811_DRIVE_MODE_250MS'.format(mode))

		# Create I2C device.
		if i2c is None:
			import Adafruit_GPIO.I2C as I2C
			i2c = I2C
		self._device = i2c.get_i2c_device(address, **kwargs)

		#set up the registers
		self._status = Adafruit_bitfield([('ERROR' , 1), ('unused', 2), ('DATA_READY' , 1), ('APP_VALID', 1), ('unused2' , 2), ('FW_MODE' , 1)])
		
		self._meas_mode = Adafruit_bitfield([('unused', 2), ('INT_THRESH', 1), ('INT_DATARDY', 1), ('DRIVE_MODE', 3)])

		self._error_id = Adafruit_bitfield([('WRITE_REG_INVALID', 1), ('READ_REG_INVALID', 1), ('MEASMODE_INVALID', 1), ('MAX_RESISTANCE', 1), ('HEATER_FAULT', 1), ('HEATER_SUPPLY', 1)])

		self._TVOC = 0
		self._eCO2 = 0
		self.tempOffset = 0

			#check that the HW id is correct
		if(self._device.readU8(CCS811_HW_ID) != CCS811_HW_ID_CODE):
			raise Exception("Device ID returned is not correct! Please check your wiring.")
		
		#try to start the app
		self._device.writeList(CCS811_BOOTLOADER_APP_START, [])
		sleep(.1)
		
		#make sure there are no errors and we have entered application mode
		if(self.checkError()):
			raise Exception("Device returned an Error! Try removing and reapplying power to the device and running the code again.")
		if(not self._status.FW_MODE):
			raise Exception("Device did not enter application mode! If you got here, there may be a problem with the firmware on your sensor.")
		
		self.disableInterrupt()
		
		#default to read every second
		self.setDriveMode(CCS811_DRIVE_MODE_1SEC) 
Example #8
Source File: HT16K33.py    From Adafruit_Python_LED_Backpack with MIT License 5 votes vote down vote up
def __init__(self, address=DEFAULT_ADDRESS, i2c=None, **kwargs):
        """Create an HT16K33 driver for device on the specified I2C address
        (defaults to 0x70) and I2C bus (defaults to platform specific bus).
        """
        if i2c is None:
            import Adafruit_GPIO.I2C as I2C
            i2c = I2C
        self._device = i2c.get_i2c_device(address, **kwargs)
        self.buffer = bytearray([0]*16) 
Example #9
Source File: ADXL345.py    From Adafruit_Python_ADXL345 with MIT License 5 votes vote down vote up
def __init__(self, address=ADXL345_ADDRESS, i2c=None, **kwargs):
        """Initialize the ADXL345 accelerometer using its I2C interface.
        """
        # Setup I2C interface for the device.
        if i2c is None:
            import Adafruit_GPIO.I2C as I2C
            i2c = I2C
        self._device = i2c.get_i2c_device(address, **kwargs)
        # Check that the acclerometer is connected, then enable it.
        if self._device.readU8(ADXL345_REG_DEVID) == 0xE5:
            self._device.write8(ADXL345_REG_POWER_CTL, 0x08)
        else:
            raise RuntimeError('Failed to find the expected device ID register value, check your wiring.') 
Example #10
Source File: BMP085.py    From SunFounder_SensorKit_for_RPi2 with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, mode=BMP085_STANDARD, address=BMP085_I2CADDR, i2c=None, **kwargs):
		self._logger = logging.getLogger('Adafruit_BMP.BMP085')
		# Check that mode is valid.
		if mode not in [BMP085_ULTRALOWPOWER, BMP085_STANDARD, BMP085_HIGHRES, BMP085_ULTRAHIGHRES]:
			raise ValueError('Unexpected mode value {0}.  Set mode to one of BMP085_ULTRALOWPOWER, BMP085_STANDARD, BMP085_HIGHRES, or BMP085_ULTRAHIGHRES'.format(mode))
		self._mode = mode
		# Create I2C device.
		if i2c is None:
			import Adafruit_GPIO.I2C as I2C
			i2c = I2C
		self._device = i2c.get_i2c_device(address, **kwargs)
		# Load calibration values.
		self._load_calibration() 
Example #11
Source File: MCP9808.py    From Adafruit_Python_MCP9808 with MIT License 5 votes vote down vote up
def __init__(self, address=MCP9808_I2CADDR_DEFAULT, i2c=None, **kwargs):
		"""Initialize MCP9808 device on the specified I2C address and bus number.
		Address defaults to 0x18 and bus number defaults to the appropriate bus
		for the hardware.
		"""
		self._logger = logging.getLogger('Adafruit_MCP9808.MCP9808')
		if i2c is None:
			import Adafruit_GPIO.I2C as I2C
			i2c = I2C
		self._device = i2c.get_i2c_device(address, **kwargs) 
Example #12
Source File: PCA9685.py    From Adafruit_Python_PCA9685 with MIT License 5 votes vote down vote up
def __init__(self, address=PCA9685_ADDRESS, i2c=None, **kwargs):
        """Initialize the PCA9685."""
        # Setup I2C interface for the device.
        if i2c is None:
            import Adafruit_GPIO.I2C as I2C
            i2c = I2C
        self._device = i2c.get_i2c_device(address, **kwargs)
        self.set_all_pwm(0, 0)
        self._device.write8(MODE2, OUTDRV)
        self._device.write8(MODE1, ALLCALL)
        time.sleep(0.005)  # wait for oscillator
        mode1 = self._device.readU8(MODE1)
        mode1 = mode1 & ~SLEEP  # wake up (reset sleep)
        self._device.write8(MODE1, mode1)
        time.sleep(0.005)  # wait for oscillator 
Example #13
Source File: PCA9685.py    From Adafruit_Python_PCA9685 with MIT License 5 votes vote down vote up
def software_reset(i2c=None, **kwargs):
    """Sends a software reset (SWRST) command to all servo drivers on the bus."""
    # Setup I2C interface for device 0x00 to talk to all of them.
    if i2c is None:
        import Adafruit_GPIO.I2C as I2C
        i2c = I2C
    self._device = i2c.get_i2c_device(0x00, **kwargs)
    self._device.writeRaw8(0x06)  # SWRST 
Example #14
Source File: HT16K33.py    From flyover with MIT License 5 votes vote down vote up
def __init__(self, address=DEFAULT_ADDRESS, i2c=None, **kwargs):
		"""Create an HT16K33 driver for devie on the specified I2C address
		(defaults to 0x70) and I2C bus (defaults to platform specific bus).
		"""
		if i2c is None:
			import Adafruit_GPIO.I2C as I2C
			i2c = I2C
		self._device = i2c.get_i2c_device(address, **kwargs)
		self.buffer = bytearray([0]*16) 
Example #15
Source File: Adafruit_BME280.py    From Adafruit_Python_BME280 with MIT License 4 votes vote down vote up
def __init__(self, t_mode=BME280_OSAMPLE_1, p_mode=BME280_OSAMPLE_1, h_mode=BME280_OSAMPLE_1,
                 standby=BME280_STANDBY_250, filter=BME280_FILTER_off, address=BME280_I2CADDR, i2c=None,
                 **kwargs):
        self._logger = logging.getLogger('Adafruit_BMP.BMP085')
        # Check that t_mode is valid.
        if t_mode not in [BME280_OSAMPLE_1, BME280_OSAMPLE_2, BME280_OSAMPLE_4,
                        BME280_OSAMPLE_8, BME280_OSAMPLE_16]:
            raise ValueError(
                'Unexpected t_mode value {0}.'.format(t_mode))
        self._t_mode = t_mode
        # Check that p_mode is valid.
        if p_mode not in [BME280_OSAMPLE_1, BME280_OSAMPLE_2, BME280_OSAMPLE_4,
                        BME280_OSAMPLE_8, BME280_OSAMPLE_16]:
            raise ValueError(
                'Unexpected p_mode value {0}.'.format(p_mode))
        self._p_mode = p_mode
        # Check that h_mode is valid.
        if h_mode not in [BME280_OSAMPLE_1, BME280_OSAMPLE_2, BME280_OSAMPLE_4,
                        BME280_OSAMPLE_8, BME280_OSAMPLE_16]:
            raise ValueError(
                'Unexpected h_mode value {0}.'.format(h_mode))
        self._h_mode = h_mode
        # Check that standby is valid.
        if standby not in [BME280_STANDBY_0p5, BME280_STANDBY_62p5, BME280_STANDBY_125, BME280_STANDBY_250,
                        BME280_STANDBY_500, BME280_STANDBY_1000, BME280_STANDBY_10, BME280_STANDBY_20]:
            raise ValueError(
                'Unexpected standby value {0}.'.format(standby))
        self._standby = standby
        # Check that filter is valid.
        if filter not in [BME280_FILTER_off, BME280_FILTER_2, BME280_FILTER_4, BME280_FILTER_8, BME280_FILTER_16]:
            raise ValueError(
                'Unexpected filter value {0}.'.format(filter))
        self._filter = filter
        # Create I2C device.
        if i2c is None:
            import Adafruit_GPIO.I2C as I2C
            i2c = I2C
        # Create device, catch permission errors
        try:
            self._device = i2c.get_i2c_device(address, **kwargs)
        except IOError:
            print("Unable to communicate with sensor, check permissions.")
            exit()
        # Load calibration values.
        self._load_calibration()
        self._device.write8(BME280_REGISTER_CONTROL, 0x24)  # Sleep mode
        time.sleep(0.002)
        self._device.write8(BME280_REGISTER_CONFIG, ((standby << 5) | (filter << 2)))
        time.sleep(0.002)
        self._device.write8(BME280_REGISTER_CONTROL_HUM, h_mode)  # Set Humidity Oversample
        self._device.write8(BME280_REGISTER_CONTROL, ((t_mode << 5) | (p_mode << 2) | 3))  # Set Temp/Pressure Oversample and enter Normal mode
        self.t_fine = 0.0 
Example #16
Source File: Adafruit_BME280.py    From iot-hub-python-raspberrypi-client-app with MIT License 4 votes vote down vote up
def __init__(self, t_mode=BME280_OSAMPLE_1, p_mode=BME280_OSAMPLE_1, h_mode=BME280_OSAMPLE_1,
                 standby=BME280_STANDBY_250, filter=BME280_FILTER_off, address=BME280_I2CADDR, i2c=None,
                 **kwargs):
        self._logger = logging.getLogger('Adafruit_BMP.BMP085')
        # Check that t_mode is valid.
        if t_mode not in [BME280_OSAMPLE_1, BME280_OSAMPLE_2, BME280_OSAMPLE_4,
                        BME280_OSAMPLE_8, BME280_OSAMPLE_16]:
            raise ValueError(
                'Unexpected t_mode value {0}.'.format(t_mode))
        self._t_mode = t_mode
        # Check that p_mode is valid.
        if p_mode not in [BME280_OSAMPLE_1, BME280_OSAMPLE_2, BME280_OSAMPLE_4,
                        BME280_OSAMPLE_8, BME280_OSAMPLE_16]:
            raise ValueError(
                'Unexpected p_mode value {0}.'.format(p_mode))
        self._p_mode = p_mode
        # Check that h_mode is valid.
        if h_mode not in [BME280_OSAMPLE_1, BME280_OSAMPLE_2, BME280_OSAMPLE_4,
                        BME280_OSAMPLE_8, BME280_OSAMPLE_16]:
            raise ValueError(
                'Unexpected h_mode value {0}.'.format(h_mode))
        self._h_mode = h_mode
        # Check that standby is valid.
        if standby not in [BME280_STANDBY_0p5, BME280_STANDBY_62p5, BME280_STANDBY_125, BME280_STANDBY_250,
                        BME280_STANDBY_500, BME280_STANDBY_1000, BME280_STANDBY_10, BME280_STANDBY_20]:
            raise ValueError(
                'Unexpected standby value {0}.'.format(standby))
        self._standby = standby
        # Check that filter is valid.
        if filter not in [BME280_FILTER_off, BME280_FILTER_2, BME280_FILTER_4, BME280_FILTER_8, BME280_FILTER_16]:
            raise ValueError(
                'Unexpected filter value {0}.'.format(filter))
        self._filter = filter
        # Create I2C device.
        if i2c is None:
            import Adafruit_GPIO.I2C as I2C
            i2c = I2C
        # Create device, catch permission errors
        try:
            self._device = i2c.get_i2c_device(address, **kwargs)
        except IOError:
            print("Unable to communicate with sensor, check permissions.")
            exit()
        # Load calibration values.
        self._load_calibration()
        self._device.write8(BME280_REGISTER_CONTROL, 0x24)  # Sleep mode
        time.sleep(0.002)
        self._device.write8(BME280_REGISTER_CONFIG, ((standby << 5) | (filter << 2)))
        time.sleep(0.002)
        self._device.write8(BME280_REGISTER_CONTROL_HUM, h_mode)  # Set Humidity Oversample
        self._device.write8(BME280_REGISTER_CONTROL, ((t_mode << 5) | (p_mode << 2) | 3))  # Set Temp/Pressure Oversample and enter Normal mode
        self.t_fine = 0.0 
Example #17
Source File: Adafruit_AMG88xx.py    From Adafruit_AMG88xx_python with MIT License 4 votes vote down vote up
def __init__(self, mode=AMG88xx_NORMAL_MODE, address=AMG88xx_I2CADDR, i2c=None, **kwargs):
		self._logger = logging.getLogger('Adafruit_BMP.BMP085')
		# Check that mode is valid.
		if mode not in [AMG88xx_NORMAL_MODE, AMG88xx_SLEEP_MODE, AMG88xx_STAND_BY_60, AMG88xx_STAND_BY_10]:
			raise ValueError('Unexpected mode value {0}.  Set mode to one of AMG88xx_NORMAL_MODE, AMG88xx_SLEEP_MODE, AMG88xx_STAND_BY_60, or AMG88xx_STAND_BY_10'.format(mode))
		self._mode = mode
		# Create I2C device.
		if i2c is None:
			import Adafruit_GPIO.I2C as I2C
			i2c = I2C
		self._device = i2c.get_i2c_device(address, **kwargs)

		#set up the registers
		self._pctl = Adafruit_bitfield([('PCTL', 8)])
		self._rst = Adafruit_bitfield([('RST', 8)])
		self._fpsc = Adafruit_bitfield([('FPS', 1)])
		self._intc = Adafruit_bitfield([('INTEN', 1), ('INTMOD', 1)])
		self._stat = Adafruit_bitfield([('unused', 1), ('INTF', 1), ('OVF_IRS', 1), ('OVF_THS', 1)])
		self._sclr = Adafruit_bitfield([('unused', 1), ('INTCLR', 1), ('OVS_CLR', 1), ('OVT_CLR', 1)])
		self._ave = Adafruit_bitfield([('unused', 5), ('MAMOD', 1)])

		self._inthl = Adafruit_bitfield([('INT_LVL_H', 8)])
		self._inthh = Adafruit_bitfield([('INT_LVL_H', 4)])

		self._intll = Adafruit_bitfield([('INT_LVL_H', 8)])
		self._intlh = Adafruit_bitfield([('INT_LVL_L', 4)])

		self._ihysl = Adafruit_bitfield([('INT_HYS', 8)])
		self._ihysh = Adafruit_bitfield([('INT_HYS', 4)])

		self._tthl = Adafruit_bitfield([('TEMP', 8)])
		self._tthh = Adafruit_bitfield([('TEMP',3), ('SIGN',1)])

		#enter normal mode
		self._pctl.PCTL = self._mode
		self._device.write8(AMG88xx_PCTL, self._pctl.get())

		#software reset
		self._rst.RST = AMG88xx_INITIAL_RESET
		self._device.write8(AMG88xx_RST, self._rst.get())

		#disable interrupts by default
		self.disableInterrupt()

		#set to 10 FPS
		self._fpsc.FPS = AMG88xx_FPS_10
		self._device.write8(AMG88xx_FPSC, self._fpsc.get())