we regret to inform you there has been More Rewriting

This commit is contained in:
Maff 2019-09-12 22:34:17 +01:00
parent be368f398c
commit 89766bc2d5
6 changed files with 133 additions and 75 deletions

View File

@ -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 { }
}

View File

@ -31,7 +31,7 @@ namespace libpaperang.Helpers {
return ~ivr;
}
public uint GetChecksumUint<T>(IEnumerable<T> 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));

View File

@ -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();
}
}
}
}

View File

@ -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<BaseTypes.Printer> 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();
}
}

View File

@ -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<BaseTypes.Printer> AvailablePrinters {
get {
List<BaseTypes.Printer> _=new List<BaseTypes.Printer>();
(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;
}
}

View File

@ -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;
}
}