| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Device::Velleman::K8055::Client; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
47614
|
use IPC::ShareLite qw( :lock ); |
|
|
0
|
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
4
|
|
|
|
|
|
|
use Time::HiRes qw(usleep); |
|
5
|
|
|
|
|
|
|
use strict; |
|
6
|
|
|
|
|
|
|
use Data::Dumper; |
|
7
|
|
|
|
|
|
|
use strict; |
|
8
|
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
BEGIN { |
|
10
|
|
|
|
|
|
|
use Exporter (); |
|
11
|
|
|
|
|
|
|
use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $share $clientnum $count $gotok $clientnum); |
|
12
|
|
|
|
|
|
|
$VERSION = '0.01'; |
|
13
|
|
|
|
|
|
|
@ISA = qw(Exporter); |
|
14
|
|
|
|
|
|
|
#Give a hoot don't pollute, do not export more than needed by default |
|
15
|
|
|
|
|
|
|
@EXPORT = qw(); |
|
16
|
|
|
|
|
|
|
@EXPORT_OK = qw(); |
|
17
|
|
|
|
|
|
|
%EXPORT_TAGS = (); |
|
18
|
|
|
|
|
|
|
} |
|
19
|
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
sub new |
|
22
|
|
|
|
|
|
|
{ |
|
23
|
|
|
|
|
|
|
my ($class, %parameters) = @_; |
|
24
|
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
$SIG{INT} = 'Device::Velleman::K8055::Client::destroy'; |
|
26
|
|
|
|
|
|
|
my $self = {}; |
|
27
|
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
$self->{'error'} = undef; |
|
29
|
|
|
|
|
|
|
$self->{'data'} = undef; |
|
30
|
|
|
|
|
|
|
$self->{'clientno'} = undef; |
|
31
|
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
bless($self, $class); |
|
33
|
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
$clientnum = IPC::ShareLite->new( |
|
36
|
|
|
|
|
|
|
-key => 8055, |
|
37
|
|
|
|
|
|
|
-create => 'no', |
|
38
|
|
|
|
|
|
|
-destroy => 'no', |
|
39
|
|
|
|
|
|
|
-exclusive => 'no' |
|
40
|
|
|
|
|
|
|
) or die $!; |
|
41
|
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
|
|
43
|
|
|
|
|
|
|
$clientnum->lock( LOCK_EX ); |
|
44
|
|
|
|
|
|
|
my $clientno = $clientnum->fetch(); |
|
45
|
|
|
|
|
|
|
$clientnum->store($clientno+1); |
|
46
|
|
|
|
|
|
|
$clientnum->unlock(); |
|
47
|
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sleep 1; |
|
50
|
|
|
|
|
|
|
print $clientno . "\n"; |
|
51
|
|
|
|
|
|
|
$share = IPC::ShareLite->new( |
|
52
|
|
|
|
|
|
|
-key => 8057 + $clientno, |
|
53
|
|
|
|
|
|
|
-create => 'no', |
|
54
|
|
|
|
|
|
|
-destroy => 'no', |
|
55
|
|
|
|
|
|
|
-exclusive => 'no' |
|
56
|
|
|
|
|
|
|
) or die $!; |
|
57
|
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
|
|
59
|
|
|
|
|
|
|
$count = 0; |
|
60
|
|
|
|
|
|
|
$gotok = 0; |
|
61
|
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
$self->{clientno} = $clientno; |
|
63
|
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
return $self; |
|
65
|
|
|
|
|
|
|
} |
|
66
|
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub sendserver { |
|
68
|
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
my $cmd = shift; |
|
71
|
|
|
|
|
|
|
my $fetch; |
|
72
|
|
|
|
|
|
|
my @data; |
|
73
|
|
|
|
|
|
|
$gotok=0; |
|
74
|
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
while($gotok ==0 ) { |
|
77
|
|
|
|
|
|
|
if($share->fetch eq "" ) { |
|
78
|
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
$share->store( $cmd ); |
|
81
|
|
|
|
|
|
|
|
|
82
|
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
$share->unlock(); |
|
84
|
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
$gotok=0; |
|
86
|
|
|
|
|
|
|
while( $gotok == 0){ |
|
87
|
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
while(!$share->lock(LOCK_EX)) { |
|
89
|
|
|
|
|
|
|
usleep(5000); |
|
90
|
|
|
|
|
|
|
} |
|
91
|
|
|
|
|
|
|
$fetch = $share->fetch; |
|
92
|
|
|
|
|
|
|
@data = split(/:/, $share->fetch ); |
|
93
|
|
|
|
|
|
|
$cmd = $data[0]; |
|
94
|
|
|
|
|
|
|
if($cmd eq "OK") { |
|
95
|
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
$share->store(""); |
|
98
|
|
|
|
|
|
|
$gotok = 1; |
|
99
|
|
|
|
|
|
|
usleep 1000; |
|
100
|
|
|
|
|
|
|
} |
|
101
|
|
|
|
|
|
|
$share->unlock(); |
|
102
|
|
|
|
|
|
|
} |
|
103
|
|
|
|
|
|
|
} else { |
|
104
|
|
|
|
|
|
|
$share->unlock(); |
|
105
|
|
|
|
|
|
|
} |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
} |
|
108
|
|
|
|
|
|
|
$share->unlock(); |
|
109
|
|
|
|
|
|
|
|
|
110
|
|
|
|
|
|
|
return $fetch; |
|
111
|
|
|
|
|
|
|
} |
|
112
|
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
sub sendcmd (@){ |
|
115
|
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
my $send = shift; |
|
117
|
|
|
|
|
|
|
|
|
118
|
|
|
|
|
|
|
while ( @_ > 0 ) { |
|
119
|
|
|
|
|
|
|
my $arg = shift; |
|
120
|
|
|
|
|
|
|
$send = $send . ":" . $arg; |
|
121
|
|
|
|
|
|
|
} |
|
122
|
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
my $fetch = sendserver($send); |
|
124
|
|
|
|
|
|
|
return split /:/, $fetch; |
|
125
|
|
|
|
|
|
|
} |
|
126
|
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
|
|
128
|
|
|
|
|
|
|
sub ReadAnalogChannel { |
|
129
|
|
|
|
|
|
|
shift; |
|
130
|
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
my @ret = sendcmd("ReadAnalogChannel",$_[0]); |
|
132
|
|
|
|
|
|
|
return $ret[1]; |
|
133
|
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
} |
|
135
|
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
sub ClearDigitalChannel { |
|
137
|
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
shift; |
|
139
|
|
|
|
|
|
|
usleep 1000; |
|
140
|
|
|
|
|
|
|
return sendcmd("ClearDigitalChannel",$_[0]); |
|
141
|
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
} |
|
143
|
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
sub SetDigitalChannel { |
|
145
|
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
shift; |
|
147
|
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
return sendcmd("SetDigitalChannel",$_[0]); |
|
149
|
|
|
|
|
|
|
|
|
150
|
|
|
|
|
|
|
} |
|
151
|
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
sub destroy { |
|
153
|
|
|
|
|
|
|
print "\nDisconnecting\n"; |
|
154
|
|
|
|
|
|
|
$share->store("DIE"); |
|
155
|
|
|
|
|
|
|
$share=""; |
|
156
|
|
|
|
|
|
|
exit $@; |
|
157
|
|
|
|
|
|
|
} |
|
158
|
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=head1 NAME |
|
160
|
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
Device::Velleman::K8055::Client - Client for connecting to K8055::Server |
|
162
|
|
|
|
|
|
|
|
|
163
|
|
|
|
|
|
|
=head1 SYNOPSIS |
|
164
|
|
|
|
|
|
|
|
|
165
|
|
|
|
|
|
|
use Device::Velleman::K8055::Client; |
|
166
|
|
|
|
|
|
|
my $k8055 = Device::Velleman::K8055::Client->new(); |
|
167
|
|
|
|
|
|
|
. |
|
168
|
|
|
|
|
|
|
$k8055->SetDigitalChannel(8); |
|
169
|
|
|
|
|
|
|
$volts = $k8055->ReadAnalogChannel(1) * 0.086; |
|
170
|
|
|
|
|
|
|
print "$volts\n"; |
|
171
|
|
|
|
|
|
|
. |
|
172
|
|
|
|
|
|
|
. |
|
173
|
|
|
|
|
|
|
$k8055->destroy(); |
|
174
|
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
=head1 DESCRIPTION |
|
177
|
|
|
|
|
|
|
|
|
178
|
|
|
|
|
|
|
Connects to Device::Velleman::K8055::Server via IPCS and sends commands for the Server to |
|
179
|
|
|
|
|
|
|
execute on the K8055 board. |
|
180
|
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
Handles multiple clients connecting at the same time and ensures only one command gets sent to the |
|
182
|
|
|
|
|
|
|
physical board at a time. |
|
183
|
|
|
|
|
|
|
|
|
184
|
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
=head1 METHODS |
|
186
|
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=head2 new() |
|
188
|
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
Create a new instance of the Client. Sets up a shared memory connection to the server. |
|
190
|
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=head2 ReadAnalogChannel($channel) |
|
192
|
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
Read the analog channel specified by channel. |
|
194
|
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
Returns the value read from the channel. |
|
196
|
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=head2 ClearDigitalChannel($channel) |
|
198
|
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
Set the digital channel $channel to 0 or low. |
|
200
|
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
=head2 SetDigitalChannel($channel) |
|
202
|
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
Set the digital channel($channel to 1 or high. |
|
204
|
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
=head2 destroy |
|
206
|
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
Kill the client connection and tell the server to free any shared memory we have. |
|
208
|
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
=head1 BUGS |
|
210
|
|
|
|
|
|
|
|
|
211
|
|
|
|
|
|
|
Does NOT ensure that two different processes arent using the same digital or |
|
212
|
|
|
|
|
|
|
analog i/o's at the same time. |
|
213
|
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
Not all functions of the K8055 (reading the digital inputs or talking to the pwm) |
|
215
|
|
|
|
|
|
|
are implemented yet. |
|
216
|
|
|
|
|
|
|
|
|
217
|
|
|
|
|
|
|
You need to be careful at which speed you talk to the board as it has 10us resolution. |
|
218
|
|
|
|
|
|
|
Any quicker than this and you will get wierd results. |
|
219
|
|
|
|
|
|
|
|
|
220
|
|
|
|
|
|
|
=head1 AUTHOR |
|
221
|
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
David Peters |
|
223
|
|
|
|
|
|
|
CPAN ID: DAVIDP |
|
224
|
|
|
|
|
|
|
davidp@electronf.com |
|
225
|
|
|
|
|
|
|
http://www.electronf.com |
|
226
|
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=head1 COPYRIGHT |
|
228
|
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
This program is free software; you can redistribute |
|
230
|
|
|
|
|
|
|
it and/or modify it under the same terms as Perl itself. |
|
231
|
|
|
|
|
|
|
|
|
232
|
|
|
|
|
|
|
The full text of the license can be found in the |
|
233
|
|
|
|
|
|
|
LICENSE file included with this module. |
|
234
|
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
|
|
236
|
|
|
|
|
|
|
=head1 SEE ALSO |
|
237
|
|
|
|
|
|
|
|
|
238
|
|
|
|
|
|
|
Device::Velleman::K8055::Server, Device::Velleman::K8055::libk8055, perl(1). |
|
239
|
|
|
|
|
|
|
|
|
240
|
|
|
|
|
|
|
=cut |
|
241
|
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
#################### main pod documentation end ################### |
|
243
|
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
|
|
245
|
|
|
|
|
|
|
|
|
246
|
|
|
|
|
|
|
|
|
247
|
|
|
|
|
|
|
1; |
|
248
|
|
|
|
|
|
|
|
|
249
|
|
|
|
|
|
|
|