本文整理汇总了Python中smbus.SMBus.close方法的典型用法代码示例。如果您正苦于以下问题:Python SMBus.close方法的具体用法?Python SMBus.close怎么用?Python SMBus.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类smbus.SMBus
的用法示例。
在下文中一共展示了SMBus.close方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: raw_input
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import close [as 别名]
print "PLL Select: (0: RF or 1: VCO)"
vco = raw_input()
user.vco = bool(int(vco))
print bcolors.WARNING + "Loading PLL" + bcolors.ENDC
pll = si5338POST(0x70, user.vco, bus, VCOREGS, PINS["INTERRUPT"], GPIO)
time.sleep(0.5)
if pll.check():
print bcolors.FAIL + "Exiting..." + bcolors.ENDC
exit()
else:
print bcolors.OKGREEN + "PLL Ready" + bcolors.ENDC
bus.close()
print "Enable Test Signal: (0: Disable or 1: Enable)"
testSignal = raw_input()
print "Select SCROD: (A or B)"
scrod = raw_input()
print "Input bunchMarkerA: (0 - 5280)"
bma = raw_input()
print "Input bunchMarkerB: (0 - 5280)"
bmb = raw_input()
print bin(int(bma))
print bin(int(bmb))
user.testSignal = bool(int(testSignal))
user.scrod = scrod
示例2: __init__
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import close [as 别名]
class Lcd:
# Modes
MODE_4BIT = 0x00
MODE_8BIT = 0x10
# Commands
FUNCTION_SET = 0x20
DISPLAY = 0x08
CLEAR = 0x01
ENTRY_MODE = 0x04
# Function set
LINES_1 = 0x00
LINES_2 = 0x08
FONT_5x8 = 0x00
FONT_5x10 = 0x04
# Display
DISPLAY_ON = 0x04
DISPLAY_OFF = 0x00
CURSOR_ON = 0x02
CURSOR_OFF = 0x00
BLINK_ON = 0x01
BLINK_OFF = 0x00
# Entry mode
ENTRY_RIGHT = 0x00
ENTRY_LEFT = 0x02
ENTRY_SHIFT_INCREMENT = 0x01
ENTRY_SHIFT_DECREMENT = 0x00
# Data modes
COMMAND = 0
# Enable bit
ENABLE = 0b00000100
# Backlight
BACKLIGHT_OFF = 0x00
BACKLIGHT_ON = 0x08
# Timing
MS_5 = 0.005
MS_60 = 0.06
US_160 = 0.00016
CLEAR_TIME = 2
# Constructor
def __init__(self, address = 0x27, bus = 1, lines = 2, font = 0):
self.address = address
self.bus = bus
self.lines = self.LINES_1 if lines < 2 else self.LINES_2
self.mode = self.MODE_8BIT
self.font = font
self.backlight_state = self.BACKLIGHT_OFF
print 'Addess: %s' % hex(self.address)
print 'Lines: %s' % hex(self.lines)
print 'Font: %s' % hex(self.font)
print 'Bus: %s' % self.bus
print 'Backlight state: %s' % hex(self.backlight_state)
self.open()
self.__initialize()
# Open bus
def open(self):
print 'Open bus connection to %s' % self.bus
self.bus = SMBus(self.bus)
# Close bus
def close(self):
print 'Close bus connection'
self.bus.close()
# Run the command
def command(self, command):
print 'Running command %s' % hex(command)
self.__send(command, self.COMMAND)
# Clear
def clear(self):
print 'Clearing'
self.command(self.CLEAR)
time.sleep(self.CLEAR_TIME)
# Turn backlight on
def backlight_on(self):
print 'Turn backlight on'
self.backlight_state = self.BACKLIGHT_ON
self.command(self.BACKLIGHT_ON)
# Initialize LCD in 4 bit mode. We are in 8 bit mode until we
# switch to 4 bit mode
def __initialize(self):
print 'Initializing LCD'
# In 8 bit mode
self.__send(0x3)
time.sleep(self.MS_5)
self.__send(0x3)
time.sleep(self.US_160)
#.........这里部分代码省略.........
示例3: I2C
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import close [as 别名]
class I2C(_Bus,NET):
"Class representing a I2C bus, locally or over TCP/IP. Use an Instance of this class as the bus parameter for any board"
Devices = {}
DeviceList= {}
class Device():
"""class respresening a device connected to the bus,
instances are created automatically"""
def __init__(self,address,InUseBy=None,Bus = None,Ident=''):
"""address = int (0x00-0xFF), address of this device
Ident = The Identification of the device for scan matching, default:''
InUseBy = If an Instance of a device-class is created, InUseBy points to that Instance"""
self.Ident = Ident
self.InUseBy = InUseBy
VersionStrip =Ident.split(' ')[0].lower()
if Ident !='' and VersionStrip in I2C.DeviceList:
self.InUseBy = I2C.DeviceList[VersionStrip](Bus,address)
else:
self.Type=None
if self.InUseBy!=None:
self.InUseBy.Ident=VersionStrip
def __init__(self,device=0,Port=None,Server=None):
"""
@param device The I2C bus to use e.g. /dev/i2c-0, /dev/i2c-1 etc.
@param Port Default=None if set to an Integer this will be the TCP/IP port to listen on.
@param Server Default=None if set to a string e.g. '192.168.200.137' the bus listening on that address/port combination will be connected to.
@todo Ckeck for Raspberry Pi, and its version in /Proc/CPUInfo
"""
self.Port = Port
self.Server=Server
if self.Server != None: # TCP Client mode
self.NetInit()
self.Transaction=self._NetTransaction
else:
try:
self.I2cBus = SMBus(device)
except :
print 'Need python-smbus for I2C bus to work'
print ''
print 'To install: sudo apt-get install python-smbus'
return None
if self.Port != None: #TCP Server Mode
self.ServerThread = threading.Thread(target=self.ListenerTread)
self.ServerThread.start()
def Close(self):
self.I2cBus.close()
def Transaction(self, OutBuffer,read=0):
if read!=0:
try:
return 0,' '+''.join([chr(m) for m in self.I2cBus.read_i2c_block_data((ord(OutBuffer[0])>>1),ord(OutBuffer[1]))])
except IOError:
return 0," "
else:
self.I2cBus.write_i2c_block_data(ord(OutBuffer[0])>>1 ,ord(OutBuffer[1]), [ord(m) for m in OutBuffer[2:]])
return 0,None
#
# TODO: change Transaction to _Bus.Read_String
#
def scan(self,Display=None,line=0):
for i in range(0x00,0xFF,0x02):
ret, buf = self.Transaction(chr(i)+chr(Ident),0x20)
Identification =""
for c in buf[2:]:
if ord(c)==0: break
if ord(c) in range(32,127):
Identification +=c
if Identification != "":
if i in self.Devices:
self.Devices[i].Ident=Identification
else:
self.Devices[i]=I2C.Device(i,Ident=Identification,Bus=self)
if Display!=None:
ProgressBar(i,minval=0, maxval=0xFF , Display = Display, y=line)
sleep(.05)
def AddDevice(self,Address, InUseBy):
self.Devices[Address]=I2C.Device(Address,InUseBy=InUseBy,Bus=self)
示例4: BerryIMUClient
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import close [as 别名]
#.........这里部分代码省略.........
self.open()
return self.bus
@property
def calibration_object(self):
return self._calibration_object
@calibration_object.setter
def calibration_object(self, new_calibration):
# TODO: Check if calibration and current client share vital settings such as full scales.
self._calibration_object = new_calibration
def open(self):
try:
self._bus = SMBus(self._bus_no)
except IOError as e:
if str(e) == '2':
raise PyBerryIMUError("/dev/i2c-{0} not found. (IOError 2)".format(self._bus_no))
elif str(e) == '5':
raise PyBerryIMUError("I2C Input/Output error. (IOError 5)".format(self._bus_no))
elif str(e) == '13':
raise PyBerryIMUError("Permission to read and/or write to "
"/dev/i2c-{0} missing. (IOError 13)".format(self._bus_no))
else:
raise PyBerryIMUError('Unhandled IOError: {0}'.format(e))
except Exception as e:
raise PyBerryIMUError('Unhandled {0}: {1}'.format(type(e), e))
else:
self._init_accelerometer()
self._init_gyroscope()
self._init_magnetometer()
self._init_barometric_pressure_sensor()
def close(self):
if self._bus is not None:
try:
self._bus.close()
except Exception as e:
# TODO: Test what errors can occur and handle these better.
print("Exception at closing of i2c bus: {0}".format(e))
def __enter__(self):
self.open()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close()
# Initialisation methods
def _create_accelerometer_settings_dict(self, setup_dict):
return {
'data_rate': setup_dict.get('data_rate', 200),
'continuous_update': setup_dict.get('continuous_update', False),
'enabled_z': setup_dict.get('enabled_z', True),
'enabled_y': setup_dict.get('enabled_y', True),
'enabled_x': setup_dict.get('enabled_x', True),
'anti_alias': setup_dict.get('anti_alias', 773),
'full_scale': setup_dict.get('full_scale', 8),
'self_test': setup_dict.get('self_test', 0)
}
def _init_accelerometer(self):
"""Initialize the accelerometer according to the settings document sent in."""
# TODO: Better init handling!
示例5: Monster
# 需要导入模块: from smbus import SMBus [as 别名]
# 或者: from smbus.SMBus import close [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)
#.........这里部分代码省略.........