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


Python SPI.readbytes方法代码示例

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


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

示例1: SPI

# 需要导入模块: from Adafruit_BBIO.SPI import SPI [as 别名]
# 或者: from Adafruit_BBIO.SPI.SPI import readbytes [as 别名]
import time
from Adafruit_BBIO.SPI import SPI

spi_in = SPI(0,0)
spi_out = SPI(0,1)

while True:
    try:
        spi_out.writebytes([0x50])
        print spi_in.readbytes(2)
        time.sleep(0.02)
    except KeyboardInterrupt:
        break

开发者ID:toshi123,项目名称:bbb_training,代码行数:15,代码来源:test_spi.py

示例2: RFM69

# 需要导入模块: from Adafruit_BBIO.SPI import SPI [as 别名]
# 或者: from Adafruit_BBIO.SPI.SPI import readbytes [as 别名]
class RFM69(BaseRadio):
	DATA = []
	DATALEN = 0
	SENDERID = 0
	TARGETID = 0
	PAYLOADLEN = 0
	ACK_REQUESTED = False
	ACK_RECEIVED = False
	RSSI = 0
	_mode = 0
	_interruptPin = DIO0_PIN
	_csPin = NSS_PIN
	_address = 0
	_promiscuousMode = False
	_powerLevel = 31
	_isRFM69HW = True
	
	def __init__(self, isRFM69HW=True, interruptPin=DIO0_PIN, csPin=NSS_PIN):
		self._isRFM69HW = isRFM69HW
		self._interruptPin = interruptPin
		self._csPin = csPin

		self.SPI = SPI(SPI_BUS, SPI_CS)
		self.SPI.bpw = 8
		self.SPI.mode = 0
		self.SPI.msh = SPI_CLK_SPEED
		self.SPI.lsbfirst = False

		GPIO.setup(self._interruptPin, GPIO.IN)
		self.lastIrqLevel = GPIO.input(self._interruptPin)
		GPIO.setup(self._csPin, GPIO.OUT)
		GPIO.output(self._csPin, GPIO.HIGH)

		self.start_time = datetime.datetime.now()

	# Convention I want to stick to is a single underscore to indicate "private" methods.
	# I'm grouping all the private stuff up at the beginning.

	def _millis(self):
		delta = datetime.datetime.now() - self.start_time
		return delta.total_seconds() * 1000

	def _readReg(self, addr):
		self._select()
		self.SPI.writebytes([addr & 0x7F])
		result = self.SPI.readbytes(1)[0]
	#	print "readReg {} = {}". format(hex(addr), hex(result))
		self._unselect()
		return result

	def _writeReg(self, addr, value):
	#	print "writeReg, Address {}:{}". format(hex(addr), hex(value))
		self._select()
		self.SPI.writebytes([addr | 0x80, value])
		self._unselect()

	# Select the transceiver
	def _select(self):
		GPIO.output(self._csPin, GPIO.LOW)

	# Unselect the transceiver chip
	def _unselect(self): 
		GPIO.output(self._csPin, GPIO.HIGH)
	
	def _setMode(self, newMode):
		if newMode == RF69_MODE_TX:
			self._writeReg(REG_OPMODE, (self._readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_TRANSMITTER)
			if self._isRFM69HW:
				self.setHighPowerRegs(True)
		elif newMode == RF69_MODE_RX:
			self._writeReg(REG_OPMODE, (self._readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_RECEIVER)
			if self._isRFM69HW:
				self.setHighPowerRegs(False)
		elif newMode == RF69_MODE_SYNTH:
			self._writeReg(REG_OPMODE, (self._readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_SYNTHESIZER)
		elif newMode == RF69_MODE_STANDBY:
			self._writeReg(REG_OPMODE, (self._readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_STANDBY)
		elif newMode == RF69_MODE_SLEEP:
			self._writeReg(REG_OPMODE, (self._readReg(REG_OPMODE) & 0xE3) | RF_OPMODE_SLEEP)

		# we are using packet mode, so this check is not really needed
		# but waiting for mode ready is necessary when going from sleep because the FIFO may not 
		# be immediately available from previous mode
		while (self._mode == RF69_MODE_SLEEP and (self._readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00): # Wait for ModeReady
			pass
		self._mode = newMode

	def _canSend(self):
		#if signal stronger than -100dBm is detected assume channel activity
		if (self._mode == RF69_MODE_RX and self.PAYLOADLEN == 0 and self.getRSSI() < CSMA_LIMIT): 
			self._setMode(RF69_MODE_STANDBY)
			return True
		return False

	def _sendFrame(self, toAddress, buffer, bufferSize, requestACK=False, sendACK=False):
		self._setMode(RF69_MODE_STANDBY) #turn off receiver to prevent reception while filling fifo
		while ((self._readReg(REG_IRQFLAGS1) & RF_IRQFLAGS1_MODEREADY) == 0x00):
			pass # Wait for ModeReady
		self._writeReg(REG_DIOMAPPING1, RF_DIOMAPPING1_DIO0_00) # DIO0 is "Packet Sent"
		if bufferSize > RF69_MAX_DATA_LEN:
#.........这里部分代码省略.........
开发者ID:herrod,项目名称:BurnerBoard,代码行数:103,代码来源:rfm69.py

示例3: __init__

# 需要导入模块: from Adafruit_BBIO.SPI import SPI [as 别名]
# 或者: from Adafruit_BBIO.SPI.SPI import readbytes [as 别名]

#.........这里部分代码省略.........
        self.RD = RD

        self.data_in = BitArray(14)
        self.vrange = {"+-5V" : 0b00,
                     "+5V" : 0b10,
                     "+-10V" : 0b01,
                     "+10V" : 0b11}

        #Create a chan dict as not standard binary
        self.chans = {0 : 0b000,
                      1 : 0b100,
                      2 : 0b001,
                      3 : 0b101,
                      4 : 0b010,
                      5 : 0b110,
                      6 : 0b011,
                      7 : 0b111}


        self.adc_reg = {"Monitor" : False,
                        "Range" : "+5V",
                        "V" : 0,
                        "Command" : 0}

        #Bitstring is terribly slow so for a register
        #read we use a preconstructed bitstring rather
        #Than create every time
        
        self.chip_reg = []
        for i in xrange(8):
            self.chip_reg.append(self.adc_reg.copy())

        self.single_ended = 0b1

        self.setup_chip()
        self.setup_spi()

    def setup_spi(self):
        #This is for BB Black
        self.spi = SPI(self.spi_bus, self.spi_client)
        self.spi.msh = self.spi_freq
        self.spi.mode = self.spi_mode
        self.bpw = self.spi_bits_per_word
        self.spi.cshigh = self.spi_cshigh
        
    def setup_chip(self):
        #Just need to setup RD and make sure it is low
        GPIO.setup(self.RD, GPIO.OUT)
        GPIO.output(self.RD, False)
     

    def set_reg(self,adc,monitor,adc_range):
        self.chip_reg[adc]["Monitor"] = monitor
        self.chip_reg[adc]["Range"] = adc_range
        self.chip_reg[adc]["Command"] = self.construct_word(adc,adc_range)

    def construct_word(self, chan_no, vrange):
        t_word = BitArray(8)
        t_word[0] = self.single_ended
        t_word[1:4] = self.chans[chan_no]
        t_word[4:6] = self.vrange[vrange]
        
        #Ignore nap and sleep
        return t_word

    def single_read(self, chan_no, v_range):
        #Need to set command and then read back so 
        #two words - Just send the same word twice
        #self.send_data(self.construct_word(chan_no, v_range))
        data_out = self.send_data(self.construct_word(chan_no, v_range))
        data_conv  = self.convert_to_v(data_out, v_range)
        return data_conv

    def register_read(self):
        #This does one pass at reading all dac inputs
        for i in xrange(8):
            if self.chip_reg[i]["Monitor"] is True:
                data_out = self.send_data(self.chip_reg[i]["Command"])
                vv = self.convert_to_v(data_out, self.chip_reg[i]["Range"])
                self.chip_reg[i]["V"] = vv
                                      
    def send_data(self,data):
        self.spi.writebytes([data.uint,0x00]) #Send data then zeros as per DS
        #at 1MHz we don't care if it's duplex read
        a,b = self.spi.readbytes(2)
        self.data_in[0:8] = a 
        self.data_in[8:] = (b >> 2)
        return self.data_in

    def convert_to_v(self,num, v_range):  
        if v_range == "+5V":
            return 5.0*num.uint/2**14
        elif v_range == "+10V":
            return 10.0*num.uint/2**14
        elif v_range == "+-5V":
                return num.int*5.0/2**13
        elif v_range == "+-10V":
            return num.int*10.0/2**13
        else:
            return -69
开发者ID:CCATObservatory,项目名称:ccat_bitbang_chips,代码行数:104,代码来源:ltc1858.py

示例4: SPI

# 需要导入模块: from Adafruit_BBIO.SPI import SPI [as 别名]
# 或者: from Adafruit_BBIO.SPI.SPI import readbytes [as 别名]
#!/usr/bin/env	python
# -*-	coding: utf-8 -*-
import time
from Adafruit_BBIO.SPI import SPI

spi_in = SPI(0, 0)
spi_out = SPI(0, 1)


while True:
	try:
		print spi_in.readbytes(1)
		time.sleep(0.03)

	except KeyboardInterrupt:
		Break

开发者ID:5montest,项目名称:spi,代码行数:18,代码来源:test_spi.py

示例5: SPI

# 需要导入模块: from Adafruit_BBIO.SPI import SPI [as 别名]
# 或者: from Adafruit_BBIO.SPI.SPI import readbytes [as 别名]
from Adafruit_BBIO.SPI import SPI 

spi = SPI(1,0) 
spi.mode=2 

spi.msh=200000 
spi.open(1,0) 

print spi.readbytes(4)
#print spi.xfer2([32, 11, 110, 22, 220]) 
spi.close() 
开发者ID:bauchter-work,项目名称:2445_git_repo,代码行数:13,代码来源:no_loopback_test.py

示例6: __init__

# 需要导入模块: from Adafruit_BBIO.SPI import SPI [as 别名]
# 或者: from Adafruit_BBIO.SPI.SPI import readbytes [as 别名]

#.........这里部分代码省略.........
            change_fDAC = - int(round(VERR_corr / fDAClsb))
        
        try:
            oldDacValues = pyon.load_file(self.fname)
            #old_cDAC = self.get_dataset("BField_stabiliser.cDAC")
            #old_fDAC = self.get_dataset("BField_stabiliser.fDAC")
        except FileNotFoundError:
            print('Error: could not find file. Using default values')
            oldDacValues = {'cDAC':54710,'fDAC':33354}
        old_cDAC = oldDacValues['cDAC']
        old_fDAC = oldDacValues['fDAC']
        
        new_cDAC = old_cDAC + change_cDAC
        new_fDAC = old_fDAC + change_fDAC
        
        # when fDAC out of range, change coarse DAC
        if (new_fDAC > self.maxDACvalue or new_fDAC < 0):
            new_cDAC += (new_fDAC-30000)/200
            new_fDAC = 30000
        
        return [new_cDAC, new_fDAC]
  
        
    def set_DAC_values(self,CDAC=-1,FDAC=-1):
        ''' this is the external function to be called for setting the coarse and fine dac '''
        if CDAC is not -1:
            self._set_coarse_dac(CDAC)
        if FDAC is not -1:
            self._set_fine_dac(FDAC)

    def read_voltage(self,verbose=0):
        ''' this is the external function to be called for reading out the ADC voltage '''
        # for AD7477:
        # 2 bytes
        bytes = self._read_value(self.CS_ADC_PIN, 2)
        # most significant bit first
        num = bytes[0] * 256 + bytes[1]
        # 4 leading zeros, 2 trailing zeros
        num = num >> 2
        # 5V reference, 10 bits
        AV = 5.0 * num / 1024
        
        # G=0.33 for voltage divider between shunt and ADC
        # convert to feedback shunt input voltage
        FB_SHNT_IN_V = 3 * AV
        
        if verbose:
            print("byte response: %X %X"% (bytes[0], bytes[1]))
            print("num respose: %d" % (num))
            print("ADC input voltage: %.2f" % (AV))
            print("Feedback shunt input voltage: %.2f" % (FB_SHNT_IN_V))
        
        return AV
    
    def test_set_pin(self,logicHigh=1,pin="P8_14"):
        ''' this is a test function to set a pin on the beaglebone to high or low '''
        GPIO.setup(pin, GPIO.OUT)
        if logicHigh:
            GPIO.output(pin, GPIO.HIGH)
        else:
            GPIO.output(pin, GPIO.LOW)
    
    def _set_coarse_dac(self,value):
        self._set_dac_value(self.CS_FB_COARSE_PIN, value)

    def _set_fine_dac(self,value):
        self._set_dac_value(self.CS_FB_FINE_PIN, value)
    
    def _set_dac_value(self, cs_pin, value):
        GPIO.output(cs_pin, GPIO.LOW)
        
        value = self._check_dac_value(value)
        
        # check if value is in range
        MSByte = value >> 8
        LSByte = value - (MSByte << 8)
        # write
        self.spi00.writebytes([MSByte,LSByte])
        
        GPIO.output(cs_pin, GPIO.HIGH)
        
    def _check_dac_value(self,value):
        value = int(value)
        if value > 65535:
            value = 65535
        if value < 0:
            value = 0
        return value
    
    def _read_value(self,cs_pin, length):
        ''' internal read value of analogue voltage '''
        GPIO.output(cs_pin, GPIO.LOW)
        
        bytes = self.spi00.readbytes(length)
        
        GPIO.output(cs_pin, GPIO.HIGH)
        return bytes

    def ping(self):
        return True
开发者ID:cjbe,项目名称:artiqDrivers,代码行数:104,代码来源:bStabBb.py


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