Today i played around with Net::DBus and the screen on my Logitech G19 keyboard.
For Linux, there is the Gnome15 software that allows plugins to access the keyboards screen. Works quite nicely. I've been using it to display a resource monitor for a year now. But being a typical PerlMonk, i wanted more. So just take a plugin, modify it and off we go...
In a classic "Do'h!" moment, i discovered everything is written in Python. But wait, the Gnome15 daemon talks DBus, which is supposed to be so easy to use, isn't it. And there is even a usable documentation of the Gnome15 DBus API online, so how hard can it be?
Uhm, pretty hard actually, if you have never worked with DBus before. And, when you finally understand how that stuff works, it still feels, well, less like an API and more like flying a plane at night through the Alps without any navigation instruments. (So, in effect, it feels exactly like trying to fix MakeMaker *smile*).
Ok, that's probably enough ranting. And i'm glad there is the DBus API, otherwise i would have no easy way to communicate to the display. So, off with the rant, on with the code!
I'm currently having a little problem displaying text, which is most likely related to a PEBCAC ("Problem Exists Between Chair And Computer"). So, instead, i'm presenting a "hyptnotic eye meditator" which will drive you crazy if you stare long enough.
Here's the code. Note, for clarity and code length, i left out all the checks.
#!/usr/bin/perl # G19 screen demo use strict; use warnings; # Load in the Net::DBus core module. use Net::DBus; use Data::Dumper; use Time::HiRes qw[sleep]; # Get the session object my $bus=Net::DBus->session(); # Connect to the service my $service=$bus->get_service("org.gnome15.Gnome15"); #Get the service object my $g19=$service->get_object("/org/gnome15/Service", "org.gnome15.Serv +ice"); # Just dump some server information my @xresult; @xresult=$g19->GetServerInformation(); print "ServerInformation: " . join('|', @xresult) . "\n"; # Try to connect to the first available screen (and hope it's a G19) @xresult=$g19->GetScreens(); my $g19screenname = $xresult[0]->[0]; print "Using screen $g19screenname\n"; my $g19screen = $service->get_object($g19screenname, "org.gnome15.Scre +en"); # Now, create a page and get its handle my $pagename = $g19screen->CreatePage('Perltest', 'Perl test!', 50); my $page = $service->get_object($pagename, "org.gnome15.Page"); sleep(1); my $linesize = 30; # Create a drawing surface $page->NewSurface(); for(my $i = 0; $i < 500; $i++) { # limit number of loops for(my $l = 0; $l < (240 / ($linesize + 2)); $l++) { my $offs = $l * ($linesize + 2); # Paint alternate color "table" lines if(($l+$i) %2 == 0) { $page->Foreground(40,40,40,255); } else { $page->Foreground(0,0,0,255); } $page->Rectangle(0, $offs, 320, $linesize + 2, 1); } # paint the "hypnotic eye for(my $l = 0; $l < 100; $l+=20) { my $radius = 120 - $l; # paint from large to small my $grey = 255 - int((($i * 5) + $l) % 255); $page->Foreground($grey,0,0,255); $page->Circle(160, 120, $radius, 0); } # Update screen $page->DrawSurface(); $page->Redraw(); sleep(0.1); } # Cleanup (delete the page) $page->Delete(); exit 0;
In line 14, we get the a new session object. In lines 17 through 31, we haggle along different objects until we finally the the screen object.
In line 34, we create a page object on that screen which we retrieve in line 35. We're still not quite ready to draw anything, NewSurface() on line 42 finally gets us a drawing surface.
The outer one (line 44) is basically the timing (each loop draw one screen).
The first inner loop (starting line 45) paints alternate colored lines (modified by the outer loop for a flimmering effect).
The second inner loop (starting line 58) paints the "hyptonic eye", which is essential for meditation/getting mad (whatever you prefer). This also depends on the outer loop for shifting colors.
The lines 66 and 67 actually update the screen.
When we're finished meditating, we delete the page we created (line 73).
That's it. It's not really a nice piece of code, but it gets the job done (and without writing a single line of Python, i might add). I think it's a simple enough demonstration how to work with Net::DBus and how to use Gnome15 supported displays from Perl.
What do you think?
BREW /very/strong/coffee HTTP/1.1 Host: goodmorning.example.com 418 I'm a teapot
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Net::DBus/Logitech G19 Meditator
by tanktarta (Initiate) on Jan 12, 2012 at 01:46 UTC | |
by cavac (Prior) on Jan 12, 2012 at 12:48 UTC | |
by tanktarta (Initiate) on Jan 15, 2012 at 23:27 UTC |