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


Python SpiDev.writebytes方法代码示例

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


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

示例1: dac8568

# 需要导入模块: from spidev import SpiDev [as 别名]
# 或者: from spidev.SpiDev import writebytes [as 别名]
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,代码行数:32,代码来源:beagledaq.py

示例2: CpPhy

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

#.........这里部分代码省略.........
				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):
		limit = TimeLimited(timeout)
		while GPIO.event_detected(self.GPIO_IRQ):
			if limit.exceed():
				return None
			limit.sleep(0.001)
		limit.add(0.5)
		while not limit.exceed():
			fc = self.spi.readbytes(1)[0]
			if fc != CpPhyMessage.RPI_PACK_NOP:
				break
		else:
			return None
		reply = [ fc ]
		reply.extend(self.spi.readbytes(CpPhyMessage.RASPI_PACK_HDR_LEN - 1))
		payloadLen = reply[1] & 0xFF
		if payloadLen:
			reply.extend(self.spi.readbytes(payloadLen))
		message = CpPhyMessage(0)
		message.setRawData(reply)
		if self.debug:
			print("[PHY] recv msg:", message)
		return message

	def __sendMessage(self, message):
		if self.debug:
			print("[PHY] send msg:", message)
		self.spi.writebytes(message.getRawData())

	def sendReset(self):
		self.__sendMessage(CpPhyMessage(CpPhyMessage.RPI_PACK_RESET))
		reply = self.poll(timeout=-1)
		if reply.fc != CpPhyMessage.RPI_PACK_ACK:
			raise PhyError("Failed to reset PHY")

	def profibusSetPhyConfig(self, baudrate=19200,
				 rxTimeoutMs=100,
				 bitErrorChecks=True,
				 rtsMode=PB_PHY_RTS_SENDING_HI):
		try:
			baudID = self.baud2id[baudrate]
		except KeyError:
			raise PhyError("Invalid baud-rate")
		if rxTimeoutMs < 1 or rxTimeoutMs > 255:
			raise PhyError("Invalid RX timeout")
		payload = [ baudID,
			    rxTimeoutMs,
			    1 if bitErrorChecks else 0,
			    rtsMode & 0xFF ]
		message = CpPhyMessage(CpPhyMessage.RPI_PACK_SETCFG, payload)
		self.__sendMessage(message)
		reply = self.poll(timeout=-1)
		if reply.fc != CpPhyMessage.RPI_PACK_ACK:
			raise PhyError("Failed to upload config")

	def profibusSend_SDN(self, telegramData):
		self.__sendMessage(CpPhyMessage(CpPhyMessage.RPI_PACK_PB_SDN,
						telegramData))

	def profibusSend_SRD(self, telegramData):
		self.__sendMessage(CpPhyMessage(CpPhyMessage.RPI_PACK_PB_SRD,
						telegramData))
开发者ID:devgituser,项目名称:raspi-profibus,代码行数:104,代码来源:phy.py


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