blib/lib/Device/ParallelPort/JayCar.pm untested

filter: Statementstmt 0.0% 0 / 63 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 113 CC 8 SCAR 19.7
CC
8
Cov
0%
init
20.0
_checkid
6.0
on
6.0
CRAP 7.2
1 package Device::ParallelPort::JayCar;
2 use strict;
3 use Carp;
4 use Device::ParallelPort;
5 our $VERSION = "0.04";
6  
7  
8 # XXX NOTE - Temporary version with DUMB mappings...
9 #
10 #
11 # Card =
12 # 8 - $card
13  
14 # C3 C2 C1 C0 Card ID Number
15 #
16 # 1 0 1 1 10
17 # 1 0 0 2 8
18 # 1 1 1 3 14
19 # 1 1 0 4 12
20 # 0 0 1 5 2
21 # 0 0 0 6 0
22 # 0 1 1 7 6
23 # 0 1 0 8 4
24 #
25 # Logic =
26 # Card shift left
27  
28 my %cardmap = (
29 0 => 10,
30 1 => 8,
31 2 => 14,
32 3 => 12,
33 4 => 2,
34 5 => 0,
35 6 => 6,
36 7 => 4,
37 );
38  
39 =head1 NAME
40  
41 Device::ParallelPort::JayCarXXX - Jaycar controlling device.
42  
43 XXX This is all wrong - need to update...
44  
45 =head1 SYNOPSIS
46  
47 This is an example driver for a fairly common (in Australia anyway) parallel
48 port controller card. It can be used for real, but has been written in an easy
49 to read manner to allow it to be a base class for future drivers.
50  
51 =head1 DESCRIPTION
52  
53 To come.
54  
55 =head1 NOTE ON NAMING
56  
57 A note on class locations. If you are writting a general controller, eg: for a
58 high speed neon sign controller. Then you would always write that in its own
59 class (see CPAN for the best base class to put that in). Thats because more
60 than likely the sign supports multiple protocols such as Parallel, RS485, USB
61 and more. Then the propert place would be:
62  
63 SomeBaseClass::MySign::drv::ParallelPort
64  
65 or simular. When you write a network class that talks TCPIP only for that sign,
66 you do not put it in the Net:: location, same for parallel port.
67  
68 =head1 NOTE ON INHERITENCE
69  
70 Should examples such
71  
72 =head1 QUESTIONS
73  
74 How to handle errors, when writting to the port?
75  
76 =head1 COPYRIGHT
77  
78 Copyright (c) 2002,2004 Scott Penrose. All rights reserved.
79 This program is free software; you can redistribute it and/or modify
80 it under the same terms as Perl itself.
81  
82 =head1 AUTHOR
83  
84 Scott Penrose L<scottp@dd.com.au>, L<http://linux.dd.com.au/>
85  
86 =head1 SEE ALSO
87  
88 L<Device::ParallelPort>
89  
90 =cut
91  
92 # How many relays (allows you to sub class and add more)
93 # (max Jaycar ID = 8, max relays = 8)
94 sub RELAYS { 8 * 8 };
95  
96 # Need: Parallel Port and Board ID
97 # Return: Object
98 sub new {
99 my ($class, $parport, $boards) = @_;
100 my $this = bless {}, ref($class) || $class;
101 $boards = [1, 2];
102 $this->init($parport, $boards);
103 return $this;
104 }
105  
106 sub init {
107 my ($this, $parport, $boards) = @_;
108 if (ref($parport)) {
109 $this->{PARPORT} = $parport;
110 } elsif (defined($parport)) {
111 $this->{PARPORT} = Device::ParallelPort->new($parport)
112 or croak("Unable to create ParPort Device");
113 } else {
114 croak "Invalid parport provided";
115 }
116  
117 # Store array of boards activated (ie: only update those boards)
118 $this->{BOARDS} = ref($boards) eq "ARRAY" ? $boards : [$boards];
119  
120 $this->{RELAYS} = [];
121 for (my $i = 0; $i < $this->RELAYS(); $i++) {
122 $this->{RELAYS}[$i] = 0;
123 }
124 }
125  
126 sub _parport {
127 my ($this) = @_;
128 return $this->{PARPORT};
129 }
130  
131 # Need: Relay Number (0-7)
132 # Return: True/False
133 # How: Must remember, can not get it from the board.
134 sub get {
135 my ($this, $id) = @_;
136 $this->_checkid($id);
137 return $this->{RELAYS}[$id];
138 }
139  
140 sub _checkid {
141 my ($this, $id) = @_;
142 if ($id < 0 || $id >= $this->RELAYS) {
143 croak "Invalid relay id specified - $id";
144 }
145 }
146  
147 # Need: Ralay Number (0-7) (optionally delay update)
148 # Return: NA
149 # How: Update memory map bit, set whole byte, flash with Board ID
150 sub on {
151 my ($this, $id, $delay) = @_;
152 $this->_checkid($id);
153 $this->{RELAY}[$id] = 1;
154 $this->update unless ($delay);
155 }
156  
157 # See relay_on
158 sub off {
159 my ($this, $id, $delay) = @_;
160 $this->_checkid($id);
161 $this->{RELAY}[$id] = 0;
162 $this->update unless ($delay);
163 }
164  
165 # Update the device.
166 # Need: NA
167 # Return: NA
168 # How: Use parport to update byte and then flash it.
169 sub update {
170 my ($this) = @_;
171  
172 foreach my $board (@{$this->{BOARDS}}) {
173 $this->_parport->set_byte(2, chr($cardmap{$board})); # Prepare
174 $this->_parport->set_byte(0, chr($this->_byte_calc($board))); # Set data
175 $this->_parport->set_byte(2, chr($cardmap{$board} + 1)); # Flash bit 1 (strobe)
176 $this->_parport->set_byte(2, chr($cardmap{$board})); # Undo flash/strobe
177 }
178 }
179  
180 # Add bits together and return as integer.
181 # Need: NA (uses stored data)
182 # Return: Integer representing byte
183 # How: Add bits together as a byte (only those turned on)
184 sub _byte_calc {
185 my ($this, $board) = @_;
186 my $ret = 0;
187 for (my $i = 0; $i < 8; $i++) {
188 if ($this->{RELAY}[$i + ($board * 8)]) {
189 $ret = $ret + (1 << $i);
190 }
191 }
192 return $ret;
193 }
194  
195 1;