tw.com.prolific.pl2303ghidsdk

## Class HidToSpi

- java.lang.Object
- - tw.com.prolific.pl2303ghidsdk.HidToGpio
  - - tw.com.prolific.pl2303ghidsdk.HidToSpi

-     public class HidToSpi
      extends HidToGpio

  PL23D3 HID to SPI Driver Class. Licensed under the Apache License, Version 2.0 http://www.apache.org/licenses/LICENSE-2.0

  * thanks to ....

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `HidToSpi()` New a HidToSpi driver object.
    `HidToSpi(android.content.Context context, java.lang.String strAppName)` New a HidToSpi driver object.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `int` | `askPermissionByDeviceIndex(int nDeviceIndex)` Obtain permission to communicate with a device by given device name
    `int` | `askPermissionByUsbDevice(android.hardware.usb.UsbDevice usbDevice)` Obtain permission to communicate with the given device.
    `int` | `closeDevice()` Close the PL23D3 device.
    `int` | `closeDeviceByDeviceIndex(int nDeviceIndex)`
    `void` | `dumpMsg(java.lang.Object objMsg)` Dump log into sdk.
    `int` | `enumDevices(int nDeviceVid)` Found a PL23B3, PL23C3, or PL23D3 device , the default VID is 067B
    `int` | `enumDevices(int nDeviceVid, int nDevicePid)` Found a PL23B3, PL23C3, or PL23D3 device by given VID and PID
    `java.lang.String` | `getDeviceName(int nDeviceIndex)` Gets device name by given index.
    `java.lang.String` | `getSdkVersion()` Gets SDK version.
    `java.lang.String` | `getSerialNumber(int nDeviceIndex)` Gets device serial number by given index.
    `android.hardware.usb.UsbDevice` | `getUsbDevice(int nDeviceIndex)` Gets UsbDevice object by given index
    `boolean` | `hasPermissionByDeviceIndex(int nDeviceIndex)` Detecting whether this device which decided by given index has permission
    `boolean` | `hasPermissionByUsbDevice(android.hardware.usb.UsbDevice usbDevice)` check if device has permission or not.
    `int` | `openDeviceByIndex(int nDeviceIndex)` Open device by given index.
    `int` | `openDeviceBySerialNumber(java.lang.String strSerialNumber)` Open device by given serial number.
    `void` | `setCommandTimeOut(int nMillSecond)` Sets device command time out value.
    `int` | `setSpiFrequency(int nDeviceIndex, byte byFreqDiv, SpiSetting.SPI_MODE nSpiMode)` Sets spi frequency.
    `int` | `spiRead(int nDeviceIndex, SpiSetting.Select nSelect, byte[] byBuffer, int nNumberOfBytesToRead, int nTimeOutms)` Read Binary Data from PL23D3 HidtoSpi chip.
    `int` | `spiReset(int nDeviceIndex)` Reset PL23D3 HidToSpi Chip configurations.
    `int` | `spiWrite(int nDeviceIndex, SpiSetting.Select nSelect, byte[] byBuffer, int nNumberOfBytesToWrite, int nTimeOutms)` Write Binary Data to PL23D3 HidtoSpi chip.
    `int` | `spiWriteRead(int nDeviceIndex, SpiSetting.Select nSelect, byte[] byWriteBuffer, int nNumberOfBytesToWrite, byte[] byReadBuffer, int nNumberOfBytesToRead, int nTimeOutms)` Write Read Data to PL23D3 HidtoSpi chip.

    - ### Methods inherited from class tw.com.prolific.pl2303ghidsdk.HidToGpio

      `getGpio, setGpio, setGpioDir`

    - ### Methods inherited from class java.lang.Object

      `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Constructor Detail

    - #### HidToSpi

          public HidToSpi()

      New a HidToSpi driver object.

    - #### HidToSpi

          public HidToSpi(android.content.Context context,
                          java.lang.String strAppName)

      New a HidToSpi driver object.
      Parameters:
        `context` - the context
        `strAppName` - ACTION_USB_PERMISSION string, for example: "tw.com.prolific.HidToSpiTest.USB_PERMISSION"

  - ### Method Detail

    - #### setSpiFrequency

          public int setSpiFrequency(int nDeviceIndex,
                                     byte byFreqDiv,
                                     SpiSetting.SPI_MODE nSpiMode)

      Sets spi frequency.
      Parameters:
        `nDeviceIndex` - The Device Index
        `byFreqDiv` - The frequency divisor, this value should be equal or larger than 3. SPI Frequency(KHz) = 24000/(nFreqDiv+1)
        `nSpiMode` - The spi mode.
      Returns:
        Zero indicates success. Nonzero indicates failure.

    - #### spiRead

          public int spiRead(int nDeviceIndex,
                             SpiSetting.Select nSelect,
                             byte[] byBuffer,
                             int nNumberOfBytesToRead,
                             int nTimeOutms)

      Read Binary Data from PL23D3 HidtoSpi chip.
      Parameters:
        `nDeviceIndex` - the device index
        `nSelect` - the chip select
        `byBuffer` - Reading buffer
        `nNumberOfBytesToRead` - the number of bytes to read
        `nTimeOutms` - The timeout value in milliseconds.
      Returns:
        Positive value indicates the actually read length , negative value indicates failure

    - #### spiWrite

          public int spiWrite(int nDeviceIndex,
                              SpiSetting.Select nSelect,
                              byte[] byBuffer,
                              int nNumberOfBytesToWrite,
                              int nTimeOutms)

      Write Binary Data to PL23D3 HidtoSpi chip.
      Parameters:
        `nDeviceIndex` - the device index
        `nSelect` - the spi chip select
        `byBuffer` - The write buffer
        `nNumberOfBytesToWrite` - the number of bytes to write
        `nTimeOutms` - The timeout value in milliseconds.
      Returns:
        Positive value indicates the actually written length, negative value indicates failure.

    - #### spiWriteRead

          public int spiWriteRead(int nDeviceIndex,
                                  SpiSetting.Select nSelect,
                                  byte[] byWriteBuffer,
                                  int nNumberOfBytesToWrite,
                                  byte[] byReadBuffer,
                                  int nNumberOfBytesToRead,
                                  int nTimeOutms)

      Write Read Data to PL23D3 HidtoSpi chip.
      Parameters:
        `nDeviceIndex` - the device index
        `nSelect` - the spi chip select
        `byWriteBuffer` - The write buffer
        `nNumberOfBytesToWrite` - the number of bytes to write
        `byReadBuffer` - Reading buffer
        `nNumberOfBytesToRead` - the number of bytes to read
        `nTimeOutms` - The timeout value in milliseconds.
      Returns:
        the int Positive value indicates the actually read length, negative value indicates failure.

    - #### spiReset

          public int spiReset(int nDeviceIndex)

      Reset PL23D3 HidToSpi Chip configurations.
      Parameters:
        `nDeviceIndex` - the device index
      Returns:
        Positive value indicates success, negative value indicates failure.

    - #### enumDevices

          public int enumDevices(int nDeviceVid)

      Found a PL23B3, PL23C3, or PL23D3 device , the default VID is 067B
      Parameters:
        `nDeviceVid` - the device vid
      Returns:
        Number of device

    - #### enumDevices

          public int enumDevices(int nDeviceVid,
                                 int nDevicePid)

      Found a PL23B3, PL23C3, or PL23D3 device by given VID and PID
      Parameters:
        `nDeviceVid` - the device vid
        `nDevicePid` - the device pid
      Returns:
        Number of device

    - #### getDeviceName

          public java.lang.String getDeviceName(int nDeviceIndex)

      Gets device name by given index.
      Parameters:
        `nDeviceIndex` - The index of device
      Returns:
        Device name, empty string indicates device not found.

    - #### getSerialNumber

          public java.lang.String getSerialNumber(int nDeviceIndex)

      Gets device serial number by given index. This function support Android version 5.0 or later,
      Parameters:
        `nDeviceIndex` - the index of device
      Returns:
        Device serial number, empty string indicates device not found.

    - #### openDeviceByIndex

          public int openDeviceByIndex(int nDeviceIndex)

      Open device by given index.
      Parameters:
        `nDeviceIndex` - the index of device
      Returns:
        Zero indicates success. Nonzero indicates failure.

    - #### openDeviceBySerialNumber

          public int openDeviceBySerialNumber(java.lang.String strSerialNumber)

      Open device by given serial number.
      Parameters:
        `strSerialNumber` - the serial number of device
      Returns:
        Zero indicates success. Nonzero indicates failure.

    - #### hasPermissionByDeviceIndex

          public boolean hasPermissionByDeviceIndex(int nDeviceIndex)

      Detecting whether this device which decided by given index has permission
      Parameters:
        `nDeviceIndex` - the index of device
      Returns:
        true indicates the device has permission to access usb device, else false.

    - #### askPermissionByDeviceIndex

          public int askPermissionByDeviceIndex(int nDeviceIndex)

      Obtain permission to communicate with a device by given device name
      Parameters:
        `nDeviceIndex` - the index of device
      Returns:
        Zero indicates success. Nonzero indicates failure.

    - #### hasPermissionByUsbDevice

          public boolean hasPermissionByUsbDevice(android.hardware.usb.UsbDevice usbDevice)

      check if device has permission or not.
      Parameters:
        `usbDevice` - the usb device
      Returns:
        true indicates the given device has permission to access usb device, else false.

    - #### askPermissionByUsbDevice

          public int askPermissionByUsbDevice(android.hardware.usb.UsbDevice usbDevice)

      Obtain permission to communicate with the given device.
      Parameters:
        `usbDevice` - the usb device
      Returns:
        Zero indicates success. Nonzero indicates failure.

    - #### closeDevice

          public int closeDevice()

      Close the PL23D3 device.
      Returns:
        Zero indicates success. Nonzero indicates failure.

    - #### closeDeviceByDeviceIndex

          public int closeDeviceByDeviceIndex(int nDeviceIndex)

    - #### getSdkVersion

          public java.lang.String getSdkVersion()

      Gets SDK version.
      Returns:
        the current SDK version

    - #### setCommandTimeOut

          public void setCommandTimeOut(int nMillSecond)

      Sets device command time out value.
      Parameters:
        `nMillSecond` - the time value in millisecond.

    - #### dumpMsg

          public void dumpMsg(java.lang.Object objMsg)

      Dump log into sdk.
      Parameters:
        `objMsg` - The message content.

    - #### getUsbDevice

          public android.hardware.usb.UsbDevice getUsbDevice(int nDeviceIndex)

      Gets UsbDevice object by given index
      Parameters:
        `nDeviceIndex` - the index of device
      Returns:
        the usbDevice object, null indicates device not found.