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

📄 aardvark.bas

📁 Aardvark Example Source Code Version: 4.00 Date: 2007-04-20 Source code which shows how to use the
💻 BAS
📖 第 1 页 / 共 3 页
字号:
Attribute VB_Name = "AARDVARK"
'==========================================================================
' Aardvark Interface Library
'--------------------------------------------------------------------------
' Copyright (c) 2002-2007 Total Phase, Inc.
' All rights reserved.
' www.totalphase.com
'
' Redistribution and use in source and binary forms, with or without
' modification, are permitted provided that the following conditions
' are met:
'
' - Redistributions of source code must retain the above copyright
'   notice, this list of conditions and the following disclaimer.
'
' - Redistributions in binary form must reproduce the above copyright
'   notice, this list of conditions and the following disclaimer in the
'   documentation and/or other materials provided with the distribution.
'
' - Neither the name of Total Phase, Inc. nor the names of its
'   contributors may be used to endorse or promote products derived from
'   this software without specific prior written permission.
'
' THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
' "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
' LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
' FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
' COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
' INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
' BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
' LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
' CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
' LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
' ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
' POSSIBILITY OF SUCH DAMAGE.
'--------------------------------------------------------------------------
' To access Aardvark devices through the API:
'
' 1) Use one of the following shared objects:
'      aardvark.so      --  Linux shared object
'      aardvark.dll     --  Windows dynamic link library
'
' 2) Along with one of the following language modules:
'      aardvark.c/h     --  C/C++ API header file and interface module
'      aardvark_py.py   --  Python API
'      aardvark.bas     --  Visual Basic 6 API
'      aardvark.cs      --  C# .NET source
'      aardvark_net.dll --  Compiled .NET binding
'==========================================================================

' Data types:
'    Handles are Long
'
'    Fixed bit-width types
'      8-bit => Byte
'     16-bit => Integer
'     32-bit => Long
'     64-bit => LongLong

Public Type LongLong
    LoValue As Long
    HiValue As Long
End Type

Public Enum AardvarkStatus
    ' General codes (0 to -99)
    AA_OK                        =    0
    AA_UNABLE_TO_LOAD_LIBRARY    =   -1
    AA_UNABLE_TO_LOAD_DRIVER     =   -2
    AA_UNABLE_TO_LOAD_FUNCTION   =   -3
    AA_INCOMPATIBLE_LIBRARY      =   -4
    AA_INCOMPATIBLE_DEVICE       =   -5
    AA_COMMUNICATION_ERROR       =   -6
    AA_UNABLE_TO_OPEN            =   -7
    AA_UNABLE_TO_CLOSE           =   -8
    AA_INVALID_HANDLE            =   -9
    AA_CONFIG_ERROR              =  -10

    ' I2C codes (-100 to -199)
    AA_I2C_NOT_AVAILABLE         = -100
    AA_I2C_NOT_ENABLED           = -101
    AA_I2C_READ_ERROR            = -102
    AA_I2C_WRITE_ERROR           = -103
    AA_I2C_SLAVE_BAD_CONFIG      = -104
    AA_I2C_SLAVE_READ_ERROR      = -105
    AA_I2C_SLAVE_TIMEOUT         = -106
    AA_I2C_DROPPED_EXCESS_BYTES  = -107
    AA_I2C_BUS_ALREADY_FREE      = -108

    ' SPI codes (-200 to -299)
    AA_SPI_NOT_AVAILABLE         = -200
    AA_SPI_NOT_ENABLED           = -201
    AA_SPI_WRITE_ERROR           = -202
    AA_SPI_SLAVE_READ_ERROR      = -203
    AA_SPI_SLAVE_TIMEOUT         = -204
    AA_SPI_DROPPED_EXCESS_BYTES  = -205

    ' GPIO codes (-400 to -499)
    AA_GPIO_NOT_AVAILABLE        = -400

    ' I2C bus monitor codes (-500 to -599)
    AA_I2C_MONITOR_NOT_AVAILABLE = -500
    AA_I2C_MONITOR_NOT_ENABLED   = -501
End Enum

Public Type AardvarkVersion
    ' Software, firmware, and hardware versions.
    software      as Integer
    firmware      as Integer
    hardware      as Integer

    ' Firmware requires that software must be >= this version.
    sw_req_by_fw  as Integer

    ' Software requires that firmware must be >= this version.
    fw_req_by_sw  as Integer

    ' Software requires that the API interface must be >= this version.
    api_req_by_sw as Integer
End Type

Public Const AA_PORT_NOT_FREE = &H8000

Public Type AardvarkExt
    ' Version matrix
    version  as AardvarkVersion

    ' Features of this device.
    features as Long
End Type

Public Const AA_FEATURE_SPI = &H00000001
Public Const AA_FEATURE_I2C = &H00000002
Public Const AA_FEATURE_GPIO = &H00000008
Public Const AA_FEATURE_I2C_MONITOR = &H00000010

Public Enum AardvarkConfig
    AA_CONFIG_GPIO_ONLY = &H00
    AA_CONFIG_SPI_GPIO  = &H01
    AA_CONFIG_GPIO_I2C  = &H02
    AA_CONFIG_SPI_I2C   = &H03
    AA_CONFIG_QUERY     = &H80
End Enum

Public Const AA_CONFIG_SPI_MASK = &H00000001
Public Const AA_CONFIG_I2C_MASK = &H00000002

Public Const AA_TARGET_POWER_NONE = &H00
Public Const AA_TARGET_POWER_BOTH = &H03
Public Const AA_TARGET_POWER_QUERY = &H80

Public Const AA_ASYNC_NO_DATA = &H00000000
Public Const AA_ASYNC_I2C_READ = &H00000001
Public Const AA_ASYNC_I2C_WRITE = &H00000002
Public Const AA_ASYNC_SPI = &H00000004
Public Const AA_ASYNC_I2C_MONITOR = &H00000008

Public Enum AardvarkI2cFlags
    AA_I2C_NO_FLAGS     = &H00
    AA_I2C_10_BIT_ADDR  = &H01
    AA_I2C_COMBINED_FMT = &H02
    AA_I2C_NO_STOP      = &H04
End Enum

Public Enum AardvarkI2cStatus
    AA_I2C_STATUS_OK            = 0
    AA_I2C_STATUS_BUS_ERROR     = 1
    AA_I2C_STATUS_SLA_ACK       = 2
    AA_I2C_STATUS_SLA_NACK      = 3
    AA_I2C_STATUS_DATA_NACK     = 4
    AA_I2C_STATUS_ARB_LOST      = 5
    AA_I2C_STATUS_BUS_LOCKED    = 6
    AA_I2C_STATUS_LAST_DATA_ACK = 7
End Enum

Public Const AA_I2C_MONITOR_DATA = &H00ff
Public Const AA_I2C_MONITOR_NACK = &H0100
Public Const AA_I2C_MONITOR_CMD_START = &Hff00
Public Const AA_I2C_MONITOR_CMD_STOP = &Hff01

Public Const AA_I2C_PULLUP_NONE = &H00
Public Const AA_I2C_PULLUP_BOTH = &H03
Public Const AA_I2C_PULLUP_QUERY = &H80

Public Enum AardvarkSpiPolarity
    AA_SPI_POL_RISING_FALLING = 0
    AA_SPI_POL_FALLING_RISING = 1
End Enum

Public Enum AardvarkSpiPhase
    AA_SPI_PHASE_SAMPLE_SETUP = 0
    AA_SPI_PHASE_SETUP_SAMPLE = 1
End Enum

Public Enum AardvarkSpiBitorder
    AA_SPI_BITORDER_MSB = 0
    AA_SPI_BITORDER_LSB = 1
End Enum

Public Enum AardvarkSpiSSPolarity
    AA_SPI_SS_ACTIVE_LOW  = 0
    AA_SPI_SS_ACTIVE_HIGH = 1
End Enum

Public Enum AardvarkGpioBits
    AA_GPIO_SCL  = &H01
    AA_GPIO_SDA  = &H02
    AA_GPIO_MISO = &H04
    AA_GPIO_SCK  = &H08
    AA_GPIO_MOSI = &H10
    AA_GPIO_SS   = &H20
End Enum

Public Const AA_GPIO_DIR_INPUT = 0
Public Const AA_GPIO_DIR_OUTPUT = 1

Public Const AA_GPIO_PULLUP_OFF = 0
Public Const AA_GPIO_PULLUP_ON = 1

Private Declare Function std_aa_find_devices Lib "aardvark.dll" (ByVal nelem As Long, ByRef devices As Integer) as Long
Private Declare Function std_aa_find_devices_ext Lib "aardvark.dll" (ByVal nelem As Long, ByRef devices As Integer, ByRef unique_ids As Long) as Long
Private Declare Function std_aa_open Lib "aardvark.dll" (ByVal port_number As Long) as Long
Private Declare Function std_aa_open_ext Lib "aardvark.dll" (ByVal port_number As Long, ByRef aa_ext As AardvarkExt) as Long
Private Declare Function std_aa_close Lib "aardvark.dll" (ByVal aardvark As Long) as Long
Private Declare Function std_aa_port Lib "aardvark.dll" (ByVal aardvark As Long) as Long
Private Declare Function std_aa_features Lib "aardvark.dll" (ByVal aardvark As Long) as Long
Private Declare Function std_aa_unique_id Lib "aardvark.dll" (ByVal aardvark As Long) as Long
Private Declare Function vb_aa_status_string Lib "aardvark.dll" (ByVal status As Long) as String
Private Declare Function std_aa_log Lib "aardvark.dll" (ByVal aardvark As Long, ByVal level As Long, ByVal handle As Long) as Long
Private Declare Function std_aa_version Lib "aardvark.dll" (ByVal aardvark As Long, ByRef version As AardvarkVersion) as Long
Private Declare Function std_aa_configure Lib "aardvark.dll" (ByVal aardvark As Long, ByVal config As Long) as Long
Private Declare Function std_aa_target_power Lib "aardvark.dll" (ByVal aardvark As Long, ByVal power_mask As Byte) as Long
Private Declare Function std_aa_sleep_ms Lib "aardvark.dll" (ByVal milliseconds As Long) as Long
Private Declare Function std_aa_async_poll Lib "aardvark.dll" (ByVal aardvark As Long, ByVal timeout As Long) as Long
Private Declare Function std_aa_i2c_free_bus Lib "aardvark.dll" (ByVal aardvark As Long) as Long
Private Declare Function std_aa_i2c_bitrate Lib "aardvark.dll" (ByVal aardvark As Long, ByVal bitrate_khz As Long) as Long
Private Declare Function std_aa_i2c_read Lib "aardvark.dll" (ByVal aardvark As Long, ByVal slave_addr As Integer, ByVal flags As Long, ByVal num_bytes As Integer, ByRef data_in As Byte) as Long
Private Declare Function std_aa_i2c_read_ext Lib "aardvark.dll" (ByVal aardvark As Long, ByVal slave_addr As Integer, ByVal flags As Long, ByVal num_bytes As Integer, ByRef data_in As Byte, ByRef num_read As Integer) as Long
Private Declare Function std_aa_i2c_write Lib "aardvark.dll" (ByVal aardvark As Long, ByVal slave_addr As Integer, ByVal flags As Long, ByVal num_bytes As Integer, ByRef data_out As Byte) as Long
Private Declare Function std_aa_i2c_write_ext Lib "aardvark.dll" (ByVal aardvark As Long, ByVal slave_addr As Integer, ByVal flags As Long, ByVal num_bytes As Integer, ByRef data_out As Byte, ByRef num_written As Integer) as Long
Private Declare Function std_aa_i2c_slave_enable Lib "aardvark.dll" (ByVal aardvark As Long, ByVal addr As Byte, ByVal maxTxBytes As Integer, ByVal maxRxBytes As Integer) as Long
Private Declare Function std_aa_i2c_slave_disable Lib "aardvark.dll" (ByVal aardvark As Long) as Long
Private Declare Function std_aa_i2c_slave_set_response Lib "aardvark.dll" (ByVal aardvark As Long, ByVal num_bytes As Byte, ByRef data_out As Byte) as Long
Private Declare Function std_aa_i2c_slave_write_stats Lib "aardvark.dll" (ByVal aardvark As Long) as Long
Private Declare Function std_aa_i2c_slave_read Lib "aardvark.dll" (ByVal aardvark As Long, ByRef addr As Byte, ByVal num_bytes As Integer, ByRef data_in As Byte) as Long
Private Declare Function std_aa_i2c_slave_write_stats_ext Lib "aardvark.dll" (ByVal aardvark As Long, ByRef num_written As Integer) as Long
Private Declare Function std_aa_i2c_slave_read_ext Lib "aardvark.dll" (ByVal aardvark As Long, ByRef addr As Byte, ByVal num_bytes As Integer, ByRef data_in As Byte, ByRef num_read As Integer) as Long
Private Declare Function std_aa_i2c_monitor_enable Lib "aardvark.dll" (ByVal aardvark As Long) as Long
Private Declare Function std_aa_i2c_monitor_disable Lib "aardvark.dll" (ByVal aardvark As Long) as Long
Private Declare Function std_aa_i2c_monitor_read Lib "aardvark.dll" (ByVal aardvark As Long, ByVal num_bytes As Integer, ByRef data As Integer) as Long
Private Declare Function std_aa_i2c_pullup Lib "aardvark.dll" (ByVal aardvark As Long, ByVal pullup_mask As Byte) as Long
Private Declare Function std_aa_spi_bitrate Lib "aardvark.dll" (ByVal aardvark As Long, ByVal bitrate_khz As Long) as Long
Private Declare Function std_aa_spi_configure Lib "aardvark.dll" (ByVal aardvark As Long, ByVal polarity As Long, ByVal phase As Long, ByVal bitorder As Long) as Long
Private Declare Function std_aa_spi_write Lib "aardvark.dll" (ByVal aardvark As Long, ByVal num_bytes As Integer, ByRef data_out As Byte, ByRef data_in As Byte) as Long
Private Declare Function std_aa_spi_slave_enable Lib "aardvark.dll" (ByVal aardvark As Long) as Long
Private Declare Function std_aa_spi_slave_disable Lib "aardvark.dll" (ByVal aardvark As Long) as Long
Private Declare Function std_aa_spi_slave_set_response Lib "aardvark.dll" (ByVal aardvark As Long, ByVal num_bytes As Byte, ByRef data_out As Byte) as Long
Private Declare Function std_aa_spi_slave_read Lib "aardvark.dll" (ByVal aardvark As Long, ByVal num_bytes As Integer, ByRef data_in As Byte) as Long
Private Declare Function std_aa_spi_master_ss_polarity Lib "aardvark.dll" (ByVal aardvark As Long, ByVal polarity As Long) as Long
Private Declare Function std_aa_gpio_direction Lib "aardvark.dll" (ByVal aardvark As Long, ByVal direction_mask As Byte) as Long
Private Declare Function std_aa_gpio_pullup Lib "aardvark.dll" (ByVal aardvark As Long, ByVal pullup_mask As Byte) as Long
Private Declare Function std_aa_gpio_get Lib "aardvark.dll" (ByVal aardvark As Long) as Long
Private Declare Function std_aa_gpio_set Lib "aardvark.dll" (ByVal aardvark As Long, ByVal value As Byte) as Long
Private Declare Function std_aa_gpio_change Lib "aardvark.dll" (ByVal aardvark As Long, ByVal timeout As Integer) as Long


'==========================================================================
' VERSION
'==========================================================================
Public Const AA_API_VERSION    = &H0400   ' v4.00
Public Const AA_REQ_SW_VERSION = &H0400   ' v4.00

Private Declare Function std_version Lib "aardvark.dll" () as Long
Dim AA_VERSION_OK as Boolean

Private Function check_version () as Boolean
    If AA_VERSION_OK Then
        check_version = True
    Else
        Dim sw_version As Long
        Dim req_api_version As Long
        sw_version = std_version() And &Hffff
        req_api_version = (std_version() / 65536) And &Hffff
        AA_VERSION_OK = ((sw_version >= AA_REQ_SW_VERSION) And (AA_API_VERSION >= req_api_version))
        check_version = AA_VERSION_OK
    End If
End Function


'==========================================================================
' STATUS CODES
'==========================================================================
' All API functions return an integer which is the result of the
' transaction, or a status code if negative.  The status codes are
' defined as follows:

'==========================================================================
' GENERAL TYPE DEFINITIONS
'==========================================================================
' Aardvark handle type definition
' typedef Aardvark => Long

' Deprecated type definitions.
'
' These are only for use with legacy code and
' should not be used for new development.
' typedef aa_u08 => Byte

' typedef aa_u16 => Integer

' typedef aa_u32 => Long

' typedef aa_s08 => s08

' typedef aa_s16 => s16

' typedef aa_s32 => s32

' Aardvark version matrix.
' 
' This matrix describes the various version dependencies
' of Aardvark components.  It can be used to determine
' which component caused an incompatibility error.
' 
' All version numbers are of the format:
'   (major << 8) | minor
' 
' ex. v1.20 would be encoded as:  0x0114

'==========================================================================
' GENERAL API
'==========================================================================
' Get a list of ports to which Aardvark devices are attached.
' 
' nelem   = maximum number of elements to return
' devices = array into which the port numbers are returned
' 
' Each element of the array is written with the port number.
' Devices that are in-use are ORed with AA_PORT_NOT_FREE (0x8000).

⌨️ 快捷键说明

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