diff --git a/lib/Exd/DeviceToBluetooth.pm b/lib/Exd/DeviceToBluetooth.pm index 17daff7..7949660 100644 --- a/lib/Exd/DeviceToBluetooth.pm +++ b/lib/Exd/DeviceToBluetooth.pm @@ -27,6 +27,8 @@ has _guts_device => ( is => 'rw' ); has _tempdir => ( is => 'rw', ); +has _obj => (is => 'lazy'); + sub _device($self) { if ( !defined $self->_guts_device ) { $self->_tempdir( Path::Tiny->tempdir ); @@ -59,14 +61,28 @@ sub lf($self) { $self->_device->lf; } +sub _build__obj($self) { + my $obj = Net::Bluetooth->newsocket('RFCOMM'); + $self->_try_to_connect($obj); + return $obj; +} + sub print($self) { $self->_device->print; $self->_guts_device(undef); - my $obj = Net::Bluetooth->newsocket('RFCOMM'); - if ( 0 != $obj->connect( $self->address, $self->port ) ) { - die 'Unable to connect to bluetooth'; - } + my $obj = $self->_obj; my $fh = $obj->perlfh; print $fh path( $self->_tempfile )->slurp_raw; } + +sub _try_to_connect($self, $obj, $retries = 3) { + if ( 0 == $obj->connect( $self->address, $self->port ) ) { + return; + } + if ($retries == 0) { + die 'Unable to connect to bluetooth'; + } + sleep 1; + return $self->_try_to_connect($obj, $retries - 1); +} 1; diff --git a/lib/Exd/Printer.pm b/lib/Exd/Printer.pm index 17457fc..8e781c7 100644 --- a/lib/Exd/Printer.pm +++ b/lib/Exd/Printer.pm @@ -329,7 +329,6 @@ sub print_text( $self, $text, $font_size ) { my $scale_h = $font_size / $ori_height; $cr->save; $cr->scale( $scale_w, $scale_h ); - say ((($metrics->get_ascent / 1024 ))); $cr->set_source_surface( $image, $x / $scale_w, diff --git a/scripts/demo.pl b/scripts/demo.pl index 0c64acd..55c037c 100644 --- a/scripts/demo.pl +++ b/scripts/demo.pl @@ -13,17 +13,30 @@ use Exd::Utils; { # my $device = Exd::DeviceToBluetooth->new( address => '5A:4A:AE:8C:E9:D2', port => 1 ); - my $device = Exd::DeviceToImage->new( output_file => 'a.png' ); + my $device = + Exd::DeviceToImage->new( output_file => 'a.png' ); my $printer = Exd::Printer->new( device => $device ); - $printer->font('Z003 Medium Italic'); + for my $font ( + 'Z003 Medium Italic', + 'Noto Sans CJK JP', + 'Shadow Into Light Regular' + ) + { + $printer->font($font); - my $hoz = Exd::Utils::get_gd_image('scripts/hoz.jpg'); + my $hoz = Exd::Utils::get_gd_image('scripts/hoz.jpg'); - $printer->print_text( - [ $hoz, 'The best software belongs to everybody', $hoz ], 40 ); - $printer->image('scripts/hoz.jpg'); - $printer->print_n_lf(4); + $printer->print_text( + [ + $hoz, ' The best software ' , $hoz , ' belongs to everybody ', + $hoz + ], + 30 + ); + $printer->image('scripts/hoz.jpg'); + $printer->print_n_lf(4); - $printer->print; + $printer->print; + } }