⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 aardvark_py.py

📁 Aardvark Example Source Code Version: 4.00 Date: 2007-04-20 Source code which shows how to use the
💻 PY
📖 第 1 页 / 共 3 页
字号:
    "usage: (int, u08, u08[], u16) = aa_i2c_slave_read_ext(Aardvark, u16)"
    if not AA_LIBRARY_LOADED: return AA_INCOMPATIBLE_LIBRARY
    data_in = array('B', [ 0 for i in range(num_bytes) ])
    (_ret_, addr, data_in, num_read) = api.py_aa_i2c_slave_read_ext(aardvark, num_bytes, data_in)
    del data_in[max(0, min(num_read, len(data_in))):]
    return (_ret_, addr, data_in, num_read)


# Enable the I2C bus monitor
# This disables all other functions on the Aardvark adapter
def aa_i2c_monitor_enable (aardvark):
    "usage: int = aa_i2c_monitor_enable(Aardvark)"
    if not AA_LIBRARY_LOADED: return AA_INCOMPATIBLE_LIBRARY
    return api.py_aa_i2c_monitor_enable(aardvark)


# Disable the I2C bus monitor
def aa_i2c_monitor_disable (aardvark):
    "usage: int = aa_i2c_monitor_disable(Aardvark)"
    if not AA_LIBRARY_LOADED: return AA_INCOMPATIBLE_LIBRARY
    return api.py_aa_i2c_monitor_disable(aardvark)


# Read the data collected by the bus monitor
AA_I2C_MONITOR_DATA = 0x00ff
AA_I2C_MONITOR_NACK = 0x0100
AA_I2C_MONITOR_CMD_START = 0xff00
AA_I2C_MONITOR_CMD_STOP = 0xff01
def aa_i2c_monitor_read (aardvark, num_bytes):
    "usage: (int, u16[]) = aa_i2c_monitor_read(Aardvark, u16)"
    if not AA_LIBRARY_LOADED: return AA_INCOMPATIBLE_LIBRARY
    data = array('H', [ 0 for i in range(num_bytes) ])
    (_ret_, data) = api.py_aa_i2c_monitor_read(aardvark, num_bytes, data)
    del data[max(0, min(_ret_, len(data))):]
    return (_ret_, data)


# Configure the I2C pullup resistors.
# This is only supported on hardware versions >= 2.00
AA_I2C_PULLUP_NONE = 0x00
AA_I2C_PULLUP_BOTH = 0x03
AA_I2C_PULLUP_QUERY = 0x80
def aa_i2c_pullup (aardvark, pullup_mask):
    "usage: int = aa_i2c_pullup(Aardvark, u08)"
    if not AA_LIBRARY_LOADED: return AA_INCOMPATIBLE_LIBRARY
    return api.py_aa_i2c_pullup(aardvark, pullup_mask)



#==========================================================================
# SPI API
#==========================================================================
# Set the SPI bit rate in kilohertz.  If a zero is passed as the
# bitrate, the bitrate is unchanged and the current bitrate is
# returned.
def aa_spi_bitrate (aardvark, bitrate_khz):
    "usage: int = aa_spi_bitrate(Aardvark, int)"
    if not AA_LIBRARY_LOADED: return AA_INCOMPATIBLE_LIBRARY
    return api.py_aa_spi_bitrate(aardvark, bitrate_khz)


# These configuration parameters specify how to clock the
# bits that are sent and received on the Aardvark SPI
# interface.
# 
#   The polarity option specifies which transition
#   constitutes the leading edge and which transition is the
#   falling edge.  For example, AA_SPI_POL_RISING_FALLING
#   would configure the SPI to idle the SCK clock line low.
#   The clock would then transition low-to-high on the
#   leading edge and high-to-low on the trailing edge.
# 
#   The phase option determines whether to sample or setup on
#   the leading edge.  For example, AA_SPI_PHASE_SAMPLE_SETUP
#   would configure the SPI to sample on the leading edge and
#   setup on the trailing edge.
# 
#   The bitorder option is used to indicate whether LSB or
#   MSB is shifted first.
# 
# See the diagrams in the Aardvark datasheet for
# more details.
# enum AardvarkSpiPolarity
AA_SPI_POL_RISING_FALLING = 0
AA_SPI_POL_FALLING_RISING = 1

# enum AardvarkSpiPhase
AA_SPI_PHASE_SAMPLE_SETUP = 0
AA_SPI_PHASE_SETUP_SAMPLE = 1

# enum AardvarkSpiBitorder
AA_SPI_BITORDER_MSB = 0
AA_SPI_BITORDER_LSB = 1

# Configure the SPI master or slave interface
def aa_spi_configure (aardvark, polarity, phase, bitorder):
    "usage: int = aa_spi_configure(Aardvark, AardvarkSpiPolarity, AardvarkSpiPhase, AardvarkSpiBitorder)"
    if not AA_LIBRARY_LOADED: return AA_INCOMPATIBLE_LIBRARY
    return api.py_aa_spi_configure(aardvark, polarity, phase, bitorder)


# Write a stream of bytes to the downstream SPI slave device.
def aa_spi_write (aardvark, data_out):
    "usage: (int, u08[]) = aa_spi_write(Aardvark, u08[])"
    if not AA_LIBRARY_LOADED: return AA_INCOMPATIBLE_LIBRARY
    if not isinstance(data_out, ArrayType) or data_out.typecode != 'B': raise TypeError("type for 'data_out' must be array('B')")
    num_bytes = len(data_out)
    data_in = array('B', [ 0 for i in range(num_bytes) ])
    (_ret_, data_in) = api.py_aa_spi_write(aardvark, num_bytes, data_out, data_in)
    del data_in[max(0, min(_ret_, len(data_in))):]
    return (_ret_, data_in)


# Enable/Disable the Aardvark as an SPI slave device
def aa_spi_slave_enable (aardvark):
    "usage: int = aa_spi_slave_enable(Aardvark)"
    if not AA_LIBRARY_LOADED: return AA_INCOMPATIBLE_LIBRARY
    return api.py_aa_spi_slave_enable(aardvark)


def aa_spi_slave_disable (aardvark):
    "usage: int = aa_spi_slave_disable(Aardvark)"
    if not AA_LIBRARY_LOADED: return AA_INCOMPATIBLE_LIBRARY
    return api.py_aa_spi_slave_disable(aardvark)


# Set the slave response in the event the Aardvark is put
# into slave mode and contacted by a Master.
def aa_spi_slave_set_response (aardvark, data_out):
    "usage: int = aa_spi_slave_set_response(Aardvark, u08[])"
    if not AA_LIBRARY_LOADED: return AA_INCOMPATIBLE_LIBRARY
    if not isinstance(data_out, ArrayType) or data_out.typecode != 'B': raise TypeError("type for 'data_out' must be array('B')")
    num_bytes = len(data_out)
    return api.py_aa_spi_slave_set_response(aardvark, num_bytes, data_out)


# Read the bytes from an SPI slave reception
def aa_spi_slave_read (aardvark, num_bytes):
    "usage: (int, u08[]) = aa_spi_slave_read(Aardvark, u16)"
    if not AA_LIBRARY_LOADED: return AA_INCOMPATIBLE_LIBRARY
    data_in = array('B', [ 0 for i in range(num_bytes) ])
    (_ret_, data_in) = api.py_aa_spi_slave_read(aardvark, num_bytes, data_in)
    del data_in[max(0, min(_ret_, len(data_in))):]
    return (_ret_, data_in)


# Change the output polarity on the SS line.
# 
# Note: When configured as an SPI slave, the Aardvark will
# always be setup with SS as active low.  Hence this function
# only affects the SPI master functions on the Aardvark.
# enum AardvarkSpiSSPolarity
AA_SPI_SS_ACTIVE_LOW  = 0
AA_SPI_SS_ACTIVE_HIGH = 1

def aa_spi_master_ss_polarity (aardvark, polarity):
    "usage: int = aa_spi_master_ss_polarity(Aardvark, AardvarkSpiSSPolarity)"
    if not AA_LIBRARY_LOADED: return AA_INCOMPATIBLE_LIBRARY
    return api.py_aa_spi_master_ss_polarity(aardvark, polarity)



#==========================================================================
# GPIO API
#==========================================================================
# The following enumerated type maps the named lines on the
# Aardvark I2C/SPI line to bit positions in the GPIO API.
# All GPIO API functions will index these lines through an
# 8-bit masked value.  Thus, each bit position in the mask
# can be referred back its corresponding line through the
# enumerated type.
# enum AardvarkGpioBits
AA_GPIO_SCL  = 0x01
AA_GPIO_SDA  = 0x02
AA_GPIO_MISO = 0x04
AA_GPIO_SCK  = 0x08
AA_GPIO_MOSI = 0x10
AA_GPIO_SS   = 0x20

# Configure the GPIO, specifying the direction of each bit.
# 
# A call to this function will not change the value of the pullup
# mask in the Aardvark.  This is illustrated by the following
# example:
#   (1) Direction mask is first set to 0x00
#   (2) Pullup is set to 0x01
#   (3) Direction mask is set to 0x01
#   (4) Direction mask is later set back to 0x00.
# 
# The pullup will be active after (4).
# 
# On Aardvark power-up, the default value of the direction
# mask is 0x00.
AA_GPIO_DIR_INPUT = 0
AA_GPIO_DIR_OUTPUT = 1
def aa_gpio_direction (aardvark, direction_mask):
    "usage: int = aa_gpio_direction(Aardvark, u08)"
    if not AA_LIBRARY_LOADED: return AA_INCOMPATIBLE_LIBRARY
    return api.py_aa_gpio_direction(aardvark, direction_mask)


# Enable an internal pullup on any of the GPIO input lines.
# 
# Note: If a line is configured as an output, the pullup bit
# for that line will be ignored, though that pullup bit will
# be cached in case the line is later configured as an input.
# 
# By default the pullup mask is 0x00.
AA_GPIO_PULLUP_OFF = 0
AA_GPIO_PULLUP_ON = 1
def aa_gpio_pullup (aardvark, pullup_mask):
    "usage: int = aa_gpio_pullup(Aardvark, u08)"
    if not AA_LIBRARY_LOADED: return AA_INCOMPATIBLE_LIBRARY
    return api.py_aa_gpio_pullup(aardvark, pullup_mask)


# Read the current digital values on the GPIO input lines.
# 
# The bits will be ordered as described by AA_GPIO_BITS.  If a
# line is configured as an output, its corresponding bit
# position in the mask will be undefined.
def aa_gpio_get (aardvark):
    "usage: int = aa_gpio_get(Aardvark)"
    if not AA_LIBRARY_LOADED: return AA_INCOMPATIBLE_LIBRARY
    return api.py_aa_gpio_get(aardvark)


# Set the outputs on the GPIO lines.
# 
# Note: If a line is configured as an input, it will not be
# affected by this call, but the output value for that line
# will be cached in the event that the line is later
# configured as an output.
def aa_gpio_set (aardvark, value):
    "usage: int = aa_gpio_set(Aardvark, u08)"
    if not AA_LIBRARY_LOADED: return AA_INCOMPATIBLE_LIBRARY
    return api.py_aa_gpio_set(aardvark, value)


# Block until there is a change on the GPIO input lines.
# Pins configured as outputs will be ignored.
# 
# The function will return either when a change has occurred or
# the timeout expires.  The timeout, specified in millisecods, has
# a precision of ~16 ms. The maximum allowable timeout is
# approximately 4 seconds. If the timeout expires, this function
# will return the current state of the GPIO lines.
#
# This function will return immediately with the current value
# of the GPIO lines for the first invocation after any of the
# following functions are called: aa_configure,
# aa_gpio_direction, or aa_gpio_pullup.
#
# If the function aa_gpio_get is called before calling
# aa_gpio_change, aa_gpio_change will only register any changes
# from the value last returned by aa_gpio_get.
def aa_gpio_change (aardvark, timeout):
    "usage: int = aa_gpio_change(Aardvark, u16)"
    if not AA_LIBRARY_LOADED: return AA_INCOMPATIBLE_LIBRARY
    return api.py_aa_gpio_change(aardvark, timeout)


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -