Fixing bugs with cat printers again.

This commit is contained in:
Sergiotarxz 2024-11-02 21:02:09 +01:00
parent 5c8903909a
commit d8a2560500

View File

@ -87,43 +87,29 @@ sub _build__properties($self) {
return $service->get_object( $self->rx, 'org.freedesktop.DBus.Properties' ); return $service->get_object( $self->rx, 'org.freedesktop.DBus.Properties' );
} }
sub try_to_connect($self) { sub connect_if_disconnected($self) {
my $device_api = $self->_device; if ( !$self->_device->Connected ) {
eval { say 'Disconnected, trying to connect';
eval { $device_api->Disconnect; }; eval {
$device_api->Pair; $self->_adapter->StartDiscovery;
$device_api->Connect; $self->_adapter->SetDiscoveryFilter(
for ( my $i = 0 ; $i < 3 ; $i++ ) { { Transport => Net::DBus::dbus_string('le') } );
return if $device_api->Connected; $self->_device->Pair;
sleep 1; 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) { sub print($self) {
my $image = $self->current_image; my $image = $self->current_image;
my $remaining_height = $image->height;
$self->_remaining_height( $image->height );
$self->_device->get_service->get_bus->timeout(1000); $self->_device->get_service->get_bus->timeout(1000);
$self->connect_if_disconnected;
sleep 1 if !$self->_device->Connected; sleep 1 if !$self->_device->Connected;
my $reactor = Net::DBus::Reactor->main; my $reactor = Net::DBus::Reactor->main;
$self->_reactor($reactor); $self->_reactor($reactor);
$self->_rx_gatt->StartNotify; $self->_rx_gatt->StartNotify;
usleep 100_000;
say $self->rx; say $self->rx;
$self->_signal( $self->_signal(
$self->_properties->connect_to_signal( $self->_properties->connect_to_signal(
@ -146,19 +132,29 @@ sub print($self) {
{ {
$self->_pause(1); $self->_pause(1);
} }
if ( '5178a3010300000e280eff' eq unpack 'H*',
$self->_bytestring(@$value) )
{
usleep 100_000;
}
} }
) )
); );
my $y = 0;
$self->_y($y);
my $buffer = ''; my $buffer = '';
my $y = 0;
$self->_y($y);
my $remaining_height = $image->height;
my $mtu = 200;
while ( $remaining_height > 0 ) { while ( $remaining_height > 0 ) {
my $height = $remaining_height > 500 ? 500 : $remaining_height; my $height = $remaining_height > 500 ? 500 : $remaining_height;
$remaining_height -= $height; $remaining_height -= $height;
$self->_remaining_height($remaining_height);
my $print_image = GD::Image->new( $image->width, $height ); my $print_image = GD::Image->new( $image->width, $height );
$print_image->copy( $image, 0, 0, 0, $y, $image->width, $height ); $print_image->copy( $image, 0, 0, 0, $y, $image->width, $height );
$y += $height; $y += $height;
$self->_y($y);
my $tempdir = Path::Tiny->tempdir; my $tempdir = Path::Tiny->tempdir;
my $in = $tempdir->child('in.png'); my $in = $tempdir->child('in.png');
my $out = $tempdir->child('out.bmp'); my $out = $tempdir->child('out.bmp');
@ -201,10 +197,10 @@ sub print($self) {
0x00, 0x00, 0x00, 0x00, 0x17 0x00, 0x00, 0x00, 0x00, 0x17
); );
} }
my $mtu = 200;
$self->_flush( \$buffer, $mtu ); $self->_flush( \$buffer, $mtu );
$self->_reactor->run; $self->_reactor->run;
$self->_rx_gatt->StopNotify; $self->_rx_gatt->StopNotify;
} }
sub _reverse_byte( $self, $i ) { sub _reverse_byte( $self, $i ) {
@ -280,7 +276,7 @@ sub _flush( $self, $buffer, $mtu ) {
return 1; return 1;
}; };
$self->_reactor->add_timeout( $self->_reactor->add_timeout(
10, 100,
Net::DBus::Callback->new( Net::DBus::Callback->new(
method => sub { method => sub {
$callback->(); $callback->();
@ -306,14 +302,26 @@ sub _needs_to_pause($self) {
} }
sub _send_command( $self, $data ) { sub _send_command( $self, $data ) {
$self->connect_if_disconnected; my $error = 1;
my $gatt = $self->_tx_gatt; my $i = 0;
$data = [ map { unpack 'C', $_ } split '', $data ]; while ($error) {
$data = [ map { Net::DBus::dbus_byte($_) } @$data ]; die 'Unable to connect' if $i++ == 50;
$gatt->WriteValue( eval {
Net::DBus::dbus_array($data), $self->connect_if_disconnected;
{ type => Net::DBus::dbus_string('command') }, 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 = '' ) { sub _make_command( $self, $command_bit, $payload, $prefix = '', $suffix = '' ) {