bless $self, $class;
+ $self->track->reset;
+
return $self;
}
die "Read error on $self->{portname}: $!"
if !$bytes_read;
+ $self->{last_read_time} = gettimeofday;
+
my @bytes = unpack("C*", $data);
# print join(' ', map { sprintf(" %02x", $_) } @bytes), "\n";
my @packet = splice @bytes, 0, $PACKET_SIZE+1;
my $rv = $self->handle_packet(@packet);
$self->log_bytes(\@packet, $rv);
+ $self->track->packet_received($self->{last_read_time});
}
if (@bad_bytes) {
while (@bytes && $bytes[0] != 0x55) {
$msg = defined $msg ? ' # ' . $msg : '';
- my $now = gettimeofday;
+ my $now = $self->{last_read_time};
if ($now - $self->{log_start} >= $LOG_ROTATE) {
close $self->{logfh};
|| $bytes[4] != 0xFF
|| $bytes[5] != 0xFF;
- return $msg;
+ return $msg; # No need to handle this
}
sub reset_packet {
|| $bytes[4] != 0xAA
|| $bytes[5] != 0xAA;
+ $self->track->reset;
+
return $msg; # FIXME - to be implemented
}
|| $bytes[4] & 0x01
|| $bytes[5] & 0x01;
- return $msg; # FIXME - to be implemented
+=comment
+ # Moving to internal timekeeping
+ my $nonzero = grep { $_ != 0 } @bytes;
+
+ my $car = $bytes[0];
+ my $round = 256*$bytes[1] + $bytes[2]
+ + ($bytes[3] & 2 ? 256 : 0)
+ + ($bytes[3] & 1 ? 1 : 0);
+ my $time = 256*$bytes[4] + $bytes[5]
+ + ($bytes[3] & 8 ? 256 : 0)
+ + ($bytes[3] & 4 ? 1 : 0);
+ if ($time == 65535) {
+ $self->track->car($car)->enter_pit_lane;
+ } else {
+ $time *= 0.01024;
+
+ if ($nonzero) {
+ $self->track->car($car)->set_lap($round);
+ $self->track->car($car)->set_laptime($time);
+ } else {
+ # FIXME - probably reset race time or whatever
+ # all-zeros packet is sent after the race setup
+ }
+ }
+=cut
+
+ return $msg;
}
|| $bytes[4] != 0xFF
|| $bytes[5] != 0xFF;
- return $msg; # FIXME - to be implemented
+ $self->track->race_setup($bytes[0] == 0x00
+ ? 0
+ : ($bytes[1] & 0x0F) * 256
+ + ($bytes[2] & 0x0F) * 16
+ + ($bytes[3] & 0x0F));
+
+ return $msg;
}
|| $bytes[4] != 0xFF
|| $bytes[5] != 0xFF;
- return $msg; # FIXME - to be implemented
+ $self->track->qualification_start;
+
+ return $msg;
}
|| $bytes[4] != 0xFF
|| $bytes[5] != 0xFF;
- return $msg; # FIXME - to be implemented
+ $self->track->race_end;
+
+ return $msg;
}
|| $bytes[4] != 0xAA
|| $bytes[5] != 0xAA;
- return $msg; # FIXME - to be implemented
+ $self->track->race_start;
+
+ return $msg;
}
my $msg = 'Strange finish_line packet'
if $fail;
- return $msg; # FIXME - to be implemented
-}
+ my $regular = 1;
+ my @cars_finished;
+ for my $i (0..5) {
+ my $byte = $bytes[$i];
+ $regular = 0
+ if $byte != 0xAA && $byte != 0xE7 && $byte != 0xFE;
+
+ push @cars_finished, $i if $byte == 0xE7;
+ }
+
+ $self->track->finish_line(
+ $self->{last_read_time},
+ $regular,
+ @cars_finished
+ );
+
+ return $msg;
+}
sub controller_status_packet {
my ($self, @bytes) = @_;
my $fail;
for my $byte (@bytes) {
- next if $byte == 0xA0;
+ next if $byte == 0xAA;
$fail = 1
if ($byte & 0xC0) != 0xC0
|| ($byte & 0x0F) > 12
my $byte = $bytes[$car];
if ($byte == 0xAA) {
- $self->track->car($car)->set_throttle(undef);
+ $self->track->car($car)->set_throttle(undef, undef,
+ $self->{last_read_time});
next;
}
my $backbutton = !($byte & 0x10);
my $throttle = $byte & 0x0f;
- $self->track->car($car)->set_throttle($throttle);
+ $self->track->car($car)->set_throttle($throttle, $backbutton,
+ $self->{last_read_time});
$self->track->car($car)->set_light($light);
- $self->track->car($car)->set_backbutton($backbutton);
}
return $msg;