Usb2-hid in the mouse

Source: Internet
Author: User

Download hid SPEC:
Http://www.usb.org/developers/hidpage#Class_Definitions
Download hid Usage Table

Http://www.usb.org/developers/hidpage#HID_Usage

The hid specification of the mouse device and host. Each time the transmission is interrupted, the Mouse sends four bytes of data to the PC. The meaning is as follows:

Data [0] -- | -- bit7: 1 indicates that the variation of the Y coordinate exceeds-256 ~ The value range is 255. 0 indicates no overflow. | -- bit6: 1 indicates that the X coordinate variation exceeds-256 ~ In the range of 255, 0 indicates no overflow. | -- bit5: Symbol bit of Y coordinate change, 1 indicates a negative number, that is, move the mouse down | -- bit4: Symbol bit of X coordinate change, 1 indicates a negative number, that is, move the mouse to the left | -- bit3: constant is 1 | -- bit2: 1 indicates that the middle button is pressed | -- bit1: 1 indicates that the right button is pressed | -- bit0: 1 indicates the left-click data [1] -- X coordinate variation, which is a 9-bit symbol number with bit4 of data. A negative number indicates moving to the left, and the positive number table shifts to the right. Use the complement code to represent the variation data [2] -- y coordinate variation, which is a 9-bit symbol with bit5 of data. A negative number indicates moving downward, and a positive number table. Use the complement code to represent the variation data [3]-scroll wheel variation. // Refer to http://group.ednchina.com/93/54358.aspx

Therefore

① Host side
In the urb interrupt function usb_mouse_irq of Linux/Drivers/HID/usbhid/usbmouse. c driven by USB mouse on the host side, the following code is provided:

        input_report_key(dev, BTN_LEFT,   data[0] & 0x01);          input_report_key(dev, BTN_RIGHT,  data[0] & 0x02);          input_report_key(dev, BTN_MIDDLE, data[0] & 0x04);            input_report_rel(dev, REL_X,     data[1]);        input_report_rel(dev, REL_Y,     data[2]);        input_report_rel(dev, REL_WHEEL, data[3]); 

In this example, data [0] is the data sent from a single-chip microcomputer,
If data [0] = 2, data [0] & 0x02 = 1. Therefore, the driver sends the btn_right press status to USB core and USB core to the driver event. The driver event then writes data to the device file in a certain format.
If data [0] <> 2, data [0] & 0x02 <> 1. Therefore, the driver sends the release status of btn_right.

② Device side
For example, a single-chip computer equipped with five buttons is used to simulate keys and rollers of the mouse.

k1--BTN_LEFTk2--REL_WHEEL (+2)k3--BTN_RIGHTk4--BTN_MIDDLEk5--REL_WHEEL (-2)

According to the hid specification, an array with a size of 4 characters is defined.

static INT8S MouseData[4] = {0,0,0,0};//typedef signed   char  INT8S;/*Byte0:keys, Byte1:up-down moving, Byte2:l-r moving, byte3:wheel*/

When the Microcontroller
K1 key detected, left click, when pressed, make mousedata [0] = 1
The K3 key is detected, right-click, and set mousedata to [0] = 2 when pressed.
The K4 key, middle key, and mousedata [0] = 4
The K2 key is detected, and the scroll wheel is rolled up. When pressed, The mousedata [3] = 2
The K5 key is detected, and the scroll wheel is rolled down. When pressed, The mousedata [3] =-2
The procedure is as follows:

    switch( KeyScan( ) )    {        case K1_PRESS://left button            MouseData[0] = 1;            break;        case K2_PRESS://wheel front            MouseData[3] = 2;            break;        case K3_PRESS://Right press            MouseData[0] = 2;            break;        case K4_PRESS://middle press            MouseData[0] = 4;            break;        case K5_PRESS://wheel back            MouseData[3] = -2;            break;        case K1_RELEASE:        case K3_RELEASE:        case K4_RELEASE:            break;        default:            btmp = 0;            break;            }

When the host interrupts the transmission request, the microcontroller sends the mousedata to the host, and each interruption transmits four bytes, as shown below:

 HID_SendData( (INT8U *)MouseData, 4 );//typedef unsigned char  INT8U;

At mini2440, after a USB mouse is inserted, the device file/dev/input/event1 is automatically generated (event0 is a touch screen device ). If the mouse is not transmitted in the key state, it is not transmitted every time the key is pressed, but the ADC value is reported on a regular basis, the host will get the ADC value and write it into the device file every time the request is interrupted. In this way, the single-chip microcomputer can be made into a USB data acquisition module. On the host side, write an application to read/dev/input/event1 to obtain the ADC data. Of course, there is no need to write the driver and read data from/dev/input/eventx according to the input model just like the mouse, and write data according to the Hybrid device, as long as it complies with the hid specification.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.