Advertisement
rwhiffen

light-strike

Sep 30th, 2011
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.73 KB | None | 0 0
  1. #!/usr/bin/perl
  2. #
  3. # Light Strike reader
  4. #
  5. # Rich Whiffen <http://rich.whiffen.org>  AND
  6. # portions are/where Copyright (C) 2007 Kenneth L. Root. <http://the-b.org>
  7. # I stole almost all of this from http://the-b.org/Usb-uirt-config.pl
  8. #
  9. # And since Kenneth's code was, so to is my hacks on top of it:
  10. #
  11. # This program is free software: you can redistribute it and/or modify
  12. # it under the terms of the GNU General Public License as published by
  13. # the Free Software Foundation, either version 3 of the License, or
  14. # (at your option) any later version.
  15. #
  16. # This program is distributed in the hope that it will be useful,
  17. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. # GNU General Public License for more details.
  20. #
  21. # You should have received a copy of the GNU General Public License
  22. # along with this program.  If not, see <http://www.gnu.org/licenses/>.
  23.  
  24.  
  25. use strict;
  26. use warnings;
  27.  
  28. use Device::SerialPort;
  29. use Getopt::Long;
  30. use Pod::Usage;
  31.  
  32. use constant TIMEOUT_DEFAULT => 100;
  33. use constant TRANSMITTING => 0x20;
  34. use constant CMDOK => 0x21;
  35. use constant CSERROR => 0x80;
  36. use constant TOERROR => 0x81;
  37. use constant CMDERROR => 0x82;
  38.  
  39.  
  40. my %config = (
  41.         'port' => '/dev/ttyUSB0',
  42.         'slot' => 0
  43. );
  44.  
  45. my $port;
  46.  
  47. my $slots = -1;
  48.  
  49. sub openPort {
  50.   my($portName) = @_;
  51.  
  52.   my $port = new Device::SerialPort($portName)
  53.         || die "Can't open $portName: $!\n";
  54.  
  55.   $port->read_char_time(0);
  56.   $port->read_const_time(1000);
  57.  
  58.   return $port;
  59. }
  60.  
  61.  
  62.  
  63. sub setUIRMode {
  64.   sendCommand(0x20);
  65.   my @res = readCommand(1);
  66.   die "Cannot set UIR mode" if ($res[0] != CMDOK);
  67. }
  68.  
  69. sub printCommand {
  70.   my ($prefix, @cmd) = @_;
  71.  
  72.   print "$prefix: ";
  73.  
  74.   map { printf "0x%02x ", $_ } @cmd;
  75.  
  76.   print "\n";
  77. }
  78.  
  79. sub readCommand {
  80.   my($numBytes) = @_;
  81.  
  82.   my $timeout = TIMEOUT_DEFAULT;
  83.   my $buffer;
  84.   my $chars = 0;
  85.  
  86.   while ($timeout > 0) {
  87.     my ($count, $saw) = $port->read(255);
  88.     if ($count > 0) {
  89.       $chars += $count;
  90.       $buffer .= $saw;
  91.       if ($chars >= $numBytes) {
  92.         my @cmd = map { ord } unpack("(Z)*", $buffer);
  93.  
  94.         printCommand("recv", @cmd) if $config{'debug'};
  95.  
  96.         return @cmd;
  97.       }
  98.     } else {
  99.       $timeout--;
  100.     }
  101.   }
  102.  
  103.   if ($timeout == 0) {
  104.     die "Couldn't communicate with USB-UIRT device after ". TIMEOUT_DEFAULT ." seconds.\n";
  105.   }
  106. }
  107.  
  108.  
  109. print "Opening port: ". $config{'port'} ."\n";
  110. $port = openPort($config{'port'});
  111.  
  112. print "Opened port ". $config{'port'} ."\n";
  113.  
  114. #if (exists $config{'get-code'}) {
  115. #  setUIRMode;
  116.   print "Shoot the gun at the IR Sensor\n";
  117.   my @res = readCommand(60);
  118.   printCommand("IR Code", @res);
  119.   exit(0);
  120. #}
  121.  
  122.  
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement