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(); Handshake();
} }
public void Handshake() { public void Handshake() {
_ = Printer.WriteBytes( WriteBytes(
Transform.Packet( Transform.Packet(
BaseTypes.Operations.CrcTransmit, BaseTypes.Operations.CrcTransmit,
Crc.GetCrcIvBytes(), Crc.GetCrcIvBytes(),
@ -49,14 +49,26 @@ namespace libpaperang.Main {
Feed(0); Feed(0);
NoOp(); NoOp();
} }
public bool HandshakeAsync() {
Handshake();
return true;
}
public void WriteBytes(byte[] packet) { public void WriteBytes(byte[] packet) {
logger?.Trace($"Writing packet with length {packet.Length} to printer"); logger?.Trace($"Writing packet with length {packet.Length} to printer");
_ = Printer.WriteBytes(packet); _ = Printer.WriteBytes(packet);
} }
public bool WriteBytesAsync(byte[] packet) {
WriteBytes(packet);
return true;
}
public void WriteBytes(byte[] packet, int ms) { public void WriteBytes(byte[] packet, int ms) {
logger?.Trace($"Writing packet with length {packet.Length} to printer with delay of {ms}ms"); logger?.Trace($"Writing packet with length {packet.Length} to printer with delay of {ms}ms");
_ = Printer.WriteBytes(packet, ms); _ = Printer.WriteBytes(packet, ms);
} }
public bool WriteBytesAsync(byte[] packet, int ms) {
WriteBytes(packet, ms);
return true;
}
public void Feed(uint ms) { public void Feed(uint ms) {
logger?.Trace($"Feeding for {ms}ms"); logger?.Trace($"Feeding for {ms}ms");
WriteBytes( WriteBytes(
@ -64,6 +76,10 @@ namespace libpaperang.Main {
Transform.Arg(BaseTypes.Operations.LineFeed, ms), Transform.Arg(BaseTypes.Operations.LineFeed, ms),
Crc)); Crc));
} }
public bool FeedAsync(uint ms) {
Feed(ms);
return true;
}
public void NoOp() => WriteBytes( public void NoOp() => WriteBytes(
Transform.Packet(BaseTypes.Operations.NoOp, new byte[] { 0, 0 }, Crc)); Transform.Packet(BaseTypes.Operations.NoOp, new byte[] { 0, 0 }, Crc));
public void Poll() { public void Poll() {
@ -71,6 +87,10 @@ namespace libpaperang.Main {
Feed(0); Feed(0);
NoOp(); NoOp();
} }
public bool PollAsync() {
Poll();
return true;
}
public void PrintBytes(byte[] data, bool autofeed = true) { public void PrintBytes(byte[] data, bool autofeed = true) {
logger?.Trace($"PrintBytes() invoked with data length of {data.Length}"); logger?.Trace($"PrintBytes() invoked with data length of {data.Length}");
List<byte[]> segments = data List<byte[]> segments = data
@ -88,5 +108,9 @@ namespace libpaperang.Main {
if(autofeed) if(autofeed)
Feed(185); 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:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:paperangapp" xmlns:local="clr-namespace:paperangapp"
mc:Ignorable="d" 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 x:Name="gMain">
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="225*"/> <RowDefinition Height="210"/>
<RowDefinition Height="225*"/> <RowDefinition/>
</Grid.RowDefinitions> </Grid.RowDefinitions>
<GroupBox Header="libpaperang Library Tester" Margin="2,0,2,0" Grid.Row="0" Grid.Column="0"> <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 Margin="0,0,0,0" Grid.Column="0" Grid.ColumnSpan="3">
@ -45,12 +45,11 @@
</GroupBox> </GroupBox>
</Grid> </Grid>
</GroupBox> </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> <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"/> <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="0,0,0,0" Click="BtClearLog_Click" Height="21" VerticalAlignment="Bottom"/> <Button x:Name="btClearLog" Content="Clear Log" MinWidth="75" Margin="2,0,2,0" Click="BtClearLog_Click" Height="21" VerticalAlignment="Bottom"/>
</Grid> </Grid>
</Expander> </Expander>
</Grid> </Grid>
</Window> </Window>

View File

@ -2,6 +2,7 @@
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging; using System.Drawing.Imaging;
using System.IO; using System.IO;
using System.Threading.Tasks;
using System.Timers; using System.Timers;
using System.Windows; using System.Windows;
using System.Windows.Forms; using System.Windows.Forms;
@ -113,15 +114,19 @@ namespace paperangapp {
} catch(Exception) { } catch(Exception) {
} finally { } } finally { }
} }
private void BtFeed_Click(object sender, RoutedEventArgs e) => mmj.Feed((uint)slFeedTime.Value); private async void BtFeed_Click(object sender, RoutedEventArgs e) => await FeedAsync((uint)slFeedTime.Value);
private void BtPrintText_Click(object sender, RoutedEventArgs e) { 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)) { if(!(txInput.Text.Length > 0)) {
logger.Warn("PrintText event but nothing to print."); logger.Warn("PrintText event but nothing to print.");
return; 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)); 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); //SizeF szText=g.MeasureString(txInput.Text, fnt);
g.Dispose(); g.Dispose();
Bitmap b=new Bitmap(mmj.Printer.LineWidth*8, (int)szText.Height); 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.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SingleBitPerPixelGridFit;
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
g.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.HighQuality; 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(); g.Flush();
PrintBitmap(b); await Task.Run(() => PrintBitmap(b));
g.Dispose(); g.Dispose();
b.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."); logger.Debug("Invoking file selection dialogue.");
OpenFileDialog r = new OpenFileDialog { OpenFileDialog r = new OpenFileDialog {
Title="Select 1 (one) image file", Title="Select 1 (one) image file",
@ -149,20 +155,24 @@ namespace paperangapp {
r.Dispose(); r.Dispose();
return; return;
} }
logger.Debug("Loading image for print"); string fn=r.FileName;
Image _=Image.FromFile(r.FileName);
logger.Debug("Loaded image " + r.FileName);
r.Dispose(); r.Dispose();
logger.Debug("Disposed of dialog"); Bitmap bmg = await Task.Run(() => {
Bitmap bimg=new Bitmap(_, mmj.Printer.LineWidth*8, 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))); (int)(mmj.Printer.LineWidth*8*(double)((double)_.Height/(double)_.Width)));
logger.Debug("Loaded image as Bitmap"); logger.Debug("Loaded image as Bitmap");
_.Dispose(); _.Dispose();
logger.Debug("Disposed of Image"); logger.Debug("Disposed of Image");
PrintBitmap(bimg); return bimg;
bimg.Dispose(); });
await Task.Run(() => PrintBitmap(bmg));
bmg.Dispose();
} }
private void PrintBitmap(Bitmap bimg) { private async Task PrintBitmap(Bitmap bimg) {
bimg = CopyToBpp(bimg); bimg = CopyToBpp(bimg);
logger.Debug("Converted Bitmap to 1-bit"); logger.Debug("Converted Bitmap to 1-bit");
int hSzImg=bimg.Height; 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)"); 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 #region GDIBitmap1bpp
static Bitmap CopyToBpp(Bitmap b) { static Bitmap CopyToBpp(Bitmap b) {

View File

@ -14,18 +14,21 @@
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic> <Deterministic>true</Deterministic>
<IsWebBootstrapper>false</IsWebBootstrapper> <IsWebBootstrapper>true</IsWebBootstrapper>
<PublishUrl>publish\</PublishUrl> <PublishUrl>c:\swdep\paperang\</PublishUrl>
<Install>true</Install> <Install>true</Install>
<InstallFrom>Disk</InstallFrom> <InstallFrom>Web</InstallFrom>
<UpdateEnabled>false</UpdateEnabled> <UpdateEnabled>true</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode> <UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval> <UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits> <UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically> <UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired> <UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions> <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> <ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust> <UseApplicationTrust>false</UseApplicationTrust>
<PublishWizardCompleted>true</PublishWizardCompleted> <PublishWizardCompleted>true</PublishWizardCompleted>