diff --git a/HelloWordKeyboard.ConsoleApp/Program.cs b/HelloWordKeyboard.ConsoleApp/Program.cs index 49f970a..b60fb5b 100644 --- a/HelloWordKeyboard.ConsoleApp/Program.cs +++ b/HelloWordKeyboard.ConsoleApp/Program.cs @@ -7,31 +7,35 @@ using SixLabors.ImageSharp.PixelFormats; using SixLabors.ImageSharp.Processing; using UsbComm; -byte[] byteArray = new byte[128*296*8]; +byte[] byteArray = new byte[128 * 36 / 8] { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xcf, 0xff, 0x81, 0xff, 0xff, 0xe0, 0x7f, 0xff, 0xe7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8f, 0xfe, 0x0, 0x7f, 0xff, 0x80, 0x1f, 0xff, 0xc7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xf, 0xfc, 0x0, 0x3f, 0xff, 0x0, 0xf, 0xff, 0x87, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, 0xf, 0xf8, 0x7c, 0x3f, 0xfe, 0xf,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xe3, 0xff, 0x1f, 0xfc, 0x7f, 0xe3, 0x0, 0x0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8f, 0xe3, 0xff, 0x1f, 0xfc, 0x7f, 0xe3, 0x0, 0x0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8f, 0xf1, 0xff, 0x1f, 0xfe, 0x3f, 0xc7, 0x0, 0x0, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8f, 0xf1, 0xfe, 0x3f, 0xfe, 0x3f, 0xc7, 0xff, 0xc7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8f, 0xf8, 0x7c, 0x3f, 0xfe, 0xf, 0x7, 0xff, 0xc7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8f, 0xf8, 0x0, 0x7e, 0x3f, 0x0, 0xf, 0xff, 0xc7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8f, 0xfc, 0x0, 0xfe, 0x3f, 0x80, 0x1f, 0xff, 0xc7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x8f, 0xff, 0x3, 0xfe, 0x3f, 0xe0, 0x7f, 0xff, 0xc7, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }; -using (var image = Image.Load("default.jpg")) -{ - // 应用灰度和二值化处理 - image.Mutate(x => x.Grayscale().BinaryThreshold(0.5f)); +//using (var image = Image.Load("default.jpg")) +//{ +// // 应用灰度和二值化处理 +// image.Mutate(x => x.Grayscale().BinaryThreshold(0.5f)); - // 保存处理后的图片 - image.Save("output.jpg"); - // 将处理后的图片转换为字节数组 - //using (MemoryStream ms = new MemoryStream()) - //{ - // image.Save(ms, new JpegEncoder()); - // byteArray = ms.ToArray(); - //} +// // 保存处理后的图片 +// image.Save("output.jpg"); +// // 将处理后的图片转换为字节数组 +// //using (MemoryStream ms = new MemoryStream()) +// //{ +// // image.Save(ms, new JpegEncoder()); +// // byteArray = ms.ToArray(); +// //} - image.CopyPixelDataTo(byteArray); -} +// image.CopyPixelDataTo(byteArray); +//} var hidDevice = new Hw75DynamicDevice(); -var data = hidDevice.SetEInkImage(byteArray, 0, 0, 128, 296, false); +hidDevice.Open(); +var data111 = hidDevice.SetEInkImage(byteArray, 0, 0, 128, 36, false); + +var data = hidDevice.GetVersion(); +//var data11 = hidDevice.GetMotorState(); Console.ReadKey(); -/* + var device = new Device("\\\\?\\hid#vid_1d50&pid_615e&mi_01#8&a8a6dc9&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}"); //Fill vendor id and product id Console.WriteLine(device.GetManufacturer()); @@ -66,7 +70,7 @@ var read = device.Read(64); var result = read[3..(read[2] + 3)]; -var data = MessageD2H.Parser.ParseFrom(result); -Console.WriteLine(data); -*/ +//var data = MessageD2H.Parser.ParseFrom(result); +//Console.WriteLine(data); + //Hid.Exit(); //Call at the end of your program diff --git a/HelloWordKeyboard.DotNet/AssembleDataExtension.cs b/HelloWordKeyboard.DotNet/AssembleDataExtension.cs index c046548..9dc2ccf 100644 --- a/HelloWordKeyboard.DotNet/AssembleDataExtension.cs +++ b/HelloWordKeyboard.DotNet/AssembleDataExtension.cs @@ -9,10 +9,16 @@ public static class AssembleDataExtension { var msgBytes = messageH2D.ToByteArray(); - var list = new List { (byte)msgBytes.Length }; + using (MemoryStream ms = new MemoryStream()) + { + CodedOutputStream output = new CodedOutputStream(ms); + output.WriteInt32(msgBytes.Length); + output.Flush(); + byte[] byteList = ms.ToArray(); - list.AddRange(msgBytes); - - return list.ToArray(); + var result = byteList.Concat(msgBytes).ToArray(); + + return result; + } } } \ No newline at end of file diff --git a/HelloWordKeyboard.DotNet/Hw75DynamicDevice.cs b/HelloWordKeyboard.DotNet/Hw75DynamicDevice.cs index 18f97df..f03fded 100644 --- a/HelloWordKeyboard.DotNet/Hw75DynamicDevice.cs +++ b/HelloWordKeyboard.DotNet/Hw75DynamicDevice.cs @@ -11,12 +11,12 @@ public class Hw75DynamicDevice : IHw75DynamicDevice { private const string Path = "\\\\?\\hid#vid_1d50&pid_615e&mi_01#8&a8a6dc9&0&0000#{4d1e55b2-f16f-11cf-88cb-001111000030}"; - private const int RePortCount = 63; - + private const int RePortCount = 63; + private const int PayloadSize = RePortCount - 1; - - private Device? _device = null; - + + private Device? _device = null; + public DeviceInfo Open() { var info = new DeviceInfo(); @@ -31,7 +31,7 @@ public class Hw75DynamicDevice : IHw75DynamicDevice } catch (Exception) { - + } return info; @@ -51,7 +51,7 @@ public class Hw75DynamicDevice : IHw75DynamicDevice Action = Action.Version }; var data = Call(version); - + return data.Version; } @@ -62,7 +62,7 @@ public class Hw75DynamicDevice : IHw75DynamicDevice Action = Action.MotorGetState }; var data = Call(motorGetState); - + return data.MotorState; } @@ -73,7 +73,7 @@ public class Hw75DynamicDevice : IHw75DynamicDevice Action = Action.EinkSetImage, EinkImage = new EinkImage() { - Id = (uint)new Random().Next()*1000000, + Id = (uint)397105, Bits = ByteString.CopyFrom(imageData), Partial = partial, } @@ -100,34 +100,39 @@ public class Hw75DynamicDevice : IHw75DynamicDevice } var bytes = h2d.EnCodeProtoMessage(); - for (int i = 0; i < bytes.Length; i+=PayloadSize) + for (int i = 0; i < bytes.Length; i += PayloadSize) { - var buf = bytes[i..(i+PayloadSize)]; + var buf = new byte[PayloadSize]; - var list =new List + if (i + PayloadSize > bytes.Length) { - 1, - (byte)buf.Length - }; - list.AddRange(buf); - _device.Write(list.ToArray()); + buf = bytes[i..]; + } + else + { + buf = bytes[i..(i + PayloadSize)]; + } + + var list = new byte[2] { 1, (byte)buf.Length }; + + var result = list.Concat(buf).ToArray(); + _device.Write(result); } - + Task.Delay(20); var byteList = new List(); - + while (true) { - var read = _device.Read(RePortCount+1); + var read = _device.Read(RePortCount + 1); int cnt = read[1]; - byteList.AddRange(read[2..cnt]); + byteList.AddRange(read[3..(cnt + 2)]); if (cnt < PayloadSize) { break; } } - return MessageD2H.Parser.ParseFrom(byteList.ToArray()); } } \ No newline at end of file