def get_humidity(self): """Reads the humidity from the sensor. This call blocks for 250ms to allow the sensor to return the data""" self.transaction( writing_bytes(self._I2C_ADDRESS, self._CMD_HUMIDITY_NO_HOLD)) time.sleep(0.250) data = self.transaction(reading(self._I2C_ADDRESS, 3))[0] if _calculate_checksum(data, 2) != data[2]: raise ChecksumFailedError("Humidity checksum failed.") else: return _get_humidity_from_buffer(data)
def get_temperature(self): """Reads the temperature from the sensor. This call blocks for 250ms to allow the sensor to return the data. """ self.transaction( writing_bytes(self._I2C_ADDRESS, self._CMD_TEMPERATURE_NO_HOLD)) time.sleep(0.250) data = self.transaction(reading(self._I2C_ADDRESS, 3))[0] if _calculate_checksum(data, 2) != data[2]: raise ChecksumFailedError("Temperature checksum failed.") else: return _get_temperature_from_buffer(data)
def get_xyz(self, raw=False, res12=True): """Returns the x, y and z values as a dictionary. By default it returns signed values at 12-bit resolution. You can specify a lower resolution (8-bit) or request the raw register values. Signed values are in G's. You can alter the recording range with `set_g_range()`. :param raw: If True: return raw, unsigned data, else: sign values :type raw: boolean (default: False) :param res12: If True: read 12-bit resolution, else: 8-bit :type res12: boolean (default: True) """ # Since we can't read arbitary registers from this chip over I2C # (because there is no decent SMBus implementation, in Python 3 at # least) we have to just read the first 7 registers and pull the # XYZ data from that. # Notes: # - 12-bit resolution from OUT MSB and OUT LSB registers: # +--------+-----------------+ # | msb | lsb | # +--------+--------+--------+ # | 8 bits | 4 bits | 4 bits | # +--------+--------+--------+ # | value (12 bits) | unused | # +--------+--------+--------+ # - 8-bit resolution just from OUT MSB: # +--------+--------+ # | msb | lsb | # +--------+--------+ # | 8 bits | 8 bits | # +--------+--------+ # | value | unused | # +--------+--------+ # bulk read works buf = self.transaction(reading(self.i2c_address, 7))[0] # status = buf[0] if res12: x = (buf[1] << 4) | (buf[2] >> 4) y = (buf[3] << 4) | (buf[4] >> 4) z = (buf[5] << 4) | (buf[6] >> 4) else: x, y, z = buf[1], buf[3], buf[5] if not raw: # get range fsr = self._xyz_data_cfg_value & 0x03 g_ranges = {XYZ_DATA_CFG_FSR_2G: 2, XYZ_DATA_CFG_FSR_4G: 4, XYZ_DATA_CFG_FSR_8G: 8} g_range = g_ranges[fsr] resolution = 12 if res12 else 8 gmul = g_range / (2 ** (resolution - 1)) x = twos_complement(x, resolution) * gmul y = twos_complement(y, resolution) * gmul z = twos_complement(z, resolution) * gmul return {'x': x, 'y': y, 'z': z}