本文整理汇总了Python中smbus.SMBus.write_byte方法的典型用法代码示例。如果您正苦于以下问题:Python SMBus.write_byte方法的具体用法?Python SMBus.write_byte怎么用?Python SMBus.write_byte使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类smbus.SMBus
的用法示例。
在下文中一共展示了SMBus.write_byte方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: HTU21D
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import write_byte [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: PCF8574
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import write_byte [as 别名]
class PCF8574(object):
def __init__(self, bus_id, address):
super().__init__()
self.__bus = SMBus(bus_id)
self.__address = address
self.__value = self.__getRealValue()
@property
def value(self):
return self.__value
@value.setter
def value(self, value):
self.__bus.write_byte(self.__address,
(~value) & 0xff
)
self.__value = value
def flipBits(self, changeBits):
self.value ^= changeBits
def __getRealValue(self):
value = self.__bus.read_byte(self.__address)
return (~value) & 0xff
示例3: MTSMBus
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import write_byte [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)
示例4: Pines
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import write_byte [as 别名]
class Pines(object):
"""docstring for Pines"""
def __init__(self,address):
conf.estado = 0b11111111
self.address = address
self.bus = SMBus(1)
def cero(self,pin):
conf.estado &=~(1<<pin)
self.bus.write_byte(self.address , conf.estado)
return conf.estado
def uno(self , pin):
conf.estado |=(1<<pin)
self.bus.write_byte(self.address , conf.estado)
return conf.estado
def toggle(self,pin):
numero = 2**pin
conf.estado = conf.estado^numero
self.bus.write_byte(self.address , conf.estado)
return conf.estado
def toggle2(self,pin1,pin2):
self.toggle(pin1)
self.toggle(pin2)
def reset(self):
self.estado = self.estado|255
self.bus.write_byte(address , self.estado)
示例5: __init__
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import write_byte [as 别名]
class i2cDevice:
def __init__(self, bus_number):
self.BC_addr = 0x25
self.bus = SMBus(bus_number)
def read_register(self, address):
self.bus.write_byte(self.BC_addr, address)
time.sleep(0.02)
data = struct.pack('B', self.bus.read_byte(self.BC_addr))
return data
def write_register(self, address, data):
self.bus.write_byte_data(self.BC_addr, address, data)
time.sleep(0.02)
示例6: Device
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import write_byte [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
示例7: get
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import write_byte [as 别名]
def get(self):
lStatus = 'ok'
lArgs = self.__mParser.parse_args()
lBusId = int(lArgs['bus_id'], 0)
lAddress = int(lArgs['address'], 0)
lValue = int(lArgs['value'], 0)
lBus = SMBus(lBusId)
try:
if lArgs['cmd'] is None:
lBus.write_byte(lAddress, lValue)
else:
lCommand = int(lArgs['cmd'], 0)
lBus.write_byte_data(lAddress, lCommand, lValue)
except IOError, pExc:
lStatus = "Error writing data: " + str(pExc)
示例8: __init__
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import write_byte [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)
示例9: Pcf8574Gpio
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import write_byte [as 别名]
class Pcf8574Gpio(object):
BCM = 0
OUT = 0
def __init__(self, busnum, address):
self.bus = SMBus(busnum)
self.address = address
# Set all outputs off
self.bus.write_byte(self.address, 0x00)
# Store P-port state
self.byte = 0x00
def _changebit(self, bit, new_value):
if new_value == 0:
self.byte &= ~(1 << bit)
elif new_value == 1:
self.byte |= (1 << bit)
def output(self, pin, value):
self._changebit(pin, value)
self.bus.write_byte(self.address, self.byte)
def setmode(self, mode):
pass
def setup(self, pin, mode):
pass
def cleanup(self):
# Set all outputs off
self.bus.write_byte(self.address, 0x00)
示例10: TemperatureSensor
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import write_byte [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
示例11: len
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import write_byte [as 别名]
import sys
import serial
import struct
from smbus import SMBus
from time import sleep
from icsp import *
sel = sys.argv[1] if len(sys.argv) > 1 else "N"
i2c0 = SMBus(0)
i2c2 = SMBus(2)
if sel == "A": # toggle A_!RST
print("selecting RFW [bus A] ...")
i2c2.write_byte(0x70, 0x5) # steer mux
ioa = i2c0.read_byte_data(0x23, 0x14)
i2c0.write_byte_data(0x23, 0x14, ioa&~0x10)
i2c0.write_byte_data(0x23, 0x14, ioa|0x10)
elif sel == "B": # toggle B_!RST
print("selecting RFE [bus B] ...")
i2c2.write_byte(0x70, 0x4) # steer mux
iob = i2c0.read_byte_data(0x22, 0x14)
i2c0.write_byte_data(0x22, 0x14, iob&~0x10)
i2c0.write_byte_data(0x22, 0x14, iob|0x10)
elif sel == "N":
print("disabling MUX ...")
i2c2.write_byte(0x70, 0x0) # disable mux
示例12: sensirion
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import write_byte [as 别名]
class SHT21 :
RHmeasure_noHold = 0xF5
Tmeasure_noHold = 0xF3
RHmeasure_Hold = 0xE5
Tmeasure_Hold = 0xE3
Soft_Reset = 0xFE
Write_Reg = 0xE6
Read_Reg = 0xE7
##############################################################################
## Experimental register found by looking for each one individually ##
## with a 10 seconds wait between each try. CheckCRC says true for all. ##
RH_Reg = 0x05 ##
T_Reg = 0x03 ##
## read_reg? 0x06 ##
## read_reg 0x07 ##
## unknown 0x09 result was 6,0,90(constant over time) ##
## unknown 0x0F result was 2,68,32(constant over time) ##
## serial number? 0x1A periodic on 64 bits? ##
## result was [25, 203, 218, 223, 71, 170, 137, 242, 217, 140, 232 ##
## , 120, 231, 86, 128, 122, 7, 151, 248, 59, 252, 255, ##
## 232, 120, 54, 99, 129, 75, 30, 92, 80, 126] ##
## serial number? 0x1B same as 0x1A with random shift ##
## T_reg? 0xE3 result was 103,88,60(made a new measure?) ##
## RH_reg? 0xE5 result was 83,206,146(new measure?) ##
## read_reg 0xE6 result was 58,30 ##
## read_reg 0xE7 result was 58,30 ##
## unknown 0xE9 result was 6,0,90 ##
## unknown 0xEF result was 2,68,32 ##
## serial number 0xFA same as 1A, check sensirion(says 64 bits ID)##
## serial number? 0xFB same as 1B ##
## device ID? 0xFF check i2c full specs, results seems random ##
############################1#################################################
class CRCError(Exception):
pass
def __init__(self, addr, busnum = 1) :
self.address = addr
self.bus = SMBus(busnum)
#self.bus.open(busnum)
self.Reset()
#reg = self.ReadReg()
reg = 0
if (reg & 0x80) and (reg & 0x01):
self.RH_res = 11
self.T_res = 11
elif (reg & 0x80) and not(reg & 0x01):
self.RH_res = 10
self.T_res = 13
elif not(reg & 0x80) and not(reg & 0x01):
self.RH_res = 12
self.T_res = 14
else:
self.RH_res = 8
self.T_res = 12
def getRH(self):
self.bus.write_byte(self.address, self.RHmeasure_noHold)
time.sleep(0.1)
RH = self.bus.read_i2c_block_data(self.address, self.RH_Reg, 3)
#print RH
if self.CheckCRC(RH):
self.RHum = RH
RH[1] &= ~0x03 #reset 2 status bits(LSB)
return -6+125.*(RH[0]*256+RH[1])/65536.
else:
#print 'CRC checksum failed, data was corrupted(RH reading)'
#return -1
raise self.CRCError
def getT(self):
self.bus.write_byte(self.address, self.Tmeasure_noHold)
time.sleep(0.1)
T = self.bus.read_i2c_block_data(self.address, self.T_Reg, 3)
#print T
if self.CheckCRC(T):
self.Temp = T
T[1] &= ~0x03 #reset 2 status bits(LSB)
return -46.85+175.72*(T[0]*256+T[1])/65536.
else:
#print 'CRC checksum failed, data was corrupted(temp reading)'
#return -1
raise self.CRCError
def Reset(self):
self.bus.write_byte(self.address, self.Soft_Reset)
time.sleep(0.02) #must wait 15ms
def ReadReg(self):
reg = self.bus.read_word_data(self.address, self.Read_Reg)
crc = [ reg & 0xFF, (reg & 0xFF00) >> 8]
if self.CheckCRC(crc):
return reg & 0xFF
else:
#print 'Error : CRC not matching !'
#return 0
raise self.CRCError
def WriteReg(self, val):
#.........这里部分代码省略.........
示例13: __init__
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import write_byte [as 别名]
class MS5611:
"""Driver for reading temperature/pressure MS5611 Pressure Sensor."""
def __init__(self, bus = 1, i2c = 0x76, elevation = 0):
"""Initialize the Driver.
Default bus is 1. If you have a Rev 1 RPi then you will need to use bus 0.
A bus object can also be passed in if you are sharing it among other modules
Arguments (All optional):
bus -- 0, 1, or a bus object
i2c -- I2C address
elevation -- Elevation in meters"""
if(bus == 0 or bus == 1):
self.bus = SMBus(bus)
else:
self.bus = bus
self.i2c = i2c
self.elevation = elevation
def setElevation(self, elevation):
self.elevation = elevation
def setElevationFt(self, elevation):
self.elevation = elevation / 3.2808
def setI2c(self, i2c):
self.i2c = i2c
def read(self):
## Get raw pressure
self.bus.write_byte(self.i2c, 0x48)
time.sleep(0.05)
D1 = self.bus.read_i2c_block_data(self.i2c, 0x00)
D1 = D1[0] * 65536 + D1[1] * 256.0 + D1[2]
time.sleep(0.05)
## Get raw temperature
self.bus.write_byte(self.i2c, 0x58)
time.sleep(0.05)
D2 = self.bus.read_i2c_block_data(self.i2c, 0x00)
D2 = D2[0] * 65536 + D2[1] * 256.0 + D2[2]
time.sleep(0.05)
## Read Constants from Sensor
if hasattr(self, 'C1'):
C1 = self.C1
else:
C1 = self.bus.read_i2c_block_data(self.i2c, 0xA2) #Pressure Sensitivity
C1 = C1[0] * 256.0 + C1[1]
self.C1 = C1
time.sleep(0.05)
if hasattr(self, 'C2'):
C2 = self.C2
else:
C2 = self.bus.read_i2c_block_data(self.i2c, 0xA4) #Pressure Offset
C2 = C2[0] * 256.0 + C2[1]
self.C2 = C2
time.sleep(0.05)
if hasattr(self, 'C3'):
C3 = self.C3
else:
C3 = self.bus.read_i2c_block_data(self.i2c, 0xA6) #Temperature coefficient of pressure sensitivity
C3 = C3[0] * 256.0 + C3[1]
self.C3 = C3
time.sleep(0.05)
if hasattr(self, 'C4'):
C4 = self.C4
else:
C4 = self.bus.read_i2c_block_data(self.i2c, 0xA8) #Temperature coefficient of pressure offset
C4 = C4[0] * 256.0 + C4[1]
self.C4 = C4
time.sleep(0.05)
if hasattr(self, 'C5'):
C5 = self.C5
else:
C5 = self.bus.read_i2c_block_data(self.i2c, 0xAA) #Reference temperature
C5 = C5[0] * 256.0 + C5[1]
self.C5 = C5
time.sleep(0.05)
if hasattr(self, 'C6'):
C6 = self.C6
else:
C6 = self.bus.read_i2c_block_data(self.i2c, 0xAC) #Temperature coefficient of the temperature
C6 = C6[0] * 256.0 + C6[1]
self.C6 = C6
time.sleep(0.05)
#.........这里部分代码省略.........
示例14: print
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import write_byte [as 别名]
dir_pin = y_dir_pin
if axis == 1:
step_pin = x_step_pin
dir_pin = x_dir_pin
print('joy_x',joy_x,'joy_y',joy_y,'button_c',button_c,'button_z',button_z)
GPIO.output(dir_pin, direction)
GPIO.output(led_pin, True)
GPIO.output(step_pin, True)
sleep(speed)
GPIO.output(led_pin, False)
GPIO.output(step_pin, False)
sleep(speed)
while 1:
bus.write_byte(0x52,0x00)
sleep(0.000001)
data = []
for i in xrange(6):
data.append(bus.read_byte(0x52))
joy_x = data[0] -121
joy_y = data[1] -112
if (joy_y > 119): joy_y = -90
accel_x = (data[2] << 2) + ((data[5] & 0x0c) >> 2)
accel_y = (data[3] << 2) + ((data[5] & 0x30) >> 4)
accel_z = (data[4] << 2) + ((data[5] & 0xc0) >> 6)
button_c = (data[5] & 0x1) ^ ((data[5] & 0x2) >> 1)
button_z = (data[5] & 0x1) ^ 1
示例15: sleep
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import write_byte [as 别名]
for note in SONG:
p.ChangeFrequency(note[0])
sleep(note[1])
p.stop()
p.ChangeFrequency(N_BOUNCE)
for led in LEDS: gpio.setup(led, gpio.OUT)
oldBugger = WIDTH * HEIGHT * [None]
oldTime = time()
counter = 0
sorry = True
while 1:
newTime = time()
counter += newTime - oldTime
if newTime - sfxStartTime > sfxLength:
p.stop()
adc.write_byte(33, 128)
knob = adc.read_word_data(33, 0)
knob = ((knob & 15) << 8 | knob >> 8) - 683
if knob < 0: knob = 0
elif knob > 2730: knob = 2730
Player0Bat = HEIGHT - int(round(knob * (HEIGHT - Player0Height) / 2731.)) - Player0Height
if sorry:
knob = adc.read_byte_data(36,0) - 9
if knob < 0: knob = 0
elif knob > 220: knob = 220
Player1Bat = HEIGHT - int(round(knob * (HEIGHT - Player1Height) / 220.)) - Player1Height
gpio.output(17, 1)
gpio.output(17, 0)
sorry = not sorry
if Player0SizeCounter < 15: Player0SizeCounter += newTime - oldTime
else: Player0Height = 3