GUI app is now responsive during print operations

This commit is contained in:
Maff 2019-10-04 13:39:41 +01:00
parent 471c9e5165
commit 1afaea4f16
4 changed files with 68 additions and 32 deletions

View File

@ -40,7 +40,7 @@ namespace libpaperang.Main {
Handshake();
}
public void Handshake() {
_ = Printer.WriteBytes(
WriteBytes(
Transform.Packet(
BaseTypes.Operations.CrcTransmit,
Crc.GetCrcIvBytes(),
@ -49,14 +49,26 @@ namespace libpaperang.Main {
Feed(0);
NoOp();
}
public bool HandshakeAsync() {
Handshake();
return true;
}
public void WriteBytes(byte[] packet) {
logger?.Trace($"Writing packet with length {packet.Length} to printer");
_ = Printer.WriteBytes(packet);
}
public bool WriteBytesAsync(byte[] packet) {
WriteBytes(packet);
return true;
}
public void WriteBytes(byte[] packet, int ms) {
logger?.Trace($"Writing packet with length {packet.Length} to printer with delay of {ms}ms");
_ = Printer.WriteBytes(packet, ms);
}
public bool WriteBytesAsync(byte[] packet, int ms) {
WriteBytes(packet, ms);
return true;
}
public void Feed(uint ms) {
logger?.Trace($"Feeding for {ms}ms");
WriteBytes(
@ -64,6 +76,10 @@ namespace libpaperang.Main {
Transform.Arg(BaseTypes.Operations.LineFeed, ms),
Crc));
}
public bool FeedAsync(uint ms) {
Feed(ms);
return true;
}
public void NoOp() => WriteBytes(
Transform.Packet(BaseTypes.Operations.NoOp, new byte[] { 0, 0 }, Crc));
public void Poll() {
@ -71,6 +87,10 @@ namespace libpaperang.Main {
Feed(0);
NoOp();
}
public bool PollAsync() {
Poll();
return true;
}
public void PrintBytes(byte[] data, bool autofeed = true) {
logger?.Trace($"PrintBytes() invoked with data length of {data.Length}");
List<byte[]> segments = data
@ -88,5 +108,9 @@ namespace libpaperang.Main {
if(autofeed)
Feed(185);
}
public bool PrintBytesAsync(byte[] data, bool autofeed=true) {
PrintBytes(data, autofeed);
return true;
}
}
}

View File

@ -5,11 +5,11 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:paperangapp"
mc:Ignorable="d"
Title="libpaperang Test Utility" Height="450" Width="800" ResizeMode="NoResize">
Title="libpaperang Test Utility" Height="450" Width="800" ResizeMode="CanMinimize" SizeToContent="Height">
<Grid x:Name="gMain">
<Grid.RowDefinitions>
<RowDefinition Height="225*"/>
<RowDefinition Height="225*"/>
<RowDefinition Height="210"/>
<RowDefinition/>
</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">
@ -45,12 +45,11 @@
</GroupBox>
</Grid>
</GroupBox>
<Expander x:Name="expander" Header="Log" Margin="2,2,2,2" Grid.Row="1" IsExpanded="True">
<Expander x:Name="expander" Header="Log" Margin="2,2,2,2" Grid.Row="1">
<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"/>
<TextBox x:Name="tbLog" Margin="2,2,2,23" MaxHeight="280" 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="2,0,2,0" Click="BtClearLog_Click" Height="21" VerticalAlignment="Bottom"/>
</Grid>
</Expander>
</Grid>
</Window>

View File

@ -2,6 +2,7 @@
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Forms;
@ -113,15 +114,19 @@ namespace paperangapp {
} catch(Exception) {
} finally { }
}
private void BtFeed_Click(object sender, RoutedEventArgs e) => mmj.Feed((uint)slFeedTime.Value);
private void BtPrintText_Click(object sender, RoutedEventArgs e) {
private async void BtFeed_Click(object sender, RoutedEventArgs e) => await FeedAsync((uint)slFeedTime.Value);
private async Task FeedAsync(uint time) => await Task.Run(() => mmj.Feed(time));
private async void BtPrintText_Click(object sender, RoutedEventArgs e) {
if(!(txInput.Text.Length > 0)) {
logger.Warn("PrintText event but nothing to print.");
return;
}
Font fnt=new Font(txFont.Text, int.Parse(txSzF.Text));
await PrintTextAsync(txInput.Text, txFont.Text, int.Parse(txSzF.Text));
}
private async Task PrintTextAsync(string text, string font, int szf) {
Font fnt=new Font(font, szf);
Graphics g=Graphics.FromImage(new Bitmap(mmj.Printer.LineWidth*8, 1));
System.Drawing.Size szText = TextRenderer.MeasureText(g, txInput.Text, fnt);
System.Drawing.Size szText = TextRenderer.MeasureText(g, text, fnt);
//SizeF szText=g.MeasureString(txInput.Text, fnt);
g.Dispose();
Bitmap b=new Bitmap(mmj.Printer.LineWidth*8, (int)szText.Height);
@ -130,13 +135,14 @@ namespace paperangapp {
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality;
TextRenderer.DrawText(g, txInput.Text, fnt, new System.Drawing.Point(0, 0), Color.Black);
TextRenderer.DrawText(g, text, fnt, new System.Drawing.Point(0, 0), Color.Black);
g.Flush();
PrintBitmap(b);
await Task.Run(() => PrintBitmap(b));
g.Dispose();
b.Dispose();
}
private void BtPrintImage_Click(object sender, RoutedEventArgs e) {
private async void BtPrintImage_Click(object sender, RoutedEventArgs e) => await PrintImageAsync();
private async Task PrintImageAsync() {
logger.Debug("Invoking file selection dialogue.");
OpenFileDialog r = new OpenFileDialog {
Title="Select 1 (one) image file",
@ -149,20 +155,24 @@ namespace paperangapp {
r.Dispose();
return;
}
logger.Debug("Loading image for print");
Image _=Image.FromFile(r.FileName);
logger.Debug("Loaded image " + r.FileName);
string fn=r.FileName;
r.Dispose();
logger.Debug("Disposed of dialog");
Bitmap bimg=new Bitmap(_, mmj.Printer.LineWidth*8,
Bitmap bmg = await Task.Run(() => {
logger.Debug($"Loading image '{fn}' for print");
Image _=Image.FromFile(fn);
logger.Debug($"Loaded image '{fn}'");
logger.Debug("Disposed of dialog");
Bitmap bimg=new Bitmap(_, mmj.Printer.LineWidth*8,
(int)(mmj.Printer.LineWidth*8*(double)((double)_.Height/(double)_.Width)));
logger.Debug("Loaded image as Bitmap");
_.Dispose();
logger.Debug("Disposed of Image");
PrintBitmap(bimg);
bimg.Dispose();
logger.Debug("Loaded image as Bitmap");
_.Dispose();
logger.Debug("Disposed of Image");
return bimg;
});
await Task.Run(() => PrintBitmap(bmg));
bmg.Dispose();
}
private void PrintBitmap(Bitmap bimg) {
private async Task PrintBitmap(Bitmap bimg) {
bimg = CopyToBpp(bimg);
logger.Debug("Converted Bitmap to 1-bit");
int hSzImg=bimg.Height;
@ -184,7 +194,7 @@ namespace paperangapp {
}
}
logger.Debug($"Have {img.Length} bytes of print data ({mmj.Printer.LineWidth * 8}x{hSzImg}@1bpp)");
mmj.PrintBytes(iimg, false);
await Task.Run(() => mmj.PrintBytes(iimg, false));
}
#region GDIBitmap1bpp
static Bitmap CopyToBpp(Bitmap b) {

View File

@ -14,18 +14,21 @@
<WarningLevel>4</WarningLevel>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
<IsWebBootstrapper>false</IsWebBootstrapper>
<PublishUrl>publish\</PublishUrl>
<IsWebBootstrapper>true</IsWebBootstrapper>
<PublishUrl>c:\swdep\paperang\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<InstallFrom>Web</InstallFrom>
<UpdateEnabled>true</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>2</ApplicationRevision>
<InstallUrl>https://swdist.ext.maff.scot/paperang/</InstallUrl>
<CreateWebPageOnPublish>true</CreateWebPageOnPublish>
<WebPage>publish.htm</WebPage>
<ApplicationRevision>4</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<PublishWizardCompleted>true</PublishWizardCompleted>