hid_example.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-04-04 tfx2001 first version
  9. */
  10. #include <stdarg.h>
  11. #include <stdint.h>
  12. #include <math.h>
  13. #include <rtthread.h>
  14. #include <tusb.h>
  15. #include "../usb_descriptor.h"
  16. #define CIRCLE_RADIUS 300
  17. static const uint8_t conv_table[128][2] = { HID_ASCII_TO_KEYCODE };
  18. static struct rt_semaphore hid_sem;
  19. void tud_hid_report_complete_cb(uint8_t instance, uint8_t const* report, uint8_t len)
  20. {
  21. (void) instance;
  22. (void) report;
  23. (void) len;
  24. rt_sem_release(&hid_sem);
  25. }
  26. static void hid_press_key(uint8_t modifier, uint8_t keycode)
  27. {
  28. uint8_t keycode_array[6] = {0};
  29. keycode_array[0] = keycode;
  30. tud_hid_keyboard_report(REPORT_ID_KEYBOARD, modifier, keycode_array);
  31. rt_sem_take(&hid_sem, 100);
  32. keycode_array[0] = 0;
  33. tud_hid_keyboard_report(REPORT_ID_KEYBOARD, 0, keycode_array);
  34. rt_sem_take(&hid_sem, 100);
  35. }
  36. static void hid_type_str_fmt(const char *fmt, ...)
  37. {
  38. uint8_t keycode;
  39. uint8_t modifier;
  40. uint32_t length;
  41. va_list args;
  42. char buffer[64];
  43. char chr;
  44. va_start(args, fmt);
  45. rt_vsnprintf(buffer, 64, fmt, args);
  46. length = rt_strlen(buffer);
  47. for (int i = 0; i < length; i++)
  48. {
  49. chr = buffer[i];
  50. if (conv_table[chr][0])
  51. {
  52. modifier = KEYBOARD_MODIFIER_LEFTSHIFT;
  53. }
  54. else
  55. {
  56. modifier = 0;
  57. }
  58. keycode = conv_table[chr][1];
  59. hid_press_key(modifier, keycode);
  60. }
  61. }
  62. void hid_example(void)
  63. {
  64. float last_x_pos = CIRCLE_RADIUS, x_pos;
  65. float last_y_pos = 0, y_pos;
  66. // Init semaphore
  67. rt_sem_init(&hid_sem, "hid", 0, RT_IPC_FLAG_PRIO);
  68. if (tud_connected())
  69. {
  70. // Make the mouse move like a circle
  71. for (int i = 0; i < 360; i++)
  72. {
  73. x_pos = cosf((float)i / 57.3F) * CIRCLE_RADIUS;
  74. y_pos = sinf((float)i / 57.3F) * CIRCLE_RADIUS;
  75. tud_hid_mouse_report(REPORT_ID_MOUSE, 0,
  76. (int8_t)(x_pos - last_x_pos), (int8_t)(y_pos - last_y_pos), 0, 0);
  77. rt_sem_take(&hid_sem, 100);
  78. last_x_pos = x_pos;
  79. last_y_pos = y_pos;
  80. }
  81. rt_thread_mdelay(500);
  82. // Open notepad and type something...
  83. hid_press_key(KEYBOARD_MODIFIER_LEFTGUI, HID_KEY_R);
  84. rt_thread_mdelay(500);
  85. hid_type_str_fmt("notepad\n\n");
  86. rt_thread_mdelay(2000);
  87. hid_type_str_fmt("This is RT-Thread TinyUSB demo.\n");
  88. }
  89. else
  90. {
  91. rt_kprintf("please connect USB port\n");
  92. }
  93. rt_sem_detach(&hid_sem);
  94. }
  95. MSH_CMD_EXPORT(hid_example, TinyUSB hid example)