Adding a new bluetooth device.

This commit is contained in:
Sergiotarxz 2024-10-11 18:43:49 +02:00
parent 97c55c6198
commit 34747d1960
2 changed files with 74 additions and 1 deletions

View File

@ -0,0 +1,72 @@
package Exd::DeviceToBluetooth;
use v5.40.0;
use strict;
use warnings;
use Moo;
use GD::Image;
use Path::Tiny;
use Printer::ESCPOS;
use Exd::DeviceToRawFile;
use Net::Bluetooth;
has address => (
is => 'ro',
required => 1,
);
has port => (
is => 'ro',
required => 1,
);
has _guts_device => ( is => 'rw' );
has _tempdir => ( is => 'rw', );
sub _device($self) {
if ( !defined $self->_guts_device ) {
$self->_tempdir( Path::Tiny->tempdir );
$self->_guts_device(
Exd::DeviceToRawFile->new(
output_file => $self->_tempfile,
)
);
}
return $self->_guts_device;
}
sub _tempfile($self) {
my $tempdir = $self->_tempdir;
return $tempdir->child('tmp.escpos');
}
sub _build__guts_device($self) {
my $device = Printer::ESCPOS->new(
driverType => 'File',
deviceFilePath => $self->output_file,
);
}
sub image( $self, $image ) {
$self->_device->image($image);
}
sub lf($self) {
$self->_device->lf;
}
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 $fh = $obj->perlfh;
print $fh path( $self->_tempfile )->slurp_raw;
}
1;

View File

@ -8,10 +8,11 @@ use warnings;
use Exd::Printer;
use Exd::DeviceToImage;
use Exd::DeviceToRawFile;
use Exd::DeviceToBluetooth;
use Exd::Utils;
{
my $device = Exd::DeviceToImage->new( output_file => 'a.png' );
my $device = Exd::DeviceToBluetooth->new( address => '5A:4A:AE:8C:E9:D2', port => 1 );
my $printer = Exd::Printer->new( device => $device );
my $hoz = Exd::Utils::get_gd_image('scripts/hoz.jpg');