Sunday, February 25, 2018

Raspberry PI USB Keyboard/Mouse Latency Test

USB Keyboard/Mouse Latency Test Using Raspberry Pi

Latency, latency, latency...
People ask those questions and I am also one of the guy who is interested in the latency...
From the USB test here and GPIO test here I have done before, I now decided to do quick latency test of keyboard or mouse.
In this test, I do the simple button press test and check how quickly the raspberry pi detect the USB input event.

Connect GPIO input to an external button:
I was too lazy to open up the unit and wire the button signal directly to the raspberry pi... sorry for the people who are interested in this post...
so I decided to wire a tact switch to the raspberry pi.
Then attach the switch to the one of the keyboard button.
Same as my previous GPIO test post, I am using GPIO20(pin #38) as interrupt input.
Also same as previous USB device input test post, I am using wireless keyboard with touchpad.
For this test, I used left button of the touchpad.

Python Code

Python code is pretty simple.
You call "import RPi.GPIO as GPIO" in the beginning and set the mode.
This is the python example code.
import RPi.GPIO as GPIO
import time
from evdev import InputDevice, categorize, ecodes
dev = InputDevice('/dev/input/event0')
GPIO.setup(20, GPIO.IN, pull_up_down=GPIO.PUD_UP)
def my_callback(channel):
              global start
raw_input("Press Enter\n")
GPIO.add_event_detect(20, GPIO.FALLING, callback=my_callback, bouncetime=300)
              for event in dev.read_loop():
                           if event.code == 272 and event.value ==1:
                                       print(str(elapsed) + " ms")
except KeyboardInterrupt:

Crate a python script file as below and copy&paste the above code.
Run the python script.


The result was not that great...
Because the my setup was too lazy to measure...
The contact of external button and touchpad button do not happen at the same time.
It is totally up to how you press button.
In my case, I needed to press a bit strong so that both external button and touchpad button were pressed nearly at the same time...
Anyway, the number I got was around 8-10 ms.
Old logitech whitepaper says around 10ms depending on the environment, so maybe result is reasonable for this simple test??
"The typical latency of an advanced 2.4 GHz device operating in a clean environment is below 10 ms. In a noisy environment, this latency may increase depending on the strength, type and occurrence of the interference. "
This is wireless keyboard with touchpad so wired mouse/keyboard would be faster I would assume.



No comments:

Post a Comment