further refinements

This commit is contained in:
Maff 2019-10-01 21:02:34 +01:00
parent e9d702f955
commit d9f9756dcb
6 changed files with 116 additions and 46 deletions

View File

@ -57,12 +57,20 @@ namespace libpaperang.Main {
logger?.Trace($"Writing packet with length {packet.Length} to printer with delay of {ms}ms");
_ = Printer.WriteBytes(packet, ms);
}
public void Feed(uint ms) => WriteBytes(
Transform.Packet(BaseTypes.Operations.LineFeed,
Transform.Arg(BaseTypes.Operations.LineFeed, ms),
public void Feed(uint ms) {
logger?.Trace($"Feeding for {ms}ms");
WriteBytes(
Transform.Packet(BaseTypes.Operations.LineFeed,
Transform.Arg(BaseTypes.Operations.LineFeed, ms),
Crc));
}
public void NoOp() => WriteBytes(
Transform.Packet(BaseTypes.Operations.NoOp, new byte[] { 0, 0 }, Crc));
public void Poll() {
logger?.Trace("Polling attached printer");
Feed(0);
NoOp();
}
public void PrintBytes(byte[] data, bool autofeed = true) {
logger?.Trace($"PrintBytes() invoked with data length of {data.Length}");
List<byte[]> segments = data

Binary file not shown.

Binary file not shown.

View File

@ -5,23 +5,52 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:paperangapp"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800">
Title="libpaperang Test Utility" Height="450" Width="800" ResizeMode="NoResize">
<Grid x:Name="gMain">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="159*"/>
<ColumnDefinition Width="151*"/>
<ColumnDefinition Width="483*"/>
</Grid.ColumnDefinitions>
<GroupBox Header="libpaperang Library Tester" Margin="10,10,9.6,0" Grid.ColumnSpan="3" Height="217" VerticalAlignment="Top">
<Grid Margin="10,10,10,10">
<Button x:Name="btClearLog" Content="Clear Log" HorizontalAlignment="Left" MinWidth="75" Margin="0,2,0,152.2" Click="BtClearLog_Click"/>
<Button x:Name="btInitUSB" Content="Initialise USB" HorizontalAlignment="Left" VerticalAlignment="Top" MinWidth="75" Margin="0,23,0,0" IsDefault="True" Height="21" Click="BtInitUSB_Click"/>
<Button x:Name="btTyP1" Content="Model P1" HorizontalAlignment="Left" VerticalAlignment="Top" MinWidth="75" Margin="100,2,0,0" Height="21" Click="BtSetP1_Click"/>
<Button x:Name="btTyP2" Content="Model P2" HorizontalAlignment="Left" VerticalAlignment="Top" MinWidth="75" Margin="100,23,0,0" Height="21" Click="BtSetP2_Click"/>
<Button x:Name="btTestLine" Content="Single-line test" HorizontalAlignment="Left" VerticalAlignment="Top" MinWidth="75" Margin="0,44,0,0" Height="21" Click="BtTestLine_Click"/>
<Button x:Name="btLoadImage" Content="Load test image" HorizontalAlignment="Left" VerticalAlignment="Top" MinWidth="75" Margin="0,65,0,0" Height="21" Click="BtLoadImage_Click"/>
<Grid.RowDefinitions>
<RowDefinition Height="225*"/>
<RowDefinition Height="225*"/>
</Grid.RowDefinitions>
<GroupBox Header="libpaperang Library Tester" Margin="2,0,2,0" Grid.Row="0" Grid.Column="0">
<Grid Margin="0,0,0,0" Grid.Column="0" Grid.ColumnSpan="3">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="10*"/>
<ColumnDefinition Width="15*"/>
<ColumnDefinition Width="30*"/>
</Grid.ColumnDefinitions>
<GroupBox x:Name="gbInit" Header="Initialisation" Margin="2,2,2,2" Grid.Column="0">
<Grid Margin="0">
<RadioButton x:Name="rbP1" Content="P1/P1S" Margin="10,10,10,0" Click="SetP1_Click" ToolTip="Paperang model P1 or P1S" GroupName="rbgModel" Height="15" VerticalAlignment="Top"/>
<RadioButton x:Name="rbP2" Content="P2/P2S" Margin="10,30,10,0" Click="SetP2_Click" VerticalAlignment="Top" GroupName="rbgModel" ToolTip="Paperang model P2 or P2S"/>
<RadioButton x:Name="rbT1" Content="T1" Margin="10,50,10,0" Click="SetT1_Click" VerticalAlignment="Top" IsChecked="True" ToolTip="Paperang model T1, a tape-esque label printer" GroupName="rbgModel"/>
<Button x:Name="btInitUSB" Content="Initialise" MinWidth="75" Margin="10,0,10,36" IsDefault="True" Click="BtInitUSB_Click" Height="21" VerticalAlignment="Bottom"/>
<Button x:Name="btDeInitUSB" Content="Deinitialise" MinWidth="75" Margin="10,0,10,10" Click="BtDeInitUSB_Click" Height="21" VerticalAlignment="Bottom" IsEnabled="False"/>
</Grid>
</GroupBox>
<GroupBox x:Name="gbOtherFunc" Header="Other Functions" Margin="2,2,2,2" Grid.Column="1" IsEnabled="False">
<Grid Margin="0,0,0,0">
<Slider x:Name="slFeedTime" Margin="10,44,10,0" Maximum="500" Value="80" TickPlacement="Both" SmallChange="5" TickFrequency="5" LargeChange="25" ToolTip="Choose how long, in milliseconds, the printer should feed paper for" IsMoveToPointEnabled="True" IsSnapToTickEnabled="True" Height="33" VerticalAlignment="Top"/>
<Button x:Name="btFeed" Content="Feed" VerticalAlignment="Top" MinWidth="75" Margin="0,13,12,0" Height="21" Click="BtFeed_Click" Width="73" HorizontalAlignment="Right"/>
<Label x:Name="label" Content="Feed (ms)" Margin="10,10,0,0" VerticalAlignment="Top" HorizontalAlignment="Left" Width="79"/>
</Grid>
</GroupBox>
<GroupBox x:Name="gbPrinting" Header="Printing" Margin="2,2,2,2" Grid.Column="2" Grid.ColumnSpan="2" IsEnabled="False">
<Grid Margin="0,0,0,0">
<Button x:Name="btPrintImage" Content="Print Image" HorizontalAlignment="Left" MinWidth="75" Margin="10,0,0,10" Click="BtPrintImage_Click" Height="21" VerticalAlignment="Bottom"/>
<Button x:Name="btPrintText" Content="Print Text" HorizontalAlignment="Left" MinWidth="75" Margin="90,0,0,10" Click="BtPrintText_Click" Height="21" VerticalAlignment="Bottom"/>
<TextBox x:Name="txInput" Margin="180,2,2,2" TextWrapping="Wrap" Text="" AcceptsReturn="True" SpellCheck.IsEnabled="True"/>
<TextBox x:Name="txFont" HorizontalAlignment="Left" Height="23" Margin="10,2,0,0" TextWrapping="Wrap" Text="Consolas" VerticalAlignment="Top" Width="165"/>
<TextBox x:Name="txSzF" HorizontalAlignment="Left" Height="23" Margin="10,30,0,0" Text="12" VerticalAlignment="Top" Width="54" UndoLimit="1" MaxLines="1" IsUndoEnabled="False" MaxLength="3" AllowDrop="False" ToolTip="Please choose a font size"/>
</Grid>
</GroupBox>
</Grid>
</GroupBox>
<TextBox x:Name="tbLog" HorizontalAlignment="Left" Margin="10,232,0,10" TextWrapping="Wrap" Width="774" Grid.ColumnSpan="3" AllowDrop="False" IsReadOnly="True" IsUndoEnabled="False" VerticalScrollBarVisibility="Visible" ScrollViewer.CanContentScroll="True" Text="{Binding Path=LogBuffer}"/>
<Expander x:Name="expander" Header="Log" Margin="2,2,2,2" Grid.Row="1" IsExpanded="True">
<Grid>
<TextBox x:Name="tbLog" Margin="2,2,2,23" TextWrapping="Wrap" AllowDrop="False" IsReadOnly="True" IsUndoEnabled="False" VerticalScrollBarVisibility="Auto" ScrollViewer.CanContentScroll="True" Text="{Binding Path=LogBuffer}" UseLayoutRounding="True"/>
<Button x:Name="btClearLog" Content="Clear Log" MinWidth="75" Margin="0,0,0,0" Click="BtClearLog_Click" Height="21" VerticalAlignment="Bottom"/>
</Grid>
</Expander>
</Grid>
</Window>

View File

@ -15,8 +15,8 @@ namespace paperangapp {
private BaseTypes.Connection mmjcx=BaseTypes.Connection.USB;
private BaseTypes.Model mmjmd=BaseTypes.Model.T1;
private Paperang mmj;
private Bitmap bimg;
// TODO: is it out of scope for this library to provide functionality for printing bitmap data?
public MainWindow() {
InitializeComponent();
logger = new LUITextbox();
@ -25,13 +25,17 @@ namespace paperangapp {
}
private void BtClearLog_Click(object sender, RoutedEventArgs e) =>
logger.Raw("!clearlog");
private void BtSetP1_Click(object sender, RoutedEventArgs e) {
private void SetP1_Click(object sender, RoutedEventArgs e) {
mmjmd = BaseTypes.Model.P1;
logger.Info("Model type set to P1");
logger.Info("Model type set to P1 or P1S");
}
private void BtSetP2_Click(object sender, RoutedEventArgs e) {
private void SetP2_Click(object sender, RoutedEventArgs e) {
mmjmd = BaseTypes.Model.P2;
logger.Info("Model type set to P2");
logger.Info("Model type set to P2 or P2S");
}
private void SetT1_Click(object sender, RoutedEventArgs e) {
mmjmd = BaseTypes.Model.T1;
logger.Info("Model type set to T1");
}
private void BtInitUSB_Click(object sender, RoutedEventArgs e) {
mmj = new Paperang(mmjcx, mmjmd);
@ -45,29 +49,45 @@ namespace paperangapp {
mmj.Initialise();
logger.Debug("PrinterInitialised? " + mmj.Printer.PrinterInitialised);
logger.Debug("Printer initialised and ready");
btInitUSB.IsEnabled = false;
btDeInitUSB.IsEnabled = true;
gbOtherFunc.IsEnabled = true;
gbPrinting.IsEnabled = true;
}
private void BtTestLine_Click(object sender, RoutedEventArgs e) {
string Font="Consolas";
int FontSize=48;
Bitmap b=new Bitmap(mmj.Printer.LineWidth*8, (FontSize+4)*10);
g=Graphics.FromImage(b);
private void BtDeInitUSB_Click(object sender, RoutedEventArgs e) {
mmj.Printer.ClosePrinter();
mmj.Printer.Deinitialise();
mmj = null;
gbPrinting.IsEnabled = false;
gbOtherFunc.IsEnabled = false;
btInitUSB.IsEnabled = true;
btDeInitUSB.IsEnabled = false;
}
private void BtFeed_Click(object sender, RoutedEventArgs e) => mmj.Feed((uint)slFeedTime.Value);
private void BtPrintText_Click(object sender, RoutedEventArgs e) {
Font fnt=new Font(txFont.Text, int.Parse(txSzF.Text));
Graphics g=Graphics.FromImage(new Bitmap(mmj.Printer.LineWidth*8, 1));
SizeF szText=g.MeasureString(txInput.Text, fnt);
g.Dispose();
Bitmap b=new Bitmap(mmj.Printer.LineWidth*8, (int)szText.Height);
g = Graphics.FromImage(b);
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
g.DrawString("hello", new Font(Font, FontSize), Brushes.Black, new RectangleF(0, 0, b.Width, b.Height));
g.DrawString(txInput.Text, fnt, Brushes.Black, new PointF(0,0));
g.Flush();
PrintBitmap(b);
g.Dispose();
b.Dispose();
}
private void BtLoadImage_Click(object sender, RoutedEventArgs e) {
private void BtPrintImage_Click(object sender, RoutedEventArgs e) {
logger.Debug("Loading image for print");
OpenFileDialog r = new OpenFileDialog {
Title="Select 1 (one) image file",
Multiselect=false,
Filter="PNG files (*.png)|*.png|JPEG files (*.jpe?g)|*.jpg *.jpeg|Jraphics Interchange Format files (*.gif)|*.gif|Bitte-Mappe files (*.bmp)|*.bmp|All of the above|*.jpg *.jpeg *.png *.gif *.bmp",
Filter="PNG files|*.png|JPEG files|*.jpg;*.jpeg|Jraphics Interchange Format files|*.gif|Bitte-Mappe files|*.bmp|All of the above|*.jpg;*.jpeg;*.png;*.gif;*.bmp",
AutoUpgradeEnabled=true
};
if (r.ShowDialog() == System.Windows.Forms.DialogResult.Cancel) {
@ -78,8 +98,8 @@ namespace paperangapp {
logger.Debug("Loaded image " + r.FileName);
r.Dispose();
logger.Debug("Disposed of dialog");
bimg=new Bitmap(_,
(mmj.Printer.LineWidth*8), (int)((double)(mmj.Printer.LineWidth*8)*(double)((double)_.Height/(double)_.Width)));
Bitmap bimg=new Bitmap(_, (mmj.Printer.LineWidth*8),
(int)((double)(mmj.Printer.LineWidth*8)*(double)((double)_.Height/(double)_.Width)));
logger.Debug("Loaded image as Bitmap");
_.Dispose();
logger.Debug("Disposed of Image");
@ -87,29 +107,28 @@ namespace paperangapp {
}
private void PrintBitmap(Bitmap bimg) {
bimg = CopyToBpp(bimg);
logger.Debug("Converted Bitmap to Bitmap with 1-bit colour depth");
//BitArray img = new BitArray(bimg.Height*96*8);
byte[] iimg = new byte[bimg.Height*mmj.Printer.LineWidth];
logger.Debug("Converted Bitmap to 1-bit");
int hSzImg=bimg.Height;
byte[] iimg = new byte[hSzImg*mmj.Printer.LineWidth];
byte[] img;
using(MemoryStream s = new MemoryStream()) {
bimg.Save(s, ImageFormat.Bmp);
img = s.ToArray();
}
logger.Debug("Got bitmap's bytes");
int startoffset=img.Length-(bimg.Height*mmj.Printer.LineWidth);
bimg.Dispose();
logger.Debug("Disposed of Bitmap");
int startoffset=img.Length-(hSzImg*mmj.Printer.LineWidth);
logger.Debug("Processing bytes with offset " + startoffset);
for(int h = 0; h < bimg.Height; h++) {
for(int h = 0; h < hSzImg; h++) {
for(int w = 0; w < mmj.Printer.LineWidth; w++) {
iimg[(mmj.Printer.LineWidth * (bimg.Height - 1 - h)) + (mmj.Printer.LineWidth - 1 - w)] = (byte)~
iimg[(mmj.Printer.LineWidth * (hSzImg - 1 - h)) + (mmj.Printer.LineWidth - 1 - w)] = (byte)~
(img[startoffset + (mmj.Printer.LineWidth * h) + (mmj.Printer.LineWidth - 1 - w)]);
}
}
logger.Debug("Have print data of length " + iimg.Length);
bimg.Dispose();
logger.Debug("Disposed of Bitmap");
logger.Debug($"Have {img.Length} bytes of print data ({mmj.Printer.LineWidth*8}x{hSzImg}@1bpp)");
mmj.PrintBytes(iimg, false);
logger.Debug("Feeding for 200ms");
mmj.Feed(200);
mmj.Feed(175);
}
static uint BitSwap1(uint x) => ((x & 0x55555555u) << 1) | ((x & (~0x55555555u)) >> 1);
static uint BitSwap2(uint x) => ((x & 0x33333333u) << 2) | ((x & (~0x33333333u)) >> 2);

View File

@ -25,9 +25,10 @@
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationRevision>2</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<PublishWizardCompleted>true</PublishWizardCompleted>
<BootstrapperEnabled>true</BootstrapperEnabled>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
@ -53,6 +54,18 @@
<Prefer32Bit>false</Prefer32Bit>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
</PropertyGroup>
<PropertyGroup>
<ManifestCertificateThumbprint>C9E8C53D36488D4F4CBA62BF7D60BC1DBE0648B1</ManifestCertificateThumbprint>
</PropertyGroup>
<PropertyGroup>
<ManifestKeyFile>paperangapp_TemporaryKey.pfx</ManifestKeyFile>
</PropertyGroup>
<PropertyGroup>
<GenerateManifests>true</GenerateManifests>
</PropertyGroup>
<PropertyGroup>
<SignManifests>true</SignManifests>
</PropertyGroup>
<ItemGroup>
<Reference Include="LibUsbDotNet.LibUsbDotNet, Version=2.2.0.0, Culture=neutral, PublicKeyToken=c677239abe1e02a9, processorArchitecture=MSIL">
<HintPath>..\packages\LibUsbDotNet.2.2.29\lib\net45\LibUsbDotNet.LibUsbDotNet.dll</HintPath>
@ -187,6 +200,7 @@
<LastGenOutput>Resources.Designer.cs</LastGenOutput>
</EmbeddedResource>
<None Include="packages.config" />
<None Include="paperangapp_TemporaryKey.pfx" />
<None Include="Properties\Settings.settings">
<Generator>SettingsSingleFileGenerator</Generator>
<LastGenOutput>Settings.Designer.cs</LastGenOutput>