本文整理汇总了Python中smbus.SMBus类的典型用法代码示例。如果您正苦于以下问题:Python SMBus类的具体用法?Python SMBus怎么用?Python SMBus使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SMBus类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
class PiGlow:
i2c_addr = 0x54 # fixed i2c address of SN3218 ic
bus = None
def __init__(self, i2c_bus=1):
self.bus = SMBus(i2c_bus)
# first we tell the SN3218 to enable output (turn on)
self.write_i2c(CMD_ENABLE_OUTPUT, 0x01)
# then we ask it to enable each bank of LEDs (0-5, 6-11, and 12-17)
self.write_i2c(CMD_ENABLE_LEDS, [0xFF, 0xFF, 0xFF])
def update_leds(self, values):
#print "update pwm"
self.write_i2c(CMD_SET_PWM_VALUES, values)
self.write_i2c(CMD_UPDATE, 0xFF)
# a helper that writes the given value or list of values to the SN3218 IC
# over the i2c protocol
def write_i2c(self, reg_addr, value):
# if a single value is provided then wrap it in a list so we can treat
# all writes in teh same way
if not isinstance(value, list):
value = [value];
# write the data to the SN3218
self.bus.write_i2c_block_data(self.i2c_addr, reg_addr, value)
示例2: AM2320
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: HTU21D
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)
示例4: __init__
class LEDSign:
def __init__(self):
self.s = SMBus(0)
self.lock = Lock()
def print_message(self, line, message):
if len(message) > 255:
message = message[:255]
if message[:-1] != "\x00":
message = "".join([message, "\x00"])
self.print_message_loop(line, message)
def print_message_loop(self, line, message):
if message == "":
return
self.lock.acquire()
self.s.write_i2c_block_data(signAddress, line, [ord(x) for x in message[0:payLoadLen]])
self.lock.release()
self.print_message_loop(line, message[payLoadLen:])
def get_status(self):
self.lock.acquire()
labStatus = self.s.read_byte(signAddress)
self.lock.release()
return labStatus
示例5: PCF8574
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
示例6: __init__
class motorControlBoard:
"""Class to allow communication with the motor
control board built by me using I2C."""
__board_I2C_address = 0
def __init__(self, board_address):
if isinstance(board_address, int):
if board_address > 0 and board_address < 0x78:
self.__board_I2C_address = board_address
else:
raise Exception("Board address must be an integer between 0 and 0b1111000 (=120) exclusive.")
else:
raise Exception("Board address must be an integer.")
self.__bus = SMBus(1) # FIXME = have an option to make this zero for the old Raspberry Pis
def set_speeds(self, left_speed, right_speed):
# Enforce limits due to 8-bit resolution
if(left_speed < -0xff): left_speed = -0xff
if(left_speed > +0xff): left_speed = +0xff
# Enforce limits due to 8-bit resolution
if(right_speed < -0xff): right_speed = -0xff
if(right_speed > +0xff): right_speed = +0xff
direction = 0x00;
if(left_speed < 0): direction |= MOTOR_CONTROL_LEFT_BACK
elif(left_speed > 0): direction |= MOTOR_CONTROL_LEFT_FORE
if(right_speed < 0): direction |= MOTOR_CONTROL_RIGHT_BACK
elif(right_speed > 0): direction |= MOTOR_CONTROL_RIGHT_FORE
self.__bus.write_i2c_block_data(self.__board_I2C_address, MOTOR_CONTROL_SET_DIR_SPEED_CMD, [direction, abs(left_speed), abs(right_speed)])
示例7: __init__
def __init__(self, busNum):
# Remove annoying init message (The Raspberry Pi Guy)
if busNum == 0:
self.__bus = SMBus(0)
else:
self.__bus = SMBus(1)
self.__addr = self.__checkI2Caddress(72)
self.__DACEnabled = 0
示例8: get_temp
def get_temp():
# zlecenie konwersji
i2c_bus = SMBus(1)
i2c_bus.write_byte_data(Register.LIGHT2_ADDRESS, 10, 1)
sleep(1)
cel = i2c_bus.read_word_data(0x20, 5)
cel = cel >> 8
return cel
示例9: test_open
def test_open():
bus = SMBus()
py.test.raises(IOError, 'bus.open(-13)')
bus.open(BUS) # does not raise
if hasattr(bus, '_fd'):
assert bus._fd != -1
示例10: Bus_Hepler_i2c
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)
示例11: __init__
def __init__(self, busNum):
#print "init PCF8591"
if busNum == 0:
self.__bus = SMBus(0) # on a Rev 1 board
#print "bus 0"
else:
self.__bus = SMBus(1) # on a Rev 2 board
self.__addr = self.__checkI2Caddress(0x48)
self.__DACEnabled = 0x00
示例12: DetectCap
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
示例13: __init__
def __init__(self, busNum):
print "init PCF8591"
if busNum == 0:
self.__bus = SMBus(0) # on a Rev 1 board
# print "bus 0"
else:
self.__bus = SMBus(1) # on a Rev 2 board
self.__addr = self.__checkI2Caddress(0x48)
self.__DACEnabled = 0x00
print self.readADC() # dummy call to raise exception if no chip presnt on the i2c bus
print "PCF8591 init completed"
示例14: __init__
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)
示例15: Pines
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)