]> www.fi.muni.cz Git - slotcarman.git/blob - gui.pl
Merge branch 'master' of ssh://calypso.fi.muni.cz/~kas/git/slotcarman
[slotcarman.git] / gui.pl
1 #!/usr/bin/perl -w
2
3 use strict;
4 use utf8;
5
6 use Gtk2 '-init';
7 use Glib qw(TRUE FALSE);
8
9 use SCX::Reader;
10
11 my @controllers = (0, 0, 0, 0, 0, 0);
12 my $no_reader = 0;
13
14 my $builder = Gtk2::Builder->new;
15 $builder->add_from_file('slotcarman.glade');
16
17 my $window = $builder->get_object('slotcarman');
18
19 $builder->connect_signals(undef);
20 $window->show();
21
22 sub quit {
23         Gtk2->main_quit;
24         return FALSE;
25 }
26
27 my $reader = SCX::Reader->new({
28         portname   => '/dev/ttyUSB0',
29         logfile    => 'log',
30         callback   => \&do_packet,
31 }) if !$no_reader;
32
33 sub scx_read {
34         my $event = shift;
35
36         $reader->read();
37         return TRUE;
38 }
39
40 Glib::IO->add_watch(fileno($reader->fh), 'in', \&scx_read, 1)
41         if !$no_reader;
42
43 Gtk2->main();
44
45 sub do_packet {
46         my (@data) = @_;
47
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;
54
55                         my $progressbar = $builder->get_object(
56                                 'progressbar_controller'.$controller);
57                         if ($byte == 0xaa) {
58                                 $progressbar->set_text('inactive');
59                                 $progressbar->set_fraction(0);
60                                 next;
61                         }
62                         my $light = !($byte & 0x20);
63                         my $backbutton = !($byte & 0x10);
64                         my $speed = $byte & 0x0f;
65
66                         my $text = ($backbutton ? '+' : '') . $speed;
67                         $progressbar->set_text($text);
68                         $progressbar->set_fraction($speed / 12);
69                 }
70         } elsif ($data[0] == 0xd6) { # fuel status
71                 my @fuel = (0,
72                         $data[1] >> 4, $data[1] & 0x0f,
73                         $data[2] >> 4, $data[2] & 0x0f,
74                         $data[3] >> 4, $data[3] & 0x0f,
75                 );
76                 for my $car (1..6) {
77                         next if defined $controllers[$car-1]
78                                 &&$controllers[$car-1] == $fuel[$car];
79                         
80                         my $progressbar = $builder->get_object(
81                                 'progressbar_fuel'.$car);
82                         $progressbar->set_fraction($fuel[$car]/8);
83                 }
84         } else {
85                 print "Unknown packet",
86                         (map { sprintf(" %02x", $_) } @data), "\n";
87         }
88 }
89                         
90 __END__
91 use Gtk2 '-init';
92
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");
96
97 $window->set_border_width(10);
98
99 my $button = Gtk2::Button->new("Button 1");
100 $button->signal_connect(clicked => \&callback, 'button 1');
101
102 my $box1 = Gtk2::HBox->new(FALSE, 0);
103 $window->add($box1);
104 $box1->pack_start($button, TRUE, TRUE, 0);
105
106 $button->show;
107 $button = Gtk2::Button->new("Button 2");
108 $button->signal_connect(clicked => \&callback, 'button 2');
109 $box1->pack_start($button, TRUE, TRUE, 0);
110
111 $button->show;
112 $box1->show;
113 $window->show;
114
115 Gtk2->main();
116
117
118 sub callback
119 {
120         my ($button, $data) = @_;
121         
122         print "Hello again - $data was pressed\n";
123 }
124