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


Python SpiDev.xfer2方法代码示例

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


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

示例1: MCP3008

# 需要导入模块: from spidev import SpiDev [as 别名]
# 或者: from spidev.SpiDev import xfer2 [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: MCP3008

# 需要导入模块: from spidev import SpiDev [as 别名]
# 或者: from spidev.SpiDev import xfer2 [as 别名]
class MCP3008(object):

    """Class for MCP3008 ADC"""

    def __init__(self, port=0, device=0):
        self.spi = SpiDev()
        # connect spi object to specified spi device
        self.spi.open(port, device)

    def readValueChannel(self, channel=0):
        """
        read SPI data from MCP3008 on channel -> digital value
            spi.xfer2() send three bytes to the device
                the first byte is 1 -> 00000001
                the second byte is 8 + channel and left shift with 4 bits
                the third byte is 0 -> 00000000
            the device return 3 bytes as responce
        """
        # perform spi transaction
        adc = self.spi.xfer2([1, (8 + channel) <<4, 0])
        # extract value from data bytes
        data = ((adc[1] & 3) << 8) + adc[2]
        return data

    def readVoltChannel(self, channel=0, vmax=3.3, places=5):
        """
        read the digital data from MCP3008 and convert it to voltage
            MCP3008: 10bit ADC -> value in number range 0-1023
            spi value -> voltage
                   0  ->  0v
                1023  ->  vmax
        """
        # read spi digital value
        adc = self.spi.xfer2([1, (8 + channel) <<4, 0])
        data = ((adc[1] & 3) << 8) + adc[2]
        # convert it to voltage
        volts = (data * vmax) / float(1023)
        # round to specified number of decimal places
        volts = round(volts, places)
        return volts
开发者ID:stevelorenz,项目名称:codes-on-rasp,代码行数:42,代码来源:mcp3008.py

示例3: SpiDevice

# 需要导入模块: from spidev import SpiDev [as 别名]
# 或者: from spidev.SpiDev import xfer2 [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

示例4: __init__

# 需要导入模块: from spidev import SpiDev [as 别名]
# 或者: from spidev.SpiDev import xfer2 [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

示例5: __init__

# 需要导入模块: from spidev import SpiDev [as 别名]
# 或者: from spidev.SpiDev import xfer2 [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

示例6: Temperature

# 需要导入模块: from spidev import SpiDev [as 别名]
# 或者: from spidev.SpiDev import xfer2 [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

示例7: SPIHardwareInterface

# 需要导入模块: from spidev import SpiDev [as 别名]
# 或者: from spidev.SpiDev import xfer2 [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

示例8: SPIDataLink

# 需要导入模块: from spidev import SpiDev [as 别名]
# 或者: from spidev.SpiDev import xfer2 [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: __init__

# 需要导入模块: from spidev import SpiDev [as 别名]
# 或者: from spidev.SpiDev import xfer2 [as 别名]
class cc2500:

    REG_MARCSTATE = 0xC0 | 0x35
    CMD_SRES = 0x30
    CMD_SFSTXON = 0x31
    CMD_SXOFF = 0x32
    CMD_XCAL = 0x33
    CMD_SRX  = 0x34
    CMD_STX  = 0x35
    CMD_SIDLE = 0x36
    CMD_SWOR = 0x38
    CMD_SPWD = 0x39
    CMD_SFRX = 0x3A
    CMD_SFTX = 0x3B
    CMD_SWORRST = 0x3C
    CMD_SNOP = 0x3D
    CMD_PATABLE = 0x3E
    CMD_TXFIFO = 0x3F

    CMD_SINGLE_WRITE = 0x00
    CMD_BRUST_WRITE = 0x40
    CMD_SINGLE_READ  = 0x80
    CMD_BRUST_READ = 0xC0



    # get Main Radio Control State Machine State
    def __init__(self, bus = 0, channel_select = 1):
        self.bus = SpiDev()
        self.bus.open(bus,channel_select)
        self.reset()
        self.buff=[]
        self.run = True
        self.timestamp = time()

    def get_STATE(self):
        return self.bus.xfer2([self.REG_MARCSTATE,0x00])[1]

    def set_reg(self,reg, byte):
        return self.bus.xfer2([reg, byte])

    def get_reg(self, reg):
        return self.bus.xfer2([0x80 | reg, 0x00])

    def info(self):
        state = self.get_STATE()
        txbyte = self.get_TXBYTES()
        rxbyte = self.get_RXBYTES()
        print "state : %d , tx: %d , rx: %d " % (state, txbyte, rxbyte)


    ## 
    ## COMMAND  
    ##
    def STX(self):
        return self.bus.xfer2([self.CMD_STX])

    def SRX(self):
        return self.bus.xfer2([self.CMD_SRX])

    def SIDLE(self):
        return self.bus.xfer2([self.CMD_SIDLE])

    def SFRX(self):
        return self.bus.xfer2([self.CMD_SFRX])
    
    def SFTX(self):
        return self.bus.xfer2([self.CMD_SFTX])

    def SRES(self):
        return self.bus.xfer2([self.CMD_SRES])

    def reset(self):
        self.SRES()
        reg_config = [0x0B, 0x2E, 0x06, 0x07, 0xD3, 0x91, 0x61, 0x04,
                      0x45, 0x00, 0x00, 0x09, 0x00, 0x5D, 0x93, 0xB1,
                      0x2D, 0x3B, 0x73, 0x22, 0xF8, 0x01, 0x07, 0x00,
                      0x18, 0x1D, 0x1C, 0xC7, 0x00, 0xB2, 0x87, 0x6B,
                      0xF8, 0xB6, 0x10, 0xEA, 0x0A, 0x00, 0x11, 0x41,
                      0x00, 0x59, 0x7F, 0x3F, 0x88, 0x31, 0x0B ]
        for reg, val in enumerate(reg_config):
            self.bus.xfer2([reg, val])
        self.SIDLE()
        self.SFRX()
        self.SFTX()



    ## FIFO buffer 64byte
    def send(self,package):
        tmp = []
        if type(package) == str:
            tmp = [ord(i) for i in package]
        else:
            tmp = list(package)
        tmp = [self.CMD_TXFIFO | self.CMD_BRUST_WRITE ,len(tmp)] + tmp
        print "send"
        print tmp
        self.bus.xfer2(tmp)    # write package to fifo buffer (max 64byte)
        if self.get_STATE() == 22:
#.........这里部分代码省略.........
开发者ID:onionys,项目名称:pymaker,代码行数:103,代码来源:cc2500.py

示例10: CC2500

# 需要导入模块: from spidev import SpiDev [as 别名]
# 或者: from spidev.SpiDev import xfer2 [as 别名]
class CC2500(object):

    REG_MARCSTATE = 0xC0 | 0x35
    CMD_SRES = 0x30
    CMD_SFSTXON = 0x31
    CMD_SXOFF = 0x32
    CMD_XCAL = 0x33
    CMD_SRX  = 0x34
    CMD_STX  = 0x35
    CMD_SIDLE = 0x36
    CMD_SWOR = 0x38
    CMD_SPWD = 0x39
    CMD_SFRX = 0x3A
    CMD_SFTX = 0x3B
    CMD_SWORRST = 0x3C
    CMD_SNOP = 0x3D
    CMD_PATABLE = 0x3E
    CMD_TXFIFO = 0x3F

    CMD_SINGLE_WRITE = 0x00
    CMD_BRUST_WRITE = 0x40
    CMD_SINGLE_READ  = 0x80
    CMD_BRUST_READ = 0xC0

    # get Main Radio Control State Machine State
    def __init__(self, bus = 0, channel_select = 1):
        self.bus = SpiDev()
        self.bus.open(bus,channel_select)
        self.reset()

    ## 
    ## COMMAND  
    ##
    def STX(self):
        self.bus.xfer2([self.CMD_STX])

    def SRX(self):
        self.bus.xfer2([self.CMD_SRX])

    def SIDLE(self):
        self.bus.xfer2([self.CMD_SIDLE])

    def SFRX(self):
        self.bus.xfer2([self.CMD_SFRX])
    
    def SFTX(self):
        self.bus.xfer2([self.CMD_SFTX])

    def SRES(self):
        self.bus.xfer2([self.CMD_SRES])

    ## Access REG

    def get_STATE(self):
        return self.bus.xfer2([0xC0 | 0x35, 0x00])[1]

    def get_RXBYTES(self):
        return self.bus.xfer2([0xC0 | 0x3B, 0x00])[1]


    def write_TXFIFO(self,package):
        tmp = []
        if type(package) == str:
            tmp = [ord(i) for i in package]
        else:
            tmp = list(package)
        tmp = [self.CMD_TXFIFO | self.CMD_BRUST_WRITE ,len(tmp)] + tmp
        self.bus.xfer2(tmp)    # write package to fifo buffer (max 64byte)


    def read_RXFIFO(self):
        len_FIFO = self.get_RXBYTES()
        return self.bus.xfer2([self.CMD_BRUST_READ | 0x3F for i in range(len_FIFO)])


    def reset(self):
        self.SRES()

        reg_config = [
            0x29,0x2E,0x06,0x07,0xD3,0x91,0x61,0x04,
            0x45,0x00,0x00,0x09,0x00,0x5E,0xC4,0xEC,
            0x2C,0x22,0x73,0x22,0xF8,0x01,0x07,0x00,
            0x18,0x1D,0x1C,0xC7,0x00,0xB2,0x87,0x6B,
            0xF8,0xB6,0x10,0xEB,0x0B,0x1D,0x11,0x41,
            0x00,0x59,0x7F,0x3C,0x88,0x31,0x0B
        ]

        for reg, val in enumerate(reg_config):
            self.bus.xfer2([reg, val])

        self.SIDLE()
        self.SFRX()
        self.SFTX()

    def read_config(self):
        reg_config = []
        for reg in range(49):
            val = self.bus.xfer2([0x80 | reg , 0x00])[1]
            reg_config.append(val)
        return reg_config
#.........这里部分代码省略.........
开发者ID:onionys,项目名称:python_code,代码行数:103,代码来源:cc2500.py

示例11: LocalPiHardwareSPI

# 需要导入模块: from spidev import SpiDev [as 别名]
# 或者: from spidev.SpiDev import xfer2 [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.xfer2方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。