本文整理汇总了Python中Adafruit_GPIO类的典型用法代码示例。如果您正苦于以下问题:Python Adafruit_GPIO类的具体用法?Python Adafruit_GPIO怎么用?Python Adafruit_GPIO使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Adafruit_GPIO类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
def __init__(self, rst=None, address=BNO055_ADDRESS_A, i2c=None, gpio=None,
serial_port=None, serial_timeout_sec=5, **kwargs):
# If reset pin is provided save it and a reference to provided GPIO
# bus (or the default system GPIO bus if none is provided).
self._rst = rst
if self._rst is not None:
if gpio is None:
import Adafruit_GPIO as GPIO
gpio = GPIO.get_platform_gpio()
self._gpio = gpio
# Setup the reset pin as an output at a high level.
self._gpio.setup(self._rst, GPIO.OUT)
self._gpio.set_high(self._rst)
# Wait a 650 milliseconds in case setting the reset high reset the chip.
time.sleep(0.65)
self._serial = None
self._i2c_device = None
if serial_port is not None:
# Use serial communication if serial_port name is provided.
# Open the serial port at 115200 baud, 8N1. Add a 5 second timeout
# to prevent hanging if device is disconnected.
self._serial = serial.Serial(serial_port, 115200, timeout=serial_timeout_sec,
writeTimeout=serial_timeout_sec)
else:
# Use I2C if no serial port is provided.
# Assume we're using platform's default I2C bus if none is specified.
if i2c is None:
import Adafruit_GPIO.I2C as I2C
i2c = I2C
# Save a reference to the I2C device instance for later communication.
self._i2c_device = i2c.get_i2c_device(address, **kwargs)
示例2: Init
def Init(self):
if not self.initialized:
_gpio = GPIO.get_platform_gpio()
#GPIO = Adafruit_GPIO.RPiGPIOAdapter(_gpio)
#GPIO.setmode(GPIO.BOARD)
_gpio.setup(11, GPIO.IN)
_gpio.setup(7, GPIO.OUT)
self.initialized = True
示例3: __init__
def __init__(
self,
width,
height,
rst,
dc=None,
sclk=None,
din=None,
cs=None,
gpio=None,
spi=None,
i2c_bus=None,
i2c_address=SSD1306_I2C_ADDRESS,
i2c=None,
):
self._log = logging.getLogger("Adafruit_SSD1306.SSD1306Base")
self._spi = None
self._i2c = None
self.width = width
self.height = height
self._pages = height / 8
self._buffer = [0] * (width * self._pages)
# Default to platform GPIO if not provided.
self._gpio = gpio
if self._gpio is None:
self._gpio = GPIO.get_platform_gpio()
# Setup reset pin.
self._rst = rst
self._gpio.setup(self._rst, GPIO.OUT)
# Handle hardware SPI
if spi is not None:
self._log.debug("Using hardware SPI")
self._spi = spi
self._spi.set_clock_hz(8000000)
# Handle software SPI
elif sclk is not None and din is not None and cs is not None:
self._log.debug("Using software SPI")
self._spi = SPI.BitBang(self._gpio, sclk, din, None, cs)
# Handle hardware I2C
elif i2c is not None:
self._log.debug("Using hardware I2C with custom I2C provider.")
self._i2c = i2c.get_i2c_device(i2c_address)
else:
self._log.debug("Using hardware I2C with platform I2C provider.")
import Adafruit_GPIO.I2C as I2C
if i2c_bus is None:
self._i2c = I2C.get_i2c_device(i2c_address)
else:
self._i2c = I2C.get_i2c_device(i2c_address, busnum=i2c_bus)
# Initialize DC pin if using SPI.
if self._spi is not None:
if dc is None:
raise ValueError("DC pin must be provided when using SPI.")
self._dc = dc
self._gpio.setup(self._dc, GPIO.OUT)
示例4: test_101_detect
def test_101_detect(self):
self.onlyRasperryTest()
import Adafruit_GPIO as GPIO
comp = self.factory[self.component_name]()
gpio = GPIO.get_platform_gpio()
comp.setup_pir(gpio, 21, GPIO.RISING, comp.callback_pir, 200)
time.sleep(5)
dist = comp.values['status'].data
print "status", dist
self.assertNotEqual(dist, None)
self.assertTrue(comp.check_heartbeat())
gpio.cleanup()
示例5: test_102_read_distance
def test_102_read_distance(self):
self.onlyRasperryTest()
import Adafruit_GPIO as GPIO
comp = self.factory[self.component_name]()
gpio = GPIO.get_platform_gpio()
comp.setup_sonic(gpio, 20, 21, GPIO.RISING, comp.callback_echo, 200)
comp.trigger_sonic(gpio, 20)
time.sleep(1)
dist = comp.values['status'].data
print "distance", dist
self.assertNotEqual(dist, None)
self.assertTrue(comp.check_heartbeat())
gpio.cleanup()
示例6: __init__
def __init__(self, clk=None, cs=None, miso=None, mosi=None, spi=None, gpio=None):
"""Initialize MAX31855 device with software SPI on the specified CLK,
CS, and DO pins. Alternatively can specify hardware SPI by sending an
Adafruit_GPIO.SPI.SpiDev device in the spi parameter.
"""
self._spi = None
# Handle hardware SPI
if spi is not None:
self._spi = spi
elif clk is not None and cs is not None and miso is not None and mosi is not None:
# Default to platform GPIO if not provided.
if gpio is None:
gpio = GPIO.get_platform_gpio()
self._spi = SPI.BitBang(gpio, clk, mosi, miso, cs)
else:
raise ValueError('Must specify either spi for for hardware SPI or clk, cs, miso, and mosi for softwrare SPI!')
self._spi.set_clock_hz(1000000)
self._spi.set_mode(0)
self._spi.set_bit_order(SPI.MSBFIRST)
示例7: stop
def stop(self):
"""Stop the component.
"""
configs = len(self.values["pin"].get_index_configs())
for config in range(configs):
try:
logger.debug("[%s] - stop GPIO Input component on pin %s", self.__class__.__name__, self.values["pin"].instances[config]['data'])
GPIO.remove_event_detect(self.values["pin"].instances[config]['data'])
except Exception:
logger.exception("[%s] - Exception when stopping GPIO component", self.__class__.__name__)
GpioComponent.stop(self)
return True
示例8: __init__
def __init__(self, cs, sclk=None, mosi=None, miso=None, gpio=None,
spi=None):
"""Create an instance of the PN532 class using either software SPI (if
the sclk, mosi, and miso pins are specified) or hardware SPI if a
spi parameter is passed. The cs pin must be a digital GPIO pin.
Optionally specify a GPIO controller to override the default that uses
the board's GPIO pins.
"""
# Default to platform GPIO if not provided.
self._gpio = gpio
if self._gpio is None:
self._gpio = GPIO.get_platform_gpio()
# Initialize CS line.
self._cs = cs
self._gpio.setup(self._cs, GPIO.OUT)
self._gpio.set_high(self._cs)
# Setup SPI provider.
if spi is not None:
logger.debug('Using hardware SPI.')
# Handle using hardware SPI.
self._spi = spi
self._spi.set_clock_hz(1000000)
else:
logger.debug('Using software SPI')
# Handle using software SPI. Note that the CS/SS pin is not used
# as it will be manually controlled by this library for better
# timing.
self._spi = SPI.BitBang(self._gpio, sclk, mosi, miso)
# Set SPI mode and LSB first bit order.
self._spi.set_mode(0)
self._spi.set_bit_order(SPI.LSBFIRST)
示例9: __init__
def __init__(self, dc, spi, rst=None, gpio=None, width=ILI9341_TFTWIDTH,
height=ILI9341_TFTHEIGHT):
"""Create an instance of the display using SPI communication. Must
provide the GPIO pin number for the D/C pin and the SPI driver. Can
optionally provide the GPIO pin number for the reset pin as the rst
parameter.
"""
self._dc = dc
self._rst = rst
self._spi = spi
self._gpio = gpio
self.width = width
self.height = height
if self._gpio is None:
self._gpio = GPIO.get_platform_gpio()
# Set DC as output.
self._gpio.setup(dc, GPIO.OUT)
# Setup reset as output (if provided).
if rst is not None:
self._gpio.setup(rst, GPIO.OUT)
# Set SPI to mode 0, MSB first.
spi.set_mode(0)
spi.set_bit_order(SPI.MSBFIRST)
# Clock nerfed for the Minnowboard
if(Platform.platform_detect() == 3):
spi.set_clock_hz(1000000)
else:
spi.set_clock_hz(64000000)
# Create an image buffer.
self.buffer = Image.new('RGB', (width, height))
示例10: begin
def begin(self, contrast=40, bias=4):
"""Initialize display."""
# Default to detecting platform GPIO.
if self._gpio is None:
self._gpio = GPIO.get_platform_gpio()
# Default to bit bang SPI.
if self._spi is None:
self._spi = SPI.BitBang(self._gpio, self._sclk, self._din, None, self._cs)
# Set pin outputs.
self._gpio.setup(self._dc, GPIO.OUT)
if self._rst is not None:
self._gpio.setup(self._rst, GPIO.OUT)
# Toggle RST low to reset.
self._gpio.set_low(self._rst)
time.sleep(0.1)
self._gpio.set_high(self._rst)
# Enter extended mode commands.
self.command(PCD8544_FUNCTIONSET | PCD8544_EXTENDEDINSTRUCTION)
# Set LCD bias.
self.command(PCD8544_SETBIAS | bias)
# Set contrast.
contrast = max(0, min(contrast, 0x7f)) # Clamp to values 0-0x7f
self.command(PCD8544_SETVOP | contrast)
# Set normal display mode.
self.command(PCD8544_FUNCTIONSET)
self.command(PCD8544_DISPLAYCONTROL | PCD8544_DISPLAYNORMAL)
示例11: __init__
def __init__(self, width, height, rst, dc=None, sclk=None, din=None, cs=None,
gpio=None, spi=None, i2c_bus=I2C.get_default_bus(), i2c_address=SSD1306_I2C_ADDRESS):
self._log = logging.getLogger('Adafruit_SSD1306.SSD1306Base')
self._spi = None
self._i2c = None
self.width = width
self.height = height
self._pages = height/8
self._buffer = [0]*(width*self._pages)
# Default to platform GPIO if not provided.
self._gpio = gpio if gpio is not None else GPIO.get_platform_gpio()
# Setup reset pin.
self._rst = rst
self._gpio.setup(self._rst, GPIO.OUT)
# Handle hardware SPI
if spi is not None:
self._log.debug('Using hardware SPI')
self._spi = spi
# Handle software SPI
elif sclk is not None and din is not None and cs is not None:
self._log.debug('Using software SPI')
self._spi = SPI.BitBang(self._gpio, sclk, din, None, cs)
# Handle hardware I2C
elif i2c_bus is not None:
self._log.debug('Using hardware I2C')
self._i2c = I2C.Device(i2c_address, i2c_bus)
else:
raise ValueError('Unable to determine if using SPI or I2C.')
# Initialize DC pin if using SPI.
if self._spi is not None:
if dc is None:
raise ValueError('DC pin must be provided when using SPI.')
self._dc = dc
self._gpio.setup(self._dc, GPIO.OUT)
示例12: __init__
def __init__(self, **kwargs):
"""
:param int bus_id: the SMBus id (see Raspberry Pi documentation)
:param kwargs: parameters transmitted to :py:class:`smbus.SMBus` initializer
"""
JNTBus.__init__(self, **kwargs)
self._spi_lock = threading.Lock()
self.load_extensions(OID)
self._ada_gpio = None
try:
self._ada_gpio = GPIO.get_platform_gpio()
except :
logger.exception("[%s] - Can't get GPIO", self.__class__.__name__)
self._ada_spi = None
try:
self._ada_spi = SPI
except :
logger.exception("[%s] - Can't get SPI", self.__class__.__name__)
self.export_attrs('_ada_spi', self._ada_spi)
self.export_attrs('_ada_gpio', self._ada_gpio)
self.export_attrs('spi_acquire', self.spi_acquire)
self.export_attrs('spi_release', self.spi_release)
self.export_attrs('spi_locked', self.spi_locked)
self.export_attrs('get_spi_device', self.get_spi_device)
self.export_attrs('get_spi_device_pin', self.get_spi_device_pin)
示例13: __init__
def __init__(self, gpio_pin, low_temp=0, high_temp=100, history_samples=256, history_time=datetime.timedelta(minutes=5).total_seconds(), polltime=.25, onoff_wait=6):
self.shutdown_requested = False
self.gpio_pin = gpio_pin
self.low_temp = low_temp
self.high_temp = high_temp
self.history_time = datetime.timedelta(seconds=history_time)
self.history_samples = history_samples
self.last_history_time = 0
self.burn = False
self.burn_start_time = 0
self.burn_stop_time = 0
self.burn_wait = False
self.burn_deadband = False
self.burn_total_secs = 0
self.mode = MODES.OFF
self.mode_time = time.time()
self.poll_time = polltime
self.onoff_wait = onoff_wait
self.sense_lock = RLock()
self.sense = Thermocouple.Thermocouple(spiDev=SPI.SpiDev(port=0, device=0), chip=0)
self.food = Thermocouple.Thermocouple(spiDev=SPI.SpiDev(port=0, device=1), chip=1)
self.sense_history = collections.deque([], maxlen=history_samples)
self.sense_last = None
self.food_history = collections.deque([], maxlen=history_samples)
self.food_last = None
self.logger = logging.getLogger("burner")
self.gpio = GPIO.get_platform_gpio()
self.gpio.setup(self.gpio_pin, GPIO.OUT)
self.gpio.output(self.gpio_pin, True)
示例14: __init__
def __init__(self, dc, spi, rst=None, gpio=None, width=ILI9341_TFTWIDTH,
height=ILI9341_TFTHEIGHT, bgr=False, bit_depth=ILI9341_16BIT):
"""Create an instance of the display using SPI communication. Must
provide the GPIO pin number for the D/C pin and the SPI driver. Can
optionally provide the GPIO pin number for the reset pin as the rst
parameter.
"""
assert bit_depth in (ILI9341_16BIT, ILI9341_18BIT)
self._dc = dc
self._rst = rst
self._spi = spi
self._gpio = gpio
self.width = width
self.height = height
self.bgr = bgr
self.bit_depth = bit_depth
if self._gpio is None:
self._gpio = GPIO.get_platform_gpio()
# Set DC as output.
self._gpio.setup(dc, GPIO.OUT)
# Setup reset as output (if provided).
if rst is not None:
self._gpio.setup(rst, GPIO.OUT)
# Set SPI to mode 0, MSB first.
spi.set_mode(0)
spi.set_bit_order(SPI.MSBFIRST)
spi.set_clock_hz(64000000)
示例15: start
def start(self, mqttc, trigger_thread_reload_cb=None):
"""Start the bus
"""
JNTBus.start(self, mqttc, trigger_thread_reload_cb)
try:
self.gpio = GPIO.get_platform_gpio()
except Exception:
logger.exception("[%s] - Exception when starting GPIO bus", self.__class__.__name__)
self.update_attrs('gpio', self.gpio)