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


Python spidev.SpiDev类代码示例

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


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

示例1: MCP3008

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,代码行数:27,代码来源:input_devices.py

示例2: ad7606

class ad7606():
    try:
        start_acq_fd = open("/sys/class/gpio/gpio145/value", 'w', 0)
    except IOError as e:
        print("Error opening start acquire gpio pin: %s" % e)
        raise

    def __init__(self, dev):
        self.dev = SpiDev()
        try:
            self.dev.open(3,dev)
            self.dev.mode = 2
        except IOError as e:
            print("Error opening /dev/spidev3.%d: %s" % (dev, e))
            raise

    def trigger_acquire(self):
        self.start_acq_fd.write(b'1')
        self.start_acq_fd.write(b'0')

    def read(self):
        self.trigger_acquire()
        buf = self.dev.readbytes(16)
        samples = [0,0,0,0,0,0,0,0]
        for i in xrange(8):
            samples[i] = buf[2*i] << 8 | buf[2*i+1] << 0
        return samples
开发者ID:bgamari,项目名称:libbeagledaq,代码行数:27,代码来源:beagledaq.py

示例3: dac8568

class dac8568():
    def __init__(self, dev):
        self.dev = SpiDev()
        try:
            self.dev.open(4,dev)
        except IOError as e:
            print("Error opening /dev/spidev4.%d: %s" % (dev, e))
            raise

    def build_command(self, control, addr, data):
        prefix = 0
        features = 0
        cmd = [0,0,0,0]
        cmd[3] = (0xf    & features) << 0 | (0xf & data) << 4
        cmd[2] = (0x0ff0 & data) >> 4
        cmd[1] = (0xf000 & data) >> 12    | (0xf & addr) << 4
        cmd[0] = (0xf    & control) << 0  | (0xf & prefix) << 4
        return cmd
       
    # indexed by label number, mapping to DAC channel number
    channel_map = (0,2,4,6,7,5,3,1)
    write_mode = { 'WRITE': 0x0,
                   'UPDATE': 0x1,
                   'WRITE_UPDATE_ALL': 0x2,
                   'WRITE_UPDATE': 0x3 }

    def write(self, addr, data, mode = write_mode['WRITE_UPDATE']):
        addr = self.channel_map[addr]
        cmd  = self.build_command(mode, addr, data)
        self.dev.writebytes(cmd)
开发者ID:bgamari,项目名称:libbeagledaq,代码行数:30,代码来源:beagledaq.py

示例4: AnalogInputDevice

class AnalogInputDevice(CompositeDevice):
    """
    Represents an analog input device connected to SPI (serial interface).
    """

    def __init__(self, device=0, bits=None):
        if bits is None:
            raise InputDeviceError('you must specify the bit resolution of the device')
        if device not in (0, 1):
            raise InputDeviceError('device must be 0 or 1')
        self._device = device
        self._bits = bits
        self._spi = SpiDev()
        self._spi.open(0, self.device)
        super(AnalogInputDevice, self).__init__()

    def close(self):
        """
        Shut down the device and release all associated resources.
        """
        if self._spi:
            s = self._spi
            self._spi = None
            s.close()
        super(AnalogInputDevice, self).close()

    @property
    def bus(self):
        """
        The SPI bus that the device is connected to. As the Pi only has a
        single (user accessible) SPI bus, this always returns 0.
        """
        return 0

    @property
    def device(self):
        """
        The select pin that the device is connected to. The Pi has two select
        pins so this will be 0 or 1.
        """
        return self._device

    def _read(self):
        raise NotImplementedError

    @property
    def value(self):
        """
        A value read from the device. This will be a floating point value
        between 0 and 1 (scaled according to the number of bits supported by
        the device).
        """
        return self._read() / (2**self._bits - 1)
开发者ID:tjguk,项目名称:python-gpiozero,代码行数:53,代码来源:input_devices.py

示例5: SpiDevice

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,代码行数:14,代码来源:spi_device.py

示例6: run

def run():
    """
    Launches the main loop
    """
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(ALERT_PIN, GPIO.OUT)
    try:
        spi = SpiDev()
        spi.open(0, 0)
        api = XivelyAPIClient(XIVELY_API_KEY)
        feed = api.feeds.get(XIVELY_FEED_ID)

        moisture = get_datastream(feed, "water")
        light = get_datastream(feed, "light")
        temp = get_datastream(feed, "temp")

        sent_notification = False

        while True:
            moisture.current_value = readadc(spi, ADC_MOISTURE_PIN)
            moisture.at = datetime.utcnow()
            light.current_value = readadc(spi, ADC_LIGHT_PIN)
            light.at = datetime.utcnow()
            temp.current_value = "%.2f" % convert_temp(readadc(spi, ADC_TMP_PIN))
            temp.at = datetime.utcnow()
            if(DEBUG):
                print("Moisture: %d, light: %d, temp: %s" % (
                    moisture.current_value,
                    light.current_value,
                    temp.current_value))
            if(moisture.current_value < MOISTURE_THRESHOLD):
                if(not sent_notification):
                    send_pushover_msg(
                            "Please water your plant: %s" % moisture.current_value)
                    sent_notification = True
                GPIO.output(ALERT_PIN, GPIO.HIGH)
            else:
                sent_notification = False
                GPIO.output(ALERT_PIN, GPIO.LOW)
            try:
                moisture.update()
                light.update()
                temp.update()
            except Exception as e:
                print("%s" % e.strerror)
            time.sleep(UPDATE_DELAY)
    finally:
        GPIO.cleanup()
开发者ID:paulollivier,项目名称:greenpi,代码行数:48,代码来源:GreenPi.py

示例7: __init__

 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()
开发者ID:onionys,项目名称:pymaker,代码行数:7,代码来源:cc2500.py

示例8: __init__

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,代码行数:16,代码来源:MCP3008.py

示例9: __init__

 def __init__(self, dev):
     self.dev = SpiDev()
     try:
         self.dev.open(4,dev)
     except IOError as e:
         print("Error opening /dev/spidev4.%d: %s" % (dev, e))
         raise
开发者ID:bgamari,项目名称:libbeagledaq,代码行数:7,代码来源:beagledaq.py

示例10: __init__

 def __init__(self, device=0, bits=None):
     if bits is None:
         raise InputDeviceError("you must specify the bit resolution of the device")
     if device not in (0, 1):
         raise InputDeviceError("device must be 0 or 1")
     self._device = device
     self._bits = bits
     self._spi = SpiDev()
     self._spi.open(0, self.device)
开发者ID:chatzin,项目名称:python-gpiozero,代码行数:9,代码来源:input_devices.py

示例11: __init__

 def __init__(self, device=0, bits=None):
     if bits is None:
         raise InputDeviceError('you must specify the bit resolution of the device')
     if device not in (0, 1):
         raise InputDeviceError('device must be 0 or 1')
     self._device = device
     self._bits = bits
     self._spi = SpiDev()
     self._spi.open(0, self.device)
     super(AnalogInputDevice, self).__init__()
开发者ID:tjguk,项目名称:python-gpiozero,代码行数:10,代码来源:input_devices.py

示例12: AnalogInputDevice

class AnalogInputDevice(object):
    """
    Represents an analog input device connected to SPI (serial interface).
    """

    def __init__(self, device=0, bits=None):
        if bits is None:
            raise InputDeviceError("you must specify the bit resolution of the device")
        if device not in (0, 1):
            raise InputDeviceError("device must be 0 or 1")
        self._device = device
        self._bits = bits
        self._spi = SpiDev()
        self._spi.open(0, self.device)

    def close(self):
        if self._spi:
            s = self._spi
            self._spi = None
            s.close()

    def __enter__(self):
        return self

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

    @property
    def bus(self):
        return 0

    @property
    def device(self):
        return self._device

    def _read(self):
        raise NotImplementedError

    @property
    def value(self):
        return self._read() / (2 ** self._bits - 1)
开发者ID:chatzin,项目名称:python-gpiozero,代码行数:41,代码来源:input_devices.py

示例13: MCP3008

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,代码行数:40,代码来源:mcp3008.py

示例14: __init__

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,代码行数:22,代码来源:MCP3008.py

示例15: __init__

	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
开发者ID:devgituser,项目名称:raspi-profibus,代码行数:51,代码来源:phy.py


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