Pages

Total Pageviews

Monday, December 28, 2015

Using an input device on Embedded Linux: An example with a USB mouse on the Intel Edison

The Intel Edison test board, along with the USB mouse

I have recently been using the Intel Edison for the Cornell robotics project team (which co-hosts the Intel-Cornell Cup USA competition). Building on my previous knowledge of embedded systems, I started learning to use and program on Linux. The distro used is Yocto (all information is available on the Intel Edison website).

One of the prototypes we worked on relied on using a wireless Playstation 4 controller for locomotion user interface. The concept of using an input device on Linux is not complicated, but can be a daunting task for someone new to Linux programming. Hence, I have decided to write this article giving an example of using an input device on an Embedded Linux platform. This demo application I am showing uses a USB mouse as the input device connected to the Intel Edison.

Prerequisite: I have assumed that you have a flashed Intel Edison board, know how to access the terminal (through the USB COM port, or through SSH) and have the Eclipse IDE installed and can program with it. Of course, if you don't have the IDE, you can compile the code through the terminal and I'll tell you how to do it at the end. If you are using a platform other than the Edison, details may change but the general idea is similar. Additionally, it is assumed that you have a basic understanding of C programming.

First thing to note when you connect the USB mouse is that the switch on
the board (labelled SW1) must be switched towards the USB-A connector
from the default position facing the microUSB port.

The device drivers in Linux abstract away the low-level nitty gritty details of the interface with the input device, presenting an input through file descriptors that can be interfaced with as files. The input devices can be viewed and read from in the Linux environment just like files, as mentioned before. The input device appears in the /dev/input directory. Initially, before the mouse is plugged in, you can see that there is an event0 and an event1 file. Upon connecting the mouse, you can see an event2 file.

You can also type it into Eclipse, hit Ctrl on your keypad and left mouse click on the header file name to view the file itself.

From the kernel documentation and the input.h file, you should find that the data output happens such that every time an event occurs, it can be "fit" into the following structure (defined in linux/input.h):

You can find that this has a total length of 16 bytes. You can look through the different data types and add, and confirm using the sizeof function in Eclipse:fprintf(stdout, "Size of event is: %d\n", sizeof(event));

Each event has a timestamp, type, code and value as you can guess from the input structure. Additionally events are separated by EV_SYN type events which are just markers. EV_SYN is defined as 0.

You can read the file in a C program and then just print out the values separated as fields in the input event structure to confirm that and observe the different types of data as you interact with the mouse. You can limit the type of event as you interact with your mouse. To understand the meaning of the numbers you receive, peruse the linux/input.h file and the kernel documentation linked above. You will see a section describing the events:

#define REL_X 0x00#define REL_Y 0x01#define REL_Z 0x02#define REL_RX 0x03#define REL_RY 0x04#define REL_RZ 0x05#define REL_HWHEEL 0x06#define REL_DIAL 0x07#define REL_WHEEL 0x08#define REL_MISC 0x09#define REL_MAX 0x0f#define REL_CNT (REL_MAX+1)
You can compare these against the values you see to see if they make sense (they should!). Then, you can proceed to mold this to read the different codes, types and values based on these. This is what I have done in my demo application, which should be commented enough for you to understand. (Obviously, if you have questions, let me know in the comments section!)

One last thing that I haven't covered yet (but you may already know) is how to do the file read. I have used the low-level file IO functions open and read:

Programming without the Eclipse IDE: As I have mentioned before, even if you don't have the Eclipse IDE (which you should get), you can still program the Edison. Here are a few ways you can do so. You can copy-paste the code from a text editor to the terminal (using PuTTY, mouse right-click is paste), or even write the code on the terminal. Additionally, you can use a program such as WinSCP to transfer a C file. Be careful with Windows files since lines end in a newline and a carriage return character, whereas on Linux, they end with only a newline character. The carriage return character will be displayed as ^M if you open the file with the text editor. Once the file is on the Edison file system somewhere, cd into that folder and compile it:

gcc -o <output name> <source file name>eg: gcc -o mouse mouse.c

Then you can run it:eg:./mouse

I have attempted to make the code self-explanatory and provide
sufficient background detail here for you to understand what's going on.
By changing the code and type checks, you can extend this to other devices. Hopefully you'll find this useful! Let me know what you think!

Recent Posts

Translate this blog

Search This Blog

Follow by Email

About Me

I am Syed Tahmid Mahbub, from Dhaka, Bangladesh, born on August 1, 1994.
Electronics is my passion and from class V, I have been learning electronics. I learnt and worked mostly on SMPS, power electronics, microcontrollers and integration of microcontrollers with SMPS and power electronics. I've used PIC and AVR microcontrollers - PIC 10F, 12F, 16F, 18F, 24F, dsPIC 30F, 33F, PIC32, ATmega and ATtiny, integrating them with various SMPS and power electronics circuits.
I have completed my Bachelor's degree from Cornell University (Class of 2017) in Ithaca, New York, USA, majoring in Electrical and Computer Engineering (ECE).
I am a member of the forum www.edaboard.com, where I am an "Advanced Member Level 5" (the highest level attainable) and also the forum allaboutcircuits.com, where I am a "Senior Member". I post to help solve electronics-related problems of engineers and engineering students from all over the world.
I love watching and playing cricket and football (soccer), and listening to music.
I am now a hardware engineer at Apple in Silicon Valley, California, USA.