7 use Glib qw(TRUE FALSE);
11 my @controllers = (0, 0, 0, 0, 0, 0);
13 my $builder = Gtk2::Builder->new;
14 $builder->add_from_file('slotcarman.glade');
16 my $window = $builder->get_object('slotcarman');
18 $builder->connect_signals(undef);
26 my $reader = SCX::Reader->new({
27 portname => '/dev/ttyUSB0',
29 callback => \&do_packet,
39 Glib::IO->add_watch(fileno($reader->fh), 'in', \&scx_read, 1);
46 if ($data[0] == 0xff) { # controller status
47 for my $controller (1..6) {
48 my $byte = $data[$controller];
49 next if defined $controllers[$controller-1]
50 && $controllers[$controller-1] == $byte;
51 $controllers[$controller-1] = $byte;
53 my $progressbar = $builder->get_object(
54 'progressbar_controller'.$controller);
56 $progressbar->set_text('inactive');
57 $progressbar->set_fraction(0);
60 my $light = !($byte & 0x20);
61 my $backbutton = !($byte & 0x10);
62 my $speed = $byte & 0x0f;
64 my $text = ($backbutton ? '+' : '') . $speed;
65 $progressbar->set_text($text);
66 $progressbar->set_fraction($speed / 12);
68 } elsif ($data[0] == 0xd6) { # fuel status
70 $data[1] >> 4, $data[1] & 0x0f,
71 $data[2] >> 4, $data[2] & 0x0f,
72 $data[3] >> 4, $data[3] & 0x0f,
75 next if defined $controllers[$car-1]
76 &&$controllers[$car-1] == $fuel[$car];
78 my $progressbar = $builder->get_object(
79 'progressbar_fuel'.$car);
80 $progressbar->set_fraction($fuel[$car]/8);
83 print "Unknown packet",
84 (map { sprintf(" %02x", $_) } @data), "\n";
91 my $window = Gtk2::Window->new('toplevel');
92 $window->signal_connect(delete_event => sub { Gtk2->main_quit; return FALSE });
93 $window->set_title("Slot Cars Manager");
95 $window->set_border_width(10);
97 my $button = Gtk2::Button->new("Button 1");
98 $button->signal_connect(clicked => \&callback, 'button 1');
100 my $box1 = Gtk2::HBox->new(FALSE, 0);
102 $box1->pack_start($button, TRUE, TRUE, 0);
105 $button = Gtk2::Button->new("Button 2");
106 $button->signal_connect(clicked => \&callback, 'button 2');
107 $box1->pack_start($button, TRUE, TRUE, 0);
118 my ($button, $data) = @_;
120 print "Hello again - $data was pressed\n";