本文整理汇总了Python中busio.SPI属性的典型用法代码示例。如果您正苦于以下问题:Python busio.SPI属性的具体用法?Python busio.SPI怎么用?Python busio.SPI使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类busio
的用法示例。
在下文中一共展示了busio.SPI属性的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _cmd_nodata
# 需要导入模块: import busio [as 别名]
# 或者: from busio import SPI [as 别名]
def _cmd_nodata(self, card, cmd, response=0xFF):
"""
Issue a command to the card with no argument.
:param busio.SPI card: The locked SPI bus.
:param int cmd: The command number.
"""
buf = self._cmdbuf
buf[0] = cmd
buf[1] = 0xFF
card.write(buf, end=2)
for _ in range(_CMD_TIMEOUT):
card.readinto(buf, end=1, write_value=0xFF)
if buf[0] == response:
return 0 # OK
return 1 # timeout
示例2: __init__
# 需要导入模块: import busio [as 别名]
# 或者: from busio import SPI [as 别名]
def __init__(self):
Sensor.__init__(self)
self.moisture_min = float(self.config["soil"]["min"])
self.moisture_max = float(self.config["soil"]["max"])
# create SPI bus
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)
# create the cs (chip select)
cs = digitalio.DigitalInOut(board.D8)
# create the mcp object
mcp = MCP.MCP3008(spi, cs)
# create an analog input channel
self.pin = []
self.pin.append(AnalogIn(mcp, MCP.P0))
self.pin.append(AnalogIn(mcp, MCP.P1))
self.pin.append(AnalogIn(mcp, MCP.P2))
self.pin.append(AnalogIn(mcp, MCP.P3))
self.pin.append(AnalogIn(mcp, MCP.P4))
self.pin.append(AnalogIn(mcp, MCP.P5))
self.pin.append(AnalogIn(mcp, MCP.P6))
self.pin.append(AnalogIn(mcp, MCP.P7))
示例3: _wait_for_ready
# 需要导入模块: import busio [as 别名]
# 或者: from busio import SPI [as 别名]
def _wait_for_ready(self, card, timeout=0.3):
"""
Wait for the card to clock out 0xff to indicate its ready.
:param busio.SPI card: The locked SPI bus.
:param float timeout: Maximum time to wait in seconds.
"""
start_time = time.monotonic()
self._single_byte[0] = 0x00
while time.monotonic() - start_time < timeout and self._single_byte[0] != 0xFF:
card.readinto(self._single_byte, write_value=0xFF)
# pylint: disable-msg=too-many-arguments
# pylint: disable=no-member
# no-member disable should be reconsidered when it can be tested
示例4: clock_end_frame
# 需要导入模块: import busio [as 别名]
# 或者: from busio import SPI [as 别名]
def clock_end_frame(self):
"""Sends an end frame to the LED strip.
As explained above, dummy data must be sent after the last real colour
information so that all of the data can reach its destination down the line.
The delay is not as bad as with the human example above.
It is only 1/2 bit per LED. This is because the SPI clock line
needs to be inverted.
Say a bit is ready on the SPI data line. The sender communicates
this by toggling the clock line. The bit is read by the LED
and immediately forwarded to the output data line. When the clock goes
down again on the input side, the LED will toggle the clock up
on the output to tell the next LED that the bit is ready.
After one LED the clock is inverted, and after two LEDs it is in sync
again, but one cycle behind. Therefore, for every two LEDs, one bit
of delay gets accumulated. For 300 LEDs, 150 additional bits must be fed to
the input of LED one so that the data can reach the last LED.
Ultimately, we need to send additional numLEDs/2 arbitrary data bits,
in order to trigger numLEDs/2 additional clock changes. This driver
sends zeroes, which has the benefit of getting LED one partially or
fully ready for the next update to the strip. An optimized version
of the driver could omit the "clockStartFrame" method if enough zeroes have
been sent as part of "clockEndFrame".
"""
# Send reset frame necessary for SK9822 type LEDs
self.send_to_spi(bytes([0] * 4))
for _ in range((self.num_led + 15) // 16):
self.send_to_spi([0x00])
示例5: show
# 需要导入模块: import busio [as 别名]
# 或者: from busio import SPI [as 别名]
def show(self):
"""Sends the content of the pixel buffer to the strip.
Todo: More than 1024 LEDs requires more than one xfer operation.
"""
self.clock_start_frame()
# xfer2 kills the list, unfortunately. So it must be copied first
# SPI takes up to 4096 Integers. So we are fine for up to 1024 LEDs.
self.send_to_spi(self.leds)
self.clock_end_frame()
示例6: cleanup
# 需要导入模块: import busio [as 别名]
# 或者: from busio import SPI [as 别名]
def cleanup(self):
"""Release the SPI device; Call this method at the end"""
# Try to unlock, in case it is still locked
try:
self.spi.unlock() # Unlock first
except ValueError:
# Do nothing, the bus was not locked
pass
self.clear_strip()
self.spi.deinit() # Close SPI port
示例7: send_to_spi
# 需要导入模块: import busio [as 别名]
# 或者: from busio import SPI [as 别名]
def send_to_spi(self, data):
"""Internal method to output data to the chosen SPI device"""
if self.use_ce:
with self.spibus as bus_device:
bus_device.write(data)
elif self.use_bitbang:
while not self.spi.try_lock():
pass
self.spi.write(data)
self.spi.unlock()
else:
self.spi.write(data)
示例8: __init__
# 需要导入模块: import busio [as 别名]
# 或者: from busio import SPI [as 别名]
def __init__(self, spi, latch, columns, lines, backlight_inverted=False):
# pylint: disable=too-many-arguments
"""Initialize character LCD connected to backpack using SPI connection
on the specified SPI bus and latch line with the specified number of
columns and lines on the display. Optionally specify if backlight is
inverted.
"""
# pylint: enable=too-many-arguments
self._shift_register = adafruit_74hc595.ShiftRegister74HC595(spi, latch)
reset = self._shift_register.get_pin(1)
enable = self._shift_register.get_pin(2)
db4 = self._shift_register.get_pin(6)
db5 = self._shift_register.get_pin(5)
db6 = self._shift_register.get_pin(4)
db7 = self._shift_register.get_pin(3)
backlight_pin = self._shift_register.get_pin(7)
super().__init__(
reset,
enable,
db4,
db5,
db6,
db7,
columns,
lines,
backlight_pin=backlight_pin,
backlight_inverted=backlight_inverted,
)
示例9: SPI
# 需要导入模块: import busio [as 别名]
# 或者: from busio import SPI [as 别名]
def SPI():
"""The singleton SPI interface"""
import busio
return busio.SPI(SCLK, MOSI, MISO)
示例10: __init__
# 需要导入模块: import busio [as 别名]
# 或者: from busio import SPI [as 别名]
def __init__(self, config: dict, main_thread_running, system_ready):
self.config = config
self.main_thread_running = main_thread_running
self.system_ready = system_ready
self.node_ready = False
spi = busio.SPI(clock=board.SCK, MISO=board.MISO, MOSI=board.MOSI)
cs = digitalio.DigitalInOut(ADCMCP3008Worker.PINS[config['pin']])
self.mcp = MCP.MCP3008(spi, cs)
self.sensors = []
self.init_sensors()
self.node_ready = True
示例11: _init_card
# 需要导入模块: import busio [as 别名]
# 或者: from busio import SPI [as 别名]
def _init_card(self, baudrate):
"""Initialize the card in SPI mode."""
# clock card at least cycles with cs high
self._clock_card(80)
with self._spi as card:
# CMD0: init card; should return _R1_IDLE_STATE (allow 5 attempts)
for _ in range(5):
if self._cmd(card, 0, 0, 0x95) == _R1_IDLE_STATE:
break
else:
raise OSError("no SD card")
# CMD8: determine card version
rb7 = bytearray(4)
r = self._cmd(card, 8, 0x01AA, 0x87, rb7, data_block=False)
if r == _R1_IDLE_STATE:
self._init_card_v2(card)
elif r == (_R1_IDLE_STATE | _R1_ILLEGAL_COMMAND):
self._init_card_v1(card)
else:
raise OSError("couldn't determine SD card version")
# get the number of sectors
# CMD9: response R2 (R1 byte + 16-byte block read)
csd = bytearray(16)
if self._cmd(card, 9, 0, 0xAF, response_buf=csd) != 0:
raise OSError("no response from SD card")
# self.readinto(csd)
csd_version = (csd[0] & 0xC0) >> 6
if csd_version >= 2:
raise OSError("SD card CSD format not supported")
if csd_version == 1:
self._sectors = ((csd[8] << 8 | csd[9]) + 1) * 1024
else:
block_length = 2 ** (csd[5] & 0xF)
c_size = ((csd[6] & 0x3) << 10) | (csd[7] << 2) | ((csd[8] & 0xC) >> 6)
mult = 2 ** (((csd[9] & 0x3) << 1 | (csd[10] & 0x80) >> 7) + 2)
self._sectors = block_length // 512 * mult * (c_size + 1)
# CMD16: set block length to 512 bytes
if self._cmd(card, 16, 512, 0x15) != 0:
raise OSError("can't set 512 block size")
# set to high data rate now that it's initialised
self._spi = spi_device.SPIDevice(
self._spi.spi, self._spi.chip_select, baudrate=baudrate, extra_clocks=8
)
示例12: _cmd
# 需要导入模块: import busio [as 别名]
# 或者: from busio import SPI [as 别名]
def _cmd(
self, card, cmd, arg=0, crc=0, response_buf=None, data_block=True, wait=True
):
"""
Issue a command to the card and read an optional data response.
:param busio.SPI card: The locked SPI bus.
:param int cmd: The command number.
:param int|buf(4) arg: The command argument
:param int crc: The crc to allow the card to verify the command and argument.
:param bytearray response_buf: Buffer to read a data block response into.
:param bool data_block: True if the response data is in a data block.
"""
# create and send the command
buf = self._cmdbuf
buf[0] = 0x40 | cmd
if isinstance(arg, int):
buf[1] = (arg >> 24) & 0xFF
buf[2] = (arg >> 16) & 0xFF
buf[3] = (arg >> 8) & 0xFF
buf[4] = arg & 0xFF
elif len(arg) == 4:
# arg can be a 4-byte buf
buf[1:5] = arg
else:
raise ValueError()
if crc == 0:
buf[5] = calculate_crc(buf[:-1])
else:
buf[5] = crc
if wait:
self._wait_for_ready(card)
card.write(buf)
# wait for the response (response[7] == 0)
for _ in range(_CMD_TIMEOUT):
card.readinto(buf, end=1, write_value=0xFF)
if not (buf[0] & 0x80):
if response_buf:
if data_block:
# Wait for the start block byte
buf[1] = 0xFF
while buf[1] != 0xFE:
card.readinto(buf, start=1, end=2, write_value=0xFF)
card.readinto(response_buf, write_value=0xFF)
if data_block:
# Read the checksum
card.readinto(buf, start=1, end=3, write_value=0xFF)
return buf[0]
return -1
# pylint: enable-msg=too-many-arguments
# pylint: disable-msg=too-many-arguments
示例13: _block_cmd
# 需要导入模块: import busio [as 别名]
# 或者: from busio import SPI [as 别名]
def _block_cmd(self, card, cmd, block, crc, response_buf=None):
"""
Issue a command to the card with a block argument.
:param busio.SPI card: The locked SPI bus.
:param int cmd: The command number.
:param int block: The relevant block.
:param int crc: The crc to allow the card to verify the command and argument.
"""
if self._cdv == 1:
return self._cmd(card, cmd, block, crc, response_buf=response_buf)
# create and send the command
buf = self._cmdbuf
buf[0] = 0x40 | cmd
# We address by byte because cdv is 512. Instead of multiplying, shift
# the data to the correct spot so that we don't risk creating a long
# int.
buf[1] = (block >> 15) & 0xFF
buf[2] = (block >> 7) & 0xFF
buf[3] = (block << 1) & 0xFF
buf[4] = 0
if crc == 0:
buf[5] = calculate_crc(buf[:-1])
else:
buf[5] = crc
result = -1
self._wait_for_ready(card)
card.write(buf)
# wait for the response (response[7] == 0)
for _ in range(_CMD_TIMEOUT):
card.readinto(buf, end=1, write_value=0xFF)
if not (buf[0] & 0x80):
result = buf[0]
break
# pylint: disable=singleton-comparison
# Disable should be removed when refactor can be tested.
if response_buf != None and result == 0:
self._readinto(card, response_buf)
return result
# pylint: enable-msg=too-many-arguments
示例14: _write
# 需要导入模块: import busio [as 别名]
# 或者: from busio import SPI [as 别名]
def _write(self, card, token, buf, start=0, end=None):
"""
Write a data block to the card.
:param busio.SPI card: The locked SPI bus.
:param int token: The start token
:param bytearray buf: The buffer to write from
:param int start: The first index to read data from
:param int end: The index after the last byte to read from.
"""
cmd = self._cmdbuf
if end is None:
end = len(buf)
self._wait_for_ready(card)
# send: start of block, data, checksum
cmd[0] = token
card.write(cmd, end=1)
card.write(buf, start=start, end=end)
cmd[0] = 0xFF
cmd[1] = 0xFF
card.write(cmd, end=2)
# check the response
# pylint: disable=no-else-return
# Disable should be removed when refactor can be tested
for _ in range(_CMD_TIMEOUT):
card.readinto(cmd, end=1, write_value=0xFF)
if not (cmd[0] & 0x80):
if (cmd[0] & 0x1F) != 0x05:
return -1
else:
break
# wait for write to finish
card.readinto(cmd, end=1, write_value=0xFF)
while cmd[0] == 0:
card.readinto(cmd, end=1, write_value=0xFF)
return 0 # worked
# pylint: enable-msg=too-many-arguments
示例15: __init__
# 需要导入模块: import busio [as 别名]
# 或者: from busio import SPI [as 别名]
def __init__(
self,
clock,
data,
n,
*,
brightness=1.0,
auto_write=True,
pixel_order=BGR,
baudrate=4000000
):
self._spi = None
try:
self._spi = busio.SPI(clock, MOSI=data)
while not self._spi.try_lock():
pass
self._spi.configure(baudrate=baudrate)
except (NotImplementedError, ValueError):
self.dpin = digitalio.DigitalInOut(data)
self.cpin = digitalio.DigitalInOut(clock)
self.dpin.direction = digitalio.Direction.OUTPUT
self.cpin.direction = digitalio.Direction.OUTPUT
self.cpin.value = False
# Supply one extra clock cycle for each two pixels in the strip.
trailer_size = n // 16
if n % 16 != 0:
trailer_size += 1
# Four empty bytes for the header.
header = bytearray(START_HEADER_SIZE)
# 0xff bytes for the trailer.
trailer = bytearray(b"\xff") * trailer_size
super().__init__(
n,
byteorder=pixel_order,
brightness=brightness,
auto_write=auto_write,
header=header,
trailer=trailer,
)