本文整理汇总了Python中smbus.SMBus.read_i2c_block_data方法的典型用法代码示例。如果您正苦于以下问题:Python SMBus.read_i2c_block_data方法的具体用法?Python SMBus.read_i2c_block_data怎么用?Python SMBus.read_i2c_block_data使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类smbus.SMBus
的用法示例。
在下文中一共展示了SMBus.read_i2c_block_data方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: HTU21D
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_i2c_block_data [as 别名]
class HTU21D():
"""Class for accessing HTU21D sensors via I2C.
Code taken from https://github.com/jasiek/HTU21D.
Args:
busno (int): The I2C bus (0 or 1, default is 1).
address (byte): The I2C address of the sensor.
"""
CMD_TRIG_TEMP_HM = 0xE3
CMD_TRIG_HUMID_HM = 0xE5
CMD_TRIG_TEMP_NHM = 0xF3
CMD_TRIG_HUMID_NHM = 0xF5
CMD_WRITE_USER_REG = 0xE6
CMD_READ_USER_REG = 0xE7
CMD_RESET = 0xFE
def __init__(self, busno=1, address=config.SENSOR_ID_HUMIDITY_EXT):
self.bus = SMBus(busno)
self.i2c_address = address
def read_temperature(self):
self.reset()
msb, lsb, crc = self.bus.read_i2c_block_data(
self.i2c_address, self.CMD_TRIG_TEMP_HM, 3)
return -46.85 + 175.72 * (msb * 256 + lsb) / 65536
def read_humidity(self):
self.reset()
msb, lsb, crc = self.bus.read_i2c_block_data(
self.i2c_address, self.CMD_TRIG_HUMID_HM, 3)
return (-6 + 125 * (msb * 256 + lsb) / 65536.0) / 100.0
def reset(self):
self.bus.write_byte(self.i2c_address, self.CMD_RESET)
示例2: AM2320
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_i2c_block_data [as 别名]
class AM2320(object):
"""
AM2320 temperature and humidity sensor class.
:param interface: I2C interface id.
:type interface: :int
:param sensor_address: AM2320 sensor I2C address. Optional, default 0x5C (92).
:type sensor_address: int
"""
def __init__(self, interface, sensor_address=0x5c):
self.interface = interface
self.address = sensor_address
self.temperature = -1000.0
self.humidity = -1
self.bus = SMBus(interface)
def _read_raw(self, command, regaddr, regcount):
try:
self.bus.write_i2c_block_data(self.address, 0x00, [])
self.bus.write_i2c_block_data(self.address, command, [regaddr, regcount])
sleep(0.002)
buf = self.bus.read_i2c_block_data(self.address, 0, 8)
except IOError, exc:
raise CommunicationError(str(exc))
buf_str = "".join(chr(x) for x in buf)
crc = unpack('<H', buf_str[-2:])[0]
if crc != self._am_crc16(buf[:-2]):
raise CommunicationError("AM2320 CRC error.")
return buf_str[2:-2]
示例3: __init__
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_i2c_block_data [as 别名]
class HTU21D:
def __init__(self, busno):
self.bus = SMBus(busno)
def read_temperature(self):
self.reset()
msb, lsb, crc = self.bus.read_i2c_block_data(I2C_ADDR, CMD_TRIG_TEMP_HM, 3)
return -46.85 + 175.72 * (msb * 256 + lsb) / 65536
def read_humidity(self):
self.reset()
msb, lsb, crc = self.bus.read_i2c_block_data(I2C_ADDR, CMD_TRIG_HUMID_HM, 3)
return -6 + 125 * (msb * 256 + lsb) / 65536.0
def reset(self):
self.bus.write_byte(I2C_ADDR, CMD_RESET)
示例4: readMPL3155
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_i2c_block_data [as 别名]
def readMPL3155():
# I2C Constants
ADDR = 0x60
CTRL_REG1 = 0x26
PT_DATA_CFG = 0x13
bus = SMBus(2)
who_am_i = bus.read_byte_data(ADDR, 0x0C)
print hex(who_am_i)
if who_am_i != 0xc4:
tfile = open('temp.txt', 'w')
tfile.write("Barometer funktioniert nicht")
tfile.close()
exit(1)
# Set oversample rate to 128
setting = bus.read_byte_data(ADDR, CTRL_REG1)
#newSetting = setting | 0x38
newSetting = 0x38
bus.write_byte_data(ADDR, CTRL_REG1, newSetting)
# Enable event flags
bus.write_byte_data(ADDR, PT_DATA_CFG, 0x07)
# Toggel One Shot
setting = bus.read_byte_data(ADDR, CTRL_REG1)
if (setting & 0x02) == 0:
bus.write_byte_data(ADDR, CTRL_REG1, (setting | 0x02))
# Read sensor data
print "Waiting for data..."
status = bus.read_byte_data(ADDR,0x00)
#while (status & 0x08) == 0:
while (status & 0x06) == 0:
#print bin(status)
status = bus.read_byte_data(ADDR,0x00)
time.sleep(1)
print "Reading sensor data..."
status = bus.read_byte_data(ADDR,0x00)
p_data = bus.read_i2c_block_data(ADDR,0x01,3)
t_data = bus.read_i2c_block_data(ADDR,0x04,2)
p_msb = p_data[0]
p_csb = p_data[1]
p_lsb = p_data[2]
t_msb = t_data[0]
t_lsb = t_data[1]
pressure = (p_msb << 10) | (p_csb << 2) | (p_lsb >> 6)
p_decimal = ((p_lsb & 0x30) >> 4)/4.0
celsius = t_msb + (t_lsb >> 4)/16.0
tfile = open('temp.txt', 'w')
tfile.write("Luftdruck "+str(pressure/100)+" Hektopascal. ")
tfile.write("Temperatur "+str("{0:.1f}".format(celsius).replace('.',','))+" Grad Celsius")
tfile.close()
示例5: Bus_Hepler_i2c
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_i2c_block_data [as 别名]
class Bus_Hepler_i2c():
def __init__(self, bus_location=1):
self.bus = SMBus(bus_location)
def write_byte(self, address, register, byte):
self.bus.write_i2c_block_data(address, register, [byte])
def read_block_data(self, address, cmd):
return self.bus.read_i2c_block_data(address, cmd)
示例6: MTSMBus
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_i2c_block_data [as 别名]
class MTSMBus(I2CBus):
""" Multi-thread compatible SMBus bus.
This is just a wrapper of SMBus, serializing I/O on the bus for use
in multi-threaded context and adding _i2c_ variants of block transfers.
"""
def __init__(self, bus_id=1, **kwargs):
"""
:param int bus_id: the SMBus id (see Raspberry Pi documentation)
:param kwargs: parameters transmitted to :py:class:`smbus.SMBus` initializer
"""
I2CBus.__init__(self, **kwargs)
self._bus = SMBus(bus_id)
# I/O serialization lock
self._lock = threading.Lock()
def read_byte(self, addr):
with self._lock:
return self._bus.read_byte(addr)
def write_byte(self, addr, data):
with self._lock:
self._bus.write_byte(addr, data)
def read_byte_data(self, addr, reg):
with self._lock:
return self._bus.read_byte_data(addr, reg)
def write_byte_data(self, addr, reg, data):
with self._lock:
self._bus.write_byte_data(addr, reg, data)
def read_word_data(self, addr, reg):
with self._lock:
return self._bus.read_word_data(addr, reg)
def write_word_data(self, addr, reg, data):
with self._lock:
self._bus.write_word_data(addr, reg, data)
def read_block_data(self, addr, reg):
with self._lock:
return self._bus.read_block_data(addr, reg)
def write_block_data(self, addr, reg, data):
with self._lock:
self._bus.write_block_data(addr, reg, data)
def read_i2c_block_data(self, addr, reg, count):
with self._lock:
return self._bus.read_i2c_block_data(addr, reg, count)
def write_i2c_block_data(self, addr, reg, data):
with self._lock:
self._bus.write_i2c_block_data(addr, reg, data)
示例7: Device
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_i2c_block_data [as 别名]
class Device(object):
def __init__(self, address, bus):
self._bus = SMBus(bus)
self._address = address
def writeRaw8(self, value):
value = value & 0xff
self._bus.write_byte(self._address, value)
def readRaw8(self):
result = self._bus.read_byte(self._address) & 0xff
return result
def write8(self, register, value):
value = value & 0xff
self._bus.write_byte_data(self._address, register, value)
def readU8(self, register):
result = self._bus.read_byte_data(self._address, register) & 0xFF
return result
def readS8(self, register):
result = self.readU8(register)
if result > 127:
result -= 256
return result
def write16(self, register, value):
value = value & 0xffff
self._bus.write_word_data(self._address, register, value)
def readU16(self, register, little_endian = True):
result = self._bus.read_word_data(self._address,register) & 0xFFFF
if not little_endian:
result = ((result << 8) & 0xFF00) + (result >> 8)
return result
def readS16(self, register, little_endian = True):
result = self.readU16(register, little_endian)
if result > 32767:
result -= 65536
return result
def writeList(self, register, data):
self._bus.write_i2c_block_data(self._address, register, data)
def readList(self, register, length):
results = self._bus.read_i2c_block_data(self._address, register, length)
return results
示例8: getPressure
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_i2c_block_data [as 别名]
def getPressure():
# I2C Constants
ADDR = 0x60
CTRL_REG1 = 0x26
PT_DATA_CFG = 0x13
bus = SMBus(1)
who_am_i = bus.read_byte_data(ADDR, 0x0C)
print hex(who_am_i)
if who_am_i != 0xc4:
print "Device not active."
exit(1)
# Set oversample rate to 128
setting = bus.read_byte_data(ADDR, CTRL_REG1)
newSetting = setting | 0x38
bus.write_byte_data(ADDR, CTRL_REG1, newSetting)
# Enable event flags
bus.write_byte_data(ADDR, PT_DATA_CFG, 0x07)
# Toggel One Shot
setting = bus.read_byte_data(ADDR, CTRL_REG1)
if (setting & 0x02) == 0:
bus.write_byte_data(ADDR, CTRL_REG1, (setting | 0x02))
status = -1;
while (status & 0x08) == 0:
status = bus.read_byte_data(ADDR,0x00)
time.sleep(.5);
print "Reading sensor data..."
p_data = bus.read_i2c_block_data(ADDR,0x01,3)
t_data = bus.read_i2c_block_data(ADDR,0x04,2)
status = bus.read_byte_data(ADDR,0x00)
print "status: "+bin(status)
p_msb = p_data[0]
p_csb = p_data[1]
p_lsb = p_data[2]
t_msb = t_data[0]
t_lsb = t_data[1]
pressure = (p_msb << 10) | (p_csb << 2) | (p_lsb >> 6)
p_decimal = ((p_lsb & 0x30) >> 4)/4.0
return pressure+p_decimal
示例9: __init__
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_i2c_block_data [as 别名]
class TMP100:
""" Class to read the onboard temperatur Sensor"""
_SLAVE_ADDR = 0x49
_CONFIG_REG = 0x01
_TEMP_REG = 0x00
# config register
_CONFIG_REG_OS = 0x01
_CONFIG_REG_RES_9B = 0x00
_CONFIG_REG_RES_12B = 0x03
_CONFIG_REG_TRIG_OS = 0x80
def __init__(self,device_number = 1):
""" """
try:
self.bus = SMBus(device_number)
except Exception:
raise i2cError()
configList = [self._CONFIG_REG_OS, self._CONFIG_REG_RES_12B]
self.configTMP100(configList)
def configTMP100(self, list):
""" Write list elements to tmp100#s configuration register"""
reg = (list[1] << 5) + list[0]
# write to config register
self.bus.write_byte_data(self._SLAVE_ADDR,self._CONFIG_REG,reg)
if DEBUG:
# read config register back
tmpReg = self.bus.read_byte_data(self._SLAVE_ADDR,self._CONFIG_REG)
print(reg,tmpReg)
def getTemperature(self):
""" Get temperature readings """
# read first config register
config = self.bus.read_byte_data(self._SLAVE_ADDR,self._CONFIG_REG)
#trigger single shot
newConfig = config + self._CONFIG_REG_TRIG_OS
# write config register new value back
self.bus.write_byte_data(self._SLAVE_ADDR,self._CONFIG_REG,newConfig)
time.sleep(0.001) # wait a bit
#read temperature register
raw = self.bus.read_i2c_block_data(self._SLAVE_ADDR,self._TEMP_REG)[:2]
val = ((raw[0] << 8) + raw[1]) >> 4
#TODO: get resolution factor properly :)
return val*0.0625
示例10: GetAlt
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_i2c_block_data [as 别名]
def GetAlt():
# get the altitude from the MPL3115a2 device
print "GetAlt()"
# device address and register addresses
altAddress = 0x60
ctrlReg1 = 0x26
ptDataCfg = 0x13
# values
oversample128 = 0x38
oneShot = 0x02
altMode = 0x80
bus = SMBus(1)
for i in range(0, 5):
whoAmI = bus.read_byte_data(altAddress, 0x0C)
if whoAmI == 0xC4:
break
elif i == 4:
sys.exit()
else:
time.sleep(0.5)
bus.write_byte_data(altAddress, ptDataCfg, 0x07)
oldSetting = bus.read_byte_data(altAddress, ctrlReg1)
newSetting = oldSetting | oversample128 | oneShot | altMode
bus.write_byte_data(altAddress, ctrlReg1, newSetting)
status = bus.read_byte_data(altAddress, 0x00)
while (status & 0x08) == 0:
status = bus.read_byte_data(altAddress, 0x00)
time.sleep(0.5)
msb, csb, lsb = bus.read_i2c_block_data(altAddress, 0x01, 3)
alt = ((msb << 24) | (csb << 16) | (lsb << 8)) / 65536.0
if alt > (1 << 15):
alt -= 1 << 16
return alt
示例11: __init__
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_i2c_block_data [as 别名]
class MB85RC04:
def __init__(self, I2C_bus_number = 1, address = 0x50):
self.bus = SMBus(I2C_bus_number)
self.address = address
def readByte(self, registerAddress):
if(registerAddress > 255):
self.address = self.address | 1
registerAddress = registerAddress - 256
else:
self.address = self.address & 0xFE
return self.bus.read_byte_data(self.address, registerAddress)
def writeByte(self, registerAddress, data):
if(registerAddress > 255):
self.address = self.address | 1
registerAddress = registerAddress - 256
else:
self.address = self.address & 0xFE
self.bus.write_byte_data(self.address, registerAddress, data)
def readBytes(self, registerAddress):
if(registerAddress > 255):
self.address = self.address | 1
registerAddress = registerAddress - 256
else:
self.address = self.address & 0xFE
return self.bus.read_i2c_block_data(self.address, registerAddress)
def writeBytes(self, registerAddress, data):
if(registerAddress > 255):
self.address = self.address | 1
registerAddress = registerAddress - 256
else:
self.address = self.address & 0xFE
self.bus.write_i2c_block_data(self.address, registerAddress, data)
示例12: I2C
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_i2c_block_data [as 别名]
#.........这里部分代码省略.........
"""
combined = self._combineXLoLoHi(xloByte, loByte, hiByte)
return combined if combined < 8388608 else (combined - 16777216)
def _getSensorRawLoHi1(self, address, outRegs):
""" Return a scalar representing the combined raw signed 16 bit
value of the output registers of a one-dimensional sensor,
e.g. temperature.
'address' is the I2C slave address.
'outRegs' is a list of the output registers to read.
"""
# Read register outputs and combine low and high byte values
xl = self._readRegister(address, outRegs[0])
xh = self._readRegister(address, outRegs[1])
xVal = self._combineSignedLoHi(xl, xh)
# Return the scalar
return xVal
def _getSensorRawXLoLoHi1(self, address, outRegs):
""" Return a scalar representing the combined raw signed 24 bit
value of the output registers of a one-dimensional sensor,
e.g. temperature.
'address' is the I2C slave address.
'outRegs' is a list of the output registers to read.
"""
# Read register outputs and combine low and high byte values
xxl = self._readRegister(address, outRegs[0])
xl = self._readRegister(address, outRegs[1])
xh = self._readRegister(address, outRegs[2])
xVal = self._combineSignedXLoLoHi(xxl, xl, xh)
# Return the scalar
return xVal
def _getSensorRawLoHi3(self, address, outRegs):
""" Return a vector (i.e. list) representing the combined
raw signed 16 bit values of the output registers of a
3-dimensional (IMU) sensor.
'address' is the I2C slave address.
'outRegs' is a list of the output registers to read.
"""
# Read register outputs and combine low and high byte values
xl = self._readRegister(address, outRegs[0])
xh = self._readRegister(address, outRegs[1])
yl = self._readRegister(address, outRegs[2])
yh = self._readRegister(address, outRegs[3])
zl = self._readRegister(address, outRegs[4])
zh = self._readRegister(address, outRegs[5])
xVal = self._combineSignedLoHi(xl, xh)
yVal = self._combineSignedLoHi(yl, yh)
zVal = self._combineSignedLoHi(zl, zh)
# Return the vector
return [xVal, yVal, zVal]
def _readRegister(self, address, register):
""" Read a single I2C register. """
return self._i2c.read_byte_data(address, register)
def _readRegisters(self, address, register, count):
""" Read (up to 32) 'count' consecutive I2C registers. """
return self._i2c.read_i2c_block_data(address, register, count)
def _read(self, address):
""" Read a single byte from the I2C device without specifying a
register.
"""
return self._i2c.read_byte(address)
def _writeRegister(self, address, register, value):
""" Write a single byte to a I2C register. Return the value the
register had before the write.
"""
valueOld = self._readRegister(address, register)
self._i2c.write_byte_data(address, register, value)
return valueOld
def _write(self, address, value):
""" Write a single byte to the I2C device without specifying a
register.
"""
return self._i2c.write_byte(address, value)
def _testRegister(self, address, register):
""" Check, if a I2C register is readable/accessible. """
try:
return self._readRegister(address, register)
except:
return -1
示例13: SMBus
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_i2c_block_data [as 别名]
#https://www.pololu.com/file/download/LPS25H.pdf?file_id=0J761
from smbus import SMBus
busNum = 1
b = SMBus(busNum)
PTS = 0x5d #Device I2C slave address
PTS_WHOAMI = 0b1011101 #Device self-id
(chip_id, version) = b.read_i2c_block_data(PTS, 0xD0, 2)
print "Chip Id:", chip_id, "Version:", version
if b.read_byte_data(PTS, 0x0f) == PTS_WHOAMI:
print 'LPS25H detected successfully.'
else:
print 'No LPS25H detected on bus '+str(busNum)+'.'
示例14: __init__
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_i2c_block_data [as 别名]
class TemperaturePressure:
address = 0x77
oversampling = 3 # 0..3
def __init__(self):
# 0 for R-Pi Rev. 1, 1 for Rev. 2
self.bus = SMBus(1)
# Read whole calibration EEPROM data
cal = self.bus.read_i2c_block_data(self.address, 0xAA, 22)
# Convert byte data to word values
self.ac1 = get_short(cal, 0)
self.ac2 = get_short(cal, 2)
self.ac3 = get_short(cal, 4)
self.ac4 = get_ushort(cal, 6)
self.ac5 = get_ushort(cal, 8)
self.ac6 = get_ushort(cal, 10)
self.b1 = get_short(cal, 12)
self.b2 = get_short(cal, 14)
self.mb = get_short(cal, 16)
self.mc = get_short(cal, 18)
self.md = get_short(cal, 20)
return
def read(self):
# temperature conversion
self.bus.write_byte_data(self.address, 0xF4, 0x2E)
sleep(0.005)
(msb, lsb) = self.bus.read_i2c_block_data(self.address, 0xF6, 2)
ut = (msb << 8) + lsb
# pressure conversion
self.bus.write_byte_data(self.address, 0xF4, 0x34 + (self.oversampling << 6))
sleep(0.04)
(msb, lsb, xsb) = self.bus.read_i2c_block_data(self.address, 0xF6, 3)
up = ((msb << 16) + (lsb << 8) + xsb) >> (8 - self.oversampling)
# calculate temperature
x1 = ((ut - self.ac6) * self.ac5) >> 15
x2 = (self.mc << 11) / (x1 + self.md)
b5 = x1 + x2
t = (b5 + 8) >> 4
# calculate pressure
b6 = b5 - 4000
b62 = b6 * b6 >> 12
x1 = (self.b2 * b62) >> 11
x2 = self.ac2 * b6 >> 11
x3 = x1 + x2
b3 = (((self.ac1 * 4 + x3) << self.oversampling) + 2) >> 2
x1 = self.ac3 * b6 >> 13
x2 = (self.b1 * b62) >> 16
x3 = ((x1 + x2) + 2) >> 2
b4 = (self.ac4 * (x3 + 32768)) >> 15
b7 = (up - b3) * (50000 >> self.oversampling)
p = (b7 * 2) / b4
x1 = (p >> 8) * (p >> 8)
x1 = (x1 * 3038) >> 16
x2 = (-7357 * p) >> 16
p = p + ((x1 + x2 + 3791) >> 4)
return (t/10.0, p / 100)
示例15: SMBus
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_i2c_block_data [as 别名]
# read Atmel AT30TSE004A temp through I2C
from smbus import SMBus
i2c = SMBus(0)
atmelraw = i2c.read_i2c_block_data(0x1e,0x05,2)
atmeltemp = 16.0*(atmelraw[0] & 0xf) + (atmelraw[1] & 0xfe)/16.0
print "Atmel temp: ", atmeltemp
# read Zynq XADC temp through IIO driver (XAPP1172, UG480)
f = open('/sys/bus/iio/devices/iio:device0/in_temp0_raw','r')
zynqraw = f.read()
f.close()
zynqtemp = int(zynqraw)*503.975/4096 - 273.15
print "Zynq temp: ", zynqtemp