Skip to content

Day44 - Input Userspace Application

Objective

  • Read input events from /dev/input/eventX
  • Decode key events (short / long / double click)
  • Implement a userspace application
  • Control LED based on input events

System Flow

GPIO Button
Kernel Input Driver
input_report_key()
/dev/input/eventX
Python Application
LED Control (sysfs)

Input Event Mapping

Action Key Code
Short Click KEY_ENTER
Long Click KEY_ESC
Double Click KEY_SPACE

Userspace Reader

File

d44-input-app/input_led_app.py

Implementation Overview

1. Open input device

open("/dev/input/eventX", "rb")

2. Parse input_event

struct input_event {
    struct timeval time;
    __u16 type;
    __u16 code;
    __s32 value;
};

3. Filter key press events

if ev_type == EV_KEY and value == 1:
    # key press detected

4. Map key to action

KEY_ENTER → SHORT
KEY_ESC   → LONG
KEY_SPACE → DOUBLE

LED Control

Check LED device

ls /sys/class/leds/

Example:

ACT

Switch to manual mode

echo none | sudo tee /sys/class/leds/ACT/trigger

Control LED

echo 1 | sudo tee /sys/class/leds/ACT/brightness
echo 0 | sudo tee /sys/class/leds/ACT/brightness

Application Logic

SHORT  → Toggle LED
LONG   → LED ON
DOUBLE → LED OFF

Build & Run

chmod +x input_led_app.py
sudo ./input_led_app.py

Expected Output

[EVENT] SHORT
[ACTION] Toggle LED

[EVENT] LONG
[ACTION] LED ON

[EVENT] DOUBLE
[ACTION] LED OFF

Validation Steps

1. Verify input events

sudo evtest /dev/input/eventX

2. Run application

sudo ./input_led_app.py

3. Perform actions

  • Press button shortly → LED toggles
  • Hold button (>2s) → LED ON
  • Double click → LED OFF

Observations

  • Input driver does not define behavior
  • Userspace decides how to interpret events
  • Same key event can map to different actions

Key Learning

Kernel:
    generates input events

Userspace:
    interprets and applies logic

Extension Ideas

  • Support multiple input devices
  • Use select/poll for event loop
  • Add debounce in userspace (compare behavior)
  • Build CLI or GUI application
  • Integrate with other subsystems (e.g. IIO trigger)

Summary

Day43:
    kernel generates input events

Day44:
    userspace consumes and uses events