From 041b40ba9d04693394c1472224cbb4285a0280c3 Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Wed, 8 Dec 2010 13:13:50 +0100 Subject: [PATCH] WIP: Towards internal timekeeping. --- SCX/Car.pm | 39 +++++++++++++++++++++++++++++++++++++-- SCX/Reader.pm | 46 ++++++++++++++++++++++++++++------------------ SCX/Track.pm | 17 +++++++++++++++-- gui.pl | 26 ++++++++++++++++---------- 4 files changed, 96 insertions(+), 32 deletions(-) diff --git a/SCX/Car.pm b/SCX/Car.pm index 4ef5543..c6e2aaa 100644 --- a/SCX/Car.pm +++ b/SCX/Car.pm @@ -50,6 +50,11 @@ sub set_throttle { $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); } @@ -82,7 +87,7 @@ sub set_lap { 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; @@ -160,6 +165,36 @@ sub set_order { $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; diff --git a/SCX/Reader.pm b/SCX/Reader.pm index 08c7288..7498e0a 100644 --- a/SCX/Reader.pm +++ b/SCX/Reader.pm @@ -49,6 +49,8 @@ sub new { bless $self, $class; + $self->track->reset; + return $self; } @@ -64,6 +66,8 @@ sub read { 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"; @@ -94,6 +98,7 @@ sub read { 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) { @@ -112,7 +117,7 @@ sub log_bytes { $msg = defined $msg ? ' # ' . $msg : ''; - my $now = gettimeofday; + my $now = $self->{last_read_time}; if ($now - $self->{log_start} >= $LOG_ROTATE) { close $self->{logfh}; @@ -182,7 +187,7 @@ sub car_programming_packet { || $bytes[4] != 0xFF || $bytes[5] != 0xFF; - return $msg; + return $msg; # No need to handle this } sub reset_packet { @@ -194,6 +199,8 @@ sub reset_packet { || $bytes[4] != 0xAA || $bytes[5] != 0xAA; + $self->track->reset; + return $msg; # FIXME - to be implemented } @@ -226,6 +233,8 @@ sub lap_time_packet { || $bytes[4] & 0x01 || $bytes[5] & 0x01; +=comment + # Moving to internal timekeeping my $nonzero = grep { $_ != 0 } @bytes; my $car = $bytes[0]; @@ -248,6 +257,7 @@ sub lap_time_packet { # all-zeros packet is sent after the race setup } } +=cut return $msg; } @@ -270,7 +280,7 @@ sub race_setup_packet { + $bytes[2] & 0x0F * 16 + $bytes[3] & 0x0F); - return $msg; # FIXME - to be implemented + return $msg; } @@ -337,7 +347,7 @@ sub end_of_race_packet { $self->track->race_end; - return $msg; # FIXME - to be implemented + return $msg; } @@ -354,7 +364,7 @@ sub race_start_packet { $self->track->race_start; - return $msg; # FIXME - to be implemented + return $msg; } @@ -388,29 +398,29 @@ sub finish_line_packet { 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 { diff --git a/SCX/Track.pm b/SCX/Track.pm index c1f66a3..09db4b7 100644 --- a/SCX/Track.pm +++ b/SCX/Track.pm @@ -98,9 +98,8 @@ sub race_setup { $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); } } @@ -133,5 +132,19 @@ sub qualification_start { $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; diff --git a/gui.pl b/gui.pl index 7fcfc7f..6d3295e 100755 --- a/gui.pl +++ b/gui.pl @@ -37,20 +37,26 @@ if ($reader) { 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); -- 2.43.5