From 4d7f9bafe67a3ef964e0a3ccc319226e1f997a0e Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Tue, 7 Dec 2010 19:58:07 +0100 Subject: [PATCH] More GUI work. --- SCX/Car.pm | 58 +++++++++++++++++++++++++++--------------------------- SCX/GUI.pm | 2 ++ gui.pl | 52 ++++++++++++++++++++++++++++++------------------ 3 files changed, 64 insertions(+), 48 deletions(-) diff --git a/SCX/Car.pm b/SCX/Car.pm index 8bb7bb9..75aa503 100644 --- a/SCX/Car.pm +++ b/SCX/Car.pm @@ -10,8 +10,13 @@ sub new { my $self = { gui => $args->{gui}, - throttle => -1, - fuel => -1, + driver => $args->{driver} || '--', + throttle => undef, + fuel => 0, + lap => 0, + laptime => 0, + avg_lap => 0, + car_img => $args->{car_img}, id => $args->{id}, track => $args->{track}, }; @@ -25,14 +30,26 @@ sub gui { return shift->{gui}; } sub track { return shift->{track}; } +sub same { + my ($self, $key, $val) = @_; + + return 1 if !defined $self->{$key} && !defined $val + || (defined $self->{$key} && defined $val + && $self->{$key} eq $val); + + $self->{$key} = $val; + return undef; +} + sub set_throttle { my ($self, $val) = @_; - return if (!defined $self->{throttle} && !defined $val) - || (defined $self->{throttle} && defined $val - && $self->{throttle} == $val); + return if $self->same('throttle', $val); - $self->{throttle} = $val; + if ($self->track->{start_in_progress} && $val) { + $self->{early_start} = 1; + $self->gui->set_driver('Early start!'); + } $self->gui->set_throttle($self->{order}, $val); } @@ -40,11 +57,7 @@ sub set_throttle { sub set_fuel { my ($self, $val) = @_; - return if (!defined $self->{fuel} && !defined $val) - || (defined $self->{fuel} && defined $val - && $self->{fuel} == $val); - - $self->{fuel} = $val; + return if $self->same('fuel', $val); $self->gui->set_fuel($self->{order}, $val); } @@ -59,22 +72,14 @@ sub set_backbutton { sub set_model { my ($self, $name) = @_; - return if (!defined $self->{car_icon} && !defined $name) - || (defined $self->{car_icon} && defined $name - && $self->{car_icon} eq $name); - - $self->{car_icon} = $name; + return if $self->same('car_img', $name); $self->gui->set_car_icon($self->{order}, $name); } sub set_lap { my ($self, $lap_nr) = @_; - return if (!defined $self->{lap} && !defined $lap_nr) - || (defined $self->{lap} && defined $lap_nr - && $self->{lap} == $lap_nr); - - $self->{lap} = $lap_nr; + 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; @@ -122,12 +127,8 @@ sub set_laptime { sub set_driver { my ($self, $driver_name) = @_; - return if (!defined $self->{driver} && !defined $driver_name) - || (defined $self->{driver} && defined $driver_name - && $self->{driver} == $driver_name); - - $self->{driver} = $driver_name; - $self->gui->set_name($self->{order}, $driver_name || '--'); + return if $self->same('driver', $driver_name); + $self->gui->set_name($self->{order}, $driver_name); } sub enter_pit_lane { @@ -153,8 +154,7 @@ sub set_order { $self->{order} = $pos; - $self->gui->set_name($self->{order}, $self->{driver} - || '--'); + $self->gui->set_name($self->{order}, $self->{driver}); $self->gui->set_car_icon($self->{order}, $self->{car_icon}); $self->gui->set_throttle($self->{order}, $self->{throttle}); $self->gui->set_lap($self->{order}, defined $self->{lap} diff --git a/SCX/GUI.pm b/SCX/GUI.pm index 3d500f6..83783ae 100755 --- a/SCX/GUI.pm +++ b/SCX/GUI.pm @@ -175,6 +175,8 @@ sub set_label { sub set_image { my ($self, $prefix, $row, $imgset, $value) = @_; + return if !defined $value; + ++$row if defined $row && $row =~ /\A\d+\z/xms; my $name = defined $row ? $prefix.$row : $prefix; diff --git a/gui.pl b/gui.pl index 0ea57b3..7fcfc7f 100755 --- a/gui.pl +++ b/gui.pl @@ -10,20 +10,17 @@ use SCX::GUI; use SCX::Track; use SCX::Reader; -my $no_reader = 0; - -my $gui = SCX::GUI->new; +my $gui = SCX::GUI->new({ img_height => 80 }); my $track = SCX::Track->new({ gui => $gui }); -if (!$no_reader) { - my $reader = SCX::Reader->new({ +my $reader; +eval { + $reader = SCX::Reader->new({ portname => '/dev/ttyUSB0', logfile => 'log', track => $track, }); - - Glib::IO->add_watch(fileno($reader->fh), 'in', \&scx_read, $reader); -} +}; $track->car(0)->set_model('F1 Ferrari'); $track->car(1)->set_model('F1 McLaren'); @@ -32,21 +29,38 @@ $track->car(3)->set_model('Citroen Xsara'); $track->car(4)->set_model('Citroen C4'); $track->car(5)->set_model('Ford Focus'); -$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(8.193); - $track->car(0)->set_driver('Ivi'); $track->car(1)->set_driver('Filípek'); $track->car(2)->set_driver('Táta'); +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); + + $track->car(0)->set_fuel(6); + $track->car(1)->set_fuel(2); + $track->car(2)->set_fuel(8); + + $track->car(0)->set_throttle(0); + $track->car(1)->set_throttle(8); + $track->car(2)->set_throttle(12); +} + $gui->show; Gtk2->main(); -- 2.43.5