diff --git a/libpaperang/Exceptions.cs b/libpaperang/Exceptions.cs index 64a7431..325e821 100644 --- a/libpaperang/Exceptions.cs +++ b/libpaperang/Exceptions.cs @@ -1,9 +1,10 @@ using System; namespace libpaperang { - public class PrinterNotInitialisedException : InvalidOperationException { } - public class PrinterNotAvailableException : NullReferenceException { } - public class PrinterVariantNotSupportedException : NotSupportedException { } - public class CrcNotAvailableException : MissingMemberException { } - public class InvalidOperationException : ArgumentOutOfRangeException { } + + 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 0fc430f..916bf25 100644 --- a/libpaperang/Helpers/CRC.cs +++ b/libpaperang/Helpers/CRC.cs @@ -31,7 +31,7 @@ namespace libpaperang.Helpers { return ~ivr; } public uint GetChecksumUint(IEnumerable data) { - if (!IsInitialised) throw new InvalidOperationException("CRC object has not been Initialise()'d first. Please do that."); + if (!IsInitialised) throw new CrcNotAvailableException(); try { return ~data.Aggregate(iv, (cti, cb) => crctable[(cti&0xFF)^Convert.ToByte(cb)]^(cti>>8)); diff --git a/libpaperang/Helpers/Transforms.cs b/libpaperang/Helpers/Transforms.cs index 0a88122..7aa2e14 100644 --- a/libpaperang/Helpers/Transforms.cs +++ b/libpaperang/Helpers/Transforms.cs @@ -7,15 +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(); - } - } + 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 8d4e3e4..14c2876 100644 --- a/libpaperang/IPrinter.cs +++ b/libpaperang/IPrinter.cs @@ -1,4 +1,5 @@ - +using System; +using System.Collections.Generic; namespace libpaperang { public abstract class BaseTypes { @@ -41,20 +42,29 @@ namespace libpaperang { public byte[] Print; public byte[] TransmitCrc; } + public struct Printer { + public Connection CommsMethod; + public Model Variant; + public Guid Id; + public string Address; + public dynamic Instance; + } } interface IPrinter { short LineWidth { get; } BaseTypes.Connection ConnectionMethod { get; } BaseTypes.Model PrinterVariant { get; } BaseTypes.State Status { get; } - bool IsPrinterAvailable(); - bool IsPrinterInitialised(); - bool Initialise(); - bool OpenPrinter(); - bool ClosePrinter(); - bool Deinitialise(); - bool WriteBytes(byte[] packet); - bool WriteBytes(byte[] packet, int delay); + bool PrinterAvailable { get; } + bool PrinterInitialised { get; } + bool PrinterOpen { get; } + List AvailablePrinters { get; } + void Initialise(); + void OpenPrinter(BaseTypes.Printer printer); + void ClosePrinter(); + void Deinitialise(); + void WriteBytes(byte[] packet); + void WriteBytes(byte[] packet, int delay); bool[] ReadBytes(); } } diff --git a/libpaperang/Interfaces/USB.cs b/libpaperang/Interfaces/USB.cs new file mode 100644 index 0000000..2b6af56 --- /dev/null +++ b/libpaperang/Interfaces/USB.cs @@ -0,0 +1,98 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using LibUsbDotNet; +using LibUsbDotNet.Main; + +namespace libpaperang.Interfaces { + public class USB : IPrinter { + // constants + private const ushort iV = 0x4348; + private const ushort iP = 0x5584; + private const short MaxDataSize = 1008; + + // types + private struct UsbComms { + public Guid id; + public UsbDevice handle; + public IUsbDevice iface; + public UsbEndpointReader rx; + public 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.Connection ConnectionMethod { get => BaseTypes.Connection.USB; } + public BaseTypes.Model PrinterVariant { get => iModel; } + + public BaseTypes.State Status { get => BaseTypes.State.Offline; } + public List AvailablePrinters { + get { + List _=new List(); + (from d in UsbDevice.AllDevices + where d.Vid == iV && + d.Pid == iP + select d).ToList().ForEach(d => { + _.Add(new BaseTypes.Printer { + Id=d.DeviceInterfaceGuids.First(), + CommsMethod=BaseTypes.Connection.USB, + Address=d.DeviceProperties["Address"].ToString(), + Instance=d + }); + }); + return _; + } + } + + public bool PrinterAvailable { get => AvailablePrinters.Count > 0; } + + public bool PrinterInitialised { get => false; } + + public bool PrinterOpen { get => false; } + + public void ClosePrinter() { + Printer.rx?.Dispose(); + Printer.tx?.Dispose(); + Printer.iface?.ReleaseInterface(0); + Printer.iface?.Close(); + Printer.handle?.Close(); + } + public void Deinitialise() => throw new NotImplementedException(); + public void Initialise() { + if (!PrinterOpen || !Printer.handle.IsOpen) throw new PrinterNotInitialisedException(); + //WinUSB-specific + Printer.iface=Printer.handle as IUsbDevice; + Printer.iface?.SetConfiguration(1); + Printer.iface?.ClaimInterface(0); + Printer.rx=Printer.handle.OpenEndpointReader(ReadEndpointID.Ep01); + Printer.tx=Printer.handle.OpenEndpointWriter(WriteEndpointID.Ep02); + + } + public void OpenPrinter(BaseTypes.Printer printer) { + bool res; + try { + res=((UsbRegistry)printer.Instance).Open(out Printer.handle); + } catch (Exception) { + throw new PrinterNotAvailableException(); + } + if (!res) throw new PrinterNotAvailableException(); + } + } + public bool[] ReadBytes() => throw new NotImplementedException(); + public void WriteBytes(byte[] packet) => throw new NotImplementedException(); + public void WriteBytes(byte[] packet, int delay) => throw new NotImplementedException(); + + public USB(BaseTypes.Model model) => iModel = model; + } +} diff --git a/libpaperang/USB.cs b/libpaperang/USB.cs deleted file mode 100644 index 62241be..0000000 --- a/libpaperang/USB.cs +++ /dev/null @@ -1,51 +0,0 @@ -using System; -using LibUsbDotNet; -using LibUsbDotNet.Main; - -namespace libpaperang { - public class USB : IPrinter { - // constants - private const ushort iV = 0x4348; - private const ushort iP = 0x5584; - private const short MaxDataSize = 1008; - - // 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.Connection ConnectionMethod { get => BaseTypes.Connection.USB; } - public BaseTypes.Model PrinterVariant { get => iModel; } - - public BaseTypes.State Status => 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; - } -}