package SCX::Reader;
+use strict;
+
use Time::HiRes qw(gettimeofday tv_interval);
use FileHandle;
+use IO::Handle;
+use POSIX;
use SCX::CRC;
our $PACKET_SIZE = 9; # 9 bytes + 0x05
sub new {
my ($class, $args) = @_;
- my $callback = $args->{callback}
- or die "callback arg not defined";
-
my $portname = $args->{portname}
or die "portname not specified";
die "stty died with code $? (no permissions?)";
}
- open my $tty, '<:raw', $portname
+ sysopen(my $fh, $portname, O_RDONLY|O_NONBLOCK)
or die "Can't open $portname: $!";
my $logfile = $args->{logfile};
my $self = {
portname => $portname,
- fh => $tty,
+ fh => $fh,
logfile => $logfile,
logfh => $logfh,
log_gen => $log_gen,
log_start => $now,
starttime => $now,
- track => $track,
+ track => $args->{track},
bytes => [],
};
sub fh { return shift->{fh}; }
+sub track { return shift->{track}; }
+
sub read {
my ($self) = @_;
my @bytes = unpack("C*", $data);
+ # print join(' ', map { sprintf(" %02x", $_) } @bytes), "\n";
push @{ $self->{bytes} }, @bytes;
@bytes = @{ $self->{bytes} };
}
my @packet = splice @bytes, 0, $PACKET_SIZE+1;
- my $rv = &{ $self->{callback} }(@packet);
- $self->log_bytes(@packet, $rv);
+ my $rv = $self->handle_packet(@packet);
+ $self->log_bytes(\@packet, $rv);
+ }
+ if (@bad_bytes) {
+ while (@bytes && $bytes[0] != 0x55) {
+ push @bad_bytes, shift @bytes;
+ }
+ $self->log_bytes(\@bad_bytes, "cannot parse packet");
}
- $self->log_bad_bytes(\@bad_bytes, "Cannot parse packet");
@{ $self->{bytes} } = @bytes;
}
if ($now - $self->{log_start} >= $LOG_ROTATE) {
close $self->{logfh};
$self->{log_gen} = $self->{log_gen} ? 0 : 1;
- open my $fh, '>', $logfile . '.' . $self->{log_gen}
- or die "Can't open $logfile.$self->{log_gen}: $!";
+ open my $fh, '>', $self->{logfile} . '.' . $self->{log_gen}
+ or die "Can't open $self->{logfile}.$self->{log_gen}: $!";
$self->{logfh} = $fh;
$self->{log_start} = $now;
}
$self->{logfh}->print(sprintf('% 10.3f', $now - $self->{starttime}),
(map { sprintf(" %02x", $_) } @$bytes),
$msg, "\n");
+ $self->{logfh}->flush;
}
our %COMMANDS = (
my ($self, @data) = @_;
my $cmd = $data[1];
- my @args = $data[2..7];
+ my @args = @data[2..7];
my $sub = $COMMANDS{$cmd};
return "Unknown packet"
my ($self, @bytes) = @_;
my $msg = 'Strange car programming packet'
- if $bytes[0] & 0xF8 != 0 || $bytes[0] & 0x07 > 5
+ if ($bytes[0] & 0xF8) != 0 || ($bytes[0] & 0x07) > 5
|| $bytes[1] != 0xFE
|| $bytes[2] != 0xFF
|| $bytes[3] != 0xFF
my ($self, @bytes) = @_;
my $msg = 'Strange standings packet'
- if $bytes[0] & 0x07 > 5
- || $bytes[1] & 0x07 > 5
- || $bytes[2] & 0x07 > 5
- || $bytes[3] & 0x07 > 5
- || $bytes[4] & 0x07 > 5
- || $bytes[5] & 0x07 > 5;
+ if ($bytes[0] != 0xFF && ($bytes[0] & 0x07) > 5)
+ || ($bytes[1] != 0xFF && ($bytes[1] & 0x07) > 5)
+ || ($bytes[2] != 0xFF && ($bytes[2] & 0x07) > 5)
+ || ($bytes[3] != 0xFF && ($bytes[3] & 0x07) > 5)
+ || ($bytes[4] != 0xFF && ($bytes[4] & 0x07) > 5)
+ || ($bytes[5] != 0xFF && ($bytes[5] & 0x07) > 5);
+
+ my @standings;
+
+ push @standings, map { $_ != 0xFF ? $_ & (0x07) : () } @bytes;
return $msg; # FIXME - to be implemented
}
if $bytes[0] > 5
|| $bytes[1] & 0x01
|| $bytes[2] & 0x01
- || $bytes[3] & 0xF8 != 0
+ || ($bytes[3] & 0xF0) != 0
|| $bytes[4] & 0x01
|| $bytes[5] & 0x01;
my $msg = 'Strange fuel_level packet'
if ($bytes[0] >> 4) > 8
- || $bytes[0] & 0x0F > 8
+ || ($bytes[0] & 0x0F) > 8
|| ($bytes[1] >> 4) > 8
- || $bytes[1] & 0x0F > 8
+ || ($bytes[1] & 0x0F) > 8
|| ($bytes[2] >> 4) > 8
- || $bytes[2] & 0x0F > 8
+ || ($bytes[2] & 0x0F) > 8
|| ($bytes[5] != 0xAA && $bytes[5] != 0xFF);
my @fuel = (
+ $bytes[0] >> 4, $bytes[0] & 0x0f,
$bytes[1] >> 4, $bytes[1] & 0x0f,
$bytes[2] >> 4, $bytes[2] & 0x0f,
- $bytes[3] >> 4, $bytes[3] & 0x0f,
);
for my $car (0..5) {
- $track->car($car)->set_fuel($fuel[$car]);
+ $self->track->car($car)->set_fuel($fuel[$car]);
}
return $msg;
my $fail;
for my $byte (@bytes) {
+ next if $byte == 0xA0;
$fail = 1
- if $byte & 0xC0 != 0xC0
- || $byte & 0x0F > 12
+ if ($byte & 0xC0) != 0xC0
+ || ($byte & 0x0F) > 12
}
my $msg = 'Strange controller_status packet'
my $byte = $bytes[$car];
if ($byte == 0xAA) {
- $track->car($car)->set_throttle(undef);
+ $self->track->car($car)->set_throttle(undef);
next;
}
my $backbutton = !($byte & 0x10);
my $throttle = $byte & 0x0f;
- $track->car($car)->set_throttle($throttle);
- $track->car($car)->set_light($light);
- $track->car($car)->set_backbutton($backbutton);
+ $self->track->car($car)->set_throttle($throttle);
+ $self->track->car($car)->set_light($light);
+ $self->track->car($car)->set_backbutton($backbutton);
}
return $msg;