def __init__(self, i2c_address, i2c_dev=None, bit_width=8, registers=None): self._bit_width = bit_width self.locked = {} self.registers = {} self.values = {} if type(i2c_address) is list: self._i2c_addresses = i2c_address self._i2c_address = i2c_address[0] else: self._i2c_addresses = [i2c_address] self._i2c_address = i2c_address self._i2c = i2c_dev if self._i2c is None: from sensor_modules.drivers.smbus2 import smbus2 as smbus self._i2c = smbus.SMBus(1) for register in registers: self.locked[register.name] = False self.values[register.name] = 0 self.registers[register.name] = register self.__dict__[register.name] = _RegisterProxy(self, register)
def __init__(self, i2c_dev=None): """Initialise virtual register class. :param bus: SMBus bus ID """ if i2c_dev is None: from sensor_modules.drivers.smbus2 import smbus2 as smbus self._i2c_bus = smbus.SMBus(1) else: self._i2c_bus = i2c_dev
def __init__(self, i2c=None, address=0x74): """Initialise the IS31FL3731. :param i2c: smbus-compatible i2c object :param address: 7-bit i2c address of attached IS31FL3731 """ self.i2c = i2c self.address = address self.clear() if self.i2c is not None: for attr in ('read_byte_data', 'write_byte_data', 'write_i2c_block_data'): if not hasattr(self.i2c, attr): raise RuntimeError( 'i2c transport must implement: "{}"'.format(attr)) if self.i2c is None: try: from sensor_modules.drivers.smbus2 import smbus2 as smbus self.i2c = smbus.SMBus(1) except ImportError as e: raise ImportError( 'You must supply an i2c device or install the smbus library.' ) except IOError as e: if hasattr(e, 'errno') and e.errno == 2: e.strerror += "\n\nMake sure you've enabled i2c in your Raspberry Pi configuration.\n" raise e try: self.reset() except IOError as e: if hasattr(e, 'errno') and e.errno == 5: e.strerror += '\n\nMake sure your Scroll pHAT HD is attached, and double-check your soldering.\n' raise e for frame in reversed(range(_NUM_FRAMES)): self.update_frame(frame) self.show_frame(frame) self.set_mode(_PICTURE_MODE) self.set_audiosync(False)
def __init__(self, i2c_addr=PA1010D_ADDR, debug=False): self._i2c_addr = i2c_addr self._i2c = smbus.SMBus(1) self._debug = debug self.timestamp = None self.latitude = None self.longitude = None self.altitude = None self.num_sats = None self.gps_qual = None self.pdop = None self.hdop = None self.vdop = None self.speed_over_ground = None self.mode_fix_type = None
def __init__(self, i2c_addr=constants.I2C_ADDR_PRIMARY, i2c_device=None): """Initialise BME680 sensor instance and verify device presence. :param i2c_addr: i2c address of BME680 :param i2c_device: Optional SMBus-compatible instance for i2c transport """ BME680Data.__init__(self) self.i2c_addr = i2c_addr self._i2c = i2c_device if self._i2c is None: from sensor_modules.drivers.smbus2 import smbus2 as smbus self._i2c = smbus.SMBus(1) try: self.chip_id = self._get_regs(constants.CHIP_ID_ADDR, 1) if self.chip_id != constants.CHIP_ID: raise RuntimeError('BME680 Not Found. Invalid CHIP ID: 0x{0:02x}'.format(self.chip_id)) except IOError: raise RuntimeError("Unable to identify BME680 at 0x{:02x} (IOError)".format(self.i2c_addr)) self._variant = self._get_regs(constants.CHIP_VARIANT_ADDR, 1) self.soft_reset() self.set_power_mode(constants.SLEEP_MODE) self._get_calibration_data() self.set_humidity_oversample(constants.OS_2X) self.set_pressure_oversample(constants.OS_4X) self.set_temperature_oversample(constants.OS_8X) self.set_filter(constants.FILTER_SIZE_3) if self._variant == constants.VARIANT_HIGH: self.set_gas_status(constants.ENABLE_GAS_MEAS_HIGH) else: self.set_gas_status(constants.ENABLE_GAS_MEAS_LOW) self.set_temp_offset(0) self.get_sensor_data()
def set_offsets(self, offset_x=0, offset_y=0, offset_z=0): if (-0.5 <= offset_z <= 0.5 and -0.5 <= offset_y <= 0.5 and -0.5 <= offset_x <= 0.5): x = self.twos_comp_to_bin(int(-offset_x / 0.0039)) y = self.twos_comp_to_bin(int(-offset_y / 0.0039)) z = self.twos_comp_to_bin(int(-offset_z / 0.0039)) self.set_raw_offsets(x, y, z) else: raise ValueError('Offset must be between -0.5g to 0.5g') if __name__ == "__main__": from sensor_modules.drivers.smbus2 import smbus2 as smbus bus = smbus.SMBus(1) accel = MSA301(i2c_dev=bus) # print(dir(accel._msa301.INTERRUPT_ENABLE_1)) accel.reset() print(hex(accel.get_part_id())) accel.set_power_mode('normal') accel.set_range(2) accel.set_resolution(14) accel.disable_all_interrupts() accel.set_power_mode('normal') accel.enable_interrupt([ 'double_tap_interrupt', 'freefall_interrupt', 'y_active_interrupt', 'freefall_interrupt' ]) print(' {0} have been enabled '.format(accel.get_interrupt())) print('double tap near sensor')
"""Return True if LED is on.""" if self.status == 1: return True else: return False def is_off(self): """Return True if LED is off.""" if self.status == 0: return True else: return False bus = None if GPIO.RPI_REVISION == 2 or GPIO.RPI_REVISION == 3: bus = smbus.SMBus(1) else: bus = smbus.SMBus(0) GPIO.setwarnings(False) GPIO.setmode(GPIO.BCM) GPIO.setup(4, GPIO.OUT) GPIO.output(4, 0) leds = LEDS() # Only initialized to turn off LEDs light = TCS3472(i2c_bus=bus) weather = BMP280(i2c_addr=0x77, i2c_dev=bus) motion = LSM303D(i2c_dev=bus) # analog = ADS1015(i2c_dev=bus)