From e22f8cbb94df063c77cc9ee5f5bae839e252777d Mon Sep 17 00:00:00 2001 From: "Jan \"Yenya\" Kasprzak" Date: Fri, 3 Dec 2010 23:11:08 +0100 Subject: [PATCH] Rework for OO infrastructure --- SCX/Car.pm | 59 +++++++++++ SCX/GUI.pm | 81 +++++++++++++++ SCX/Reader.pm | 73 ++++++-------- SCX/Track.pm | 32 ++++++ gui.pl | 117 ++++------------------ img/throttle13.svg | 245 +++++++++++++++++++++++++++++++++++++++++++++ slotcarman.glade | 188 +++++++++++++++++----------------- 7 files changed, 566 insertions(+), 229 deletions(-) create mode 100644 SCX/Car.pm create mode 100755 SCX/GUI.pm create mode 100644 SCX/Track.pm create mode 100644 img/throttle13.svg diff --git a/SCX/Car.pm b/SCX/Car.pm new file mode 100644 index 0000000..e55676d --- /dev/null +++ b/SCX/Car.pm @@ -0,0 +1,59 @@ +#!/usr/bin/perl -w + +package SCX::Car; + +use strict; + +sub new { + my ($class, $args) = @_; + + my $self = { + gui => $args->{gui}, + throttle => -1, + fuel => -1, + order => $args->{order}, + }; + + bless $self, $class; + + $self->set_throttle(undef); + $self->set_fuel(undef); + + return $self; +} + +sub gui { return shift->{gui}; } + +sub set_throttle { + my ($self, $val) = @_; + + return if (!defined $self->{throttle} && !defined $val) + || (defined $self->{throttle} && defined $val + && $self->{throttle} == $val); + + $self->{throttle} = $val; + $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; + $self->gui->set_fuel($self->{order}, $val); +} + +sub set_light { + # TODO +} + +sub set_backbutton { + # TODO +} + +1; + diff --git a/SCX/GUI.pm b/SCX/GUI.pm new file mode 100755 index 0000000..9190fe3 --- /dev/null +++ b/SCX/GUI.pm @@ -0,0 +1,81 @@ +#!/usr/bin/perl -w + +package SCX::GUI; + +use strict; +use utf8; + +use Gtk2 '-init'; +use Glib qw(TRUE FALSE); + +use SCX::Reader; + +sub new { + my ($class, $args) = @_; + + my $self = { + throttle_images => [ + load_image_set('img/throttle%d.svg', 13, 100) + ], + fuel_images => [ + load_image_set('img/fuel%d.svg', 8, 100) + ], + builder => Gtk2::Builder->new, + }; + + bless $self, $class; + + $self->{builder}->add_from_file('slotcarman.glade'); + + $self->{builder}->connect_signals(undef); + + return $self; +} + +sub show { + my ($self) = @_; + + my $window = $self->{builder}->get_object('slotcarman'); + $window->show(); +} + +sub get_object { return shift->{builder}->get_object(@_); } + +sub load_image_set { + my ($pattern, $limit, $height) = @_; + + my @rv; + for my $i (0..$limit) { + my $file = sprintf($pattern, $i); + + my $dummy = Gtk2::Gdk::Pixbuf->new_from_file($file); + my $width = $dummy->get_width * $height / $dummy->get_height; + push @rv, Gtk2::Gdk::Pixbuf->new_from_file_at_scale( + $file, $width, $height, TRUE + ); + } + return @rv; +} + +sub set_throttle { + my ($self, $row, $val) = @_; + + $row++; + $val = 13 if !defined $val; + + my $image = $self->get_object("image_throttle$row"); + $image->set_from_pixbuf($self->{throttle_images}->[$val]); +} + +sub set_fuel { + my ($self, $row, $val) = @_; + + $row++; + $val = 0 if !defined $val; + + my $image = $self->get_object("image_fuel$row"); + $image->set_from_pixbuf($self->{fuel_images}->[$val]); +} + +1; + diff --git a/SCX/Reader.pm b/SCX/Reader.pm index c60ec95..ea012b4 100644 --- a/SCX/Reader.pm +++ b/SCX/Reader.pm @@ -42,7 +42,7 @@ sub new { log_gen => $log_gen, log_start => $now, starttime => $now, - callback => $callback, + track => $track, bytes => [], }; @@ -243,23 +243,17 @@ sub fuel_level_packet { || $bytes[2] & 0x0F > 8 || ($bytes[5] != 0xAA && $bytes[5] != 0xFF); -=comment - my @fuel = (0, - $data[1] >> 4, $data[1] & 0x0f, - $data[2] >> 4, $data[2] & 0x0f, - $data[3] >> 4, $data[3] & 0x0f, - ); - for my $car (1..6) { - next if defined $controllers[$car-1] - &&$controllers[$car-1] == $fuel[$car]; - - my $progressbar = $builder->get_object( - 'progressbar_fuel'.$car); - $progressbar->set_fraction($fuel[$car]/8); - } -=cut + my @fuel = ( + $bytes[1] >> 4, $bytes[1] & 0x0f, + $bytes[2] >> 4, $bytes[2] & 0x0f, + $bytes[3] >> 4, $bytes[3] & 0x0f, + ); - return $msg; # FIXME - to be implemented + for my $car (0..5) { + $track->car($car)->set_fuel($fuel[$car]); + } + + return $msg; } @@ -362,31 +356,30 @@ sub controller_status_packet { my $msg = 'Strange controller_status packet' if $fail; -=comment - for my $controller (1..6) { - my $byte = $data[$controller]; - next if defined $controllers[$controller-1] - && $controllers[$controller-1] == $byte; - $controllers[$controller-1] = $byte; - - my $progressbar = $builder->get_object( - 'progressbar_controller'.$controller); - if ($byte == 0xaa) { - $progressbar->set_text('inactive'); - $progressbar->set_fraction(0); - next; - } - my $light = !($byte & 0x20); - my $backbutton = !($byte & 0x10); - my $speed = $byte & 0x0f; - - my $text = ($backbutton ? '+' : '') . $speed; - $progressbar->set_text($text); - $progressbar->set_fraction($speed / 12); + my @fuel = ( + $bytes[1] >> 4, $bytes[1] & 0x0f, + $bytes[2] >> 4, $bytes[2] & 0x0f, + $bytes[3] >> 4, $bytes[3] & 0x0f, + ); + + for my $car (0..5) { + my $byte = $bytes[$car]; + + if ($byte == 0xAA) { + $track->car($car)->set_throttle(undef); + next; } -=cut - return $msg; # FIXME - to be implemented + my $light = !($byte & 0x20); + my $backbutton = !($byte & 0x10); + my $throttle = $byte & 0x0f; + + $track->car($car)->set_throttle($throttle); + $track->car($car)->set_light($light); + $track->car($car)->set_backbutton($backbutton); + } + + return $msg; } 1; diff --git a/SCX/Track.pm b/SCX/Track.pm new file mode 100644 index 0000000..ab12a44 --- /dev/null +++ b/SCX/Track.pm @@ -0,0 +1,32 @@ +#!/usr/bin/perl -w + +package SCX::Track; + +use strict; +use Carp; + +use SCX::Car; + +sub new { + my ($class, $args) = @_; + + my $self; + + $self->{gui} = $args->{gui} or croak; + $self->{race_running} = 0; + $self->{lap_counting_up} = 1; + + for my $i (0..5) { + $self->{cars}->[$i] = SCX::Car->new({ + gui => $self->{gui}, + order => $i, + }); + } + + bless $self, $class; +} + +sub car { return shift->{cars}->[shift]; } + +1; + diff --git a/gui.pl b/gui.pl index 1df07de..9311331 100755 --- a/gui.pl +++ b/gui.pl @@ -6,119 +6,40 @@ use utf8; use Gtk2 '-init'; use Glib qw(TRUE FALSE); +use SCX::GUI; +use SCX::Track; use SCX::Reader; -my @controllers = (0, 0, 0, 0, 0, 0); my $no_reader = 0; -my $builder = Gtk2::Builder->new; -$builder->add_from_file('slotcarman.glade'); +my $gui = SCX::GUI->new; +my $track = SCX::Track->new({ gui => $gui }); -my $window = $builder->get_object('slotcarman'); +if (!$no_reader) { + my $reader = SCX::Reader->new({ + portname => '/dev/ttyUSB0', + logfile => 'log', + track => $track, + }); -$builder->connect_signals(undef); -$window->show(); + Glib::IO->add_watch(fileno($reader->fh), 'in', \&scx_read, $reader); +} + +$gui->show; + +Gtk2->main(); + +exit 0; sub quit { Gtk2->main_quit; return FALSE; } -my $reader = SCX::Reader->new({ - portname => '/dev/ttyUSB0', - logfile => 'log', - callback => \&do_packet, -}) if !$no_reader; - sub scx_read { - my $event = shift; + my ($event, $reader) = @_; $reader->read(); return TRUE; } -Glib::IO->add_watch(fileno($reader->fh), 'in', \&scx_read, 1) - if !$no_reader; - -Gtk2->main(); - -sub do_packet { - my (@data) = @_; - - if ($data[0] == 0xff) { # controller status - for my $controller (1..6) { - my $byte = $data[$controller]; - next if defined $controllers[$controller-1] - && $controllers[$controller-1] == $byte; - $controllers[$controller-1] = $byte; - - my $progressbar = $builder->get_object( - 'progressbar_controller'.$controller); - if ($byte == 0xaa) { - $progressbar->set_text('inactive'); - $progressbar->set_fraction(0); - next; - } - my $light = !($byte & 0x20); - my $backbutton = !($byte & 0x10); - my $speed = $byte & 0x0f; - - my $text = ($backbutton ? '+' : '') . $speed; - $progressbar->set_text($text); - $progressbar->set_fraction($speed / 12); - } - } elsif ($data[0] == 0xd6) { # fuel status - my @fuel = (0, - $data[1] >> 4, $data[1] & 0x0f, - $data[2] >> 4, $data[2] & 0x0f, - $data[3] >> 4, $data[3] & 0x0f, - ); - for my $car (1..6) { - next if defined $controllers[$car-1] - &&$controllers[$car-1] == $fuel[$car]; - - my $progressbar = $builder->get_object( - 'progressbar_fuel'.$car); - $progressbar->set_fraction($fuel[$car]/8); - } - } else { - print "Unknown packet", - (map { sprintf(" %02x", $_) } @data), "\n"; - } -} - -__END__ -use Gtk2 '-init'; - -my $window = Gtk2::Window->new('toplevel'); -$window->signal_connect(delete_event => sub { Gtk2->main_quit; return FALSE }); -$window->set_title("Slot Cars Manager"); - -$window->set_border_width(10); - -my $button = Gtk2::Button->new("Button 1"); -$button->signal_connect(clicked => \&callback, 'button 1'); - -my $box1 = Gtk2::HBox->new(FALSE, 0); -$window->add($box1); -$box1->pack_start($button, TRUE, TRUE, 0); - -$button->show; -$button = Gtk2::Button->new("Button 2"); -$button->signal_connect(clicked => \&callback, 'button 2'); -$box1->pack_start($button, TRUE, TRUE, 0); - -$button->show; -$box1->show; -$window->show; - -Gtk2->main(); - - -sub callback -{ - my ($button, $data) = @_; - - print "Hello again - $data was pressed\n"; -} - diff --git a/img/throttle13.svg b/img/throttle13.svg new file mode 100644 index 0000000..c3a4505 --- /dev/null +++ b/img/throttle13.svg @@ -0,0 +1,245 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + diff --git a/slotcarman.glade b/slotcarman.glade index 45c33e0..e31b056 100644 --- a/slotcarman.glade +++ b/slotcarman.glade @@ -267,25 +267,88 @@ - + + True + gtk-missing-image + + + 10 + 11 + 1 + 2 + - + + True + gtk-missing-image + + + 10 + 11 + 2 + 3 + - + + True + gtk-missing-image + + + 10 + 11 + 3 + 4 + - + + True + gtk-missing-image + + + 10 + 11 + 4 + 5 + - + + True + gtk-missing-image + + + 10 + 11 + 5 + 6 + - + + True + gtk-missing-image + + + 10 + 11 + 6 + 7 + - + + True + gtk-missing-image + + + 9 + 10 + 1 + 2 + @@ -441,139 +504,82 @@ - - True - - - 10 - 11 - 1 - 2 - + - - True - - - 10 - 11 - 2 - 3 - + - - True - - - 10 - 11 - 3 - 4 - + - - True - - - 10 - 11 - 4 - 5 - + - - True - - - 10 - 11 - 5 - 6 - + - - True - - - 10 - 11 - 6 - 7 - + - - True - bottom-to-top - - - 8 - 9 - 1 - 2 - + - + True - bottom-to-top + gtk-missing-image - 8 - 9 + 9 + 10 2 3 - + True - bottom-to-top + gtk-missing-image - 8 - 9 + 9 + 10 3 4 - + True - bottom-to-top + gtk-missing-image - 8 - 9 + 9 + 10 4 5 - + True - bottom-to-top + gtk-missing-image - 8 - 9 + 9 + 10 5 6 - + True - bottom-to-top + gtk-missing-image - 8 - 9 + 9 + 10 6 7 -- 2.43.5