tinyusb库具体使用HID的C语言例子
时间: 2024-05-09 08:15:56 浏览: 265
以下是一个使用TinyUSB库的C语言HID例子,它实现了将按键状态发送到计算机:
```c
#include "tusb.h"
#define KEYBOARD_REPORT_ID 1
#define KEYBOARD_MODIFIER_LEFT (1 << 0)
#define KEYBOARD_MODIFIER_RIGHT (1 << 1)
#define KEYBOARD_KEY_A 0x04
#define KEYBOARD_KEY_B 0x05
#define KEYBOARD_KEY_C 0x06
#define KEYBOARD_KEY_D 0x07
#define KEYBOARD_KEY_E 0x08
#define KEYBOARD_KEY_F 0x09
#define KEYBOARD_KEY_G 0x0a
#define KEYBOARD_KEY_H 0x0b
#define KEYBOARD_KEY_I 0x0c
#define KEYBOARD_KEY_J 0x0d
#define KEYBOARD_KEY_K 0x0e
#define KEYBOARD_KEY_L 0x0f
#define KEYBOARD_KEY_M 0x10
#define KEYBOARD_KEY_N 0x11
#define KEYBOARD_KEY_O 0x12
#define KEYBOARD_KEY_P 0x13
#define KEYBOARD_KEY_Q 0x14
#define KEYBOARD_KEY_R 0x15
#define KEYBOARD_KEY_S 0x16
#define KEYBOARD_KEY_T 0x17
#define KEYBOARD_KEY_U 0x18
#define KEYBOARD_KEY_V 0x19
#define KEYBOARD_KEY_W 0x1a
#define KEYBOARD_KEY_X 0x1b
#define KEYBOARD_KEY_Y 0x1c
#define KEYBOARD_KEY_Z 0x1d
static const uint8_t keyboard_report_descriptor[] =
{
TUD_HID_REPORT_DESCRIPTOR_KEYBOARD( HID_REPORT_ID(KEYBOARD_REPORT_ID),
HID_REPORT_ITEM_USAGE_PAGE( HID_USAGE_PAGE_GENERIC_DESKTOP ),
HID_REPORT_ITEM_USAGE_MINIMUM( 0x00 ),
HID_REPORT_ITEM_USAGE_MAXIMUM( 0x65 ),
HID_REPORT_ITEM_LOGICAL_MINIMUM( 0x00 ),
HID_REPORT_ITEM_LOGICAL_MAXIMUM( 0x01 ),
HID_REPORT_ITEM_COLLECTION_APPLICATION( HID_REPORT_ITEM_COLLECTION_DATA, HID_REPORT_ITEM_COLLECTION_PHYSICAL ),
HID_REPORT_ITEM_USAGE_PAGE( HID_USAGE_PAGE_KEYBOARD ),
HID_REPORT_ITEM_USAGE_MINIMUM( 0x00 ),
HID_REPORT_ITEM_USAGE_MAXIMUM( 0x65 ),
HID_REPORT_ITEM_LOGICAL_MINIMUM( 0x00 ),
HID_REPORT_ITEM_LOGICAL_MAXIMUM( 0x01 ),
HID_REPORT_ITEM_REPORT_SIZE( 0x01 ),
HID_REPORT_ITEM_REPORT_COUNT( 0x65 ),
HID_REPORT_ITEM_INPUT( HID_CONST | HID_DATA | HID_VARIABLE | HID_ABSOLUTE ),
HID_REPORT_ITEM_END_COLLECTION,
),
};
static inline uint8_t keyboard_report_find_key(uint8_t keycode)
{
switch (keycode)
{
case 'a':
case 'A':
return KEYBOARD_KEY_A;
case 'b':
case 'B':
return KEYBOARD_KEY_B;
case 'c':
case 'C':
return KEYBOARD_KEY_C;
case 'd':
case 'D':
return KEYBOARD_KEY_D;
case 'e':
case 'E':
return KEYBOARD_KEY_E;
case 'f':
case 'F':
return KEYBOARD_KEY_F;
case 'g':
case 'G':
return KEYBOARD_KEY_G;
case 'h':
case 'H':
return KEYBOARD_KEY_H;
case 'i':
case 'I':
return KEYBOARD_KEY_I;
case 'j':
case 'J':
return KEYBOARD_KEY_J;
case 'k':
case 'K':
return KEYBOARD_KEY_K;
case 'l':
case 'L':
return KEYBOARD_KEY_L;
case 'm':
case 'M':
return KEYBOARD_KEY_M;
case 'n':
case 'N':
return KEYBOARD_KEY_N;
case 'o':
case 'O':
return KEYBOARD_KEY_O;
case 'p':
case 'P':
return KEYBOARD_KEY_P;
case 'q':
case 'Q':
return KEYBOARD_KEY_Q;
case 'r':
case 'R':
return KEYBOARD_KEY_R;
case 's':
case 'S':
return KEYBOARD_KEY_S;
case 't':
case 'T':
return KEYBOARD_KEY_T;
case 'u':
case 'U':
return KEYBOARD_KEY_U;
case 'v':
case 'V':
return KEYBOARD_KEY_V;
case 'w':
case 'W':
return KEYBOARD_KEY_W;
case 'x':
case 'X':
return KEYBOARD_KEY_X;
case 'y':
case 'Y':
return KEYBOARD_KEY_Y;
case 'z':
case 'Z':
return KEYBOARD_KEY_Z;
default:
return 0;
}
}
void tud_hid_report_complete_cb(uint8_t instance, uint8_t report_id, uint8_t status)
{
(void) instance;
(void) report_id;
(void) status;
}
int main(void)
{
tusb_init();
while (1)
{
tud_task();
if (tud_hid_ready())
{
uint8_t keycode = getchar();
if (keycode)
{
uint8_t keycode_id = keyboard_report_find_key(keycode);
if (keycode_id)
{
uint8_t report[8];
memset(report, 0, sizeof(report));
report[0] = KEYBOARD_REPORT_ID;
report[1] = KEYBOARD_MODIFIER_LEFT;
report[2] = keycode_id;
tud_hid_report(0, report, sizeof(report));
}
}
}
}
return 0;
}
```
此代码示例使用了TinyUSB库的HID模块。在主函数中,`tusb_init()`函数初始化TinyUSB库,然后在`while`循环中调用`tud_task()`函数以处理USB任务。当HID模块已就绪时,程序会等待用户输入,并将按键状态打包成HID报告发送到计算机。键盘报告的描述符存储在`keyboard_report_descriptor`数组中,并使用TinyUSB库的`TUD_HID_REPORT_DESCRIPTOR_KEYBOARD()`宏生成。
此代码示例还实现了一个`keyboard_report_find_key()`函数,该函数将ASCII字符转换为HID键码。在`main()`函数中,程序等待用户输入,并使用此函数将输入字符转换为HID键码。如果键码存在,则程序将创建一个包含键码和修饰符信息的HID报告,并使用`tud_hid_report()`函数将其发送到计算机。
阅读全文