From: Jan "Yenya" Kasprzak Date: Thu, 2 Dec 2010 00:59:11 +0000 (+0100) Subject: Proof-of-concept GUI X-Git-Url: https://www.fi.muni.cz/~kas/git//home/kas/public_html/git/?p=slotcarman.git;a=commitdiff_plain;h=68ff3ebd0073097de84fc55b22047cb5b9d98db7 Proof-of-concept GUI --- diff --git a/SCX/Reader.pm b/SCX/Reader.pm index 1b7c44f..5a98a6d 100644 --- a/SCX/Reader.pm +++ b/SCX/Reader.pm @@ -82,13 +82,23 @@ sub read { my @bad_bytes; - while (@bytes >= $PACKET_SIZE) { - if ($bytes[0] != 0x55 || $bytes[9] != 0x05 - || SCX::CRC::digest(@bytes[0..7]) != $bytes[8]) { + while (@bytes >= 2) { + if ($bytes[0] != 0x55) { push @bad_bytes, shift @bytes; next; } + my $cmd = $bytes[1]; + my $packet_size = $cmd >= 0x40 && $cmd <= 0x46 ? 4 : 9; + last if @bytes <= $packet_size; + + if ($bytes[$packet_size] != 0x05 + || SCX::CRC::digest(@bytes[0..$packet_size-2]) + != $bytes[$packet_size-1]) { + push @bad_bytes, shift @bytes; + next; + } + if (@bad_bytes) { $self->{logfh}->print("Cannot parse bytes", (map { sprintf(' %02x', $_) } @bad_bytes), @@ -97,8 +107,8 @@ sub read { } $self->{logfh}->print("Callback\n"); - &{ $self->{callback} }(@bytes[1..7]); - splice @bytes, 0, 10; + &{ $self->{callback} }(@bytes[1..$packet_size]); + splice @bytes, 0, $packet_size+1; } if (@bad_bytes) { $self->{logfh}->print("Cannot parse bytes", diff --git a/gui.pl b/gui.pl new file mode 100755 index 0000000..5e1411a --- /dev/null +++ b/gui.pl @@ -0,0 +1,122 @@ +#!/usr/bin/perl -w + +use strict; +use utf8; + +use Gtk2 '-init'; +use Glib qw(TRUE FALSE); + +use SCX::Reader; + +my @controllers = (0, 0, 0, 0, 0, 0); + +my $builder = Gtk2::Builder->new; +$builder->add_from_file('slotcarman.glade'); + +my $window = $builder->get_object('slotcarman'); + +$builder->connect_signals(undef); +$window->show(); + +sub quit { + Gtk2->main_quit; + return FALSE; +} + +my $reader = SCX::Reader->new({ + portname => '/dev/ttyUSB0', + logfile => 'log', + callback => \&do_packet, +}); + +sub scx_read { + my $event = shift; + + $reader->read(); + return TRUE; +} + +Glib::IO->add_watch(fileno($reader->fh), 'in', \&scx_read, 1); + +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/slotcarman.glade b/slotcarman.glade new file mode 100644 index 0000000..45c33e0 --- /dev/null +++ b/slotcarman.glade @@ -0,0 +1,600 @@ + + + + + + Slot Cars Race Manager + + + True + + + True + + + True + False + _File + True + + + True + + + gtk-new + True + False + True + True + + + + + gtk-open + True + False + True + True + + + + + gtk-save + True + False + True + True + + + + + gtk-save-as + True + False + True + True + + + + + True + False + + + + + gtk-quit + True + False + True + True + + + + + + + + + + True + False + _Edit + True + + + True + + + gtk-cut + True + False + True + True + + + + + gtk-copy + True + False + True + True + + + + + gtk-paste + True + False + True + True + + + + + gtk-delete + True + False + True + True + + + + + + + + + True + False + _View + True + + + + + True + False + _Help + True + + + True + + + gtk-about + True + False + True + True + + + + + + + + + + False + 0 + + + + + True + 7 + 11 + + + True + <span weight="ultrabold" size="large">1</span> + True + right + 1 + True + + + 1 + 2 + + + + + True + <span weight="ultrabold" size="large">2</span> + True + right + 1 + True + + + 2 + 3 + + + + + True + 0.50999999046325684 + <span weight="ultrabold" size="large">3</span> + True + right + 1 + True + + + 3 + 4 + + + + + True + <span weight="ultrabold" size="large">4</span> + True + right + 1 + True + + + 4 + 5 + + + + + True + <span weight="ultrabold" size="large">5</span> + True + right + 1 + True + + + 5 + 6 + + + + + True + <span weight="ultrabold" size="large">6</span> + True + right + 1 + True + + + 6 + 7 + + + + + True + Ivi + end + 25 + True + + + 1 + 2 + 1 + 2 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 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 + + + 8 + 9 + 2 + 3 + + + + + True + bottom-to-top + + + 8 + 9 + 3 + 4 + + + + + True + bottom-to-top + + + 8 + 9 + 4 + 5 + + + + + True + bottom-to-top + + + 8 + 9 + 5 + 6 + + + + + True + bottom-to-top + + + 8 + 9 + 6 + 7 + + + + + 1 + + + + + True + 2 + False + + + False + 2 + + + + + +