本文整理汇总了Python中smbus.SMBus.open方法的典型用法代码示例。如果您正苦于以下问题:Python SMBus.open方法的具体用法?Python SMBus.open怎么用?Python SMBus.open使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类smbus.SMBus
的用法示例。
在下文中一共展示了SMBus.open方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_open
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import open [as 别名]
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
示例2: Monster
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import open [as 别名]
class Monster():
"""Monster class. Should only be used with a context manager!
All GPIOs are BCM GPIO numbers.
"""
I2C_BUS_NUM = 0
SERVO_I2C_ADDR = 0xa
SERVO_CMD_OPEN = 1
SERVO_CMD_CLOSE = 2
SERVO_CMD_TWITCH = 3
DISTANCE_UPDATE_SECONDS = .2
def __init__(self, solenoid_gpio_num=17,
echo_trigger_gpio_num=24, echo_gpio_num=25):
self._gpios = {
'solenoid': solenoid_gpio_num,
'echo_trigger': echo_trigger_gpio_num,
'echo': echo_gpio_num,
}
self._rangefinder_settled = False
self._distance_lock = threading.Lock()
self._distance = 999999999
def __enter__(self):
PWM.set_loglevel(PWM.LOG_LEVEL_ERRORS)
GPIO.setup(self._gpios['solenoid'], GPIO.OUT)
GPIO.setup(self._gpios['echo_trigger'], GPIO.OUT)
GPIO.setup(self._gpios['echo'], GPIO.IN)
self._i2c_bus = SMBus()
self._i2c_bus.open(Monster.I2C_BUS_NUM)
self.close_door()
return self
def __exit__(self, exc_type, exc_value, traceback):
self._cm_active = False
self._i2c_bus.close()
GPIO.cleanup()
def activate_solenoid(self):
GPIO.output(self._gpios['solenoid'], True)
def deactivate_solenoid(self):
GPIO.output(self._gpios['solenoid'], False)
def fire_ball(self, active_time=.3):
"""Activate the solenoid for `active_time' seconds."""
self.activate_solenoid()
time.sleep(active_time)
self.deactivate_solenoid()
def i2c_write(self, cmd, max_iters):
for i in range(max_iters):
try:
self._i2c_bus.write_byte(Monster.SERVO_I2C_ADDR, cmd)
return
except IOError:
time.sleep(.5)
pass
print "I2C Contention! Couldn't send command:", cmd
def close_door(self):
self.i2c_write(Monster.SERVO_CMD_CLOSE, 10)
def open_door(self):
self.i2c_write(Monster.SERVO_CMD_OPEN, 10)
def twitch_door(self):
self.i2c_write(Monster.SERVO_CMD_TWITCH, 10)
def toggle_door(self, time_open=.8):
self.open_door()
time.sleep(time_open)
self.close_door()
def ball_and_door(self):
self.twitch_door()
time.sleep(1)
self.fire_ball()
time.sleep(1)
self.fire_ball()
# based on http://www.modmypi.com/blog/hc-sr04-ultrasonic-range-sensor-on-the-raspberry-pi
def measure_distance(self):
"""Returns the distance (in meters) to the object being looked at.
Probably should only happen in a thread due to all the sleeping
"""
if not self._rangefinder_settled:
# let the sensor settle
GPIO.output(self._gpios['echo_trigger'], False)
time.sleep(2)
self._rangefinder_settled = True
# 10 us pulse
GPIO.output(self._gpios['echo_trigger'], True)
time.sleep(0.00001)
GPIO.output(self._gpios['echo_trigger'], False)
#.........这里部分代码省略.........