blib/lib/Device/MatrixOrbital/GLK.pm

filter: Statementstmt 11.2% 21 / 186 Branchbran 0.0% 0 / 140 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 18.1% 6 / 33 Podpod 92.0% 23 / 25 totaltotal 12.7% 50 / 393 CC 74 SCAR 70.0
CC
74
Cov
6%
get_lcd_type
2352.0
new
182.0
get_lcd_dimensions
56.0
CRAP 1092.9
↑ Summary
1 package Device::MatrixOrbital::GLK;
2  
3 ################
4 #
5 # Perl module for controling the Matrix Orbital graphic LCD displays
6 #
7 # Nicholas J Humfrey
8 # njh@cpan.org
9 #
10  
11 1 ▶use Device::SerialPort;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Params::Util qw(_SCALAR _POSINT);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use Time::HiRes qw(sleep alarm);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 1 ▶use vars qw/$VERSION @ISA/;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 1 @ISA = "Device::SerialPort";
19 1 $VERSION="0.01";
20  
21  
22  
23 0 ▶sub new {
Subroutine new: not called CC=13 SCAR=52.0
Pod documented
24 0 my $class = shift;
25 0 ▶ my $port = shift || '/dev/ttyS0';
Condition0%0/2
shift() || '/dev/ttyS0'
l!l
00
MC/DC0%0/1
shift() || '/dev/ttyS0'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
26 0 ▶ my $baudrate = shift || 19200;
Condition0%0/2
shift() || 19200
l!l
00
MC/DC0%0/1
shift() || 19200
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
27 0 my $lcd_type = shift;
28
29  
30 # Create self using super class
31 0 ▶ my $self = $class->SUPER::new( $port )
Branch: 1 branch
BranchTrueFalseTotal
my $self = $class->SUPER::new($port) or die "Failed to create SerialPort object: $!" 0 0 0
32 or die "Failed to create SerialPort object: $!";
33
34 # Configure the Serial Port
35 0 ▶ $self->baudrate($baudrate) || die ("Failed to set baud rate");
Branch: 1 branch
BranchTrueFalseTotal
$self->baudrate($baudrate) or die "Failed to set baud rate" 0 0 0
36 0 ▶ $self->parity("none") || die ("Failed to set parity");
Branch: 1 branch
BranchTrueFalseTotal
$self->parity("none") or die "Failed to set parity" 0 0 0
37 0 ▶ $self->databits(8) || die ("Failed to set data bits");
Branch: 1 branch
BranchTrueFalseTotal
$self->databits(8) or die "Failed to set data bits" 0 0 0
38 0 ▶ $self->stopbits(1) || die ("Failed to set stop bits");
Branch: 1 branch
BranchTrueFalseTotal
$self->stopbits(1) or die "Failed to set stop bits" 0 0 0
39 0 ▶ $self->handshake("none") || die ("Failed to disable handshaking");
Branch: 1 branch
BranchTrueFalseTotal
$self->handshake("none") or die "Failed to disable handshaking" 0 0 0
40 0 ▶ $self->write_settings || die ("Failed to write settings");
Branch: 1 branch
BranchTrueFalseTotal
$self->write_settings or die "Failed to write settings" 0 0 0
41  
42 # Check for features
43 0 ▶ die "status isn't available for serial port: $port"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can_status 0 0 0
44 unless ($self->can_status());
45 0 ▶ die "write_done isn't available for serial port: $port"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->can_write_done 0 0 0
46 unless ($self->can_write_done());
47
48 # Set a serial timeout default of 5 seconds
49 0 $self->{'timeout'} = 5;
50  
51  
52 # Check LCD type
53 0 ▶ if (defined $lcd_type) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $lcd_type) { } 0 0 0
54 0 $self->{'lcd_type'} = $lcd_type;
55 } else {
56 0 $self->{'lcd_type'} = $self->get_lcd_type();
57 }
58
59 0 return $self;
60 }
61  
62  
63  
64 0 ▶sub backlight_on {
Subroutine backlight_on: not called CC=2 SCAR=17.9
Pod documented
65 0 my $self = shift;
66 0 ▶ my $min = $_[0] || 0;
Condition0%0/2
$_[0] || 0
l!l
00
MC/DC0%0/1
$_[0] || 0
$_[0]
Truth table0%0/2
A
A= $_[0]
Aresult
11
00
67 0 $self->send_command( 0x42, $min );
68 }
69  
70 0 ▶sub backlight_off {
Subroutine backlight_off: not called CC=1 SCAR=6.9
Pod documented
71 0 my $self = shift;
72 0 $self->send_command( 0x46 );
73 }
74  
75 0 ▶sub cursor_home {
Subroutine cursor_home: not called CC=1 SCAR=6.9
Pod documented
76 0 my $self = shift;
77 0 $self->send_command( 0x48 );
78 }
79  
80 0 ▶sub set_contrast {
Subroutine set_contrast: not called CC=1 SCAR=6.9
Pod documented
81 0 my $self = shift;
82 0 my ($value) = @_;
83 0 $self->send_command( 0x50, $value );
84 }
85  
86 0 ▶sub set_and_save_contrast {
Subroutine set_and_save_contrast: not called CC=1 SCAR=6.9
Pod documented
87 0 my $self = shift;
88 0 my ($value) = @_;
89 0 $self->send_command( 0x91, $value );
90 }
91  
92 0 ▶sub set_brightness {
Subroutine set_brightness: not called CC=1 SCAR=6.9
Pod documented
93 0 my $self = shift;
94 0 my ($value) = @_;
95 0 $self->send_command( 0x99, $value );
96 }
97  
98 0 ▶sub set_and_save_brightness {
Subroutine set_and_save_brightness: not called CC=1 SCAR=6.9
Pod documented
99 0 my $self = shift;
100 0 my ($value) = @_;
101 0 $self->send_command( 0x98, $value );
102 }
103  
104 0 ▶sub set_autoscroll_on {
Subroutine set_autoscroll_on: not called CC=1 SCAR=6.9
Pod documented
105 0 my $self = shift;
106 0 $self->send_command( 0x51 );
107 }
108  
109 0 ▶sub set_autoscroll_off {
Subroutine set_autoscroll_off: not called CC=1 SCAR=6.9
Pod documented
110 0 my $self = shift;
111 0 $self->send_command( 0x52 );
112 }
113  
114  
115 0 ▶sub set_drawing_color {
Subroutine set_drawing_color: not called CC=1 SCAR=6.9
Pod documented
116 0 my $self = shift;
117 0 my ($color) = @_;
118 0 $self->send_command( 0x63, $color );
119 }
120  
121 0 ▶sub clear_screen {
Subroutine clear_screen: not called CC=1 SCAR=6.9
Pod documented
122 0 my $self = shift;
123 0 $self->send_command( 0x58 );
124 }
125  
126 0 ▶sub draw_bitmap {
Subroutine draw_bitmap: not called CC=1 SCAR=6.9
Pod documented
127 0 my $self = shift;
128 0 my ($refid, $x, $y) = @_;
129 0 $self->send_command( 0x62, $refid, $x, $y );
130 }
131  
132 0 ▶sub draw_pixel {
Subroutine draw_pixel: not called CC=1 SCAR=6.9
Pod documented
133 0 my $self = shift;
134 0 my ($x, $y) = @_;
135 0 $self->send_command( 0x70, $x, $y );
136 }
137  
138 0 ▶sub draw_line {
Subroutine draw_line: not called CC=1 SCAR=6.9
Pod documented
139 0 my $self = shift;
140 0 my ($x1, $y1, $x2, $y2) = @_;
141 0 $self->send_command( 0x6C, $x1, $y1, $x2, $y2 );
142 }
143  
144 0 ▶sub draw_line_continue {
Subroutine draw_line_continue: not called CC=1 SCAR=6.9
Pod documented
145 0 my $self = shift;
146 0 my ($x, $y) = @_;
147 0 $self->send_command( 0x65, $x, $y );
148 }
149  
150 0 ▶sub draw_rect {
Subroutine draw_rect: not called CC=1 SCAR=6.9
Pod documented
151 0 my $self = shift;
152 0 my ($colour, $x1, $y1, $x2, $y2) = @_;
153 0 $self->send_command( 0x72, $colour, $x1, $y1, $x2, $y2 );
154 }
155  
156 0 ▶sub draw_solid_rect {
Subroutine draw_solid_rect: not called CC=1 SCAR=6.9
Pod documented
157 0 my $self = shift;
158 0 my ($colour, $x1, $y1, $x2, $y2) = @_;
159 0 $self->send_command( 0x78, $colour, $x1, $y1, $x2, $y2 );
160 }
161  
162 0 ▶sub get_lcd_type {
Subroutine get_lcd_type: not called CC=48 SCAR=77.6
Pod documented
163 0 my $self = shift;
164 0 ▶ unless (defined $self->{'lcd_type'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"lcd_type"}) 0 0 0
165 0 $self->send_command( 0x37 );
166  
167 0 my $value = $self->getchar();
168 0 ▶ unless (defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 0 0
169 0 warn "Failed to read single byte from LCD screen";
170 0 return undef;
171 }
172
173 0 ▶ if ($value==0x01) { $self->{'lcd_type'}='LCD0821' }
Branch: 45 branches
BranchTrueFalseTotal
if ($value == 1) { } 0 0 0
elsif ($value == 2) { } 0 0 0
elsif ($value == 5) { } 0 0 0
elsif ($value == 6) { } 0 0 0
elsif ($value == 7) { } 0 0 0
elsif ($value == 8) { } 0 0 0
elsif ($value == 9) { } 0 0 0
elsif ($value == 10) { } 0 0 0
elsif ($value == 11) { } 0 0 0
elsif ($value == 12) { } 0 0 0
elsif ($value == 13) { } 0 0 0
elsif ($value == 14) { } 0 0 0
elsif ($value == 15) { } 0 0 0
elsif ($value == 16) { } 0 0 0
elsif ($value == 19) { } 0 0 0
elsif ($value == 21) { } 0 0 0
elsif ($value == 34) { } 0 0 0
elsif ($value == 36) { } 0 0 0
elsif ($value == 49) { } 0 0 0
elsif ($value == 50) { } 0 0 0
elsif ($value == 51) { } 0 0 0
elsif ($value == 52) { } 0 0 0
elsif ($value == 53) { } 0 0 0
elsif ($value == 54) { } 0 0 0
elsif ($value == 55) { } 0 0 0
elsif ($value == 56) { } 0 0 0
elsif ($value == 57) { } 0 0 0
elsif ($value == 58) { } 0 0 0
elsif ($value == 59) { } 0 0 0
elsif ($value == 60) { } 0 0 0
elsif ($value == 61) { } 0 0 0
elsif ($value == 62) { } 0 0 0
elsif ($value == 64) { } 0 0 0
elsif ($value == 65) { } 0 0 0
elsif ($value == 66) { } 0 0 0
elsif ($value == 67) { } 0 0 0
elsif ($value == 68) { } 0 0 0
elsif ($value == 69) { } 0 0 0
elsif ($value == 70) { } 0 0 0
elsif ($value == 71) { } 0 0 0
elsif ($value == 72) { } 0 0 0
elsif ($value == 73) { } 0 0 0
elsif ($value == 74) { } 0 0 0
elsif ($value == 75) { } 0 0 0
elsif ($value == 76) { } 0 0 0
174 0 elsif ($value==0x02) { $self->{'lcd_type'}='LCD2021' }
175 0 elsif ($value==0x05) { $self->{'lcd_type'}='LCD2041' }
176 0 elsif ($value==0x06) { $self->{'lcd_type'}='LCD4021' }
177 0 elsif ($value==0x07) { $self->{'lcd_type'}='LCD4041' }
178 0 elsif ($value==0x08) { $self->{'lcd_type'}='LK202-25' }
179 0 elsif ($value==0x09) { $self->{'lcd_type'}='LK204-25' }
180 0 elsif ($value==0x0A) { $self->{'lcd_type'}='LK404-55' }
181 0 elsif ($value==0x0B) { $self->{'lcd_type'}='VFD2021' }
182 0 elsif ($value==0x0C) { $self->{'lcd_type'}='VFD2041' }
183 0 elsif ($value==0x0D) { $self->{'lcd_type'}='VFD4021' }
184 0 elsif ($value==0x0E) { $self->{'lcd_type'}='VK202-25' }
185 0 elsif ($value==0x0F) { $self->{'lcd_type'}='VK204-25' }
186 0 elsif ($value==0x10) { $self->{'lcd_type'}='GLC12232' }
187 0 elsif ($value==0x13) { $self->{'lcd_type'}='GLC24064' }
188 0 elsif ($value==0x15) { $self->{'lcd_type'}='GLK24064-25' }
189 0 elsif ($value==0x22) { $self->{'lcd_type'}='GLK12232-25-WBL' }
190 0 elsif ($value==0x24) { $self->{'lcd_type'}='GLK12232-25-SM' }
191 0 elsif ($value==0x31) { $self->{'lcd_type'}='LK404-AT' }
192 0 elsif ($value==0x32) { $self->{'lcd_type'}='MOS-AV-162A' }
193 0 elsif ($value==0x33) { $self->{'lcd_type'}='LK402-12' }
194 0 elsif ($value==0x34) { $self->{'lcd_type'}='LK162-12' }
195 0 elsif ($value==0x35) { $self->{'lcd_type'}='LK204-25PC' }
196 0 elsif ($value==0x36) { $self->{'lcd_type'}='LK202-24-USB' }
197 0 elsif ($value==0x37) { $self->{'lcd_type'}='VK202-24-USB' }
198 0 elsif ($value==0x38) { $self->{'lcd_type'}='LK204-24-USB' }
199 0 elsif ($value==0x39) { $self->{'lcd_type'}='VK204-24-USB' }
200 0 elsif ($value==0x3A) { $self->{'lcd_type'}='PK162-12' }
201 0 elsif ($value==0x3B) { $self->{'lcd_type'}='VK162-12' }
202 0 elsif ($value==0x3C) { $self->{'lcd_type'}='MOS-AP-162A' }
203 0 elsif ($value==0x3D) { $self->{'lcd_type'}='PK202-25' }
204 0 elsif ($value==0x3E) { $self->{'lcd_type'}='MOS-AL-162A' }
205 0 elsif ($value==0x40) { $self->{'lcd_type'}='MOS-AV-202A' }
206 0 elsif ($value==0x41) { $self->{'lcd_type'}='MOS-AP-202A' }
207 0 elsif ($value==0x42) { $self->{'lcd_type'}='PK202-24-USB' }
208 0 elsif ($value==0x43) { $self->{'lcd_type'}='MOS-AL-082' }
209 0 elsif ($value==0x44) { $self->{'lcd_type'}='MOS-AL-204' }
210 0 elsif ($value==0x45) { $self->{'lcd_type'}='MOS-AV-204' }
211 0 elsif ($value==0x46) { $self->{'lcd_type'}='MOS-AL-402' }
212 0 elsif ($value==0x47) { $self->{'lcd_type'}='MOS-AV-402' }
213 0 elsif ($value==0x48) { $self->{'lcd_type'}='LK082-12' }
214 0 elsif ($value==0x49) { $self->{'lcd_type'}='VK402-12' }
215 0 elsif ($value==0x4A) { $self->{'lcd_type'}='VK404-55' }
216 0 elsif ($value==0x4B) { $self->{'lcd_type'}='LK402-25' }
217 0 elsif ($value==0x4C) { $self->{'lcd_type'}='VK402-25' }
218 0 else { printf STDERR ("Unknown/unsupported LCD type 0x%x", $value); }
219 }
220
221 0 return $self->{'lcd_type'};
222 }
223  
224 0 ▶sub get_lcd_dimensions {
Subroutine get_lcd_dimensions: not called CC=7 SCAR=40.3
Pod documented
225 0 my $self = shift;
226  
227 # We need the LCD type first
228 0 my $lcd = $self->get_lcd_type();
229  
230 0 ▶ if ($lcd eq 'GLC12232') { return (122,32) }
Branch: 6 branches
BranchTrueFalseTotal
if ($lcd eq 'GLC12232') { } 0 0 0
elsif ($lcd eq 'GLC24064') { } 0 0 0
elsif ($lcd eq 'GLK24064-25') { } 0 0 0
elsif ($lcd eq 'GLK12232-25-WBL') { } 0 0 0
elsif ($lcd eq 'GLK12232-25-SM') { } 0 0 0
elsif ($lcd eq 'GLK240128-25') { } 0 0 0
231 0 elsif ($lcd eq 'GLC24064') { return (240,64) }
232 0 elsif ($lcd eq 'GLK24064-25') { return (240,64) }
233 0 elsif ($lcd eq 'GLK12232-25-WBL') { return (122,32) }
234 0 elsif ($lcd eq 'GLK12232-25-SM') { return (122,32) }
235 0 elsif ($lcd eq 'GLK240128-25') { return (240,128) }
236 else {
237 0 warn "Unknown pixel dimensions for LCD: $lcd";
238 0 return undef;
239 }
240 }
241  
242 0 ▶sub get_firmware_version {
Subroutine get_firmware_version: not called CC=2 SCAR=17.9
Pod documented
243 0 my $self = shift;
244 0 ▶ unless (defined $self->{'firmware_version'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"firmware_version"}) 0 0 0
245 0 $self->send_command( 0x36 );
246  
247 0 my $value = sprintf("%2.2x", $self->getchar() );
248 0 my ($major, $minor) = ($value =~ /(\w{1})(\w{1})/);
249 0 $self->{'firmware_version'} = "$major.$minor";
250 }
251
252 0 return $self->{'firmware_version'};
253 }
254  
255  
256  
257 #### --------------------------------------------------------- ####
258  
259  
260  
261 ## Send a command to the display
262 0 ▶sub send_command {
Subroutine send_command: not called CC=1 SCAR=6.9
Pod undocumented
263 0 my $self = shift;
264 0 $self->print( pack( 'C*', 0xFE, @_ ) );
265 }
266  
267  
268 ## Send a string to the display
269 0 ▶sub print {
Subroutine print: not called CC=4 SCAR=30.0
Pod documented
270 0 my $self = shift;
271 0 my ($string) = @_;
272 0 my $bytes = 0;
273  
274 0 eval {
275 0 ▶ local $SIG{ALRM} = sub { die "Timed out."; };
Subroutine __ANON__: not called CC=1 SCAR=6.9
276 0 alarm($self->{'timeout'});
277
278 # Send it
279 0 $bytes = $self->write( $string );
280
281 # Block until it is sent
282 0 ▶ while(($self->write_done(0))[0] == 0) {}
Branch: 1 branch
BranchTrueFalseTotal
if (($self->write_done(0))[0] == 0) 0 0 0
283
284 0 alarm 0;
285 };
286
287 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
288 0 ▶ die unless $@ eq "Timed out.\n"; # propagate unexpected errors
Branch: 1 branch
BranchTrueFalseTotal
unless $@ eq "Timed out.\n" 0 0 0
289 # timed out
290 0 warn "Timed out while writing to serial port.\n";
291 }
292  
293 0 return $bytes;
294 }
295  
296  
297 ## Send a formatted string to the display
298 0 ▶sub printf {
Subroutine printf: not called CC=1 SCAR=6.9
Pod documented
299 0 my $self = shift;
300
301 0 $self->print( sprintf( @_ ) );
302 }
303  
304  
305 ## Read a single byte from the serial port and return it as an integer
306 0 ▶sub getchar {
Subroutine getchar: not called CC=2 SCAR=17.9
Pod undocumented
307 0 my $self = shift;
308  
309 # don't wait for each character
310 0 $self->read_char_time(0);
311
312 # milliseconds per unfulfilled "read" call
313 0 $self->read_const_time($self->{'timeout'}*1000);
314  
315 # Read one charater
316 0 my ($count,$data) = $self->read(1);
317 0 ▶ return undef if ($count<1);
Branch: 1 branch
BranchTrueFalseTotal
if $count < 1 0 0 0
318 0 return unpack('C',$data);
319 }
320  
321  
322 ## Close the serial port
323 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=3 SCAR=24.8
324 0 my $self=shift;
325
326 0 ▶ if (defined $self->{'serial'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"serial"}) 0 0 0
327 0 ▶ $self->{'serial'}->close || warn "Failed to close serial port.";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"serial"}->close or warn "Failed to close serial port." 0 0 0
328 0 undef $self->{'serial'};
329 }
330 }
331  
332  
333 1 1;
334  
335 __END__
↑ Summary