本文整理汇总了Python中smbus.SMBus.read_byte_data方法的典型用法代码示例。如果您正苦于以下问题:Python SMBus.read_byte_data方法的具体用法?Python SMBus.read_byte_data怎么用?Python SMBus.read_byte_data使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类smbus.SMBus
的用法示例。
在下文中一共展示了SMBus.read_byte_data方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: getPressure
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_byte_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
示例2: __init__
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_byte_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
示例3: __init__
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_byte_data [as 别名]
class IMU:
def __init__(self):
# 0 for R-Pi Rev. 1, 1 for Rev. 2
self.bus = SMBus(1)
#initialise the accelerometer
self.writeACC(CTRL_REG1_XM, 0b01100111) #z,y,x axis enabled, continuos update, 100Hz data rate
self.writeACC(CTRL_REG2_XM, 0b00100000) #+/- 16G full scale
def writeACC(self, register,value):
self.bus.write_byte_data(ACC_ADDRESS , register, value)
return -1
def readACCx(self):
acc_l = self.bus.read_byte_data(ACC_ADDRESS, OUT_X_L_A)
acc_h = self.bus.read_byte_data(ACC_ADDRESS, OUT_X_H_A)
acc_combined = (acc_l | acc_h <<8)
return acc_combined if acc_combined < 32768 else acc_combined - 65536
def readACCy(self):
acc_l = self.bus.read_byte_data(ACC_ADDRESS, OUT_Y_L_A)
acc_h = self.bus.read_byte_data(ACC_ADDRESS, OUT_Y_H_A)
acc_combined = (acc_l | acc_h <<8)
return acc_combined if acc_combined < 32768 else acc_combined - 65536
def readACCz(self):
acc_l = self.bus.read_byte_data(ACC_ADDRESS, OUT_Z_L_A)
acc_h = self.bus.read_byte_data(ACC_ADDRESS, OUT_Z_H_A)
acc_combined = (acc_l | acc_h <<8)
return acc_combined if acc_combined < 32768 else acc_combined - 65536
def read_simple_accelerometer(self):
ACCx = self.readACCx()
ACCy = self.readACCy()
ACCz = self.readACCz()
return (ACCx, ACCy, ACCz)
def get_acceleration_norm(self):
x = self.readACCx() * 0.732 / 1000
y = self.readACCy() * 0.732 / 1000
z = self.readACCz() * 0.732 / 1000
return math.sqrt(x*x+y*y+z*z);
示例4: MTSMBus
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_byte_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)
示例5: DetectCap
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_byte_data [as 别名]
def DetectCap(i2c_addr, i2c_bus, product_id):
bus = SMBus(i2c_bus)
try:
if bus.read_byte_data(i2c_addr, R_PRODUCT_ID) == product_id:
return True
else:
return False
except IOError:
return False
示例6: GetAlt
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_byte_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
示例7: TemperatureSensor
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_byte_data [as 别名]
class TemperatureSensor(object):
def __init__(self):
log.debug("Initialising temperature/humidity sensor")
self.bus = SMBus(1)
self.address = 0x40
self.MEASURE_RELATIVE_TEMPERATURE = 0xE3
self.MEASURE_RELATIVE_HUMIDITY = 0xE5
self.READ_FIRMWARE_VERSION = '\x84\xb8'
def read_firmware_version(self):
self.bus.write_byte(self.address, 0x84)
self.bus.write_byte(self.address, 0xB8)
response = self.bus.read_byte(self.address)
print 'firmware version:', response
# response = self.bus.read_byte_data(self.address,
# 0xB8)
# print 'firmware version:', response
return response
def read_temperature(self):
# Return dummy data for now
# return 20. + random.random()
response = self.bus.read_byte_data(self.address,
self.MEASURE_RELATIVE_TEMPERATURE)
print 'temperature:', response
return response
def read_humidity(self):
# Return dummy data for now
# return random.randint(40, 90)
response = self.bus.read_byte_data(self.address,
self.MEASURE_RELATIVE_HUMIDITY)
print 'humidity:', response
return response
示例8: readMPL3155
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_byte_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()
示例9: PressureSensor
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_byte_data [as 别名]
class PressureSensor(object):
def __init__(self):
log.debug("Initialising pressure sensor")
self.bus = SMBus(1)
self.address = 0x60
def read_pressure(self):
# Return dummy data for now
# return 100. + random.random()
response = self.bus.read_byte_data(self.address,
0x02)
print 'data:', response
return response
示例10: Device
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_byte_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
示例11: Magnetometer
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_byte_data [as 别名]
class Magnetometer(RPCComponentThreaded):
"""
HMC5883L I2C magnetometer
"""
def main_prepare(self):
self.bus = SMBus(self.Configuration['bus'])
self.address = self.Configuration['address']
self.bus.write_byte_data(self.address, 2, 0)
self.values = [0, 0, 0]
def mainthread(self):
for i, offset in enumerate(range(3, 8, 2)):
msb = self.bus.read_byte_data(self.address, offset)
lsb = self.bus.read_byte_data(self.address, offset + 1)
value = (msb << 8) + lsb
if value >= 32768:
value -= 65536
self.values[i] = value
for subscriber, func in self.subscribers.items():
self.send(Message(sender=self.name, recipient=subscriber, func=func,
arg={'x': self.values[0], 'y': self.values[2], 'z': self.values[1]}),
"outbox")
sleep(0.1)
示例12: get
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_byte_data [as 别名]
def get(self):
lStatus = "ok"
lCommand = ""
lValue = ""
lArgs = self.__mParser.parse_args()
lBusId = int(lArgs['bus_id'], 0)
lBus = SMBus(lBusId)
lAddress = int(lArgs['address'], 0)
try:
if lArgs['cmd'] is None:
lValue = lBus.read_byte(lAddress)
else:
lCommand = int(lArgs['cmd'], 0)
lValue = lBus.read_byte_data(lAddress, lCommand)
except IOError, pExc:
lStatus = "Error reading from bus: " + str(pExc)
示例13: __init__
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_byte_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)
示例14: MyIMU
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_byte_data [as 别名]
#.........这里部分代码省略.........
YP_10 = 0.0
YP_11 = 0.0
KFangleX = 0.0
KFangleY = 0.0
def __init__(self):
from smbus import SMBus
self.b = SMBus(self.busNum)
self.detectTest()
#Set up the chips for reading ----------------------
self.b.write_byte_data(self.LSM, self.LSM_CTRL_1, 0b1010111) # enable accelerometer, 50 hz sampling
self.b.write_byte_data(self.LSM, self.LSM_CTRL_2, 0x00) #set +/- 2g full scale
self.b.write_byte_data(self.LSM, self.LSM_CTRL_5, 0b01100100) #high resolution mode, thermometer off, 6.25hz ODR
self.b.write_byte_data(self.LSM, self.LSM_CTRL_6, 0b00100000) # set +/- 4 gauss full scale
self.b.write_byte_data(self.LSM, self.LSM_CTRL_7, 0x00) #get magnetometer out of low power mode
self.b.write_byte_data(self.LGD, self.LGD_CTRL_1, 0x0F) #turn on gyro and set to normal mode
#Read data from the chips ----------------------
#while True:
# time.sleep(0.5)
# print self.readSensors()
self.newRead()
def twos_comp_combine(self, msb, lsb):
twos_comp = 256*msb + lsb
if twos_comp >= 32768:
return twos_comp - 65536
else:
return twos_comp
def detectTest(self):
#Ensure chip is detected properly on the bus ----------------------
if self.b.read_byte_data(self.LSM, self.LSM_WHOAMI_ADDRESS) == self.LSM_WHOAMI_CONTENTS:
print 'LSM303D detected successfully on I2C bus '+str(self.busNum)+'.'
else:
print 'No LSM303D detected on bus on I2C bus '+str(self.busNum)+'.'
if self.b.read_byte_data(self.LGD, self.LGD_WHOAMI_ADDRESS) == self.LGD_WHOAMI_CONTENTS:
print 'L3GD20H detected successfully on I2C bus '+str(self.busNum)+'.'
else:
print 'No L3GD20H detected on bus on I2C bus '+str(self.busNum)+'.'
def readSensors(self):
data = (self.readMagX(), self.readMagY(), self.readMagZ(), self.readAccX(), self.readAccY(), self.readAccZ(), self.readGyroX(), self.readGyroY(), self.readGyroZ())
return data
def readMagX(self):
return self.twos_comp_combine(self.b.read_byte_data(self.LSM, self.LSM_MAG_X_MSB), self.b.read_byte_data(self.LSM, self.LSM_MAG_X_LSB))
def readMagY(self):
return self.twos_comp_combine(self.b.read_byte_data(self.LSM, self.LSM_MAG_Y_MSB), self.b.read_byte_data(self.LSM, self.LSM_MAG_Y_LSB))
def readMagZ(self):
return self.twos_comp_combine(self.b.read_byte_data(self.LSM, self.LSM_MAG_Z_MSB), self.b.read_byte_data(self.LSM, self.LSM_MAG_Z_LSB))
def readAccX(self):
return self.twos_comp_combine(self.b.read_byte_data(self.LSM, self.LSM_ACC_X_MSB), self.b.read_byte_data(self.LSM, self.LSM_ACC_X_LSB))
def readAccY(self):
return self.twos_comp_combine(self.b.read_byte_data(self.LSM, self.LSM_ACC_Y_MSB), self.b.read_byte_data(self.LSM, self.LSM_ACC_Y_LSB))
def readAccZ(self):
return self.twos_comp_combine(self.b.read_byte_data(self.LSM, self.LSM_ACC_Z_MSB), self.b.read_byte_data(self.LSM, self.LSM_ACC_Z_LSB))
示例15: SMBus
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import read_byte_data [as 别名]
from smbus import SMBus
import time
# Special Chars
deg = u'\N{DEGREE SIGN}'
# 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))
# Read sensor data
print "Waiting for data..."
status = bus.read_byte_data(ADDR,0x00)
while (status & 0x08) == 0:
#print bin(status)
status = bus.read_byte_data(ADDR,0x00)
time.sleep(0.5)