File Coverage

blib/lib/Printer/HP/Display.pm
Criterion Covered Total %
statement 15 50 30.0
branch 0 8 0.0
condition n/a
subroutine 5 10 50.0
pod 4 4 100.0
total 24 72 33.3


line stmt bran cond sub pod time code
1             package Printer::HP::Display;
2              
3 1     1   26424 use warnings;
  1         12  
  1         28  
4 1     1   4 use strict;
  1         2  
  1         29  
5 1     1   632530 use Encode;
  1         90670  
  1         126  
6 1     1   403315 use IO::Socket::INET;
  1         32632  
  1         10  
7              
8             use constant {
9 1         641 PJL_PORT => 9100,
10             ESC => "\033",
11 1     1   940 };
  1         2  
12              
13             =head1 NAME
14              
15             Printer::HP::Display - Change the default ready message on your HP laser printer
16              
17             =head1 VERSION
18              
19             Version 0.01
20              
21             =cut
22              
23             our $VERSION = '0.01';
24              
25              
26             =head1 SYNOPSIS
27              
28             This module allows you to change the value of the ready message (usually 'Ready') on the tiny LCD display that practically all HP laser printers have. You can also retrieve the value of the currently set message. The module communicates with the printer using Printer Job Language (PJL). See: http://en.wikipedia.org/wiki/Printer_Job_Language
29              
30             At the moment this module is just a fun project; somewhat on the lines of ACME::LOLCAT. For example, at Cricinfo we use it to show cricket scores on our printer screen (http://twitpic.com/26yt2d). You should be careful with what you do to the printers at your office - not all IT managers have a funny bone :-).
31              
32             Here's how you'd use it in you code:
33              
34             use Printer::HP::Display;
35              
36             my $printer_ip = "192.168.0.1";
37             my $printer = Printer::HP::Display->new($printer_ip);
38              
39             my $message = "I am ready. Are you?";
40              
41             $printer->set_display($message);
42              
43             print $printer->get_display; #currently set message
44             print $printer->get_status; #complete dump of PJL INFO STATUS command
45              
46             =head1 SUBROUTINES/METHODS
47              
48             =head2 new()
49              
50             Create a Printer::HP::Display object.
51              
52             =cut
53              
54             sub new {
55 0 0   0 1   die 'Usage: Printer::HP::Display->new($printer_host_or_ip)' unless $#_ == 1;
56              
57 0           my $class = shift;
58 0           my ($host) = @_;
59 0           bless { _host => $host }, $class;
60             }
61              
62             =head2 set_display($message)
63              
64             Set the ready message on the printer's display to something of your choice. The string must be pure ASCII - you'll get ? in place of characters that are not ASCII. At the moment set_display doesn't check the length of the string. Anything between 20-50 is a good idea but check your printer's display and tweak accordingly. Some models will truncate the string to fit the available space others will simply refuse to set it.
65              
66             =cut
67              
68             sub set_display {
69 0 0   0 1   die 'Usage: $obj->set_display("string")' unless $#_ == 1;
70              
71 0           my $self = shift;
72 0           my ($message) = @_;
73              
74 0           my $send_string = ESC . '%-12345X@PJL RDYMSG DISPLAY = "' . $message . "\"\r\n";
75 0           $send_string = $send_string . ESC . '%-12345X' . "\r\n";
76              
77 0           my $printer_string = encode("ascii", $send_string);
78              
79 0           my $sock = _socket($self->{_host});
80 0           $sock->send($printer_string);
81 0           $sock->close;
82             }
83              
84             =head2 get_display()
85              
86             Get the currently set ready message.
87              
88             =cut
89              
90             sub get_display {
91              
92 0     0 1   my $self = shift;
93 0           my @status = $self->get_status;
94              
95 0           my $display = "";
96            
97 0           for my $status (@status) {
98 0 0         if($status =~ /DISPLAY=\"(.*)\"/g) {
99 0           $display = $1;
100 0           last;
101             }
102             }
103              
104 0           return $display;
105             }
106              
107             =head2 get_status()
108              
109             Get a raw dump of the PJL INFO STATUS command. Returns an array with one element per line of message received from the printer.
110              
111             =cut
112              
113             sub get_status {
114              
115 0     0 1   my $self = shift;
116 0           my $send_string = "\@PJL INFO STATUS\r\n";
117              
118 0           my $printer_string = encode("ascii", $send_string);
119              
120 0           my $sock = _socket($self->{_host});
121 0           $sock->send($printer_string);
122            
123 0           my @status = ();
124              
125 0           for (0..3) {
126 0           my $read = <$sock>;
127 0           push @status, $read;
128             }
129              
130 0           $sock->close;
131              
132 0           return @status;
133             }
134              
135             sub _socket {
136 0     0     my $host = shift;
137              
138 0 0         my $sock = IO::Socket::INET->new(
139             PeerAddr => $host,
140             PeerPort => PJL_PORT,
141             Proto => 'tcp'
142             ) or die $!;
143              
144 0           return $sock;
145             }
146             =head1 AUTHOR
147              
148             Deepak Gulati, C<< >>
149              
150             =head1 BUGS
151              
152             Please report any bugs or feature requests to C, or through
153             the web interface at L. I will be notified, and then you'll
154             automatically be notified of progress on your bug as I make changes.
155              
156              
157              
158              
159             =head1 SUPPORT
160              
161             You can find documentation for this module with the perldoc command.
162              
163             perldoc Printer::HP::Display
164              
165              
166             You can also look for information at:
167              
168             =over 4
169              
170             =item * RT: CPAN's request tracker
171              
172             L
173              
174             =item * AnnoCPAN: Annotated CPAN documentation
175              
176             L
177              
178             =item * CPAN Ratings
179              
180             L
181              
182             =item * Search CPAN
183              
184             L
185              
186             =back
187              
188              
189             =head1 ACKNOWLEDGEMENTS
190              
191             Inspired by Scott Allen's article and C# code at: http://odetocode.com/humor/68.aspx
192              
193             =head1 LICENSE AND COPYRIGHT
194              
195             Copyright 2010 Deepak Gulati.
196              
197             This program is free software; you can redistribute it and/or modify it
198             under the terms of either: the GNU General Public License as published
199             by the Free Software Foundation; or the Artistic License.
200              
201             See http://dev.perl.org/licenses/ for more information.
202              
203              
204             =cut
205              
206             1; # End of Printer::HP::Display