| 1 | package Device::ParallelPort::drv; | ||||||||||||||||||||
| 2 | 1 | ▶ | use Carp; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | |||||||||||||||||||||
| 4 | =head1 NAME | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | Device::ParallelPort::drv - Standard pacakge to be imported by all drivers | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | =head1 SYNOPSIS | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | (Not Applicable) - do not use this directly, use another driver | ||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | * Device::ParallelPort::drv::auto | ||||||||||||||||||||
| 13 | * Device::ParallelPort::drv::linux | ||||||||||||||||||||
| 14 | * Device::ParallelPort::drv::parport | ||||||||||||||||||||
| 15 | * Device::ParallelPort::drv::win32 | ||||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | =head1 DESCRIPTION | ||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | This driver is the base class recommended for all Parallel Port Drivers. | ||||||||||||||||||||
| 20 | It is not useful in itself. Although against proper OO | ||||||||||||||||||||
| 21 | design, this particular module does not work by itself. | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | =head1 METHODS | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | =head2 new | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | =head2 get_bit | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | =head2 get_byte | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | =head2 set_bit | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | =head2 set_byte | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | =head1 NOTES | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | =head2 Device Names | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | A special system of device names has been deviced. | ||||||||||||||||||||
| 40 | Basically we are trying to be compatible with most systems, and not force | ||||||||||||||||||||
| 41 | people to learn something new. | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | You can enter parallel port device in a number of ways | ||||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | - N Unix style, where 0 is the first port | ||||||||||||||||||||
| 46 | - lptN Windows style, where 1 is the first port | ||||||||||||||||||||
| 47 | - 0xNNN Direct hardware location | ||||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | This is totally dependent on the driver being used. | ||||||||||||||||||||
| 50 | For example the script driver would not use these but the auto driver does. | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | Now these are not necessarily supported in all operating systems. By default | ||||||||||||||||||||
| 53 | this base driver converts lpt notation into lp notation, it then optionally | ||||||||||||||||||||
| 54 | converts all lp notation into a hardware location. However what would not work | ||||||||||||||||||||
| 55 | for parport control, which is generally done as parport device, mapping the | ||||||||||||||||||||
| 56 | same number as the lp above (check that?), in that case passing the direct | ||||||||||||||||||||
| 57 | hardware location is pointless. | ||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | =head1 COPYRIGHT | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | Copyright (c) 2002,2004 Scott Penrose. All rights reserved. | ||||||||||||||||||||
| 62 | This program is free software; you can redistribute it and/or modify | ||||||||||||||||||||
| 63 | it under the same terms as Perl itself. | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | =head1 AUTHOR | ||||||||||||||||||||
| 66 | |||||||||||||||||||||
| 67 | Scott Penrose L<scottp@dd.com.au>, L<http://linux.dd.com.au/> | ||||||||||||||||||||
| 68 | |||||||||||||||||||||
| 69 | =head1 SEE ALSO | ||||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | L<Device::ParallelPort> | ||||||||||||||||||||
| 72 | |||||||||||||||||||||
| 73 | =cut | ||||||||||||||||||||
| 74 | |||||||||||||||||||||
| 75 | # Some constants that are useful | ||||||||||||||||||||
| 76 | 0 | ▶ | sub BASE_0 { 0x378; } # Intel x86 Base Port | ||||||||||||||||||
|
Subroutine
BASE_0: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 77 | 0 | ▶ | sub OFFSET_DATA { 0; } | ||||||||||||||||||
|
Subroutine
OFFSET_DATA: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 78 | 0 | ▶ | sub OFFSET_STATUS { 1; } | ||||||||||||||||||
|
Subroutine
OFFSET_STATUS: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 79 | 0 | ▶ | sub OFFSET_CONTROL { 2; } | ||||||||||||||||||
|
Subroutine
OFFSET_CONTROL: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 2 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=12.0
Pod
documented
| |||||||||||||||||||||
| 82 | 2 | my ($class, $str, @params) = @_; | |||||||||||||||||||
| 83 | 2 | ▶ | my $this = bless {}, ref($class) || $class; | ||||||||||||||||||
|
Condition33%1/3
ref $class || $class
MC/DC0%0/2
ref $class || $class
ref $class
$class
Truth table33%1/3
A || B
A= ref $classB= $class
| |||||||||||||||||||||
| 84 | 2 | ▶ | $this->init($str, @params) if ($this->can('init')); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 85 | 2 | return $this; | |||||||||||||||||||
| 86 | } | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | # ------------------------------------------------------------------------------ | ||||||||||||||||||||
| 89 | # BIT -> BYTE and BYTE -> BIT Autoamtic Support | ||||||||||||||||||||
| 90 | # ------------------------------------------------------------------------------ | ||||||||||||||||||||
| 91 | # This seciton basically provides | ||||||||||||||||||||
| 92 | 6 | ▶ | sub get_bit { | ||||||||||||||||||
|
Subroutine
get_bit: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||||||||||||
| 93 | 6 | my ($this, $bit) = @_; | |||||||||||||||||||
| 94 | 0 | ▶ | unless ($this->INFO->{type} eq "byte") { croak "Unsupported in this driver"; } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 95 | # Find the byte | ||||||||||||||||||||
| 96 | 6 | my $byte = int($bit / 8); | |||||||||||||||||||
| 97 | 6 | $bit = $bit - ($byte * 8); | |||||||||||||||||||
| 98 | 6 | return _bit_from_byte($this->get_byte($byte), $bit); | |||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | 14 | ▶ | sub _bit_from_byte { | ||||||||||||||||||
|
Subroutine
_bit_from_byte: called CC=2 SCAR=6.9
| |||||||||||||||||||||
| 102 | 14 | my ($byte, $bit) = @_; | |||||||||||||||||||
| 103 | 14 | ▶ | return ((ord($byte) & (1 << $bit)) == (1 << $bit)) ? 1 : 0; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | |||||||||||||||||||||
| 106 | 6 | ▶ | sub get_byte { | ||||||||||||||||||
|
Subroutine
get_byte: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||
| 107 | 6 | my ($this, $byte) = @_; | |||||||||||||||||||
| 108 | 0 | ▶ | unless ($this->INFO->{type} eq "bit") { croak "Unsupported in this driver"; } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 109 | 6 | my $ret = 0; | |||||||||||||||||||
| 110 | 6 | my $first_bit = ($byte * 8); | |||||||||||||||||||
| 111 | 6 | ▶ | for (my $bit = $first_bit; $bit < ($first_bit + 8); $bit++) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 112 | 48 | ▶ | if ($this->get_bit($bit)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 113 | 7 | $ret = $ret + (1 << ($bit - ($byte * 8))); | |||||||||||||||||||
| 114 | } | ||||||||||||||||||||
| 115 | } | ||||||||||||||||||||
| 116 | 6 | return chr($ret); | |||||||||||||||||||
| 117 | } | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 0 | ▶ | sub get_byte_uninvert { | ||||||||||||||||||
|
Subroutine
get_byte_uninvert: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 120 | 0 | my ($this, $byte) = @_; | |||||||||||||||||||
| 121 | 0 | return $this->uninvert($byte, $this->get_byte($byte)); | |||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 0 | ▶ | sub set_byte_uninvert { | ||||||||||||||||||
|
Subroutine
set_byte_uninvert: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 125 | 0 | my ($this, $byte, $val) = @_; | |||||||||||||||||||
| 126 | 0 | return $this->set_byte($byte, $this->uninvert($byte, $val)); | |||||||||||||||||||
| 127 | } | ||||||||||||||||||||
| 128 | |||||||||||||||||||||
| 129 | 3 | ▶ | sub set_bit { | ||||||||||||||||||
|
Subroutine
set_bit: called CC=4 SCAR=15.0
Pod
documented
| |||||||||||||||||||||
| 130 | 3 | my ($this, $bit, $val) = @_; | |||||||||||||||||||
| 131 | 0 | ▶ | unless ($this->INFO->{type} eq "byte") { croak "Unsupported in this driver"; } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 132 | 3 | my $byte = int($bit / 8); | |||||||||||||||||||
| 133 | 3 | $bit = $bit - ($byte * 8); | |||||||||||||||||||
| 134 | 3 | my $current = $this->get_byte($byte); | |||||||||||||||||||
| 135 | 3 | ▶ | if (defined($current)) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 136 | 3 | $current = ord($current); | |||||||||||||||||||
| 137 | } else { | ||||||||||||||||||||
| 138 | 0 | $current = 0; | |||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | 3 | ▶ | if ($val) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 141 | 3 | $current = $current | (1 << $bit); | |||||||||||||||||||
| 142 | } else { | ||||||||||||||||||||
| 143 | 0 | $current = $current & (~ (1 << $bit)); | |||||||||||||||||||
| 144 | } | ||||||||||||||||||||
| 145 | 3 | $this->set_byte($byte, chr($current)); | |||||||||||||||||||
| 146 | } | ||||||||||||||||||||
| 147 | |||||||||||||||||||||
| 148 | 1 | ▶ | sub set_byte { | ||||||||||||||||||
|
Subroutine
set_byte: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||
| 149 | 1 | my ($this, $byte, $val) = @_; | |||||||||||||||||||
| 150 | 0 | ▶ | unless ($this->INFO->{type} eq "bit") { croak "Unsupported in this driver"; } | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 151 | 1 | ▶ | for(my $i = 0; $i < 8; $i++) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 152 | 8 | $this->set_bit( | |||||||||||||||||||
| 153 | $i + ($byte * 8), | ||||||||||||||||||||
| 154 | _bit_from_byte($val, $i), | ||||||||||||||||||||
| 155 | ); | ||||||||||||||||||||
| 156 | } | ||||||||||||||||||||
| 157 | } | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | # Shortcuts for those who want data, control and status for standard parallel | ||||||||||||||||||||
| 160 | # ports seprarately. | ||||||||||||||||||||
| 161 | |||||||||||||||||||||
| 162 | 0 | ▶ | sub get_data { | ||||||||||||||||||
|
Subroutine
get_data: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 163 | 0 | my ($this) = @_; | |||||||||||||||||||
| 164 | 0 | return $this->get_byte($this->OFFSET_DATA); | |||||||||||||||||||
| 165 | } | ||||||||||||||||||||
| 166 | |||||||||||||||||||||
| 167 | 0 | ▶ | sub get_control { | ||||||||||||||||||
|
Subroutine
get_control: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 168 | 0 | my ($this) = @_; | |||||||||||||||||||
| 169 | 0 | return $this->get_byte($this->OFFSET_CONTROL); | |||||||||||||||||||
| 170 | } | ||||||||||||||||||||
| 171 | |||||||||||||||||||||
| 172 | 0 | ▶ | sub get_status { | ||||||||||||||||||
|
Subroutine
get_status: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 173 | 0 | my ($this) = @_; | |||||||||||||||||||
| 174 | 0 | return $this->get_byte($this->OFFSET_STATUS); | |||||||||||||||||||
| 175 | } | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | 0 | ▶ | sub set_data { | ||||||||||||||||||
|
Subroutine
set_data: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 178 | 0 | my ($this, $val) = @_; | |||||||||||||||||||
| 179 | 0 | return $this->set_byte($this->OFFSET_DATA, $val); | |||||||||||||||||||
| 180 | } | ||||||||||||||||||||
| 181 | |||||||||||||||||||||
| 182 | 0 | ▶ | sub set_control { | ||||||||||||||||||
|
Subroutine
set_control: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 183 | 0 | my ($this, $val) = @_; | |||||||||||||||||||
| 184 | 0 | return $this->set_byte($this->OFFSET_CONTROL, $val); | |||||||||||||||||||
| 185 | } | ||||||||||||||||||||
| 186 | |||||||||||||||||||||
| 187 | 0 | ▶ | sub set_status { | ||||||||||||||||||
|
Subroutine
set_status: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 188 | 0 | my ($this, $val) = @_; | |||||||||||||||||||
| 189 | 0 | return $this->set_byte($this->OFFSET_STATUS, $val); | |||||||||||||||||||
| 190 | } | ||||||||||||||||||||
| 191 | |||||||||||||||||||||
| 192 | # HELPER METHODS | ||||||||||||||||||||
| 193 | |||||||||||||||||||||
| 194 | # TODO - Real byte converter - CHAR from Number | ||||||||||||||||||||
| 195 | # Convert an integer to a real byte - just in case someone has passed in a | ||||||||||||||||||||
| 196 | # number instead of a byte. | ||||||||||||||||||||
| 197 | #sub real_byte { | ||||||||||||||||||||
| 198 | # my ($this, $val) = @_; | ||||||||||||||||||||
| 199 | # return $val; | ||||||||||||||||||||
| 200 | #} | ||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | # TODO - Invert / Uninverter - Just swaps bits | ||||||||||||||||||||
| 203 | # Convert all inverted bits back to normal | ||||||||||||||||||||
| 204 | #sub uninvert { | ||||||||||||||||||||
| 205 | # my ($this, $byte, $val, $bits) = @_; | ||||||||||||||||||||
| 206 | # if ($byte == 0) { # BYTE | ||||||||||||||||||||
| 207 | # $bits ||= []; | ||||||||||||||||||||
| 208 | # } elsif ($byte == 1) { # STATUS | ||||||||||||||||||||
| 209 | # $bits ||= [qw/7/]; | ||||||||||||||||||||
| 210 | # } elsif ($byte == 2) { # STATUS | ||||||||||||||||||||
| 211 | # $bits ||= [qw/0 1 3/]; | ||||||||||||||||||||
| 212 | # } | ||||||||||||||||||||
| 213 | # my $ret = $val; | ||||||||||||||||||||
| 214 | # foreach my $bit (@$bits) { | ||||||||||||||||||||
| 215 | # # XXX XOR Bit here | ||||||||||||||||||||
| 216 | # } | ||||||||||||||||||||
| 217 | #} | ||||||||||||||||||||
| 218 | |||||||||||||||||||||
| 219 | # ADDRESS METHODS | ||||||||||||||||||||
| 220 | |||||||||||||||||||||
| 221 | 0 | ▶ | sub address_to_num { | ||||||||||||||||||
|
Subroutine
address_to_num: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 222 | 0 | my ($this, $address) = @_; | |||||||||||||||||||
| 223 | 0 | ▶ | if (($address * 1) == $address) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 224 | 0 | return $address; | |||||||||||||||||||
| 225 | } elsif ($address =~/^lpt(\d)$/) { | ||||||||||||||||||||
| 226 | 0 | return $1 - 1; | |||||||||||||||||||
| 227 | } else { | ||||||||||||||||||||
| 228 | 0 | croak "Unable to convert $address to something meaninful to Device::ParallelPort - try 0..9 or lpt1..lpt9"; | |||||||||||||||||||
| 229 | } | ||||||||||||||||||||
| 230 | } | ||||||||||||||||||||
| 231 | |||||||||||||||||||||
| 232 | 0 | ▶ | sub num_to_hardware { | ||||||||||||||||||
|
Subroutine
num_to_hardware: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 233 | 0 | my ($this, $num) = @_; | |||||||||||||||||||
| 234 | 0 | ▶ | if ($num == 0) { | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 235 | 0 | return 0x378; | |||||||||||||||||||
| 236 | } elsif ($num == 1) { | ||||||||||||||||||||
| 237 | 0 | return 0x278; | |||||||||||||||||||
| 238 | } else { | ||||||||||||||||||||
| 239 | 0 | croak "No known lookup for hardware address $num to Device::ParallelPort - Try 0..1"; | |||||||||||||||||||
| 240 | } | ||||||||||||||||||||
| 241 | } | ||||||||||||||||||||
| 242 | |||||||||||||||||||||
| 243 | 1 | 1; | |||||||||||||||||||