Embedded Systems and Power Electronics

Total Pageviews

About Me

My photo
I am currently a PhD student at UC Berkeley, following a 6-year journey working at Apple after my undergrad years at Cornell University. I grew up in Dhaka, Bangladesh where my interest in electronics was cultivated, resulting in the creation of this blog.

BTemplates.com

Powered by Blogger.

Dec 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.

 
Fig. 1: Input device files without mouse connected

 
Fig. 2: Input device files with mouse connected

By reading the event2 file, you can read the mouse data. To dump data from the file, you can use the od command (man page: http://man7.org/linux/man-pages/man1/od.1.html)

For example, to view the output dump in hex format:
od -x event2

Move the mouse around, press the buttons, scroll the wheel and you'll see data appear on the console:

Fig. 3: File event2 data dump using od command

Hit Ctrl+C when you're satisfied you've seen enough of the dump.

Now to make sense of this input, decipher it and meaningfully use it, I have written a simple C application. I'll walk you through the process of developing it before I provide the code.

First thing to do is to go through these references as part of the kernel documentation:
https://www.kernel.org/doc/Documentation/input/event-codes.txt
https://www.kernel.org/doc/Documentation/input/input.txt

Additionally, you should go through the linux/input.h header file. You can find a copy here:
http://lxr.free-electrons.com/source/include/linux/input.h?v=2.6.38

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):

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


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:

/*
 * Event types
 */

#define EV_SYN            0x00
#define EV_KEY            0x01
#define EV_REL            0x02
#define EV_ABS            0x03
#define EV_MSC            0x04
#define EV_SW             0x05
#define EV_LED            0x11
#define EV_SND            0x12
#define EV_REP            0x14
#define EV_FF             0x15
#define EV_PWR            0x16
#define EV_FF_STATUS      0x17
#define EV_MAX            0x1f
#define EV_CNT            (EV_MAX+1)


You can also find a section describing the different keys/buttons for a keyboard, gamepad, mouse, etc. The section describing the mouse is:

#define BTN_MOUSE       0x110
#define BTN_LEFT        0x110
#define BTN_RIGHT       0x111
#define BTN_MIDDLE      0x112
#define BTN_SIDE        0x113
#define BTN_EXTRA       0x114
#define BTN_FORWARD     0x115
#define BTN_BACK        0x116
#define BTN_TASK        0x117


/*
 * Relative axes
 */

#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:

Opening the file:

// Use low-level Linux file IO operations
// Device is presented as a file, event2 in /dev/input for the Edison
int fid = open("/dev/input/event2", O_RDONLY);
if (fid == 0){
    fprintf(stderr, "Could not open event2 device!\n");
    return EXIT_FAILURE;
}
fprintf(stdout, "Opened event2 device!\n");


Reading the file:

int nbytes;
struct input_event event;

// Event type from <linux/input.h>
nbytes = read(fid, &event, sizeof(event));

The demo application prints out messages describing mouse motion, wheel motion and left, middle (wheel) and right button presses. See Fig. 4 below.

You can find the full code for my demo project here: https://drive.google.com/file/d/0B4SoPFPRNziHUUVCRHVPNjkwZ2s/view?usp=sharing

A typical output is shown below:
 
Fig. 4: Output of the demo application 


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!

1 comment:

  1. Is it possible to implement this into an Arduino program? Perhaps as a library or something? It'd be nice to be able to control lights and motors with the board using the mouse or keyboard.

    ReplyDelete