7 use Glib qw(TRUE FALSE);
11 my @controllers = (0, 0, 0, 0, 0, 0);
14 my $builder = Gtk2::Builder->new;
15 $builder->add_from_file('slotcarman.glade');
17 my $window = $builder->get_object('slotcarman');
19 $builder->connect_signals(undef);
27 my $reader = SCX::Reader->new({
28 portname => '/dev/ttyUSB0',
30 callback => \&do_packet,
40 Glib::IO->add_watch(fileno($reader->fh), 'in', \&scx_read, 1)
48 if ($data[0] == 0xff) { # controller status
49 for my $controller (1..6) {
50 my $byte = $data[$controller];
51 next if defined $controllers[$controller-1]
52 && $controllers[$controller-1] == $byte;
53 $controllers[$controller-1] = $byte;
55 my $progressbar = $builder->get_object(
56 'progressbar_controller'.$controller);
58 $progressbar->set_text('inactive');
59 $progressbar->set_fraction(0);
62 my $light = !($byte & 0x20);
63 my $backbutton = !($byte & 0x10);
64 my $speed = $byte & 0x0f;
66 my $text = ($backbutton ? '+' : '') . $speed;
67 $progressbar->set_text($text);
68 $progressbar->set_fraction($speed / 12);
70 } elsif ($data[0] == 0xd6) { # fuel status
72 $data[1] >> 4, $data[1] & 0x0f,
73 $data[2] >> 4, $data[2] & 0x0f,
74 $data[3] >> 4, $data[3] & 0x0f,
77 next if defined $controllers[$car-1]
78 &&$controllers[$car-1] == $fuel[$car];
80 my $progressbar = $builder->get_object(
81 'progressbar_fuel'.$car);
82 $progressbar->set_fraction($fuel[$car]/8);
85 print "Unknown packet",
86 (map { sprintf(" %02x", $_) } @data), "\n";
93 my $window = Gtk2::Window->new('toplevel');
94 $window->signal_connect(delete_event => sub { Gtk2->main_quit; return FALSE });
95 $window->set_title("Slot Cars Manager");
97 $window->set_border_width(10);
99 my $button = Gtk2::Button->new("Button 1");
100 $button->signal_connect(clicked => \&callback, 'button 1');
102 my $box1 = Gtk2::HBox->new(FALSE, 0);
104 $box1->pack_start($button, TRUE, TRUE, 0);
107 $button = Gtk2::Button->new("Button 2");
108 $button->signal_connect(clicked => \&callback, 'button 2');
109 $box1->pack_start($button, TRUE, TRUE, 0);
120 my ($button, $data) = @_;
122 print "Hello again - $data was pressed\n";