| 1 | #!/usr/bin/env perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | use blib; | ||
| 6 | use Device::SaleaeLogic; | ||
| 7 | |||
| 8 | my $done = 0; | ||
| 9 | |||
| 10 | my $g_id = 0; | ||
| 11 | sub on_connect { | ||
| 12 | my ($self, $id) = @_; | ||
| 13 | my $subname = 'on_connect'; | ||
| 14 | $done = 0; | ||
| 15 | $g_id = $id; | ||
| 16 | print "$subname: Device with id: $id connected\n"; | ||
| 17 | print "$subname: Device SDK ID: ", $self->get_device_id($id), "\n"; | ||
| 18 | print "$subname: Device type: Logic16\n" if $self->is_logic16($id); | ||
| 19 | print "$subname: Device type: Logic\n" if $self->is_logic($id); | ||
| 20 | print "$subname: Device channel count: ", $self->get_channel_count($id), "\n"; | ||
| 21 | print "$subname: Device sample rate: ", $self->get_sample_rate($id), " Hz\n"; | ||
| 22 | print "$subname: USB 2.0 is supported\n" if $self->is_usb2($id); | ||
| 23 | my $arr = $self->get_supported_sample_rates($id); | ||
| 24 | print "$subname: Supported sample rates: ", join(", ", @$arr), "\n"; | ||
| 25 | $arr = $self->get_active_channels($id); | ||
| 26 | print "$subname: Active channels: ", join(", ", @$arr), "\n"; | ||
| 27 | print "$subname: Use 5 Volts: ", $self->get_use5volts($id), "\n"; | ||
| 28 | } | ||
| 29 | |||
| 30 | sub on_disconnect { | ||
| 31 | my ($self, $id) = @_; | ||
| 32 | my $subname = 'on_disconnect'; | ||
| 33 | print "$subname: Device with id: $id disconnected\n"; | ||
| 34 | $done = 1; | ||
| 35 | if ($self->is_streaming($id)) { | ||
| 36 | $self->stop($id); | ||
| 37 | } | ||
| 38 | } | ||
| 39 | sub on_error { | ||
| 40 | my ($self, $id) = @_; | ||
| 41 | my $subname = 'on_error'; | ||
| 42 | print "$subname: Device with id: $id has error\n"; | ||
| 43 | } | ||
| 44 | sub on_readdata { | ||
| 45 | my ($self, $id, $data, $len) = @_; | ||
| 46 | my $subname = 'on_readdata'; | ||
| 47 | print "$subname: Device with id: $id reads data of length $len\n"; | ||
| 48 | if ($len > 0) { | ||
| 49 | use bytes; | ||
| 50 | print "Length: ", length($data), " from perl and given $len\n"; | ||
| 51 | return; | ||
| 52 | for (my $i = 0; $i < length($data); ++$i) { | ||
| 53 | printf "%02X", hex(substr($data, $i, 1)); | ||
| 54 | print "\n" if $i % 32 == 0; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | } | ||
| 58 | sub on_writedata { | ||
| 59 | my ($self, $id, $data, $len) = @_; | ||
| 60 | my $subname = 'on_writedata'; | ||
| 61 | print "$subname: Device with id: $id writes data of length $len\n"; | ||
| 62 | } | ||
| 63 | |||
| 64 | my $sl = Device::SaleaeLogic->new( | ||
| 65 | on_connect => \&on_connect, | ||
| 66 | on_disconnect => \&on_disconnect, | ||
| 67 | on_error => \&on_error, | ||
| 68 | on_readdata => \&on_readdata, | ||
| 69 | on_writedata => \&on_writedata, | ||
| 70 | verbose => 1, | ||
| 71 | ); | ||
| 72 | $sl->begin; | ||
| 73 | |||
| 74 | my $once = 0; | ||
| 75 | my $read_once = 0; | ||
| 76 | my $t1 = time; | ||
| 77 | until ($done) { | ||
| 78 | sleep 5; | ||
| 79 | if ($g_id > 0 and not $once) { | ||
| 80 | my $subname = 'main'; | ||
| 81 | print "$subname: Device SDK ID: ", $sl->get_device_id($g_id), "\n"; | ||
| 82 | print "$subname: Device type: Logic16\n" if $sl->is_logic16($g_id); | ||
| 83 | print "$subname: Device type: Logic\n" if $sl->is_logic($g_id); | ||
| 84 | print "$subname: Device channel count: ", $sl->get_channel_count($g_id), "\n"; | ||
| 85 | print "$subname: Device sample rate: ", $sl->get_sample_rate($g_id), " Hz\n"; | ||
| 86 | print "$subname: USB 2.0 is supported\n" if $sl->is_usb2($g_id); | ||
| 87 | my $arr = $sl->get_supported_sample_rates($g_id); | ||
| 88 | print "$subname: Supported sample rates: ", join(", ", @$arr), "\n"; | ||
| 89 | $sl->set_sample_rate($g_id, $arr->[0]); | ||
| 90 | sleep 1; | ||
| 91 | print "$subname: Device sample rate is now: ", $sl->get_sample_rate($g_id), " Hz\n"; | ||
| 92 | $arr = $sl->get_active_channels($g_id); | ||
| 93 | print "$subname: Active channels: ", join(", ", @$arr), "\n"; | ||
| 94 | print "$subname: Use 5 Volts: ", $sl->get_use5volts($g_id), "\n"; | ||
| 95 | $once++; # once is 1 | ||
| 96 | } | ||
| 97 | my $t2 = time; | ||
| 98 | if ($g_id > 0 and ($t2 - $t1) > 10) { | ||
| 99 | unless ($sl->is_streaming($g_id)) { | ||
| 100 | if (not $read_once) { | ||
| 101 | $sl->read_start($g_id); | ||
| 102 | print "Starting Read\n"; | ||
| 103 | } | ||
| 104 | } | ||
| 105 | if (($t2 - $t1) > 30) { | ||
| 106 | if ($sl->is_streaming($g_id)) { | ||
| 107 | $sl->stop($g_id); | ||
| 108 | print "Stopping Read\n"; | ||
| 109 | $t1 = time; | ||
| 110 | $read_once++; # read_once is 1 | ||
| 111 | } | ||
| 112 | } | ||
| 113 | } | ||
| 114 | } | ||
| 115 | print "Done\n"; |