Пример #1
0
 public void ExitProtocol()
 {
     for (int i = 0; i < j.Count; ++i)
     {
         j[i].Detach();
     }
     HIDapi.hid_exit();
 }
        private static int rw(uint start_page, int bytes, byte[] in_buf, bool read = true, bool eeprom_write = false)
        {
            if (eeprom_write)
            {
                read = true;
            }
            bool connect_success = true;

            HIDapi.hid_init();
            IntPtr ptr = HIDapi.hid_enumerate(USB_VID, USB_PID);

            if (ptr == IntPtr.Zero)
            {
                HIDapi.hid_free_enumeration(ptr);
                HIDapi.hid_exit();
                connect_success = false;
                return(-4);
            }
            hid_device_info enumerate = (hid_device_info)Marshal.PtrToStructure(ptr, typeof(hid_device_info));
            IntPtr          handle    = HIDapi.hid_open_path(enumerate.path);

            HIDapi.hid_set_nonblocking(handle, 1);
            HIDapi.hid_free_enumeration(ptr);
            if (!connect_success)
            {
                HIDapi.hid_close(handle);
                HIDapi.hid_exit();
                return(-4);
            }
            int numPages = bytes / pagelen;

            if (bytes % pagelen != 0)
            {
                ++numPages;
            }
            int in_buf_ind = 0;

            byte[] r = new byte[recvlen];
            byte[] s = new byte[sendlen];
            uint   i = 0;

            for (i = start_page; i < start_page + numPages; ++i)
            {
                s[0] = 0;
                // pack address bytes in big endian
                s[1] = 0;
                s[2] = (byte)((i >> 8) & 0xff);
                s[3] = (byte)(i & 0xff);
                byte   crc = 0;
                string formatstr;
                if (!read | eeprom_write)
                {
                    for (int j = addrlen + 1; j < sendlen; ++j)
                    {
                        if (in_buf_ind < bytes)
                        {
                            s[j] = in_buf[in_buf_ind];
                            ++in_buf_ind;
                        }
                        crc = crc7_table[crc ^ s[j]];
                    }
                    crc >>= 1;
                    s[1]  = (byte)0x80;
                    s[1] |= crc;
                }
                if (eeprom_write)
                {
                    s[1] = (byte)0x20;
                }
                if (debugdump)
                {
                    formatstr = "sent page: {0:S}";
                    PrintArray(s, format: formatstr);
                }
                int res = 0;
                int err = 0;
                res = HIDapi.hid_send_feature_report(handle, s, new UIntPtr(sendlen));
                if (res < 1)
                {
                    err = -1;
                }
                else
                {
                    res = HIDapi.hid_read_timeout(handle, r, new UIntPtr(recvlen), 1000);
                    if (res < 1)
                    {
                        err = -2;
                    }
                    if (r[1] != s[1] || r[2] != s[2] || r[3] != s[3])
                    {
                        err = -3;
                    }
                }
                if (err == 0)
                {
                    if (read)
                    {
                        for (int j = addrlen + 1; j < recvlen; ++j)
                        {
                            if (in_buf_ind < bytes)
                            {
                                in_buf[in_buf_ind] = r[j];
                                ++in_buf_ind;
                            }
                        }
                    }
                    if (debugdump)
                    {
                        formatstr = string.Concat(String.Format("page {0:D}, read: {1:B}, err: {2:D}, data: ", i, read, err), "{0:S}");
                        PrintArray(r, format: formatstr);
                    }
                }
                else
                {
                    if (debugdump)
                    {
                        print("error reading");
                        print(err.ToString());
                    }

                    HIDapi.hid_close(handle);
                    HIDapi.hid_exit();
                    return(err);
                }
                formatstr = string.Concat(String.Format("page {0:D}, read: {1:B}, data: ", i, read), "{0:S}");
                PrintArray(r, format: formatstr);
            }
            // tell device to copy flash memory out of buffer
            if (!read && !eeprom_write)
            {
                s[1] = 0x40;
                s[2] = (byte)((start_page >> 8) & 0xff);
                s[3] = (byte)(start_page & 0xff);
                s[4] = (byte)((i >> 8) & 0xff);
                s[5] = (byte)(i & 0xff);
                int res = HIDapi.hid_send_feature_report(handle, s, new UIntPtr(sendlen));
                if (res < 1)
                {
                    HIDapi.hid_close(handle);
                    HIDapi.hid_exit();
                    return(-2);
                }
                res = HIDapi.hid_read_timeout(handle, r, new UIntPtr(recvlen), 1000);
                if (res < 1)
                {
                    HIDapi.hid_close(handle);
                    HIDapi.hid_exit();
                    return(-2);
                }
                if (r[1] != s[1] || r[2] != s[2] || r[3] != s[3])
                {
                    HIDapi.hid_close(handle);
                    HIDapi.hid_exit();
                    return(-3);
                }
            }
            HIDapi.hid_close(handle);
            HIDapi.hid_exit();

            return(0);
        }
        public static int SetLEDBrightness(float brightness_)
        {
            if (led_brightness_locked)
            {
                return(-4);
            }
            if (brightness_ > 1f)
            {
                return(-5);
            }
            HIDapi.hid_init();
            byte   brightness = (byte)(brightness_ * 255);
            IntPtr ptr        = HIDapi.hid_enumerate(USB_VID, USB_PID);

            if (ptr == IntPtr.Zero)
            {
                HIDapi.hid_free_enumeration(ptr);
                HIDapi.hid_exit();
                return(-4);
            }
            hid_device_info enumerate = (hid_device_info)Marshal.PtrToStructure(ptr, typeof(hid_device_info));
            IntPtr          handle    = HIDapi.hid_open_path(enumerate.path);

            HIDapi.hid_set_nonblocking(handle, 1);
            HIDapi.hid_free_enumeration(ptr);
            byte[] r = new byte[recvlen];
            byte[] s = new byte[sendlen];
            for (int i = 0; i < sendlen; ++i)
            {
                s[i] = 0;
            }
            s[1] = (byte)0x10;
            s[2] = brightness;
            int res = 0;
            int err = 0;

            res = HIDapi.hid_send_feature_report(handle, s, new UIntPtr(sendlen));
            if (res < 1)
            {
                err = -1;
            }
            else
            {
                res = HIDapi.hid_read_timeout(handle, r, new UIntPtr(recvlen), 1000);
                if (res < 1)
                {
                    err = -2;
                }
                if (r[1] != s[1] || r[2] != s[2] || r[3] != s[3])
                {
                    led_brightness_locked = true;
                    err = -3;
                }
                if (r[4] == 0xff)
                {
                    led_brightness_locked = true;
                    err = -4;
                }
            }
            HIDapi.hid_close(handle);
            HIDapi.hid_exit();
            return(err);
        }