分类: 嵌入式
2012-02-20 22:11:55
The LK201 was a detachable computer keyboard introduced by of , in 1982. It was first used by Digital's ANSI/ASCII terminal and was subsequently used by the , , and microcomputers and many of Digital's computer workstations such as the and families.
The keyboard layout was new at the time, adding a set of cursor and miscellaneous keys between the main keyboard and the . The cursor keys were arranged in what has now become the standard "Inverted T" arrangement seen on essentially all contemporary full-sized computer keyboards. Ergonomic considerations caused the keyboard to be designed with a very low profile; it was very thin, especially when compared to the keyboard used on the VT100. The keyboard connected using a 4 position over which flowed 12 power and 4800 bit/s data.
I hope you watched my first video on how to build a . In that tutorial, one of the key components was the USB HID report descriptor, where I showed you how to utilize an example descriptor. This may leave you with some questions about how USB HID report descriptors work, I will explain them to you in this tutorial.
First, go to this page http://www.usb.org/developers/hidpage/ and find the document titled "Device Class Definition for HID". What I will be talking about is essentially paraphrasing the important sections of that document.
Second, go get the HID descriptor tool from the same page. You'll want to play with it as you go through this tutorial. It is an absolute headache to write the HID report descriptors manually (converting between binary and hex and looking up the meanings of the numbers) so this tool is essential. What is a USB HID report descriptor?
"The HID protocol makes implementation of devices very simple. Devices define their data packets and then present a "HID descriptor" to the host. The HID descriptor is a hard coded array of bytes that describe the device's data packets. This includes: how many packets the device supports, how large are the packets, and the purpose of each byte and bit in the packet. For example, a keyboard with a calculator program button can tell the host that the button's pressed/released state is stored as the 2nd bit in the 6th byte in data packet number 4 (note: these locations are only illustrative and are device specific). The device typically stores the HID descriptor in ROM and does not need to intrinsically understand or parse the HID descriptor. Some mouse and keyboard hardware in the market today are implemented using only an 8-bit CPU." - Wikipedia on Human Interface Device
I'm going to try teaching you about USB HID report descriptors by walking you through writing a few.
For a simple starting point, let us make a standard mouse. Just three buttons, and movement on the X and Y axis. So we want to send data regarding the buttons and movement. It takes one bit to represent each button, and one byte to represent the movement on one axis as a signed integer. So we can say that we want the data structure to look something like this
Bit 7 | Bit 6 | Bit 5 | Bit 4 | Bit 3 | Bit 2 | Bit 1 | Bit 0 | |
Byte 0 | Useless | Useless | Useless | Useless | Useless | Left Button | Middle Button | Right Button |
Byte 1 | X Axis Relative Movement as Signed Integer | |||||||
Byte 2 | Y Axis Relative Movement as Signed Integer |
And then we can say our data structure in C looks like
点击(此处)折叠或打开
点击(此处)折叠或打开
each button status is represented by a bit, 0 or 1
点击(此处)折叠或打开
there are three of these bits
点击(此处)折叠或打开
send this variable data to the computer
点击(此处)折叠或打开
点击(此处)折叠或打开
that will represent the buttons
but what about the five useless padding bits?
点击(此处)折叠或打开
now we make the X axis movement
点击(此处)折叠或打开
点击(此处)折叠或打开
点击(此处)折叠或打开
点击(此处)折叠或打开
点击(此处)折叠或打开
点击(此处)折叠或打开
点击(此处)折叠或打开
点击(此处)折叠或打开
点击(此处)折叠或打开
点击(此处)折叠或打开
Cool, at this point, you will have encountered some concepts that you may have questions about, you should research the following: Usage Pages
There's one thing that I think isn't explained well in the documentation, USAGE, USAGE_PAGE, USAGE_MINIMUM and USAGE_MAXIMUM. In a descriptor, you first set a USAGE_PAGE, and certain USAGEs are available. In the mouse example, USAGE_PAGE (Generic Desktop) allowed you to use USAGE (Mouse), and when the usage page was changed to USAGE_PAGE (Button), then the USAGE_MINIMUM and USAGE_MAXIMUM allowed you to specify the buttons, and before you can use USAGE (X) and USAGE (Y), the usage page was changed back to USAGE_PAGE (Generic Desktop). The usage page is like a namespace, changing the usage page affects what "usages" are available. Read the documentation called " HID Usage Tables" for more info. Collections
Read the documentation about the official proper use of collections. In my own words, collections can be used to organize your data, for example, a keyboard may have a built-in touchpad, then the data for the keyboard should be kept in one application collection while the touchpad data is kept in another. We can assign an "Report ID" to each collection, which I will show you later.
Hey here's something you can do, by turning "USAGE (Mouse)" into "USAGE (Gamepad)", you make the computer think that it's a game pad with one joystick and 3 buttons.
TODO screenshot
Or... How about converting a Playstation 2 controller into a USB gamepad? The controller has 16 buttons and two thumb sticks, so we want the data to look like
Bit 7 | Bit 6 | Bit 5 | Bit 4 | Bit 3 | Bit 2 | Bit 1 | Bit 0 | |
Byte 0 | Button | Button | Button | Button | Button | Button | Button | Button |
Byte 1 | Button | Button | Button | Button | Button | Button | Button | Button |
Byte 2 | Left X Axis as Signed Integer | |||||||
Byte 3 | Left Y Axis as Signed Integer | |||||||
Byte 4 | Right X Axis as Signed Integer | |||||||
Byte 5 | Right Y Axis as Signed Integer |
So our data structure looks like