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},
};
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);
}
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);
}
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;
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 {
$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}
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');
$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();