This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Understanding hid mouse example

Hi,

I'm trying to understand mouse_movement_send() function in HID mouse example. Can you please explain what is exactly happening inside this function?

I also didnt understand the following lines-

buffer[0] = x_delta & 0x00ff;
buffer[1] = ((y_delta & 0x000f) << 4) | ((x_delta & 0x0f00) >> 8);
buffer[2] = (y_delta & 0x0ff0) >> 4;

Also is there any code explanation for hid keyboard example?

Thanks.

  • Hi,

    You need to look at the full else block in order to understand what is going on.

    What I read from the code, is that the mouse movement HID report has two 12 bit fields for x and y movement. The lines that you posted takes the 12 least significant bits from each of x_delta and y_delta, and packs them after each other in a 24 bit buffer (i.e. a buffer of three bytes).

    << and >> are bitwise shift operators, which are commonly used for bit manipulation in C. I recommend reading up on "bitwise operators" and "bit manipulation", in order to understand what is going on. (A web search for those terms should point you to pages teaching you all about them.)

    I am sorry but we do not have any step-by-step code walk-through of the HID examples.

    Regards,
    Terje

Related