$self->{early_start} = 1;
$self->gui->set_driver('Early start!');
}
+
+ if ($self->{in_pit_lane} && defined $val && $val > 3) {
+ $self->leave_pit_lane;
+ }
+
$self->gui->set_throttle($self->{order}, $val);
}
return if $self->same('lap', $lap_nr);
$self->gui->set_lap($self->{order}, defined $lap_nr ? $lap_nr : '--');
if ($self->track->{race_running} && $self->{lap} > 1) {
- my $now = gettimeofday;
+ my $now = $self->{last_finish_time};
my $avg = ($now - $self->track->{race_running_since})
/ ($self->{lap} - 1);
$self->{avg_lap} = $avg;
$self->gui->enter_pit_lane;
}
}
-
+
+sub reset {
+ my ($self) = @_;
+
+ $self->set_lap(0);
+ $self->set_laptime(undef);
+ $self->{in_pit_lane} = 0;
+ $self->{early_start} = undef;
+}
+
+sub finish_line {
+ my ($self, $time, $regular) = @_;
+
+ return undef
+ if defined $self->{last_finish_time}
+ && $time - $self->{last_finish_time} < 2;
+
+ if ($regular) {
+ if (defined $self->{last_finish_time}) {
+ $self->set_laptime($time - $self->{last_finish_time});
+ }
+ } else {
+ $self->enter_pit_lane;
+ }
+
+ $self->{last_finish_time} = $time;
+ $self->set_lap(++$self->{lap});
+
+ return 1;
+}
+
1;
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;
+=comment
+ # Moving to internal timekeeping
my $nonzero = grep { $_ != 0 } @bytes;
my $car = $bytes[0];
# all-zeros packet is sent after the race setup
}
}
+=cut
return $msg;
}
+ $bytes[2] & 0x0F * 16
+ $bytes[3] & 0x0F);
- return $msg; # FIXME - to be implemented
+ return $msg;
}
$self->track->race_end;
- return $msg; # FIXME - to be implemented
+ return $msg;
}
$self->track->race_start;
- return $msg; # FIXME - to be implemented
+ return $msg;
}
my $msg = 'Strange finish_line packet'
if $fail;
- my $pit_lane_entry;
- my $too_fast;
+ my $regular = 1;
my @cars_finished;
for my $i (0..5) {
my $byte = $bytes[$i];
- $pit_lane_entry = 1 if $byte == 0xF0;
- $too_fast = 1 if $byte == 0xFF;
+ $regular = 0
+ if $byte != 0xAA && $byte != 0xE7 && $byte != 0xFE;
push @cars_finished, $i if $byte == 0xE7;
}
- if ($pit_lane_entry) {
- for my $car (@cars_finished) {
- $self->track->car($car)->enter_pit_lane;
- }
- } else {
- for my $car (@cars_finished) {
- $self->track->car($car)->leave_pit_lane;
- }
+ my $processed;
+ for my $car (@cars_finished) {
+ $processed ||= $self->track->car($car)->finish_line(
+ $self->{last_read_time}, $regular
+ );
}
- return $msg; # FIXME - to be implemented
+ if ($processed) {
+ $self->track->recalc_order;
+ }
+
+ return $msg;
}
sub controller_status_packet {
$self->{gui}->best_lap(undef);
for my $car (0..5) {
+ $self->car($car)->reset;
$self->car($car)->set_order($car);
- $self->car($car)->set_lap(0);
- $self->car($car)->set_laptime(undef);
}
}
$self->{gui}->best_lap(undef);
}
+sub packet_received {
+ my ($self, $time) = @_;
+
+ if ($self->{race_running}) {
+ $self->gui->time($time - $self->{race_running_since});
+ }
+}
+
+sub recalc_order {
+ my ($self) = @_;
+
+ # FIXME: Implement me
+}
+
1;
Glib::IO->add_watch(fileno($reader->fh), 'in', \&scx_read, $reader);
} else {
# Just display some random values
+
$gui->rounds('5/13');
$gui->time('53.4');
$gui->best_lap('12.3');
- $track->car(0)->set_lap(13);
- $track->car(1)->set_lap(11);
- $track->car(2)->set_lap(10);
- $track->car(3)->set_lap(undef);
- $track->car(4)->set_lap(undef);
- $track->car(5)->set_lap(undef);
-
- $track->car(0)->set_laptime(9.12322);
- $track->car(1)->set_laptime(15.313);
- $track->car(2)->set_laptime(5.989);
+ my $now = time;
+ $track->{race_running_since} = $now;
+ $track->{race_running} = 1;
+
+ $track->car(0)->finish_line($now + 2.5, 1);
+# $track->car(0)->set_lap(13);
+# $track->car(1)->set_lap(11);
+# $track->car(2)->set_lap(10);
+# $track->car(3)->set_lap(undef);
+# $track->car(4)->set_lap(undef);
+# $track->car(5)->set_lap(undef);
+
+# $track->car(0)->set_laptime(9.12322);
+# $track->car(1)->set_laptime(15.313);
+# $track->car(2)->set_laptime(5.989);
$track->car(0)->set_fuel(6);
$track->car(1)->set_fuel(2);