+++ /dev/null
-#!/usr/bin/perl -w
-
-use strict;
-
-use Getopt::Std;
-use FileHandle;
-use Time::HiRes qw(time usleep);
-
-my %opt;
-if (!getopts('b:e:fp', \%opt) || !$ARGV[0]) {
- die "Usage: $0 [-b starttime] [-e endtime] [-f] [-p] logfile\n"
- . "-f ... replay as fast as possible, without pausing\n"
- . "-p ... at the end, want for a keypress\n";
-}
-
-my $logfile = shift @ARGV;
-
-open my $logfh, '<', $logfile
- or die "Can't open $logfile: $!";
-
-my $start_prog_time = time;
-my $start_log_time;
-
-while (my $line = <$logfh>) {
- chomp $line;
- $line =~ s/\A\s+//xms;
- my ($l_time, @data) = split(/\s+/, $line);
-
- next if ($opt{b} && $opt{b} > $l_time);
- next if ($opt{e} && $opt{e} < $l_time);
-
- print pack("C*", map { hex $_ } @data);
- flush STDOUT;
-
- if (!$opt{f}) {
- $start_log_time ||= $l_time;
-
- my $log_delay = $l_time - $start_log_time;
- my $real_delay = time - $start_prog_time;
- if ($real_delay < $log_delay) {
- # print STDERR "usleep for ", $log_delay - $real_delay, "\n";
- usleep(1000_000 * ($log_delay - $real_delay));
- }
- }
-}
-
-if ($opt{p}) {
- print STDERR "Replay log finished - press a key: ";
- flush STDERR;
- $_ = <STDIN>;
-}
-
use strict;
use utf8;
+use Getopt::Std;
+
use Gtk2 '-init';
use Glib qw(TRUE FALSE);
+use Time::HiRes qw(time usleep);
use SCX::GUI;
use SCX::Track;
use SCX::Reader;
+use SCX::LogReader;
use SCX::RaceParser;
+my %opt;
+if (!getopts('b:e:ft:l:', \%opt)) {
+ usage();
+}
+
+if (($opt{t} && $opt{l})
+ || (!$opt{l} && ($opt{b} || $opt{e} || $opt{f}))) {
+ usage();
+}
+
my $gui = SCX::GUI->new({ img_height => 100 });
my $track = SCX::Track->new({ gui => $gui });
my $logfile = 'log';
-my $tty = '/dev/ttyUSB0';
-
-if (defined $ARGV[0] && $ARGV[0] eq '-') {
- $logfile = undef;
- $tty = '-';
-}
my $parser = SCX::RaceParser->new({
track => $track,
logfile => $logfile,
});
-my $reader = eval {
- SCX::Reader->new({
- filename => $tty,
- parser => $parser,
- });
-};
+my ($log_reader, $tty_reader);
-if ($@) {
- print STDERR "Demo mode only: $@";
+if ($opt{l}) { # Log file replay mode
+ $log_reader = SCX::LogReader->new({
+ filename => $opt{l},
+ begin_time => $opt{b},
+ end_time => $opt{e},
+ });
+} else {
+ my $tty = $opt{t} || '/dev/ttyUSB0';
+
+ $tty_reader = eval {
+ SCX::Reader->new({
+ filename => $tty,
+ parser => $parser,
+ });
+ };
+
+ if ($@) {
+ if ($opt{t}) {
+ exit 1;
+ }
+ print STDERR "Demo mode only: $@";
+ }
}
$track->car(0)->set_model('F1 Ferrari');
$track->car(1)->set_driver('McLaren');
$track->car(2)->set_driver('Xsara');
-if ($reader) {
- Glib::IO->add_watch(fileno($reader->fh), 'in', \&scx_read, $reader);
+if ($tty_reader) {
+ Glib::IO->add_watch(fileno($tty_reader->fh), 'in',
+ \&scx_read, $tty_reader);
+} elsif ($log_reader) {
+ Glib::Idle->add(\&log_read_start, [ $log_reader, $parser, $opt{f} ]);
} else {
# Just display some random values
return TRUE;
}
+sub usage {
+ die "Usage: $0 [-t tty_line]\n"
+ . "or: $0 -l logfile [-b starttime] [-e endtime] [-f]\n"
+ . "-f ... replay as fast as possible\n";
+}
+
+
+sub log_read_start {
+ my ($data) = @_;
+ my ($logreader, $parser, $fast) = @$data;
+
+ our ($starttime, $log_starttime);
+
+ my ($l_time, @data) = $logreader->get_data();
+ return FALSE if !$l_time;
+
+ my $now = time;
+ $starttime ||= $now;
+
+ $log_starttime ||= $l_time;
+
+ my $time_diff = ($l_time - $log_starttime) - ($now - $starttime);
+ if (!$fast && $time_diff > 0) {
+ # print "sleeping for $time_diff s\n";
+ usleep(1_000_000 * $time_diff);
+ }
+
+ $parser->add_data($l_time, @data);
+
+ return TRUE;
+}