From: Jan "Yenya" Kasprzak Date: Sun, 13 Feb 2011 22:29:47 +0000 (+0100) Subject: Log replay made internal. X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?a=commitdiff_plain;h=6476b9984754241f18fe4268543016776518c386;p=slotcarman.git Log replay made internal. --- diff --git a/replay_log b/replay_log deleted file mode 100755 index 763a868..0000000 --- a/replay_log +++ /dev/null @@ -1,52 +0,0 @@ -#!/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; - $_ = ; -} - diff --git a/slotcarman b/slotcarman index 0efe3f3..5e3a04f 100755 --- a/slotcarman +++ b/slotcarman @@ -3,39 +3,62 @@ 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'); @@ -50,8 +73,11 @@ $track->car(0)->set_driver('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 @@ -107,3 +133,34 @@ sub scx_read { 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; +}