[Purdue-pm] Perl code to use steering wheel on Linux

Mark Senn mark at purdue.edu
Sat Apr 7 13:31:57 PDT 2012


Purdue Perl Mongers,

I was thinking about giving a talk about this at the next Purdue Perl
Mongers meeting but decided it wasn't a broad enough topic.  Below is
Perl code that reads values from a steering wheel, gas pedal, and brake
pedal attached to a Linux computer.

(T'm building a 18" long radio controlled six wheel drive car that I plan
to drive using a steering wheel on my computer using a web browser.  The
idea is the can could be used anywhere in the world where a web server
would be within radio controlled range of the car.  A makerspace/hackerspace
is in the process of being formed in Lafayette to work on crazy projects
like this.  See
    http://LafayetteMakerspace.org
to sign up for the (two messages a month) mailing list if you're interested.)

-mark

#!/usr/bin/perl
#
#  .index  read information from Genious TwinWheel steering wheel
#
#  .revised  t.pl  2012-04-07  Mark Senn  http://engineering.purdue.edu/~mark
#  .created  t.pl  2012-03-30  Mark Senn  http://engineering.purdue.edu/~mark
#
#  .description
#      .p
#          This program shows how to use Perl to read information from
#          a "Genius TwinWheel F1 Vibration Feedback Racing Wheel for
#          PC ad PS2 games".  This program is based on
#          .verbatim
#              http://search.cpan.org/~bwatson/Linux-Joystick-0.0.1/Joystick.pm#USAGE
#          .everbatim
#      .ep
#      .p
#          The configuration of buttons on the Genius TwinWheel F1 is
#          .verbatim
#                        L2       R2
#                       L1         R1
#        
#                 DV+                   NOR
#              DH-   DH+             WES   EAS
#                 DV-                   SOU
#        
#                        L3       R3
#                         SE    ST
#                           MODE
#          .everbatim
#          where
#          .table
#              BUTTON  DESCRIPTION              BUTTON  DESCRIPTION
#              DV+     D-pad Vertical+          NOR     NORTH
#              DH-     D-pad Horizontal-        WES     WEST
#              DH+     D-pad Horizontal+        EAS     EAST
#              DV-     D-pad Vertical-          SOU     SOUTH
#    
#              SE     SELECT
#              ST     START
#          .etable
#      .ep
#  .edesciption
#
#  .history
#      .he 2012-03-30
#          .p
#              Started.
#          .ep
#      .ehe
#      .he 2012-03-31
#          .p
#              Did more work.
#          .ep
#      .ehe
#      .he 2012-04-07
#          .p
#              Improved code and comments.
#          .ep
#      .ehe
#  .ehistory
#

use strict;
use warnings;

use feature 'say';

use Linux::Joystick;

# Make a steering wheel object.
my $sw = new Linux::Joystick;

say 'The steerig wheel has:';
say '    ', $sw->axisCount(),   ' axes';
say '    ', $sw->buttonCount(), ' buttons';
say 'Pulling right-hand lever towards you is equivalent to pushing the north button.';
say 'Pulling left-hand  lever towards you is equivalent to pushing the east  button.';
say 'Pressing the "MODE" button does not generate an event.';

# Map button numbers to button names.
my %button =
(
     0 => 'NORTH',
     1 => 'EAST',
     2 => 'SOUTH',
     3 => 'WEST',
     4 => 'R2',
     5 => 'L2',
     6 => 'L1',
     7 => 'R1',
     8 => 'SELECT',
     9 => 'START',
    10 => 'L3',
    11 => 'R3',
);


# Printf format.
my $f = "%-6s %-10s %s\n";

# Use blocking reads to wait for the next event.
while (my $e = $sw->nextEvent)
{
    if ($e->isAxis && $e->axis == 0) { printf $f, 'Steer', '',                                $e->axisValue;       next; }
    if ($e->isAxis && $e->axis == 1) { printf $f, 'Speed', '',                                $e->axisValue;       next; }
    if ($e->isAxis && $e->axis == 2) { printf $f, 'D-pad', 'HORIZONTAL',                      $e->axisValue;       next; }
    if ($e->isAxis && $e->axis == 3) { printf $f, 'D-pad', 'VERTICAL',                        $e->axisValue;       next; }
    if ($e->isButton)                { printf $f, 'Button', ($e->buttonDown) ? 'down' : 'up', $button{$e->button}; next; }

    die 'Unknown event ' . $e->hexDump;
}

# If the while loop terminates, we have a false (undefined) event.
die 'Error reading joystick: ' . $sw->errorString;


More information about the Purdue-pm mailing list