本文整理汇总了Python中Adafruit_BBIO.SPI.SPI.writebytes方法的典型用法代码示例。如果您正苦于以下问题:Python SPI.writebytes方法的具体用法?Python SPI.writebytes怎么用?Python SPI.writebytes使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Adafruit_BBIO.SPI.SPI
的用法示例。
在下文中一共展示了SPI.writebytes方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: DAC
# 需要导入模块: from Adafruit_BBIO.SPI import SPI [as 别名]
# 或者: from Adafruit_BBIO.SPI.SPI import writebytes [as 别名]
class DAC():
""" This class uses an actual DAC """
def __init__(self, channel):
""" Channel is the pwm output is on (0..15) """
self.channel = channel
self.offset = 0.0
if 'SPI' in globals():
# init the SPI for the DAC
try:
self.spi2_0 = SPI(0, 0)
except IOError:
self.spi2_0 = SPI(1, 0)
self.spi2_0.bpw = 8
self.spi2_0.mode = 1
else:
logging.warning("Unable to set up SPI")
self.spi2_0 = None
def set_voltage(self, voltage):
logging.debug("Setting voltage to "+str(voltage))
if self.spi2_0 is None:
logging.debug("SPI2_0 missing")
return
v_ref = 3.3 # Voltage reference on the DAC
dacval = int((voltage * 256.0) / v_ref)
byte1 = ((dacval & 0xF0) >> 4) | (self.channel << 4)
byte2 = (dacval & 0x0F) << 4
self.spi2_0.writebytes([byte1, byte2]) # Update all channels
self.spi2_0.writebytes([0xA0, 0xFF])
示例2: SPI
# 需要导入模块: from Adafruit_BBIO.SPI import SPI [as 别名]
# 或者: from Adafruit_BBIO.SPI.SPI import writebytes [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
示例3: SPI
# 需要导入模块: from Adafruit_BBIO.SPI import SPI [as 别名]
# 或者: from Adafruit_BBIO.SPI.SPI import writebytes [as 别名]
from time import sleep
spi = SPI(0,0)
spi.bpw = 12
spi.msh = 100000
spi.lsbfirst = False
spi.mode = 0
spi.open
tlc5947_count = 2
tlc5947_channels = 24
# buffer = [0x000] * 48
buffer = [0x000] * (tlc5947_count * tlc5947_channels)
#spi.writebytes(buffer)
#print buffer
spi.writebytes(buffer)
#sleep(1)
spi.close
# CS_0 P9_17 lat
# DO P9_21 din
# DI P9_18 n/c
# SCLK P9_22 clk
示例4: MAX7221
# 需要导入模块: from Adafruit_BBIO.SPI import SPI [as 别名]
# 或者: from Adafruit_BBIO.SPI.SPI import writebytes [as 别名]
class MAX7221(Display.Display):
def __init__(self, bus, device):
super(MAX7221, self).__init__(8, 8)
SPI(bus, device).mode = 0
self.spi_bus = SPI(bus, device)
self.buf = [0, 0, 0, 0, 0, 0, 0, 0]
self.OP_NOP = 0x0
self.OP_DIG0 = 0x1
self.OP_DIG1 = 0x2
self.OP_DIG2 = 0x3
self.OP_DIG3 = 0x4
self.OP_DIG4 = 0x5
self.OP_DIG5 = 0x6
self.OP_DIG6 = 0x7
self.OP_DIG7 = 0x8
self.OP_DECODEMODE = 0x9
self.OP_INTENSITY = 0xA
self.OP_SCANLIMIT = 0xB
self.OP_SHUTDOWN = 0xC
self.OP_DISPLAYTEST = 0xF
self.init()
def rotate(self, x, n):
return (x << n) | (x >> (8 - n))
def init(self):
self.write_command(self.OP_DISPLAYTEST, 0x0)
self.write_command(self.OP_SCANLIMIT, 0x7)
self.write_command(self.OP_DECODEMODE, 0x0)
self.shutdown(False)
self.update_display()
def shutdown(self, off):
if off:
off = 0
else:
off = 1
self.write_command(self.OP_SHUTDOWN, off)
def write_spi(self, reg, data):
self.spi_bus.writebytes([reg, data])
def write_command(self, command, arg):
self.write_spi(command, arg)
def write_display(self, buffer):
for col in range(0, len(buffer)):
self.write_spi(col + 0x1, buffer[col])
def update_display(self):
self.write_display(self.buf)
def draw_pixel(self, x, y, color):
if color == 1:
self.buf[y] = self.buf[y] | (1 << x)
elif color == 0:
self.buf[y] = self.buf[y] & ~(1 << x)
self.update_display()
def draw_fast_hline(self, x, y, w, color):
self.write_spi(0x1 + y, (0xFF >> (8 - w)) << x)
示例5: RFM69
# 需要导入模块: from Adafruit_BBIO.SPI import SPI [as 别名]
# 或者: from Adafruit_BBIO.SPI.SPI import writebytes [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:
#.........这里部分代码省略.........
示例6: __init__
# 需要导入模块: from Adafruit_BBIO.SPI import SPI [as 别名]
# 或者: from Adafruit_BBIO.SPI.SPI import writebytes [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
示例7: LED_LPD8806
# 需要导入模块: from Adafruit_BBIO.SPI import SPI [as 别名]
# 或者: from Adafruit_BBIO.SPI.SPI import writebytes [as 别名]
class LED_LPD8806(object):
# Constructor
def __init__(self):
self.spi = SPI(0,0) #/dev/spidev1.0 (be sure to run Python with su if 'no permission'
self.spi.msh=1000000 #SPI clock set to 1MHz (slowed from 10MHz for better stability across setups)
self.spi.bpw = 8 # bits per word
self.spi.threewire = False # not half-duplex
self.spi.lsbfirst = False # we want MSB first
self.spi.mode = 0 # options are modes 0 through 3
self.spi.cshigh = False # we want chip select to be active low
self.spi.open(0,0) # make it so
time.sleep(0.05)
def setup(self, led_pixels, debug=False):
if (debug):
print "Initializing LED strip"
global pixels
pixels = [[0x80 for x in range(3)] for y in range(led_pixels)]
for i in range(led_pixels):
pixels[i]=[0x00, 0x00, 0x00]
# Define LED functions:
# --------------------
# Update pixel display with a given delay time between pixel updates:
def writestrip(self, delay):
if (delay < 0):
delay = 0
for i in range(len(pixels)):
self.spi.writebytes([0x00, 0x00, 0x00]) #prepare write
for i in range(len(pixels)):
self.spi.writebytes(pixels[i]) #write colors to pixels
time.sleep(delay)
# Turn off all LEDs:
def clearstrip(self):
global pixels
for i in range(len(pixels)):
self.spi.writebytes([0x00, 0x00, 0x00]) #prepare write
for i in range(len(pixels)):
pixels[i] = [0x80, 0x80, 0x80]
self.writestrip(0)
# Set an individual pixel to a specific color (to display later):
def setpixelcolor(self, n, g, r, b):
global pixels
if (n >= len(pixels)):
return
if (n < 0):
return
if (g > 0xFF):
g = 0xFF
if (g < 0x80):
g = 0x80
if (r > 0xFF):
r = 0xFF
if (r < 0x80):
r = 0x80
if (b > 0xFF):
b = 0xFF
if (b < 0x80):
b = 0x80
pixels[n] = [g, r, b]
# Update display with warmer colors (more red light) by a specified amount with a delay between pixels
def warmstrip(self, warmth, delay):
global pixels
if (delay < 0):
delay = 0
for n in range(len(pixels)):
if((pixels[n][1] + warmth) < 0x80):
pixels[n][1] = 0x80
elif((pixels[n][2] + warmth) > 0xFF):
pixels[n][1] = 0xFF
else:
pixels[n][1] = pixels[n][1]+warmth
self.writestrip(delay)
# Update display with cooler colors (more blue) by a specified amount with a delay between each pixel
def coolstrip(self, coolfactor, delay):
global pixels
if (delay < 0):
delay = 0
for n in range(len(pixels)):
if((pixels[n][2] + coolfactor) < 0x80):
pixels[n][2] = 0x80
elif((pixels[n][2] + coolfactor) > 0xFF):
pixels[n][2] = 0xFF
else:
pixels[n][2] = pixels[n][2]+coolfactor
self.writestrip(delay)
# Update display with greener colors by a specified amount with a set delay between each pixel
def greenstrip(self, lushness, delay):
global pixels
if (delay < 0):
delay = 0
for n in range(len(pixels)):
#.........这里部分代码省略.........
示例8: __init__
# 需要导入模块: from Adafruit_BBIO.SPI import SPI [as 别名]
# 或者: from Adafruit_BBIO.SPI.SPI import writebytes [as 别名]
class ledstrip:
def __init__(self, length, missing):
# the important piece of this for reuse is setting up the interface
# the rest sets up the strip of leds for what we intend to do with them
self.interface = SPI(0,1)
self.full_length = length
self.missing_leds = missing
self.outbuff = [[128, 128, 128]] * length
self.reset_buffer([128, 128, 128])
def reset_buffer(self, color):
for i in range(0, len(self.outbuff), 1):
self.outbuff[i] = color
def write(self):
# this guy here, plus a small amount of init code elsewhere,
# is all we really need to make the led strips work. The rest is just
# for ease of use.
self.interface.writebytes([0] * (int(self.full_length / 32) + 1) + [0])
for i in range(0, len(self.outbuff), 1):
if not i in self.missing_leds:
self.interface.writebytes(self.outbuff[i])
def twocolorfade(self, bcolor, tcolor, length):
outcolor = []
totalshift = [0, 0, 0]
for i in range(0, 3, 1):
totalshift[i] = tcolor[i] - bcolor[i]
for i in range(0, length, 1):
outcolor.append([])
for j in range(0, 3, 1):
outcolor[i].append(bcolor[j] + (int(totalshift[j] / float(length) * i)))
return outcolor
def movingtwocolor(self, bcols, tcols, strip_length, gradient_width, delay):
bfade = self.twocolorfade(bcols[0], bcols[1], gradient_width)
for frame in bfade:
seg1 = self.twocolorfade(frame, tcols[0], strip_length)
seg2 = [] + seg1
seg2.reverse()
self.outbuff = seg1 + seg2 + seg1
self.write()
time.sleep(delay)
bfade = self.twocolorfade(tcols[0], tcols[1], gradient_width)
for frame in bfade:
seg1 = self.twocolorfade(bcols[1], frame, strip_length)
seg2 = [] + seg1
seg2.reverse()
self.outbuff = seg1 + seg2 + seg1
self.write()
time.sleep(delay)
bfade = self.twocolorfade(tcols[1], tcols[0], gradient_width)
for frame in bfade:
seg1 = self.twocolorfade(bcols[1], frame, strip_length)
seg2 = [] + seg1
seg2.reverse()
self.outbuff = seg1 + seg2 + seg1
self.write()
time.sleep(delay)
bfade = self.twocolorfade(bcols[1], bcols[0], gradient_width)
for frame in bfade:
seg1 = self.twocolorfade(frame, tcols[0], strip_length)
seg2 = [] + seg1
seg2.reverse()
self.outbuff = seg1 + seg2 + seg1
self.write()
time.sleep(delay)
def run_sequence(self, fcol, bcol, length, delay, sequence, step, loops, timechange):
# with run_sequence, we can do pretty much anything,
# provided we can set up the sequence properly
# long list of parameters gives us what we need to make that happen
# fcol : foreground color (the color value for the leds in sequence)
# bcol : background color (the color value for the leds not in sequence)
# length : number of leds still active trailing the "current" one in sequence
# delay : time between steps in sequence
# sequence : a list (processed in the order provided) of leds to activate
# step : number of leds in sequence to skip per step (useful for moving a whole group at once)
# loops : number of times to iterate through the sequence completely
# timechange : somewhat handy but not wholly necessary per loop multiplier for delay variable
# can be used to increase or decrease timestep over several loops without re-running sequence
pos = 0
loops = loops - 1
firstrun = True
self.reset_buffer(bcol)
while pos < len(sequence):
self.reset_buffer(bcol)
for tail in range(0, length, 1):
if pos - tail >= 0 or not firstrun:
self.outbuff[sequence[pos - tail]] = fcol
if pos < len(sequence):
if pos == len(sequence) - 1 and loops:
pos = 0
firstrun = False
loops = loops - 1
delay = delay * timechange
elif pos == len(sequence) - 1 and length:
length = length - 1
#.........这里部分代码省略.........
示例9: print
# 需要导入模块: from Adafruit_BBIO.SPI import SPI [as 别名]
# 或者: from Adafruit_BBIO.SPI.SPI import writebytes [as 别名]
### H27 Mar 16
import os, sys
import time
import Adafruit_BBIO.GPIO as GPIO
from Adafruit_BBIO.SPI import SPI
GPIO.setup("P9_12",GPIO.OUT)
GPIO.output("P9_12",GPIO.HIGH)
print("GPIO >> HIGH")
spi_in = SPI(0, 0)
spi_out = SPI(0, 1)
spi_out.msh = 500000
while True:
GPIO.output("P9_12",GPIO.LOW)
print("GPIO >> LOW")
spi_out.writebytes([0b00001111])
print("write >> num")
print spi_in.readbytes(1)
GPIO.output("P9_12",GPIO.HIGH)
print("GPIO >> HIGH")
time.sleep(1)
GPIO.cleanup()
示例10: __init__
# 需要导入模块: from Adafruit_BBIO.SPI import SPI [as 别名]
# 或者: from Adafruit_BBIO.SPI.SPI import writebytes [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