From be368f398c169b6c06e7be797d5bbdba27fb3d7f Mon Sep 17 00:00:00 2001 From: Matthew Connelly Date: Thu, 12 Sep 2019 17:03:16 +0100 Subject: [PATCH] further refactoring --- libpaperang/Exceptions.cs | 9 +++++++ libpaperang/Helpers/CRC.cs | 2 +- libpaperang/Helpers/Transforms.cs | 11 +++++++- libpaperang/IPrinter.cs | 8 +++--- libpaperang/USB.cs | 44 +++++++++++++++++++++++++------ 5 files changed, 60 insertions(+), 14 deletions(-) create mode 100644 libpaperang/Exceptions.cs diff --git a/libpaperang/Exceptions.cs b/libpaperang/Exceptions.cs new file mode 100644 index 0000000..64a7431 --- /dev/null +++ b/libpaperang/Exceptions.cs @@ -0,0 +1,9 @@ +using System; + +namespace libpaperang { + public class PrinterNotInitialisedException : InvalidOperationException { } + public class PrinterNotAvailableException : NullReferenceException { } + public class PrinterVariantNotSupportedException : NotSupportedException { } + public class CrcNotAvailableException : MissingMemberException { } + public class InvalidOperationException : ArgumentOutOfRangeException { } +} diff --git a/libpaperang/Helpers/CRC.cs b/libpaperang/Helpers/CRC.cs index becad64..0fc430f 100644 --- a/libpaperang/Helpers/CRC.cs +++ b/libpaperang/Helpers/CRC.cs @@ -9,7 +9,7 @@ namespace libpaperang.Helpers { private uint poly = 0xedb88320; private uint[] crctable; public bool IsInitialised=false; - public CRC() { this.iv=0; } + public CRC() { iv=0; } public CRC(uint iv) => this.iv=iv; public void Initialise() { crctable=Enumerable.Range(0, 256).Select(i => { diff --git a/libpaperang/Helpers/Transforms.cs b/libpaperang/Helpers/Transforms.cs index 0ca4233..0a88122 100644 --- a/libpaperang/Helpers/Transforms.cs +++ b/libpaperang/Helpers/Transforms.cs @@ -7,6 +7,15 @@ namespace libpaperang.Helpers { Frame=FrameConstruction; Op=Operations; } - + public byte[] Oper(BaseTypes.Operations Operation) { + switch (Operation) { + case BaseTypes.Operations.NoOp: return Op.NoOp; + case BaseTypes.Operations.LineFeed: return Op.LineFeed; + case BaseTypes.Operations.CrcTransmit: return Op.TransmitCrc; + case BaseTypes.Operations.Print: return Op.Print; + default: throw new InvalidOperationException(); + } + } + } } diff --git a/libpaperang/IPrinter.cs b/libpaperang/IPrinter.cs index 6caa9cf..8d4e3e4 100644 --- a/libpaperang/IPrinter.cs +++ b/libpaperang/IPrinter.cs @@ -36,10 +36,10 @@ namespace libpaperang { Print } public struct Opcodes { - byte[] NoOp; - byte[] LineFeed; - byte[] Print; - byte[] TransmitCrc; + public byte[] NoOp; + public byte[] LineFeed; + public byte[] Print; + public byte[] TransmitCrc; } } interface IPrinter { diff --git a/libpaperang/USB.cs b/libpaperang/USB.cs index 93f6b69..62241be 100644 --- a/libpaperang/USB.cs +++ b/libpaperang/USB.cs @@ -1,23 +1,51 @@ using System; +using LibUsbDotNet; +using LibUsbDotNet.Main; namespace libpaperang { - public class USB : IPrinter { - public short LineWidth => throw new NotImplementedException(); + public class USB : IPrinter { + // constants + private const ushort iV = 0x4348; + private const ushort iP = 0x5584; + private const short MaxDataSize = 1008; - public BaseTypes.Connection ConnectionMethod => throw new NotImplementedException(); + // types + private struct UsbComms { + Guid id; + UsbDevice handle; + UsbEndpointReader rx; + UsbEndpointWriter tx; + } + private UsbComms Printer; + private BaseTypes.Model iModel; + public short LineWidth { get { + switch (iModel) { + case BaseTypes.Model.P1: return 48; + case BaseTypes.Model.P2: return 72; + case BaseTypes.Model.P2S: return 72; //assumption + case BaseTypes.Model.T1: throw new PrinterVariantNotSupportedException(); + default: throw new InvalidOperationException(); + } + } } - public BaseTypes.Model PrinterVariant => throw new NotImplementedException(); + public BaseTypes.Connection ConnectionMethod { get => BaseTypes.Connection.USB; } + public BaseTypes.Model PrinterVariant { get => iModel; } - public BaseTypes.State Status => throw new NotImplementedException(); + public BaseTypes.State Status => throw new NotImplementedException(); - public bool ClosePrinter() => throw new NotImplementedException(); - public bool Deinitialise() => throw new NotImplementedException(); - public bool Initialise() => throw new NotImplementedException(); + public bool ClosePrinter() => throw new NotImplementedException(); + public bool Deinitialise() => throw new NotImplementedException(); + public bool Initialise() { + if (!IsPrinterAvailable()) return false; + return true; + } public bool IsPrinterAvailable() => throw new NotImplementedException(); public bool IsPrinterInitialised() => throw new NotImplementedException(); public bool OpenPrinter() => throw new NotImplementedException(); public bool[] ReadBytes() => throw new NotImplementedException(); public bool WriteBytes(byte[] packet) => throw new NotImplementedException(); public bool WriteBytes(byte[] packet, int delay) => throw new NotImplementedException(); + + public USB(BaseTypes.Model model) => iModel = model; } }