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