本文整理汇总了Python中smbus2.SMBus方法的典型用法代码示例。如果您正苦于以下问题:Python smbus2.SMBus方法的具体用法?Python smbus2.SMBus怎么用?Python smbus2.SMBus使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类smbus2
的用法示例。
在下文中一共展示了smbus2.SMBus方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: track_duty
# 需要导入模块: import smbus2 [as 别名]
# 或者: from smbus2 import SMBus [as 别名]
def track_duty(self):
# FIXME: prefer to do this in constructor, but it breaks test_txrx
self.i2c_bus = smbus2.SMBus(1)
# Fetch any existing actuation counts from EEPROM.
self._read_actuations()
# Every 5 minutes, or when cancelled, save actuations back to
# EEPROM.
last_actuations = self.row_actuations
try:
while True:
await asyncio.sleep(ACTUATION_SAVE_PERIOD)
if self.row_actuations != last_actuations:
self._save_actuations()
last_actuations = self.row_actuations
except asyncio.CancelledError:
self._save_actuations()
raise
示例2: __init__
# 需要导入模块: import smbus2 [as 别名]
# 或者: from smbus2 import SMBus [as 别名]
def __init__(self, bus=None):
if bus is None:
try:
import RPi.GPIO as GPIO
# use the bus that matches your raspi version
rev = GPIO.RPI_REVISION
except:
rev = 3
if rev == 2 or rev == 3:
bus = 1 # for Pi 2+
else:
bus = 0
if not Bus.instance:
Bus.instance = smbus.SMBus(bus)
self.bus = bus
self.msg = i2c_msg
示例3: readVoltageRaspiUPS
# 需要导入模块: import smbus2 [as 别名]
# 或者: from smbus2 import SMBus [as 别名]
def readVoltageRaspiUPS():
# "This function returns as float the voltage from the Raspi UPS Hat via the provided SMBus object"
if enable:
bus = smbus2.SMBus(1)
address = 0x36
read = bus.read_word_data(address, 2)
swapped = struct.unpack("<H", struct.pack(">H", read))[0]
voltage = swapped * 1.25 / 1000 / 16
bus.close()
return voltage
else:
return 0
示例4: readCapacityRaspiUPS
# 需要导入模块: import smbus2 [as 别名]
# 或者: from smbus2 import SMBus [as 别名]
def readCapacityRaspiUPS():
# "This function returns as a float the remaining capacity of the battery connected to the Raspi UPS Hat via the provided SMBus object"
if enable:
bus = smbus2.SMBus(1)
address = 0x36
read = bus.read_word_data(address, 4)
bus.close()
swapped = struct.unpack("<H", struct.pack(">H", read))[0]
capacity = swapped / 256
capacity = int(capacity)
return capacity
else:
return 0
示例5: read_eeprom
# 需要导入模块: import smbus2 [as 别名]
# 或者: from smbus2 import SMBus [as 别名]
def read_eeprom(i2c_bus=None):
"""Return a class representing EEPROM contents, or none."""
try:
if i2c_bus is None:
try:
from smbus2 import SMBus
except ImportError:
raise ImportError('This library requires the smbus2 module\nInstall with: sudo pip install smbus2')
i2c_bus = SMBus(1)
i2c_bus.write_i2c_block_data(EEP_ADRESS, 0x00, [0x00])
return EPDType.from_bytes(i2c_bus.read_i2c_block_data(0x50, 0, 29))
except IOError:
return None
示例6: look_for_device_address
# 需要导入模块: import smbus2 [as 别名]
# 或者: from smbus2 import SMBus [as 别名]
def look_for_device_address(address):
bus = smbus2.SMBus(1)
is_device_found = False
for device in range(128):
try:
bus.read_byte(device)
if hex(device) == address:
is_device_found = True
return is_device_found
except:
pass # discard errors that we get when trying to read from empty address
return is_device_found
示例7: __init__
# 需要导入模块: import smbus2 [as 别名]
# 或者: from smbus2 import SMBus [as 别名]
def __init__(self):
self.port = 1
self.address = 0x76
self.bus = smbus2.SMBus(self.port)
self.json_temperature_data = None
self.raw_sensor_data = None
try:
bme280.load_calibration_params(self.bus, self.address)
except Exception as e:
print(e)
示例8: __init__
# 需要导入模块: import smbus2 [as 别名]
# 或者: from smbus2 import SMBus [as 别名]
def __init__(self, cols=16, rows=2, addr=0x3f, bus=1):
self.cols = cols
self.rows = rows
self.bus_num = bus
self.bus = smbus2.SMBus(self.bus_num)
self.addr = addr
self.display_init()
示例9: mock_ioctl
# 需要导入模块: import smbus2 [as 别名]
# 或者: from smbus2 import SMBus [as 别名]
def mock_ioctl(fd, command, msg):
print("Mocking ioctl")
assert fd == MOCK_FD
assert command is not None
# Reproduce i2c capability of a Raspberry Pi 3
if command == I2C_FUNCS:
msg.value = 0xeff0009
return
# Reproduce ioctl read operations
if command == I2C_SMBUS and msg.read_write == I2C_SMBUS_READ:
offset = msg.command
if msg.size == I2C_SMBUS_BYTE_DATA:
msg.data.contents.byte = test_buffer[offset]
elif msg.size == I2C_SMBUS_WORD_DATA:
msg.data.contents.word = test_buffer[offset + 1] * 256 + test_buffer[offset]
elif msg.size == I2C_SMBUS_I2C_BLOCK_DATA:
for k in range(msg.data.contents.byte):
msg.data.contents.block[k + 1] = test_buffer[offset + k]
# Reproduce a failing Quick write transaction
if command == I2C_SMBUS and \
msg.read_write == I2C_SMBUS_WRITE and \
msg.size == I2C_SMBUS_QUICK:
raise IOError("Mocking SMBus Quick failed")
# Override open, close and ioctl with our mock functions
示例10: test_func
# 需要导入模块: import smbus2 [as 别名]
# 或者: from smbus2 import SMBus [as 别名]
def test_func(self):
bus = SMBus(1)
print("\nSupported I2C functionality: %x" % bus.funcs)
bus.close()
示例11: test_enter_exit
# 需要导入模块: import smbus2 [as 别名]
# 或者: from smbus2 import SMBus [as 别名]
def test_enter_exit(self):
for id in (1, '/dev/i2c-alias'):
with SMBus(id) as bus:
self.assertIsNotNone(bus.fd)
self.assertIsNone(bus.fd, None)
with SMBus() as bus:
self.assertIsNone(bus.fd)
bus.open(2)
self.assertIsNotNone(bus.fd)
self.assertIsNone(bus.fd)
示例12: test_open_close
# 需要导入模块: import smbus2 [as 别名]
# 或者: from smbus2 import SMBus [as 别名]
def test_open_close(self):
for id in (1, '/dev/i2c-alias'):
bus = SMBus()
self.assertIsNone(bus.fd)
bus.open(id)
self.assertIsNotNone(bus.fd)
bus.close()
self.assertIsNone(bus.fd)
示例13: test_read
# 需要导入模块: import smbus2 [as 别名]
# 或者: from smbus2 import SMBus [as 别名]
def test_read(self):
res = []
res2 = []
res3 = []
bus = SMBus(1)
# Read bytes
for k in range(2):
x = bus.read_byte_data(80, k)
res.append(x)
self.assertEqual(len(res), 2, msg="Result array of incorrect length.")
# Read word
x = bus.read_word_data(80, 0)
res2.append(x & 255)
res2.append(x / 256)
self.assertEqual(len(res2), 2, msg="Result array of incorrect length.")
self.assertListEqual(res, res2, msg="Byte and word reads differ")
# Read block of N bytes
n = 2
x = bus.read_i2c_block_data(80, 0, n)
res3.extend(x)
self.assertEqual(len(res3), n, msg="Result array of incorrect length.")
self.assertListEqual(res, res3, msg="Byte and block reads differ")
bus.close()
示例14: __get_smbus
# 需要导入模块: import smbus2 [as 别名]
# 或者: from smbus2 import SMBus [as 别名]
def __get_smbus():
"""
Internal method for getting an instance of the i2c bus
:return: i2c bus for target device
:rtype: SMBus
:raises IOError: Could not open the i2c bus
"""
i2c__bus = 1
# detect the device that is being used
device = platform.uname()[1]
if device == "orangepione": # running on orange pi one
i2c__bus = 0
elif device == "orangepiplus": # running on orange pi plus
i2c__bus = 0
elif device == "orangepipcplus": # running on orange pi pc plus
i2c__bus = 0
elif device == "linaro-alip": # running on Asus Tinker Board
i2c__bus = 1
elif device == "raspberrypi": # running on raspberry pi
# detect i2C port number and assign to i2c__bus
for line in open('/proc/cpuinfo').readlines():
model = re.match('(.*?)\\s*:\\s*(.*)', line)
if model:
(name, value) = (model.group(1), model.group(2))
if name == "Revision":
if value[-4:] in ('0002', '0003'):
i2c__bus = 0
else:
i2c__bus = 1
break
try:
return SMBus(i2c__bus)
except IOError:
raise IOError('Could not open the i2c bus')
示例15: get_smbus
# 需要导入模块: import smbus2 [as 别名]
# 或者: from smbus2 import SMBus [as 别名]
def get_smbus():
"""
Internal method for getting an instance of the i2c bus
:return: i2c bus for target device
:rtype: SMBus
:raises IOError: Could not open the i2c bus
"""
i2c__bus = 1
# detect the device that is being used
device = platform.uname()[1]
if device == "orangepione": # running on orange pi one
i2c__bus = 0
elif device == "orangepiplus": # running on orange pi plus
i2c__bus = 0
elif device == "orangepipcplus": # running on orange pi pc plus
i2c__bus = 0
elif device == "linaro-alip": # running on Asus Tinker Board
i2c__bus = 1
elif device == "raspberrypi": # running on raspberry pi
# detect i2C port number and assign to i2c__bus
for line in open('/proc/cpuinfo').readlines():
model = re.match('(.*?)\\s*:\\s*(.*)', line)
if model:
(name, value) = (model.group(1), model.group(2))
if name == "Revision":
if value[-4:] in ('0002', '0003'):
i2c__bus = 0
else:
i2c__bus = 1
break
try:
return SMBus(i2c__bus)
except IOError:
raise 'Could not open the i2c bus'