本文整理汇总了Python中smbus.SMBus.write_word_data方法的典型用法代码示例。如果您正苦于以下问题:Python SMBus.write_word_data方法的具体用法?Python SMBus.write_word_data怎么用?Python SMBus.write_word_data使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类smbus.SMBus
的用法示例。
在下文中一共展示了SMBus.write_word_data方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: MTSMBus
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import write_word_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)
示例2: Device
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import write_word_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
示例3: DAC12
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import write_word_data [as 别名]
class DAC12(object):
def __init__(self, dac_num, maximum=100.0):
bus, self.addr = get_dac_addr(dac_num)
self.bus = SMBus(bus)
self.coeff = 4096/float(maximum)
def conv(self, valeur):
return int(ceil(valeur * self.coeff)) & 0xFFFF
def set_eeprom(self, valeur):
self.bus.write_word_data(self.addr, WRITE_DAC_AND_EEPROM, self.conv(valeur))
def set(self, valeur):
self.bus.write_word_data(self.addr, WRITE_DAC, self.conv(valeur))
示例4: I2CDevice
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import write_word_data [as 别名]
class I2CDevice(object):
"""
Class for communicating with an I2C device.
Allows reading and writing 8-bit, 16-bit, and byte array values to
registers on the device.
It can handle signed, unsigned and endianness.
:var uint address: Assigned I2C address.
:var uint8 busid: Assigned IC2 bus identifier.
:param uint address: I2C address.
:param uint busid: IC2 bus identifier.
:param class i2c_class: Class implementing the I2C reading interface.
If None, smbus.SMBus will be used.
"""
def __init__(self, busnum, address, i2c_class=None):
self._busnum = busnum
self._address = address
if i2c_class is None:
from smbus import SMBus
self._bus = SMBus(busnum)
else:
self._bus = i2c_class(busnum)
self._logger = logging.getLogger(
'/dev/i2c-{}/{:#x}'.format(busnum, address)
)
def _debug(self):
self._logger.setLevel(logging.DEBUG)
self._logger.addHandler(logging.StreamHandler())
@property
def busnum(self):
return self._busnum
@property
def address(self):
return self._address
def write(self, value):
"""
Write the specified 8-bit value to the device base address.
"""
assert bound_bits(value, 8)
self._bus.write_byte(self._address, value)
self._logger.debug(
'Wrote value {:#x}'.format(value)
)
def register_write_u8(self, register, value):
"""
Write an 8-bit value to the specified 8-bit register.
"""
assert bound_bits(register, 8)
assert bound_bits(value, 8)
self._bus.write_byte_data(self._address, register, value)
self._logger.debug(
'Wrote to register {:#x} value {:#x}'.format(register, value)
)
def register_write_u16(self, register, value):
assert bound_bits(register, 8)
assert bound_bits(value, 16)
self._bus.write_word_data(self._address, register, value)
self._logger.debug(
'Wrote to register pair {:#x}, {:#x} value {:#x} '.format(
register, register + 1, value
)
)
def read(self):
"""
Read the device base address and return a 8-bit value.
"""
result = self._bus.read_byte(self._address) & 0xFF
self._logger.debug(
'Read value {:#x}'.format(result)
)
return result
def register_read_u8(self, register):
"""
Read the specified 8-bit register and return a 8-bit value.
"""
assert bound_bits(register, 8)
result = self._bus.read_byte_data(self._address, register) & 0xFF
self._logger.debug(
'Read from register {:#x} returns {:#x}'.format(register, result)
)
return result
#.........这里部分代码省略.........
示例5: I2CInterface
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import write_word_data [as 别名]
class I2CInterface(object):
""" I2C interface class.
It writes VU Meter stereo data into the I2C port defined in the configuration file.
The I2C addresses for the left and right channel should be defined in config.txt.
Default values 0x21, 0x20. The property output.size defines the number of bits used
for the I2C VU Meter. The default value is 10. For the smaller/larger numbers the
bit_patterns array should be modified accordingly.
"""
def __init__(self, config, data_source):
""" Initializer """
self.data_source = data_source
self.port = config[I2C_INTERFACE][PORT]
self.left_channel_address = config[I2C_INTERFACE][LEFT_CHANNEL_ADDRESS]
self.right_channel_address = config[I2C_INTERFACE][RIGHT_CHANNEL_ADDRESS]
self.output_size = config[I2C_INTERFACE][OUTPUT_SIZE]
self.update_period = config[I2C_INTERFACE][UPDATE_PERIOD]
self.step = int(100/self.output_size)
if "win" in sys.platform:
self.i2c_interface = DummySMBus()
else:
from smbus import SMBus
self.i2c_interface = SMBus(self.port)
self.i2c_interface.write_byte_data(self.left_channel_address, 0x00, 0x00)
self.i2c_interface.write_byte_data(self.left_channel_address, 0x01, 0x00)
self.i2c_interface.write_byte_data(self.right_channel_address, 0x00, 0x00)
self.i2c_interface.write_byte_data(self.right_channel_address, 0x01, 0x00)
self.bit_patterns = {}
self.bit_patterns[10] = 0b10000000
self.bit_patterns[20] = 0b11000000
self.bit_patterns[30] = 0b11100000
self.bit_patterns[40] = 0b11110000
self.bit_patterns[50] = 0b11111000
self.bit_patterns[60] = 0b11111100
self.bit_patterns[70] = 0b11111110
self.bit_patterns[80] = 0b11111111
self.bit_patterns[90] = 0b111111111
self.bit_patterns[100] = 0b1111111111
f = "{:0" + str(self.output_size) + "b}"
self.logging_template = "I2C left: " + f + " right: " + f
def start_writing(self):
""" Start writing thread """
self.running = True
thread = Thread(target = self.write_data)
thread.start()
def write_data(self):
""" Method of the writing thread """
while self.running:
v = self.data_source.get_value()
left = self.get_bits(v[0])
right = self.get_bits(v[1])
logging.debug(self.logging_template.format(left, right))
self.i2c_interface.write_word_data(self.left_channel_address, 0x12, left)
self.i2c_interface.write_word_data(self.right_channel_address, 0x12, right)
time.sleep(self.update_period)
def stop_writing(self):
""" Stop writing thread and nullify values in I2C """
self.running = False
time.sleep(self.update_period)
self.i2c_interface.write_word_data(self.left_channel_address, 0x12, 0)
self.i2c_interface.write_word_data(self.right_channel_address, 0x12, 0)
def get_bits(self, n):
""" Return bit pattern for the defined value in range 0-100
:param n: data value
:return: bit pattern
"""
if n == 0:
return 0
else:
k = int(math.ceil(n / self.step)) * self.step
v = self.bit_patterns[k]
return v
示例6: it
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import write_word_data [as 别名]
class I2CBus:
# The device ids supported
# Note: Under linux, the enumeration of the i2c devices is not guaranteed to match the BBB device id, i.e., i2c0 may
# not map to /dev/i2c-0. Here is a link on this topic: https://datko.net/2013/11/03/bbb_i2c
# What is important from a software perspective is that pins and the device id match since we don't use the linux
# device name. That is, we need to use an id, 0, 1, 2, which corresponds to a specific device connected on specific
# pins.
# Note: I2C device 0 is not enabled by default. There is a way to enable it (see above) but there are also possible
# conflicts with existing capes.
DEV_I2C_0 = 0
DEV_I2C_1 = 1
DEV_I2C_2 = 2
# The following formatting is taken from struct and maps the character designations to number of bytes in the type
__TYPE_SIZES = {'d': 8, # double - 8 bytes
'f': 4, # float - 4 bytes
'L': 4, # uint32 - 4 bytes
'l': 4, # int32 - 4 bytes
'H': 2, # uint16 - 2 bytes
'h': 2, # int16 - 2 bytes
'B': 1, # uint8 - 1 byte
'b': 1 # int8 - 1 byte
}
def __init__(self, device):
try:
self._smbus = SMBus(device)
except RuntimeError:
raise I2CBusError("Unable to open SMBus using {}".format(device))
def _read_multiple_bytes(self, address, offset, num_bytes):
return self._smbus.read_i2c_block_data(address, offset, num_bytes)
def _write_multiple_bytes(self, address, offset, byte_values):
self._smbus.write_i2c_block_data(address, offset, list(byte_values))
def WriteUint8(self, address, offset, value):
self._smbus.write_byte_data(address, offset, value)
def WriteUint16(self, address, offset, value):
self._smbus.write_word_data(address, offset, value)
def WriteInt16(self, address, offset, value):
self._smbus.write_word_data(address, offset, value)
def WriteUint32(self, address, offset, value):
bytes = bytearray(struct.pack('L', value))
self._write_multiple_bytes(address, offset, bytes)
def WriteFloat(self, address, offset, value):
bytes = bytearray(struct.pack('f',value))
self._write_multiple_bytes(address, offset, bytes)
def WriteArray(self, address, offset, values, type, endian=sys.byteorder):
# Convert each value to its byte representation and place into a bytearray before writing to the bus
# Note: struct.pack returns a string representation of the value. For a 1-byte value, it is a
# string representation of 1 byte, for a 2 or 4 byte value, it is a 2-byte of 4-byte representation
# Therefore, it is necessary to concatentate the strings before converting to a bytearray
if endian == 'little':
format = '<'+type
else:
format = '>'+type
byte_values = ''
for value in values:
byte_values += struct.pack(format, value)
self._write_multiple_bytes(address, offset, bytearray(byte_values))
def ReadUint8(self, address, offset):
return self._smbus.read_byte_data(address, offset)
def ReadUint16(self, address, offset):
return self._smbus.read_word_data(address, offset)
def ReadInt16(self, address, offset):
return self._smbus.read_word_data(address, offset)
def ReadUint32(self, address, offset):
bytes = self._read_multiple_bytes(address, offset, I2CBus.__TYPE_SIZES['L'])
return struct.unpack('L', str(bytearray(bytes)))[0]
def ReadInt32(self, address, offset):
bytes = self._read_multiple_bytes(address, offset, I2CBus.__TYPE_SIZES['l'])
return struct.unpack('l', str(bytearray(bytes)))[0]
def ReadFloat(self, address, offset):
values = self._read_multiple_bytes(address, offset, I2CBus.__TYPE_SIZES['f'])
return struct.unpack('f', str(bytearray(values)))[0]
def ReadArray(self, address, offset, num_values, type, endian=sys.byteorder):
# Create a format specifier based on the number of values requested.
# All of the values will be read as the same type, e.g., all floats, all long, etc
# The format specifies the number of float values to convert
format = '%s%s' % (num_values, type)
# Calculate number of bytes to read
# - num_values is the number of values to read
# - num_bytes is num_values * size of each value
#.........这里部分代码省略.........