liveBook · Manning

Linux input subsystem

As explained in Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi

The Linux kernel subsystem that provides a common interface for input devices such as joysticks, keyboards, and mice.

The Linux input subsystem exposes a connected joystick as a device file, letting applications read its button and axis activity. Joystick device files are created automatically when controllers connect, commonly as /dev/input/js0, then /dev/input/js1 and /dev/input/js2 for additional devices. This provides a direct path from physical controller movement to a Python application. section 7.5.1 section 7.8

$ ls /dev/input/js*
/dev/input/js0

List joystick device files before choosing the controller to inspect or read. section 7.5.1

Use jstest with a joystick device file for a live view of detected axes and buttons. The display identifies available controls, reports every button as on or off, and shows axis values. Press controls one at a time to record the mapping your application needs: on the illustrated PlayStation controller, cross is button 0, circle is button 1, triangle is button 2, and square is button 3. The same process identifies A, B, X, and Y on an Xbox controller. section 7.5.1

A practical joystick workflow is:

  • Connect the joystick and list /dev/input/js* to find its device file. section 7.5.1
  • Install and run jstest on that file to see its controls and current event state. section 7.5.1
  • Press buttons and move axes to note controller-specific numbers and values. section 7.5.1
  • Read and parse the resulting joystick events in Python with struct, then use the input in a joystick application. section 7.2 section 7.8

Joystick event record

A joystick event has four fields: a millisecond timestamp, a value, an event type, and an axis or button number. These fields let an application distinguish which control changed and obtain its state or position while reading raw device events. section 7.5.1

struct js_event {
    __u32 time;     /* event timestamp in milliseconds */
    __s16 value;    /* value */
    __u8 type;      /* event type */
    __u8 number;    /* axis/button number */
};

Inspect the js_event layout used when parsing raw joystick events with Python struct. section 7.5.1

For a remotely controlled robot, joystick data can become movement requests sent over HTTP to a robot server. Pygame offers an initial route for working with joystick devices, while direct struct parsing gives access to Linux input events; Bluetooth drivers can provide a wireless controller connection. section 7.2 section 7.8

How Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi covers it

How deep it goes
Explains device discovery, jstest inspection, control mapping, and the raw js_event record without detailing kernel internals.
Examples
Uses /dev/input/js0, jstest output, PlayStation button mappings, Xbox controls, and a js_event structure.
What it assumes
Starts with connected joystick hardware and teaches device discovery and control mapping through terminal commands.
What you can do after
Apply it via jstest and Python struct parsing to inspect joystick inputs and drive robot-control requests.
How it connects
Connects joystick devices with Pygame, Python struct parsing, Bluetooth, HTTP, and robot motors.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Build Your Own Robot: Using Python, CRICKIT, and Raspberry Pi. The section links point to the parts of the book it draws on.