def __init__(self, side_str, device_addr=None, transposition=(0, 1, 2), scaling=(1, 1, 1)): self._accel = Vector3d(transposition, scaling, self._accel_callback, sensor_type='accel') self._gyro = Vector3d(transposition, scaling, self._gyro_callback, sensor_type='gyro') self.buf1 = bytearray( 1) # Pre-allocated buffers for reads: allows reads to self.buf2 = bytearray(2) # be done in interrupt handlers self.buf3 = bytearray(3) self.buf6 = bytearray(6) sleep_ms(200) # Ensure PSU and device have settled if isinstance(side_str, str): # Non-pyb targets may use other than X or Y self._mpu_i2c = I2C(side_str) elif hasattr(side_str, 'readfrom_mem_into' ): # Soft or hard I2C instance. See issue #3097 self._mpu_i2c = side_str else: raise ValueError("Invalid I2C instance") if device_addr is None: devices = set(self._mpu_i2c.scan()) mpus = devices.intersection(set(self._mpu_addr)) number_of_mpus = len(mpus) if number_of_mpus == 0: raise MPUException("No MPU's detected") elif number_of_mpus == 1: self.mpu_addr = mpus.pop() else: raise ValueError( "Two MPU's detected: must specify a device address") else: if device_addr not in (0, 1): raise ValueError('Device address must be 0 or 1') self.mpu_addr = self._mpu_addr[device_addr] self.chip_id # Test communication by reading chip_id: throws exception on error # Can communicate with chip. Set it up. self.wake() # wake it up self.passthrough = True # Enable mag access from main I2C bus self.accel_range = 0 # default to highest sensitivity self.gyro_range = 0 # Likewise for gyro
def __init__(self, addr, blen=1): #global BUS #BUS = smbus.SMBus(1) self.bus = I2C() self.addr = addr self.blen = blen self.send_command(0x33) # Must initialize to 8-line mode at first time.sleep(0.005) self.send_command(0x32) # Then initialize to 4-line mode time.sleep(0.005) self.send_command(0x28) # 2 Lines & 5*7 dots time.sleep(0.005) self.send_command(0x0C) # Enable display without cursor time.sleep(0.005) self.send_command(0x01) # Clear Screen self.bus._i2c_write_byte(self.addr, 0x08)
def __init__(self, width, height, dc=None, sclk=None, din=None, cs=None, gpio=None, spi=None, i2c_bus=None, i2c_address=SSD1306_I2C_ADDRESS, i2c=None): self._log = logging.getLogger('Adafruit_SSD1306.SSD1306Base') self._spi = None self._i2c = I2C() self.addr = i2c_address self.width = width self.height = height self._pages = height//8 self._buffer = [0]*(width*self._pages) # Default to platform GPIO if not provided. self._gpio = gpio # if self._gpio is None: # self._gpio = GPIO.get_platform_gpio() # Setup reset pin. if spi is not None: self._log.debug('Using hardware SPI') self._spi = spi self._spi.set_clock_hz(8000000) # Handle software SPI elif sclk is not None and din is not None and cs is not None: self._log.debug('Using software SPI') self._spi = SPI.BitBang(self._gpio, sclk, din, None, cs) # Handle hardware I2C # elif i2c is not None: # self._log.debug('Using hardware I2C with custom I2C provider.') # self._i2c = i2c.get_i2c_device(i2c_address) else: self._log.debug('Using hardware I2C with platform I2C provider.') # import Adafruit_GPIO.I2C as I2C # if i2c_bus is None: # self._i2c = I2C.get_i2c_device(i2c_address) # else: # self._i2c = I2C.get_i2c_device(i2c_address, busnum=i2c_bus) # Initialize DC pin if using SPI. if self._spi is not None: if dc is None: raise ValueError('DC pin must be provided when using SPI.') self._dc = dc self._gpio.setup(self._dc, GPIO.OUT)
from sensor_hat.mpu9250 import MPU9250 from ezblock import I2C i2c = I2C() imu = MPU9250(i2c) imu.start_imu() def forever(): while True: imu.accel_gyro_calibrate() if __name__ == "__main__": forever()
def __init__(self, address=0x53): self.i2c = I2C() self.address = address