line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
|
2
|
|
|
|
|
|
|
# USB Dongle TEMPer1F |
3
|
|
|
|
|
|
|
# The Dongle has Vendor ID 0x0C45 and Product ID 0x7401 |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package Device::USB::TEMPer1F; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '0.04'; |
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
15620
|
use 5.008008; |
|
1
|
|
|
|
|
3
|
|
10
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
25
|
|
11
|
1
|
|
|
1
|
|
3
|
use warnings; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
26
|
|
12
|
1
|
|
|
1
|
|
280
|
use Device::USB; |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new{ |
15
|
|
|
|
|
|
|
my $class = shift; |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
# Basic Configuration |
18
|
|
|
|
|
|
|
my %cfg = ( |
19
|
|
|
|
|
|
|
vid => 0x0C45, # Vendor ID |
20
|
|
|
|
|
|
|
pid => 0x7401, # Product ID |
21
|
|
|
|
|
|
|
tim => 500, # Timeout |
22
|
|
|
|
|
|
|
epi => 0x82, # Endoint in |
23
|
|
|
|
|
|
|
); |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
my $self = bless{ |
26
|
|
|
|
|
|
|
cfg => \%cfg, |
27
|
|
|
|
|
|
|
buffer => 0 |
28
|
|
|
|
|
|
|
}, $class; |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
# There are two devices! |
31
|
|
|
|
|
|
|
# Need the device for: |
32
|
|
|
|
|
|
|
# bInterfaceProtocol = 2 |
33
|
|
|
|
|
|
|
# and |
34
|
|
|
|
|
|
|
# bInterfaceNumber = 1 |
35
|
|
|
|
|
|
|
return eval{ |
36
|
|
|
|
|
|
|
my $usb = Device::USB->new; |
37
|
|
|
|
|
|
|
my $device = undef; |
38
|
|
|
|
|
|
|
foreach my $d( $usb->list_devices ){ |
39
|
|
|
|
|
|
|
next if $d->idVendor != $cfg{vid}; |
40
|
|
|
|
|
|
|
next if $d->idProduct != $cfg{pid}; |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
foreach my $cfg( $d->config ){ |
43
|
|
|
|
|
|
|
foreach my $ifs( $cfg->interfaces ){ |
44
|
|
|
|
|
|
|
foreach my $if( @$ifs ){ |
45
|
|
|
|
|
|
|
if( $if->bInterfaceNumber == 1 ){ |
46
|
|
|
|
|
|
|
$device = $d; |
47
|
|
|
|
|
|
|
last; |
48
|
|
|
|
|
|
|
} |
49
|
|
|
|
|
|
|
} |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
die "Device TEMPer1F not found!\n" unless $device; |
55
|
|
|
|
|
|
|
$device->open() or die "Can not open the device TEMPer1F!\n"; |
56
|
|
|
|
|
|
|
0 == $device->set_configuration(1) || |
57
|
|
|
|
|
|
|
die "Cannot set configuration for device TEMPer1F!\n"; |
58
|
|
|
|
|
|
|
0 == $device->claim_interface(1) || |
59
|
|
|
|
|
|
|
die "Cannot claim interface 1 for device TEMPer1F!\n"; |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
$self->{device} = $device; |
62
|
|
|
|
|
|
|
$self; |
63
|
|
|
|
|
|
|
} |
64
|
|
|
|
|
|
|
} |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
# fetch temperature in °C ################################################# |
67
|
|
|
|
|
|
|
sub fetch{ |
68
|
|
|
|
|
|
|
my $self = shift; |
69
|
|
|
|
|
|
|
if(! ref $self ){ |
70
|
|
|
|
|
|
|
$self = __PACKAGE__->new or die $@; |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
$self->_control(); |
74
|
|
|
|
|
|
|
8 == $self->{device}->interrupt_read( |
75
|
|
|
|
|
|
|
$self->{cfg}{epi}, |
76
|
|
|
|
|
|
|
$self->{buffer}, |
77
|
|
|
|
|
|
|
8, |
78
|
|
|
|
|
|
|
$self->{cfg}{tim} |
79
|
|
|
|
|
|
|
) || die "Cannot read the the temperature!\n"; |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
my $r = [unpack "C8", $self->{buffer}]; |
82
|
|
|
|
|
|
|
return sprintf "%0.2f", $r->[4] + $r->[5]/256; |
83
|
|
|
|
|
|
|
} |
84
|
|
|
|
|
|
|
|
85
|
|
|
|
|
|
|
############################ private methods ############################## |
86
|
|
|
|
|
|
|
# set up a control message |
87
|
|
|
|
|
|
|
sub _control{ |
88
|
|
|
|
|
|
|
my $self = shift; |
89
|
|
|
|
|
|
|
my $buffer = pack("C8", 0x1,0x80,0x33,0x1,0x0,0x0,0x0,0x0); |
90
|
|
|
|
|
|
|
my $check = $self->{device}->control_msg( |
91
|
|
|
|
|
|
|
0x21, |
92
|
|
|
|
|
|
|
0x09, |
93
|
|
|
|
|
|
|
0x0200, |
94
|
|
|
|
|
|
|
0x01, |
95
|
|
|
|
|
|
|
$buffer, |
96
|
|
|
|
|
|
|
8, |
97
|
|
|
|
|
|
|
$self->{cfg}{tim} |
98
|
|
|
|
|
|
|
); |
99
|
|
|
|
|
|
|
die "Cannot setup a control_message!\n" if $check != 8; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
1;######################################################################### |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
# my $temper = Device::USB::TEMPer1F->new or die $@; |
106
|
|
|
|
|
|
|
# print $temper->fetch; |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
__END__ |