using System;
using System.Runtime.InteropServices;

namespace CP210xRuntime_DLL
{
   internal class CP210xRuntime
   {
      public const int CP210x_MAX_MAXPOWER = 250;

      //GetPartNumber()
      public const byte CP210x_CP2108_VERSION = ((Byte)(0xFF & 0x08));

      // GetProductString() function flags
      public const byte CP210x_RETURN_SERIAL_NUMBER = 0x00;
      public const byte CP210x_RETURN_DESCRIPTION = 0x01;
      public const byte CP210x_RETURN_FULL_PATH = 0x02;

      // GetDeviceVersion() return codes Deprecated
      //public const byte CP210x_USBXPRESS_EFM8 = 0x80;
      //public const byte CP210x_USBXPRESS_EFM32 = 0x81;
      //public const byte CP210x_CP2101_VERSION = 0x01;
      //public const byte CP210x_CP2102_VERSION = 0x02;
      //public const byte CP210x_CP2103_VERSION = 0x03;
      //public const byte CP210x_CP2104_VERSION = 0x04;
      //public const byte CP210x_CP2105_VERSION = 0x05;
      //public const byte CP210x_CP2108_VERSION = 0x08;
      //public const byte CP210x_CP2109_VERSION = 0x09;
      //public const byte CP210x_CP2102N_QFN28_VERSION = 0x20;
      //public const byte CP210x_CP2102N_QFN24_VERSION = 0x21;
      //public const byte CP210x_CP2102N_QFN20_VERSION = 0x22;

      // Return codes
      public const byte CP210x_SUCCESS = 0x00;
      public const byte CP210x_DEVICE_NOT_FOUND = 0xFF;
      public const byte CP210x_INVALID_HANDLE = 0x01;
      public const byte CP210x_INVALID_PARAMETER = 0x02;
      public const byte CP210x_DEVICE_IO_FAILED = 0x03;
      public const byte CP210x_FUNCTION_NOT_SUPPORTED = 0x04;
      public const byte CP210x_GLOBAL_DATA_ERROR = 0x05;
      public const byte CP210x_FILE_ERROR = 0x06;
      public const byte CP210x_COMMAND_FAILED = 0x08;
      public const byte CP210x_INVALID_ACCESS_TYPE = 0x09;

      // Buffer size limits
      //public const int CP2108_MAX_PRODUCT_STRLEN = 126;
      //public const int CP2108_MAX_SERIAL_STRLEN = 63;

      // Type Definitions
      //readonly char CP210x_PRODUCT_STRING[] = new char[CP2108_MAX_PRODUCT_STRLEN]();
      //char CP210x_SERIAL_STRING[CP2108_MAX_SERIAL_STRLEN];

      // Mask and Latch value bit definitions
      public const UInt32 CP210x_GPIO_0 = 0x0001; // (1<<0)
      public const UInt32 CP210x_GPIO_1 = 0x0002; // (1<<1)
      public const UInt32 CP210x_GPIO_2 = 0x0004; // (1<<2)
      public const UInt32 CP210x_GPIO_3 = 0x0008;// etc.
      public const UInt32 CP210x_GPIO_4 = 0x0010;
      public const UInt32 CP210x_GPIO_5 = 0x0020;
      public const UInt32 CP210x_GPIO_6 = 0x0040;
      public const UInt32 CP210x_GPIO_7 = 0x0080;
      public const UInt32 CP210x_GPIO_8 = 0x0100;
      public const UInt32 CP210x_GPIO_9 = 0x0200;
      public const UInt32 CP210x_GPIO_10 = 0x0400;
      public const UInt32 CP210x_GPIO_11 = 0x0800;
      public const UInt32 CP210x_GPIO_12 = 0x1000;
      public const UInt32 CP210x_GPIO_13 = 0x2000;
      public const UInt32 CP210x_GPIO_14 = 0x4000;
      public const UInt32 CP210x_GPIO_15 = 0x8000;

      [DllImport("CP210xRuntime.dll")]
      public static extern int CP210xRT_GetNumDevices(
      ref uint lpdwNumDevices
      );

      [DllImport("CP210xRuntime.dll")]
      public static extern int CP210xRT_Open(
      uint deviceIndex,
      ref IntPtr pcyHandle
      );

      [DllImport("CP210xRuntime.dll")]
      public static extern int CP210xRT_Close(
      IntPtr cyHandle
      );

      [DllImport("CP210xRuntime.dll")]
      public static extern int CP210xRT_ReadLatch(
      IntPtr cyHandle,
      ref ushort lpwLatch
      );

      [DllImport("CP210xRuntime.dll")]
      public static extern int CP210xRT_WriteLatch(
      IntPtr cyHandle,
      ushort Mask,
      ushort Latch
      );

      [DllImport("CP210xRuntime.dll")]
      public static extern int CP210xRT_GetPartNumber(
      IntPtr cyHandle,
      ref byte lpbPartNum
      );

      [DllImport("CP210xRuntime.dll")]
      public static extern int CP210xRT_GetDeviceProductString(
      IntPtr cyHandle,
      [Out] byte[] lpProduct,
      ref byte lpbLength,
      bool bConvertToASCII
      );

      [DllImport("CP210xRuntime.dll")]
      public static extern int CP210xRT_GetDeviceSerialNumber(
      IntPtr cyHandle,
      [Out] byte[] lpSerialNumberString,
      ref byte lpbLength,
      bool bConvertToASCII
      );

      [DllImport("CP210xRuntime.dll")]
      public static extern int CP210xRT_GetDeviceInterfaceString(
      IntPtr cyHandle,
      [Out] byte[] lpInterfaceString,
      ref byte lpbLength,
      bool bConvertToASCII
      );

      [DllImport("CP210xRuntime.dll")]
      public static extern int CP210xRT_GetReceiverMaxTimeout(
      IntPtr cyHandle,
      ref UInt16 maxTimeout
      );

      [DllImport("CP210xRuntime.dll")]
      public static extern int CP210xRT_SetReceiverMaxTimeout(
      IntPtr cyHandle,
      UInt16 maxTimeout
      );
   }
}
