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' );
}
sub try_to_connect($self) {
my $device_api = $self->_device;
sub connect_if_disconnected($self) {
if ( !$self->_device->Connected ) {
say 'Disconnected, trying to connect';
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;
}
$self->_adapter->StartDiscovery;
$self->_adapter->SetDiscoveryFilter(
{ Transport => Net::DBus::dbus_string('le') } );
$self->_device->Pair;
eval { $self->_device->Connect; };
};
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 );
$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 $buffer = '';
my $y = 0;
$self->_y($y);
my $buffer = '';
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,6 +302,11 @@ sub _needs_to_pause($self) {
}
sub _send_command( $self, $data ) {
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 ];
@ -314,6 +315,13 @@ sub _send_command( $self, $data ) {
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 = '' ) {