]> www.fi.muni.cz Git - slotcarman.git/blob - slotcarman
Make GUI fit 800x600
[slotcarman.git] / slotcarman
1 #!/usr/bin/perl -w
2
3 use strict;
4 use utf8;
5
6 use Getopt::Std;
7
8 use Gtk2 '-init';
9 use Glib qw(TRUE FALSE);
10 use Time::HiRes qw(time usleep);
11
12 use SCX::GUI;
13 use SCX::Track;
14 use SCX::Reader;
15 use SCX::LogReader;
16 use SCX::RaceParser;
17
18 my %opt;
19 if (!getopts('b:e:ft:l:', \%opt)) {
20         usage();
21 }
22
23 if (($opt{t} && $opt{l})
24         || (!$opt{l} && ($opt{b} || $opt{e} || $opt{f}))) {
25         usage();
26 }
27
28 my $gui = SCX::GUI->new({ img_height => 70 });
29 my $track = SCX::Track->new({ gui => $gui, no_semaphore => $opt{f} });
30
31 my $logfile = 'log';
32
33 my $parser = SCX::RaceParser->new({
34         track => $track,
35         logfile => $opt{l} ? undef : $logfile,
36 });
37
38 my ($log_reader, $tty_reader);
39
40 if ($opt{l}) { # Log file replay mode
41         $log_reader = SCX::LogReader->new({
42                 filename   => $opt{l},
43                 begin_time => $opt{b},
44                 end_time   => $opt{e},
45         });
46 } else {
47         my $tty = $opt{t} || '/dev/ttyUSB0';
48
49         $tty_reader = eval {
50                 SCX::Reader->new({
51                         filename   => $tty,
52                         parser     => $parser,
53                 });
54         };
55
56         if ($@) {
57                 if ($opt{t}) {
58                         exit 1;
59                 }
60                 print STDERR "Demo mode only: $@";
61         }
62 }
63
64 $track->car(0)->set_model('F1 Ferrari');
65 $track->car(1)->set_model('F1 McLaren');
66 #$track->car(2)->set_model('F1 Williams');
67 $track->car(2)->set_model('Citroen Xsara');
68 $track->car(3)->set_model('Chevrolet Corvette gelb');
69 $track->car(4)->set_model('Ferrari 360 Red Bull');
70 $track->car(5)->set_model('Ferrari 360 Cric Crac');
71
72 $track->car(0)->set_driver('Ferrari');
73 $track->car(1)->set_driver('McLaren');
74 $track->car(2)->set_driver('Xsara');
75
76 if ($tty_reader) {
77         Glib::IO->add_watch(fileno($tty_reader->fh), 'in',
78                 \&scx_read, $tty_reader);
79 } elsif ($log_reader) {
80         Glib::Idle->add(\&log_read_start, [ $log_reader, $parser, $opt{f} ]);
81 } else {
82         # Just display some random values
83
84         $gui->rounds('5/13');
85         $gui->time('53.4');
86         $gui->best_lap('12.3');
87
88         my $now = time;
89         $track->{race_running_since} = $now;
90         $track->{race_running} = 1;
91
92         $track->finish_line($now + 0.5, 1, 2);
93         $track->finish_line($now + 0.7, 1, 1);
94         $track->finish_line($now + 4.9, 1, 1);
95         $track->finish_line($now + 5.3, 1, 2);
96         $track->finish_line($now + 7.2, 1, 1);
97         $track->finish_line($now + 8.1, 0, 2);
98 #       $track->car(0)->set_lap(13);
99 #       $track->car(1)->set_lap(11);
100 #       $track->car(2)->set_lap(10);
101 #       $track->car(3)->set_lap(undef);
102 #       $track->car(4)->set_lap(undef);
103 #       $track->car(5)->set_lap(undef);
104
105         $track->car(0)->set_laptime(9.12322);
106         $track->car(1)->set_laptime(15.313);
107         $track->car(2)->set_laptime(5.989);
108
109         $track->car(0)->set_fuel(6);
110         $track->car(1)->set_fuel(2);
111         $track->car(2)->set_fuel(8);
112
113         $track->car(0)->set_throttle(0);
114         $track->car(1)->set_throttle(8, 1);
115         $track->car(2)->set_throttle(12);
116 }
117
118 $gui->show;
119
120 Gtk2->main();
121
122 exit 0;
123
124 sub quit {
125         Gtk2->main_quit;
126         return FALSE;
127 }
128
129 sub scx_read {
130         my ($event, $fh, $reader) = @_;
131
132         $reader->read();
133         return TRUE;
134 }
135
136 sub usage {
137         die "Usage: $0 [-t tty_line]\n"
138                 . "or: $0 -l logfile [-b starttime] [-e endtime] [-f]\n"
139                 . "-f ... replay as fast as possible\n";
140 }
141
142
143 sub log_read_start {
144         my ($data) = @_;
145         my ($logreader, $parser, $fast) = @$data;
146
147         our ($starttime, $log_starttime);
148
149         my ($l_time, @data) = $logreader->get_data();
150         return FALSE if !$l_time;
151
152         my $now = time;
153         $starttime ||= $now;
154
155         $log_starttime ||= $l_time;
156
157         my $time_diff = ($l_time - $log_starttime) - ($now - $starttime);
158         if (!$fast && $time_diff > 0) {
159                 # print "sleeping for $time_diff s\n";
160                 usleep(1_000_000 * $time_diff);
161         }
162
163         $parser->add_data($l_time, @data);
164
165         return TRUE;
166 }