# From http://www.robot-electronics.co.uk/htm/usb_iss_tech.htm

# USB-ISS command
CMD_USB_ISS = 0x5A

# USB ISS response codes
# (NOTE: For I2C, NACK = 0, ACK = non-zero)
USB_ISS_NACK = 0x00
USB_ISS_ACK = 0xFF

# USB_ISS sub-commands
USB_ISS_ISS_VERSION = 0x01
USB_ISS_ISS_MODE = 0x02
USB_ISS_GET_SER_NUM = 0x03

# ISS_MODE values
ISS_MODE_IO_MODE = 0x00
ISS_MODE_IO_CHANGE = 0x10
ISS_MODE_I2C_S_20KHZ = 0x20
ISS_MODE_I2C_S_50KHZ = 0x30
ISS_MODE_I2C_S_100KHZ = 0x40
ISS_MODE_I2C_S_400KHZ = 0x50
ISS_MODE_I2C_H_100KHZ = 0x60
ISS_MODE_I2C_H_400KHZ = 0x70
ISS_MODE_I2C_H_1000KHZ = 0x80
ISS_MODE_SPI_MODE = 0x90
ISS_MODE_SERIAL = 0x01

# ISS_MODE errors
ISS_MODE_UNKNOWN_COMMAND = 0x05
ISS_MODE_INTERNAL_ERROR_1 = 0x06
ISS_MODE_INTERNAL_ERROR_2 = 0x07

# IO_TYPE values for each I/O
IO_TYPE_IO1_OUTPUT_LOW = 0x00
IO_TYPE_IO1_OUTPUT_HIGH = 0x01
IO_TYPE_IO1_DIGITAL_INPUT = 0x02
IO_TYPE_IO1_ANALOGUE_INPUT = 0x03
IO1_TYPES = [
    IO_TYPE_IO1_OUTPUT_LOW,
    IO_TYPE_IO1_OUTPUT_HIGH,
    IO_TYPE_IO1_DIGITAL_INPUT,
    IO_TYPE_IO1_ANALOGUE_INPUT,
    ]

IO_TYPE_IO2_OUTPUT_LOW = 0x00
IO_TYPE_IO2_OUTPUT_HIGH = 0x04
IO_TYPE_IO2_DIGITAL_INPUT = 0x08
IO_TYPE_IO2_ANALOGUE_INPUT = 0x0C
IO2_TYPES = [
    IO_TYPE_IO2_OUTPUT_LOW,
    IO_TYPE_IO2_OUTPUT_HIGH,
    IO_TYPE_IO2_DIGITAL_INPUT,
    IO_TYPE_IO2_ANALOGUE_INPUT,
    ]

IO_TYPE_IO3_OUTPUT_LOW = 0x00
IO_TYPE_IO3_OUTPUT_HIGH = 0x10
IO_TYPE_IO3_DIGITAL_INPUT = 0x20
IO_TYPE_IO3_ANALOGUE_INPUT = 0x30
IO3_TYPES = [
    IO_TYPE_IO3_OUTPUT_LOW,
    IO_TYPE_IO3_OUTPUT_HIGH,
    IO_TYPE_IO3_DIGITAL_INPUT,
    IO_TYPE_IO3_ANALOGUE_INPUT,
    ]

IO_TYPE_IO4_OUTPUT_LOW = 0x00
IO_TYPE_IO4_OUTPUT_HIGH = 0x40
IO_TYPE_IO4_DIGITAL_INPUT = 0x80
IO_TYPE_IO4_ANALOGUE_INPUT = 0xC0
IO4_TYPES = [
    IO_TYPE_IO4_OUTPUT_LOW,
    IO_TYPE_IO4_OUTPUT_HIGH,
    IO_TYPE_IO4_DIGITAL_INPUT,
    IO_TYPE_IO4_ANALOGUE_INPUT,
    ]

# SPI_MODE values
SPI_MODE_TX_ACTIVE_TO_IDLE_IDLE_LOW = 0x90
SPI_MODE_TX_ACTIVE_TO_IDLE_IDLE_HIGH = 0x91
SPI_MODE_TX_IDLE_TO_ACTIVE_IDLE_LOW = 0x92
SPI_MODE_TX_IDLE_TO_ACTIVE_IDLE_HIGH = 0x93

# I2C commands
I2C_SGL = 0x53
I2C_AD0 = 0x54
I2C_AD1 = 0x55
I2C_AD2 = 0x56
I2C_DIRECT = 0x57
I2C_TEST = 0x58

# Maximum number of bytes that can be read/written in a single command
I2C_AD1_MAX_WRITE_BYTE_COUNT = 60
I2C_AD1_MAX_READ_BYTE_COUNT = 60
I2C_AD2_MAX_WRITE_BYTE_COUNT = 59
I2C_AD2_MAX_READ_BYTE_COUNT = 64

# I2C_DIRECT commands
I2C_DIRECT_START = 0x01
I2C_DIRECT_RESTART = 0x02
I2C_DIRECT_STOP = 0x03
I2C_DIRECT_NACK = 0x04
I2C_DIRECT_READ1 = 0x20
I2C_DIRECT_READ2 = 0x21
I2C_DIRECT_READ3 = 0x22
I2C_DIRECT_READ4 = 0x23
I2C_DIRECT_READ5 = 0x24
I2C_DIRECT_READ6 = 0x25
I2C_DIRECT_READ7 = 0x26
I2C_DIRECT_READ8 = 0x27
I2C_DIRECT_READ9 = 0x28
I2C_DIRECT_READ10 = 0x29
I2C_DIRECT_READ11 = 0x2A
I2C_DIRECT_READ12 = 0x2B
I2C_DIRECT_READ13 = 0x2C
I2C_DIRECT_READ14 = 0x2D
I2C_DIRECT_READ15 = 0x2E
I2C_DIRECT_READ16 = 0x2F
I2C_DIRECT_WRITE1 = 0x30
I2C_DIRECT_WRITE2 = 0x31
I2C_DIRECT_WRITE3 = 0x32
I2C_DIRECT_WRITE4 = 0x33
I2C_DIRECT_WRITE5 = 0x34
I2C_DIRECT_WRITE6 = 0x35
I2C_DIRECT_WRITE7 = 0x36
I2C_DIRECT_WRITE8 = 0x37
I2C_DIRECT_WRITE9 = 0x38
I2C_DIRECT_WRITE10 = 0x39
I2C_DIRECT_WRITE11 = 0x3A
I2C_DIRECT_WRITE12 = 0x3B
I2C_DIRECT_WRITE13 = 0x3C
I2C_DIRECT_WRITE14 = 0x3D
I2C_DIRECT_WRITE15 = 0x3E
I2C_DIRECT_WRITE16 = 0x3F

# I2C_DIRECT errors
I2C_DIRECT_DEVICE_ERROR = 0x01
I2C_DIRECT_BUFFER_OVERFLOW = 0x02
I2C_DIRECT_BUFFER_UNDERFLOW = 0x03
I2C_DIRECT_UNKNOWN_COMMAND = 0x04

# I2C_TEST response
I2C_TEST_NO_DEVICE = 0x00

# IO commands
IO_SET_PINS = 0x63
IO_GET_PINS = 0x64
IO_GET_AD = 0x65

# PIN_STATE masks
IO_PIN_1 = 0x01
IO_PIN_2 = 0x02
IO_PIN_3 = 0x04
IO_PIN_4 = 0x08
