From d8a2560500c4a1580bc8ee1a0a4b31304ff7b099 Mon Sep 17 00:00:00 2001 From: Sergiotarxz Date: Sat, 2 Nov 2024 21:02:09 +0100 Subject: [PATCH] Fixing bugs with cat printers again. --- lib/Exd/DeviceToCatPrinter.pm | 86 +++++++++++++++++++---------------- 1 file changed, 47 insertions(+), 39 deletions(-) diff --git a/lib/Exd/DeviceToCatPrinter.pm b/lib/Exd/DeviceToCatPrinter.pm index 6eb8087..77daf5f 100644 --- a/lib/Exd/DeviceToCatPrinter.pm +++ b/lib/Exd/DeviceToCatPrinter.pm @@ -87,43 +87,29 @@ sub _build__properties($self) { return $service->get_object( $self->rx, 'org.freedesktop.DBus.Properties' ); } -sub try_to_connect($self) { - my $device_api = $self->_device; - eval { - eval { $device_api->Disconnect; }; - $device_api->Pair; - $device_api->Connect; - for ( my $i = 0 ; $i < 3 ; $i++ ) { - return if $device_api->Connected; - sleep 1; +sub connect_if_disconnected($self) { + if ( !$self->_device->Connected ) { + say 'Disconnected, trying to connect'; + eval { + $self->_adapter->StartDiscovery; + $self->_adapter->SetDiscoveryFilter( + { Transport => Net::DBus::dbus_string('le') } ); + $self->_device->Pair; + eval { $self->_device->Connect; }; + }; + if ($@) { + warn $@; } - }; - if ($@) { - warn $@; } } -sub connect_if_disconnected($self) { - eval { - $self->_adapter->StartDiscovery; - $self->_adapter->SetDiscoveryFilter( - { Transport => Net::DBus::dbus_string('le') } ); - $self->_device->Disconnect; - $self->_device->Pair; - } if !$self->_device->Connected; -} - sub print($self) { - my $image = $self->current_image; - my $remaining_height = $image->height; - $self->_remaining_height( $image->height ); + my $image = $self->current_image; $self->_device->get_service->get_bus->timeout(1000); - $self->connect_if_disconnected; sleep 1 if !$self->_device->Connected; my $reactor = Net::DBus::Reactor->main; $self->_reactor($reactor); $self->_rx_gatt->StartNotify; - usleep 100_000; say $self->rx; $self->_signal( $self->_properties->connect_to_signal( @@ -146,19 +132,29 @@ sub print($self) { { $self->_pause(1); } + if ( '5178a3010300000e280eff' eq unpack 'H*', + $self->_bytestring(@$value) ) + { + usleep 100_000; + } } ) ); - my $y = 0; - $self->_y($y); my $buffer = ''; + my $y = 0; + $self->_y($y); + my $remaining_height = $image->height; + my $mtu = 200; while ( $remaining_height > 0 ) { my $height = $remaining_height > 500 ? 500 : $remaining_height; $remaining_height -= $height; + $self->_remaining_height($remaining_height); my $print_image = GD::Image->new( $image->width, $height ); $print_image->copy( $image, 0, 0, 0, $y, $image->width, $height ); $y += $height; + $self->_y($y); + my $tempdir = Path::Tiny->tempdir; my $in = $tempdir->child('in.png'); my $out = $tempdir->child('out.bmp'); @@ -201,10 +197,10 @@ sub print($self) { 0x00, 0x00, 0x00, 0x00, 0x17 ); } - my $mtu = 200; $self->_flush( \$buffer, $mtu ); $self->_reactor->run; $self->_rx_gatt->StopNotify; + } sub _reverse_byte( $self, $i ) { @@ -280,7 +276,7 @@ sub _flush( $self, $buffer, $mtu ) { return 1; }; $self->_reactor->add_timeout( - 10, + 100, Net::DBus::Callback->new( method => sub { $callback->(); @@ -306,14 +302,26 @@ sub _needs_to_pause($self) { } sub _send_command( $self, $data ) { - $self->connect_if_disconnected; - my $gatt = $self->_tx_gatt; - $data = [ map { unpack 'C', $_ } split '', $data ]; - $data = [ map { Net::DBus::dbus_byte($_) } @$data ]; - $gatt->WriteValue( - Net::DBus::dbus_array($data), - { type => Net::DBus::dbus_string('command') }, - ); + my $error = 1; + my $i = 0; + while ($error) { + die 'Unable to connect' if $i++ == 50; + eval { + $self->connect_if_disconnected; + my $gatt = $self->_tx_gatt; + $data = [ map { unpack 'C', $_ } split '', $data ]; + $data = [ map { Net::DBus::dbus_byte($_) } @$data ]; + $gatt->WriteValue( + Net::DBus::dbus_array($data), + { type => Net::DBus::dbus_string('command') }, + ); + $error = 0; + }; + if ($@) { + warn $@; + } + usleep 100_000 if $error; + } } sub _make_command( $self, $command_bit, $payload, $prefix = '', $suffix = '' ) {