当前位置: 首页>>代码示例>>Python>>正文


Python SpiDev.close方法代码示例

本文整理汇总了Python中spidev.SpiDev.close方法的典型用法代码示例。如果您正苦于以下问题:Python SpiDev.close方法的具体用法?Python SpiDev.close怎么用?Python SpiDev.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在spidev.SpiDev的用法示例。


在下文中一共展示了SpiDev.close方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: MCP3008

# 需要导入模块: from spidev import SpiDev [as 别名]
# 或者: from spidev.SpiDev import close [as 别名]
class MCP3008(object):
    """
    MCP3008 ADC (Analogue-to-Digital converter).
    """
    def __init__(self, bus=0, device=0, channel=0):
        self.bus = bus
        self.device = device
        self.channel = channel
        self.spi = SpiDev()

    def __enter__(self):
        self.open()
        return self

    def open(self):
        self.spi.open(self.bus, self.device)

    def read(self):
        adc = self.spi.xfer2([1, (8 + self.channel) << 4, 0])
        data = ((adc[1] & 3) << 8) + adc[2]
        return data

    def __exit__(self, type, value, traceback):
        self.close()

    def close(self):
        self.spi.close()
开发者ID:TheRinger,项目名称:python-gpiozero,代码行数:29,代码来源:input_devices.py

示例2: SpiDevice

# 需要导入模块: from spidev import SpiDev [as 别名]
# 或者: from spidev.SpiDev import close [as 别名]
class SpiDevice(object):
    def __init__(self, bus, device):
        self.spi = SpiDev()
        self.bus = bus
        self.device = device

    def init(self):
        self.spi.open(self.bus, self.device)

    def transfer(self, data):
        return self.spi.xfer2(data)

    def close(self):
        self.spi.close()
开发者ID:cberes,项目名称:raspberry-pi-weather,代码行数:16,代码来源:spi_device.py

示例3: __init__

# 需要导入模块: from spidev import SpiDev [as 别名]
# 或者: from spidev.SpiDev import close [as 别名]
class MCP3008:
    def __init__(self, bus = 0, device = 0):
        self.bus, self.device = bus, device
        self.spi = SpiDev()
        self.open()

    def open(self):
        self.spi.open(self.bus, self.device)
    
    def read(self, channel = 0):
        adc = self.spi.xfer2([1, (8 + channel) << 4, 0])
        data = ((adc[1] & 3) << 8) + adc[2]
        return data
            
    def close(self):
        self.spi.close()
开发者ID:joneskys7,项目名称:pi,代码行数:18,代码来源:MCP3008.py

示例4: __init__

# 需要导入模块: from spidev import SpiDev [as 别名]
# 或者: from spidev.SpiDev import close [as 别名]
class MCP3008:
    def __init__(self, bus = 0, device = 0, channel = 0):
        self.bus, self.device, self.channel = bus, device, channel
        self.spi = SpiDev()

    def __enter__(self):
        self.open()
        return self

    def open(self):
        self.spi.open(self.bus, self.device)
    
    def read(self):
        adc = self.spi.xfer2([1, (8 + self.channel) << 4, 0])
        data = ((adc[1] & 3) << 8) + adc[2]
        return data

    def __exit__(self, type, value, traceback):
            self.close()
            
    def close(self):
        self.spi.close()
开发者ID:martinohanlon,项目名称:PiLadyAnneRadio,代码行数:24,代码来源:MCP3008.py

示例5: Temperature

# 需要导入模块: from spidev import SpiDev [as 别名]
# 或者: from spidev.SpiDev import close [as 别名]
class Temperature(object):
    def __init__(self, major=0, minor=0):
        self.spi = SpiDev()
        self.spi.open(major, minor)

    def rawread(self):
        return self.spi.xfer2([0, 0])

    def read(self):
        return self.calc_temp(self.rawread())

    @staticmethod
    def calc_temp(buf):
        return (((buf[0] << 8) | buf[1]) >> 3) * 0.0625

    def cleanup(self):
        self.spi.close()

    def __enter__(self):
        return self

    def __exit__(self, type_, value, traceback):
        self.cleanup()
开发者ID:bwduncan,项目名称:autoboiler,代码行数:25,代码来源:autoboiler.py

示例6: SPIHardwareInterface

# 需要导入模块: from spidev import SpiDev [as 别名]
# 或者: from spidev.SpiDev import close [as 别名]
class SPIHardwareInterface(Device):
    def __init__(self, port, device):
        self._device = None
        super(SPIHardwareInterface, self).__init__()
        # XXX How can we detect conflicts with existing GPIO instances? This
        # isn't ideal ... in fact, it's downright crap and doesn't guard
        # against conflicts created *after* this instance, but it's all I can
        # come up with right now ...
        conflicts = (11, 10, 9, (8, 7)[device])
        with _PINS_LOCK:
            for pin in _PINS:
                if pin.number in conflicts:
                    raise GPIOPinInUse(
                        'pin %r is already in use by another gpiozero object' % pin
                    )
        self._device_num = device
        self._device = SpiDev()
        self._device.open(port, device)
        self._device.max_speed_hz = 500000

    def close(self):
        if self._device:
            try:
                self._device.close()
            finally:
                self._device = None
        super(SPIHardwareInterface, self).close()

    @property
    def closed(self):
        return self._device is None

    def __repr__(self):
        try:
            self._check_open()
            return (
                "hardware SPI on clock_pin=11, mosi_pin=10, miso_pin=9, "
                "select_pin=%d" % (
                    8 if self._device_num == 0 else 7))
        except DeviceClosed:
            return "hardware SPI closed"

    def read(self, n):
        return self.transfer((0,) * n)

    def write(self, data):
        return len(self.transfer(data))

    def transfer(self, data):
        """
        Writes data (a list of integer words where each word is assumed to have
        :attr:`bits_per_word` bits or less) to the SPI interface, and reads an
        equivalent number of words, returning them as a list of integers.
        """
        return self._device.xfer2(data)

    def _get_clock_mode(self):
        return self._device.mode

    def _set_clock_mode(self, value):
        self._device.mode = value

    def _get_clock_polarity(self):
        return bool(self.mode & 2)

    def _set_clock_polarity(self, value):
        self.mode = self.mode & (~2) | (bool(value) << 1)

    def _get_clock_phase(self):
        return bool(self.mode & 1)

    def _set_clock_phase(self, value):
        self.mode = self.mode & (~1) | bool(value)

    def _get_lsb_first(self):
        return self._device.lsbfirst

    def _set_lsb_first(self, value):
        self._device.lsbfirst = bool(value)

    def _get_select_high(self):
        return self._device.cshigh

    def _set_select_high(self, value):
        self._device.cshigh = bool(value)

    def _get_bits_per_word(self):
        return self._device.bits_per_word

    def _set_bits_per_word(self, value):
        self._device.bits_per_word = value

    clock_polarity = property(_get_clock_polarity, _set_clock_polarity)
    clock_phase = property(_get_clock_phase, _set_clock_phase)
    clock_mode = property(_get_clock_mode, _set_clock_mode)
    lsb_first = property(_get_lsb_first, _set_lsb_first)
    select_high = property(_get_select_high, _set_select_high)
    bits_per_word = property(_get_bits_per_word, _set_bits_per_word)
开发者ID:EdwardBetts,项目名称:python-gpiozero,代码行数:100,代码来源:spi.py

示例7: CpPhy

# 需要导入模块: from spidev import SpiDev [as 别名]
# 或者: from spidev.SpiDev import close [as 别名]
class CpPhy(object):
	# Profibus baud-rates
	PB_PHY_BAUD_9600	= 0
	PB_PHY_BAUD_19200	= 1
	PB_PHY_BAUD_45450	= 2
	PB_PHY_BAUD_93750	= 3
	PB_PHY_BAUD_187500	= 4
	PB_PHY_BAUD_500000	= 5
	PB_PHY_BAUD_1500000	= 6
	PB_PHY_BAUD_3000000	= 7
	PB_PHY_BAUD_6000000	= 8
	PB_PHY_BAUD_12000000	= 9

	# RTS mode
	PB_PHY_RTS_ALWAYS_LO	= 0
	PB_PHY_RTS_ALWAYS_HI	= 1
	PB_PHY_RTS_SENDING_HI	= 2
	PB_PHY_RTS_SENDING_LO	= 3

	# GPIO numbers (BCM)
	GPIO_RESET		= 17
	GPIO_IRQ		= 27
	GPIO_SS			= 8
	GPIO_MISO		= 9
	GPIO_MOSI		= 10
	GPIO_SCK		= 11

	baud2id = {
		9600		: PB_PHY_BAUD_9600,
		19200		: PB_PHY_BAUD_19200,
		45450		: PB_PHY_BAUD_45450,
		93750		: PB_PHY_BAUD_93750,
		187500		: PB_PHY_BAUD_187500,
		500000		: PB_PHY_BAUD_500000,
		1500000		: PB_PHY_BAUD_1500000,
		3000000		: PB_PHY_BAUD_3000000,
		6000000		: PB_PHY_BAUD_6000000,
		12000000	: PB_PHY_BAUD_12000000,
	}

	def __init__(self, device=0, chipselect=0, debug=False):
		self.device = device
		self.chipselect = chipselect
		self.debug = debug

		try:
			try:
				# Initialize GPIOs
				GPIO.setmode(GPIO.BCM) # Use Broadcom numbers
				GPIO.setwarnings(False)
				GPIO.setup(self.GPIO_RESET, GPIO.OUT, initial=GPIO.LOW)
				GPIO.setup(self.GPIO_IRQ, GPIO.IN, pull_up_down=GPIO.PUD_OFF)
				GPIO.add_event_detect(self.GPIO_IRQ, GPIO.RISING)
				time.sleep(0.05)
			except RuntimeError as e:
				raise PhyError("Failed to initialize GPIOs: %s" %\
					str(e))

			# Initialize SPI
			try:
				self.spi = SpiDev()
				self.spi.open(device, chipselect)
			except IOError as e:
				raise PhyError("Failed to open SPI device %d.%d: %s" %\
					(device, chipselect, str(e)))
			try:
				self.spi.mode = 0;
				self.spi.bits_per_word = 8;
				self.spi.cshigh = False
				self.spi.lsbfirst = False
				self.spi.max_speed_hz = 200000;
			except IOError as e:
				try:
					self.spi.close()
					self.spi = None
				except:
					pass
				raise PhyError("Failed to configure SPI device %d.%d: %s" %\
					(device, chipselect, str(e)))

			# Get the controller out of hardware reset
			GPIO.output(self.GPIO_RESET, GPIO.HIGH)
			time.sleep(0.2)

			# Send a software reset
			self.sendReset()
			# Upload default config
			self.profibusSetPhyConfig()
		except:
			GPIO.cleanup()
			raise

	def cleanup(self):
		self.spi.close()
		self.spi = None
		GPIO.cleanup()

	# Poll for received packet.
	# timeout => In seconds. 0 = none, Negative = unlimited.
	def poll(self, timeout=0):
#.........这里部分代码省略.........
开发者ID:devgituser,项目名称:raspi-profibus,代码行数:103,代码来源:phy.py

示例8: SPIDataLink

# 需要导入模块: from spidev import SpiDev [as 别名]
# 或者: from spidev.SpiDev import close [as 别名]
class SPIDataLink(FullDuplexDataLink):
    """Clase que gestiona un enlace Serial Peripheral Interface (SPI).

    :param bus: Identificador del bus SPI que se usa para el enlace de datos.
    :param device: Línea de selección de chip SPI activa en el enlace de datos.
    :param configuration: Configuración del enlace de datos

    Ejemplo de uso:

    >>> from pida.links import SPIDataLinkConfiguration, SPIDataLink
    >>> configuration = SPIDataLinkConfiguration(mode=0, max_speed_hz=32000000)
    >>> with SPIDataLink(0, 0, configuration) as link:
            request = [0x00, 0x01, 0xFF]
            response = link.transfer(request)
    >>> response
    [0, 1, 255]
    """
    def __init__(self, bus, device, configuration):
        self._bus = bus
        self._device = device
        self._configuration = configuration
        self._spi = SpiDev()

    def _apply_configuration(self):
        self._spi.mode = self._configuration.mode
        self._spi.max_speed_hz = self._configuration.max_speed_hz

    @property
    def bus(self):
        """Identificador del bus SPI que se usa para el enlace de datos.

        .. note:: Raspberry Pi ofrece a través de su puerto GPIO
                  un único bus SPI cuyo identificador es 0.

        Es una propiedad de sólo lectura.
        """
        return self._bus

    @property
    def device(self):
        """Línea de selección de chip SPI activa en el enlace de datos.

        .. note:: El bus SPI 0 de Raspberry Pi puede, a través del puerto GPIO,
                  activar dos líneas de selección de chip SPI: 0 y 1.

        Es una propiedad de sólo lectura.
        """
        return self._device

    def open(self):
        self._spi.open(self._bus, self._device)
        self._apply_configuration()

    def close(self):
        self._spi.close()

    def transfer(self, data):
        return self._spi.xfer2(data)

    def __enter__(self):
        self.open()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.close()

    @property
    def max_speed_hz(self):
        return self._configuration.max_speed_hz

    @property
    def mode(self):
        return self._configuration.mode
开发者ID:noeldiazro,项目名称:pida,代码行数:75,代码来源:links.py

示例9: range

# 需要导入模块: from spidev import SpiDev [as 别名]
# 或者: from spidev.SpiDev import close [as 别名]
	print "read eeprom"
	cmd = [0x03, 0] + [0 for i in range(8*16)]
	res = spi.xfer2(cmd)
	print res


if __name__ == "__main__":
	GPIO.setup("P9_11", GPIO.OUT)
	GPIO.setup("P9_12", GPIO.OUT)
	GPIO.setup("P9_13", GPIO.OUT)
	GPIO.setup("P9_14", GPIO.OUT)
	GPIO.setup("P9_15", GPIO.OUT)

	#spi = SPI()
	#spi.open(0, 0)
	#spi.msh = 0

	spi = SpiDev(1,0)
	spi.mode = 3
	spi.max_speed_hz = 100000


	check_eeprom_status()
	write_eeprom(3, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15])

	GPIO.cleanup()
	spi.close()


开发者ID:kimsiang,项目名称:SiPM_QC,代码行数:29,代码来源:eeprom_test_original.py

示例10: Matrix

# 需要导入模块: from spidev import SpiDev [as 别名]
# 或者: from spidev.SpiDev import close [as 别名]
class Matrix(object):
    '''
        The class which models the operation of the Olimex 8x8 RGB LED matrix.
        The numbering scheme used when defining the pins are with respect to
        the BCM numbering scheme.

        Wiring:
            - VCC = driving voltage for the matrix. 5 volts.
            - GND = ground connection from the matrix.
            - DIN = data in for the matrix, GPIO pin 10 (SPI MOSI).
            - CS = chip select, depending on SPI channel selection.
            - CLK = serial clock, GPIO pin 11 (SPI SCK).

    '''
    def __init__(self, spidevice=0):
        '''
            Basic constructor for our driver class.

            @param: spidevice - the SPI device to be used.
                Acts as a chip-enable. The Raspberry PI B+ has two such device
                output pins in-built.
                Defaults to 0.

            @return: None
        '''
        if spidevice != 1:
            spidevice = 0

        self.__spi = SpiDev()
        self.__spi.mode = 0b01
        self.__spi.open(0, spidevice)

        self.__buffer = [0] * 24

    def drawpixel(self, pixel):
        '''
            Draws a given Pixel object to the internal buffer.
            The buffer is formed of 24 bytes.
            Each byte represents a single color, the n'th bit being whether
            that particular color is active in the n'th led of that row.
            The colors are ordered in reverse. (BGR).

            @param: pixel - a Pixel object.

            @return: the Pixel encoded as a byte.
        '''
        # current row we're on:
        row = 3 * pixel.y

        # clear currently present color by unsetting the corresponding bit from
        # the three color bytes:
        self.__buffer[row] &= ~(1 << pixel.x)       # clear red.
        self.__buffer[row + 1] &= ~(1 << pixel.x)   # clear green.
        self.__buffer[row + 2] &= ~(1 << pixel.x)   # clear blue.

        # set red bit for this pixel, if necessary:
        if pixel.color in [Color.red, Color.white, Color.brown, Color.purple]:
            self.__buffer[row] |= 1 << pixel.x
        # set green bit:
        if pixel.color in [Color.green, Color.white, Color.turquoise, Color.brown]:
            self.__buffer[row + 1] |= 1 << pixel.x
        # set blue bit:
        if pixel.color in [Color.blue, Color.white, Color.turquoise, Color.purple]:
            self.__buffer[row + 2] |= 1 << pixel.x

    def write(self):
        '''
            Serially writes the whole of the video buffer to the matrix.
        '''
        self.__spi.xfer(self.__buffer)

    def clear(self):
        '''
            Clears both the internal buffer and the matrix.
        '''
        self.__buffer = [0] * 24
        self.write()

    def cleanup(self):
        '''
            Clears all registers and terminates the SPI connection.
        '''
        self.clear()
        self.__spi.close()
开发者ID:aznashwan,项目名称:pytris,代码行数:86,代码来源:matrix.py

示例11: LocalPiHardwareSPI

# 需要导入模块: from spidev import SpiDev [as 别名]
# 或者: from spidev.SpiDev import close [as 别名]
class LocalPiHardwareSPI(SPI, Device):
    def __init__(self, factory, port, device):
        self._port = port
        self._device = device
        self._interface = None
        if SpiDev is None:
            raise ImportError('failed to import spidev')
        super(LocalPiHardwareSPI, self).__init__()
        pins = SPI_HARDWARE_PINS[port]
        self.pin_factory.reserve_pins(
            self,
            pins['clock'],
            pins['mosi'],
            pins['miso'],
            pins['select'][device]
            )
        self._interface = SpiDev()
        self._interface.open(port, device)
        self._interface.max_speed_hz = 500000

    def close(self):
        if getattr(self, '_interface', None):
            self._interface.close()
        self._interface = None
        self.pin_factory.release_all(self)
        super(LocalPiHardwareSPI, self).close()

    @property
    def closed(self):
        return self._interface is None

    def __repr__(self):
        try:
            self._check_open()
            return 'SPI(port=%d, device=%d)' % (self._port, self._device)
        except DeviceClosed:
            return 'SPI(closed)'

    def transfer(self, data):
        """
        Writes data (a list of integer words where each word is assumed to have
        :attr:`bits_per_word` bits or less) to the SPI interface, and reads an
        equivalent number of words, returning them as a list of integers.
        """
        return self._interface.xfer2(data)

    def _get_clock_mode(self):
        return self._interface.mode

    def _set_clock_mode(self, value):
        self._interface.mode = value

    def _get_lsb_first(self):
        return self._interface.lsbfirst

    def _set_lsb_first(self, value):
        self._interface.lsbfirst = bool(value)

    def _get_select_high(self):
        return self._interface.cshigh

    def _set_select_high(self, value):
        self._interface.cshigh = bool(value)

    def _get_bits_per_word(self):
        return self._interface.bits_per_word

    def _set_bits_per_word(self, value):
        self._interface.bits_per_word = value
开发者ID:DirkUK,项目名称:python-gpiozero,代码行数:71,代码来源:local.py


注:本文中的spidev.SpiDev.close方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。